2 * Copyright (C) 2005 Junio C Hamano
12 #include "xdiff-interface.h"
14 static int use_size_cache
;
16 static int diff_detect_rename_default
= 0;
17 static int diff_rename_limit_default
= -1;
18 static int diff_use_color_default
= 0;
20 /* "\033[1;38;5;2xx;48;5;2xxm\0" is 23 bytes */
21 static char diff_colors
[][24] = {
25 "\033[36m", /* cyan */
27 "\033[32m", /* green */
28 "\033[33m" /* yellow */
31 static int parse_diff_color_slot(const char *var
, int ofs
)
33 if (!strcasecmp(var
+ofs
, "plain"))
35 if (!strcasecmp(var
+ofs
, "meta"))
37 if (!strcasecmp(var
+ofs
, "frag"))
39 if (!strcasecmp(var
+ofs
, "old"))
41 if (!strcasecmp(var
+ofs
, "new"))
43 if (!strcasecmp(var
+ofs
, "commit"))
45 die("bad config variable '%s'", var
);
48 static int parse_color(const char *name
, int len
)
50 static const char * const color_names
[] = {
51 "normal", "black", "red", "green", "yellow",
52 "blue", "magenta", "cyan", "white"
56 for (i
= 0; i
< ARRAY_SIZE(color_names
); i
++) {
57 const char *str
= color_names
[i
];
58 if (!strncasecmp(name
, str
, len
) && !str
[len
])
61 i
= strtol(name
, &end
, 10);
62 if (*name
&& !*end
&& i
>= -1 && i
<= 255)
67 static int parse_attr(const char *name
, int len
)
69 static const int attr_values
[] = { 1, 2, 4, 5, 7 };
70 static const char * const attr_names
[] = {
71 "bold", "dim", "ul", "blink", "reverse"
74 for (i
= 0; i
< ARRAY_SIZE(attr_names
); i
++) {
75 const char *str
= attr_names
[i
];
76 if (!strncasecmp(name
, str
, len
) && !str
[len
])
77 return attr_values
[i
];
82 static void parse_diff_color_value(const char *value
, const char *var
, char *dst
)
84 const char *ptr
= value
;
89 if (!strcasecmp(value
, "reset")) {
90 strcpy(dst
, "\033[m");
94 /* [fg [bg]] [attr] */
96 const char *word
= ptr
;
99 while (word
[len
] && !isspace(word
[len
]))
103 while (*ptr
&& isspace(*ptr
))
106 val
= parse_color(word
, len
);
118 val
= parse_attr(word
, len
);
119 if (val
< 0 || attr
!= -1)
124 if (attr
>= 0 || fg
>= 0 || bg
>= 0) {
140 dst
+= sprintf(dst
, "38;5;%d", fg
);
150 dst
+= sprintf(dst
, "48;5;%d", bg
);
158 die("bad config value '%s' for variable '%s'", value
, var
);
162 * These are to give UI layer defaults.
163 * The core-level commands such as git-diff-files should
164 * never be affected by the setting of diff.renames
165 * the user happens to have in the configuration file.
167 int git_diff_ui_config(const char *var
, const char *value
)
169 if (!strcmp(var
, "diff.renamelimit")) {
170 diff_rename_limit_default
= git_config_int(var
, value
);
173 if (!strcmp(var
, "diff.color")) {
175 diff_use_color_default
= 1; /* bool */
176 else if (!strcasecmp(value
, "auto")) {
177 diff_use_color_default
= 0;
178 if (isatty(1) || pager_in_use
) {
179 char *term
= getenv("TERM");
180 if (term
&& strcmp(term
, "dumb"))
181 diff_use_color_default
= 1;
184 else if (!strcasecmp(value
, "never"))
185 diff_use_color_default
= 0;
186 else if (!strcasecmp(value
, "always"))
187 diff_use_color_default
= 1;
189 diff_use_color_default
= git_config_bool(var
, value
);
192 if (!strcmp(var
, "diff.renames")) {
194 diff_detect_rename_default
= DIFF_DETECT_RENAME
;
195 else if (!strcasecmp(value
, "copies") ||
196 !strcasecmp(value
, "copy"))
197 diff_detect_rename_default
= DIFF_DETECT_COPY
;
198 else if (git_config_bool(var
,value
))
199 diff_detect_rename_default
= DIFF_DETECT_RENAME
;
202 if (!strncmp(var
, "diff.color.", 11)) {
203 int slot
= parse_diff_color_slot(var
, 11);
204 parse_diff_color_value(value
, var
, diff_colors
[slot
]);
207 return git_default_config(var
, value
);
210 static char *quote_one(const char *str
)
217 needlen
= quote_c_style(str
, NULL
, NULL
, 0);
220 xp
= xmalloc(needlen
+ 1);
221 quote_c_style(str
, xp
, NULL
, 0);
225 static char *quote_two(const char *one
, const char *two
)
227 int need_one
= quote_c_style(one
, NULL
, NULL
, 1);
228 int need_two
= quote_c_style(two
, NULL
, NULL
, 1);
231 if (need_one
+ need_two
) {
232 if (!need_one
) need_one
= strlen(one
);
233 if (!need_two
) need_one
= strlen(two
);
235 xp
= xmalloc(need_one
+ need_two
+ 3);
237 quote_c_style(one
, xp
+ 1, NULL
, 1);
238 quote_c_style(two
, xp
+ need_one
+ 1, NULL
, 1);
239 strcpy(xp
+ need_one
+ need_two
+ 1, "\"");
242 need_one
= strlen(one
);
243 need_two
= strlen(two
);
244 xp
= xmalloc(need_one
+ need_two
+ 1);
246 strcpy(xp
+ need_one
, two
);
250 static const char *external_diff(void)
252 static const char *external_diff_cmd
= NULL
;
253 static int done_preparing
= 0;
256 return external_diff_cmd
;
257 external_diff_cmd
= getenv("GIT_EXTERNAL_DIFF");
259 return external_diff_cmd
;
262 #define TEMPFILE_PATH_LEN 50
264 static struct diff_tempfile
{
265 const char *name
; /* filename external diff should read from */
268 char tmp_path
[TEMPFILE_PATH_LEN
];
271 static int count_lines(const char *data
, int size
)
273 int count
, ch
, completely_empty
= 1, nl_just_seen
= 0;
280 completely_empty
= 0;
284 completely_empty
= 0;
287 if (completely_empty
)
290 count
++; /* no trailing newline */
294 static void print_line_count(int count
)
304 printf("1,%d", count
);
309 static void copy_file(int prefix
, const char *data
, int size
)
311 int ch
, nl_just_seen
= 1;
323 printf("\n\\ No newline at end of file\n");
326 static void emit_rewrite_diff(const char *name_a
,
328 struct diff_filespec
*one
,
329 struct diff_filespec
*two
)
332 diff_populate_filespec(one
, 0);
333 diff_populate_filespec(two
, 0);
334 lc_a
= count_lines(one
->data
, one
->size
);
335 lc_b
= count_lines(two
->data
, two
->size
);
336 printf("--- %s\n+++ %s\n@@ -", name_a
, name_b
);
337 print_line_count(lc_a
);
339 print_line_count(lc_b
);
342 copy_file('-', one
->data
, one
->size
);
344 copy_file('+', two
->data
, two
->size
);
347 static int fill_mmfile(mmfile_t
*mf
, struct diff_filespec
*one
)
349 if (!DIFF_FILE_VALID(one
)) {
350 mf
->ptr
= (char *)""; /* does not matter */
354 else if (diff_populate_filespec(one
, 0))
357 mf
->size
= one
->size
;
361 struct emit_callback
{
362 struct xdiff_emit_state xm
;
363 int nparents
, color_diff
;
364 const char **label_path
;
367 const char *diff_get_color(int diff_use_color
, enum color_diff ix
)
370 return diff_colors
[ix
];
374 static void fn_out_consume(void *priv
, char *line
, unsigned long len
)
377 struct emit_callback
*ecbdata
= priv
;
378 const char *set
= diff_get_color(ecbdata
->color_diff
, DIFF_METAINFO
);
379 const char *reset
= diff_get_color(ecbdata
->color_diff
, DIFF_RESET
);
381 if (ecbdata
->label_path
[0]) {
382 printf("%s--- %s%s\n", set
, ecbdata
->label_path
[0], reset
);
383 printf("%s+++ %s%s\n", set
, ecbdata
->label_path
[1], reset
);
384 ecbdata
->label_path
[0] = ecbdata
->label_path
[1] = NULL
;
387 /* This is not really necessary for now because
388 * this codepath only deals with two-way diffs.
390 for (i
= 0; i
< len
&& line
[i
] == '@'; i
++)
392 if (2 <= i
&& i
< len
&& line
[i
] == ' ') {
393 ecbdata
->nparents
= i
- 1;
394 set
= diff_get_color(ecbdata
->color_diff
, DIFF_FRAGINFO
);
396 else if (len
< ecbdata
->nparents
)
399 int nparents
= ecbdata
->nparents
;
400 int color
= DIFF_PLAIN
;
401 for (i
= 0; i
< nparents
&& len
; i
++) {
403 color
= DIFF_FILE_OLD
;
404 else if (line
[i
] == '+')
405 color
= DIFF_FILE_NEW
;
407 set
= diff_get_color(ecbdata
->color_diff
, color
);
409 if (len
> 0 && line
[len
-1] == '\n')
412 fwrite (line
, len
, 1, stdout
);
416 static char *pprint_rename(const char *a
, const char *b
)
421 int pfx_length
, sfx_length
;
422 int len_a
= strlen(a
);
423 int len_b
= strlen(b
);
425 /* Find common prefix */
427 while (*old
&& *new && *old
== *new) {
429 pfx_length
= old
- a
+ 1;
434 /* Find common suffix */
438 while (a
<= old
&& b
<= new && *old
== *new) {
440 sfx_length
= len_a
- (old
- a
);
446 * pfx{mid-a => mid-b}sfx
447 * {pfx-a => pfx-b}sfx
448 * pfx{sfx-a => sfx-b}
451 if (pfx_length
+ sfx_length
) {
452 int a_midlen
= len_a
- pfx_length
- sfx_length
;
453 int b_midlen
= len_b
- pfx_length
- sfx_length
;
454 if (a_midlen
< 0) a_midlen
= 0;
455 if (b_midlen
< 0) b_midlen
= 0;
457 name
= xmalloc(pfx_length
+ a_midlen
+ b_midlen
+ sfx_length
+ 7);
458 sprintf(name
, "%.*s{%.*s => %.*s}%s",
460 a_midlen
, a
+ pfx_length
,
461 b_midlen
, b
+ pfx_length
,
462 a
+ len_a
- sfx_length
);
465 name
= xmalloc(len_a
+ len_b
+ 5);
466 sprintf(name
, "%s => %s", a
, b
);
472 struct xdiff_emit_state xm
;
476 struct diffstat_file
{
478 unsigned is_unmerged
:1;
479 unsigned is_binary
:1;
480 unsigned is_renamed
:1;
481 unsigned int added
, deleted
;
485 static struct diffstat_file
*diffstat_add(struct diffstat_t
*diffstat
,
489 struct diffstat_file
*x
;
490 x
= xcalloc(sizeof (*x
), 1);
491 if (diffstat
->nr
== diffstat
->alloc
) {
492 diffstat
->alloc
= alloc_nr(diffstat
->alloc
);
493 diffstat
->files
= xrealloc(diffstat
->files
,
494 diffstat
->alloc
* sizeof(x
));
496 diffstat
->files
[diffstat
->nr
++] = x
;
498 x
->name
= pprint_rename(name_a
, name_b
);
502 x
->name
= strdup(name_a
);
506 static void diffstat_consume(void *priv
, char *line
, unsigned long len
)
508 struct diffstat_t
*diffstat
= priv
;
509 struct diffstat_file
*x
= diffstat
->files
[diffstat
->nr
- 1];
513 else if (line
[0] == '-')
517 static const char pluses
[] = "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++";
518 static const char minuses
[]= "----------------------------------------------------------------------";
519 const char mime_boundary_leader
[] = "------------";
521 static void show_stats(struct diffstat_t
* data
)
523 int i
, len
, add
, del
, total
, adds
= 0, dels
= 0;
524 int max
, max_change
= 0, max_len
= 0;
525 int total_files
= data
->nr
;
530 for (i
= 0; i
< data
->nr
; i
++) {
531 struct diffstat_file
*file
= data
->files
[i
];
533 len
= strlen(file
->name
);
537 if (file
->is_binary
|| file
->is_unmerged
)
539 if (max_change
< file
->added
+ file
->deleted
)
540 max_change
= file
->added
+ file
->deleted
;
543 for (i
= 0; i
< data
->nr
; i
++) {
544 const char *prefix
= "";
545 char *name
= data
->files
[i
]->name
;
546 int added
= data
->files
[i
]->added
;
547 int deleted
= data
->files
[i
]->deleted
;
549 if (0 < (len
= quote_c_style(name
, NULL
, NULL
, 0))) {
550 char *qname
= xmalloc(len
+ 1);
551 quote_c_style(name
, qname
, NULL
, 0);
553 data
->files
[i
]->name
= name
= qname
;
557 * "scale" the filename
568 slash
= strchr(name
, '/');
575 * scale the add/delete
581 if (data
->files
[i
]->is_binary
) {
582 printf(" %s%-*s | Bin\n", prefix
, len
, name
);
583 goto free_diffstat_file
;
585 else if (data
->files
[i
]->is_unmerged
) {
586 printf(" %s%-*s | Unmerged\n", prefix
, len
, name
);
587 goto free_diffstat_file
;
589 else if (!data
->files
[i
]->is_renamed
&&
590 (added
+ deleted
== 0)) {
592 goto free_diffstat_file
;
601 if (max_change
> 0) {
602 total
= (total
* max
+ max_change
/ 2) / max_change
;
603 add
= (add
* max
+ max_change
/ 2) / max_change
;
606 printf(" %s%-*s |%5d %.*s%.*s\n", prefix
,
607 len
, name
, added
+ deleted
,
608 add
, pluses
, del
, minuses
);
610 free(data
->files
[i
]->name
);
611 free(data
->files
[i
]);
614 printf(" %d files changed, %d insertions(+), %d deletions(-)\n",
615 total_files
, adds
, dels
);
619 struct xdiff_emit_state xm
;
620 const char *filename
;
624 static void checkdiff_consume(void *priv
, char *line
, unsigned long len
)
626 struct checkdiff_t
*data
= priv
;
628 if (line
[0] == '+') {
633 /* check space before tab */
634 for (i
= 1; i
< len
&& (line
[i
] == ' ' || line
[i
] == '\t'); i
++)
637 if (line
[i
- 1] == '\t' && spaces
)
638 printf("%s:%d: space before tab:%.*s\n",
639 data
->filename
, data
->lineno
, (int)len
, line
);
641 /* check white space at line end */
642 if (line
[len
- 1] == '\n')
644 if (isspace(line
[len
- 1]))
645 printf("%s:%d: white space at end: %.*s\n",
646 data
->filename
, data
->lineno
, (int)len
, line
);
647 } else if (line
[0] == ' ')
649 else if (line
[0] == '@') {
650 char *plus
= strchr(line
, '+');
652 data
->lineno
= strtol(plus
, NULL
, 10);
658 static unsigned char *deflate_it(char *data
,
660 unsigned long *result_size
)
663 unsigned char *deflated
;
666 memset(&stream
, 0, sizeof(stream
));
667 deflateInit(&stream
, zlib_compression_level
);
668 bound
= deflateBound(&stream
, size
);
669 deflated
= xmalloc(bound
);
670 stream
.next_out
= deflated
;
671 stream
.avail_out
= bound
;
673 stream
.next_in
= (unsigned char *)data
;
674 stream
.avail_in
= size
;
675 while (deflate(&stream
, Z_FINISH
) == Z_OK
)
678 *result_size
= stream
.total_out
;
682 static void emit_binary_diff(mmfile_t
*one
, mmfile_t
*two
)
688 unsigned long orig_size
;
689 unsigned long delta_size
;
690 unsigned long deflate_size
;
691 unsigned long data_size
;
693 printf("GIT binary patch\n");
694 /* We could do deflated delta, or we could do just deflated two,
695 * whichever is smaller.
698 deflated
= deflate_it(two
->ptr
, two
->size
, &deflate_size
);
699 if (one
->size
&& two
->size
) {
700 delta
= diff_delta(one
->ptr
, one
->size
,
702 &delta_size
, deflate_size
);
704 void *to_free
= delta
;
705 orig_size
= delta_size
;
706 delta
= deflate_it(delta
, delta_size
, &delta_size
);
711 if (delta
&& delta_size
< deflate_size
) {
712 printf("delta %lu\n", orig_size
);
715 data_size
= delta_size
;
718 printf("literal %lu\n", two
->size
);
721 data_size
= deflate_size
;
724 /* emit data encoded in base85 */
727 int bytes
= (52 < data_size
) ? 52 : data_size
;
731 line
[0] = bytes
+ 'A' - 1;
733 line
[0] = bytes
- 26 + 'a' - 1;
734 encode_85(line
+ 1, cp
, bytes
);
735 cp
= (char *) cp
+ bytes
;
742 #define FIRST_FEW_BYTES 8000
743 static int mmfile_is_binary(mmfile_t
*mf
)
746 if (FIRST_FEW_BYTES
< sz
)
747 sz
= FIRST_FEW_BYTES
;
748 if (memchr(mf
->ptr
, 0, sz
))
753 static void builtin_diff(const char *name_a
,
755 struct diff_filespec
*one
,
756 struct diff_filespec
*two
,
757 const char *xfrm_msg
,
758 struct diff_options
*o
,
759 int complete_rewrite
)
764 const char *set
= diff_get_color(o
->color_diff
, DIFF_METAINFO
);
765 const char *reset
= diff_get_color(o
->color_diff
, DIFF_RESET
);
767 a_one
= quote_two("a/", name_a
);
768 b_two
= quote_two("b/", name_b
);
769 lbl
[0] = DIFF_FILE_VALID(one
) ? a_one
: "/dev/null";
770 lbl
[1] = DIFF_FILE_VALID(two
) ? b_two
: "/dev/null";
771 printf("%sdiff --git %s %s%s\n", set
, a_one
, b_two
, reset
);
772 if (lbl
[0][0] == '/') {
774 printf("%snew file mode %06o%s\n", set
, two
->mode
, reset
);
775 if (xfrm_msg
&& xfrm_msg
[0])
776 printf("%s%s%s\n", set
, xfrm_msg
, reset
);
778 else if (lbl
[1][0] == '/') {
779 printf("%sdeleted file mode %06o%s\n", set
, one
->mode
, reset
);
780 if (xfrm_msg
&& xfrm_msg
[0])
781 printf("%s%s%s\n", set
, xfrm_msg
, reset
);
784 if (one
->mode
!= two
->mode
) {
785 printf("%sold mode %06o%s\n", set
, one
->mode
, reset
);
786 printf("%snew mode %06o%s\n", set
, two
->mode
, reset
);
788 if (xfrm_msg
&& xfrm_msg
[0])
789 printf("%s%s%s\n", set
, xfrm_msg
, reset
);
791 * we do not run diff between different kind
794 if ((one
->mode
^ two
->mode
) & S_IFMT
)
795 goto free_ab_and_return
;
796 if (complete_rewrite
) {
797 emit_rewrite_diff(name_a
, name_b
, one
, two
);
798 goto free_ab_and_return
;
802 if (fill_mmfile(&mf1
, one
) < 0 || fill_mmfile(&mf2
, two
) < 0)
803 die("unable to read files to diff");
805 if (!o
->text
&& (mmfile_is_binary(&mf1
) || mmfile_is_binary(&mf2
))) {
806 /* Quite common confusing case */
807 if (mf1
.size
== mf2
.size
&&
808 !memcmp(mf1
.ptr
, mf2
.ptr
, mf1
.size
))
809 goto free_ab_and_return
;
811 emit_binary_diff(&mf1
, &mf2
);
813 printf("Binary files %s and %s differ\n",
817 /* Crazy xdl interfaces.. */
818 const char *diffopts
= getenv("GIT_DIFF_OPTS");
822 struct emit_callback ecbdata
;
824 memset(&ecbdata
, 0, sizeof(ecbdata
));
825 ecbdata
.label_path
= lbl
;
826 ecbdata
.color_diff
= o
->color_diff
;
827 xpp
.flags
= XDF_NEED_MINIMAL
| o
->xdl_opts
;
828 xecfg
.ctxlen
= o
->context
;
829 xecfg
.flags
= XDL_EMIT_FUNCNAMES
;
832 else if (!strncmp(diffopts
, "--unified=", 10))
833 xecfg
.ctxlen
= strtoul(diffopts
+ 10, NULL
, 10);
834 else if (!strncmp(diffopts
, "-u", 2))
835 xecfg
.ctxlen
= strtoul(diffopts
+ 2, NULL
, 10);
836 ecb
.outf
= xdiff_outf
;
838 ecbdata
.xm
.consume
= fn_out_consume
;
839 xdl_diff(&mf1
, &mf2
, &xpp
, &xecfg
, &ecb
);
848 static void builtin_diffstat(const char *name_a
, const char *name_b
,
849 struct diff_filespec
*one
,
850 struct diff_filespec
*two
,
851 struct diffstat_t
*diffstat
,
852 struct diff_options
*o
,
853 int complete_rewrite
)
856 struct diffstat_file
*data
;
858 data
= diffstat_add(diffstat
, name_a
, name_b
);
861 data
->is_unmerged
= 1;
864 if (complete_rewrite
) {
865 diff_populate_filespec(one
, 0);
866 diff_populate_filespec(two
, 0);
867 data
->deleted
= count_lines(one
->data
, one
->size
);
868 data
->added
= count_lines(two
->data
, two
->size
);
871 if (fill_mmfile(&mf1
, one
) < 0 || fill_mmfile(&mf2
, two
) < 0)
872 die("unable to read files to diff");
874 if (mmfile_is_binary(&mf1
) || mmfile_is_binary(&mf2
))
877 /* Crazy xdl interfaces.. */
882 xpp
.flags
= XDF_NEED_MINIMAL
| o
->xdl_opts
;
885 ecb
.outf
= xdiff_outf
;
887 xdl_diff(&mf1
, &mf2
, &xpp
, &xecfg
, &ecb
);
891 static void builtin_checkdiff(const char *name_a
, const char *name_b
,
892 struct diff_filespec
*one
,
893 struct diff_filespec
*two
)
896 struct checkdiff_t data
;
901 memset(&data
, 0, sizeof(data
));
902 data
.xm
.consume
= checkdiff_consume
;
903 data
.filename
= name_b
? name_b
: name_a
;
906 if (fill_mmfile(&mf1
, one
) < 0 || fill_mmfile(&mf2
, two
) < 0)
907 die("unable to read files to diff");
909 if (mmfile_is_binary(&mf2
))
912 /* Crazy xdl interfaces.. */
917 xpp
.flags
= XDF_NEED_MINIMAL
;
920 ecb
.outf
= xdiff_outf
;
922 xdl_diff(&mf1
, &mf2
, &xpp
, &xecfg
, &ecb
);
926 struct diff_filespec
*alloc_filespec(const char *path
)
928 int namelen
= strlen(path
);
929 struct diff_filespec
*spec
= xmalloc(sizeof(*spec
) + namelen
+ 1);
931 memset(spec
, 0, sizeof(*spec
));
932 spec
->path
= (char *)(spec
+ 1);
933 memcpy(spec
->path
, path
, namelen
+1);
937 void fill_filespec(struct diff_filespec
*spec
, const unsigned char *sha1
,
941 spec
->mode
= canon_mode(mode
);
942 memcpy(spec
->sha1
, sha1
, 20);
943 spec
->sha1_valid
= !!memcmp(sha1
, null_sha1
, 20);
948 * Given a name and sha1 pair, if the dircache tells us the file in
949 * the work tree has that object contents, return true, so that
950 * prepare_temp_file() does not have to inflate and extract.
952 static int work_tree_matches(const char *name
, const unsigned char *sha1
)
954 struct cache_entry
*ce
;
958 /* We do not read the cache ourselves here, because the
959 * benchmark with my previous version that always reads cache
960 * shows that it makes things worse for diff-tree comparing
961 * two linux-2.6 kernel trees in an already checked out work
962 * tree. This is because most diff-tree comparisons deal with
963 * only a small number of files, while reading the cache is
964 * expensive for a large project, and its cost outweighs the
965 * savings we get by not inflating the object to a temporary
966 * file. Practically, this code only helps when we are used
967 * by diff-cache --cached, which does read the cache before
974 pos
= cache_name_pos(name
, len
);
977 ce
= active_cache
[pos
];
978 if ((lstat(name
, &st
) < 0) ||
979 !S_ISREG(st
.st_mode
) || /* careful! */
980 ce_match_stat(ce
, &st
, 0) ||
981 memcmp(sha1
, ce
->sha1
, 20))
983 /* we return 1 only when we can stat, it is a regular file,
984 * stat information matches, and sha1 recorded in the cache
985 * matches. I.e. we know the file in the work tree really is
986 * the same as the <name, sha1> pair.
991 static struct sha1_size_cache
{
992 unsigned char sha1
[20];
995 static int sha1_size_cache_nr
, sha1_size_cache_alloc
;
997 static struct sha1_size_cache
*locate_size_cache(unsigned char *sha1
,
1002 struct sha1_size_cache
*e
;
1005 last
= sha1_size_cache_nr
;
1006 while (last
> first
) {
1007 int cmp
, next
= (last
+ first
) >> 1;
1008 e
= sha1_size_cache
[next
];
1009 cmp
= memcmp(e
->sha1
, sha1
, 20);
1021 /* insert to make it at "first" */
1022 if (sha1_size_cache_alloc
<= sha1_size_cache_nr
) {
1023 sha1_size_cache_alloc
= alloc_nr(sha1_size_cache_alloc
);
1024 sha1_size_cache
= xrealloc(sha1_size_cache
,
1025 sha1_size_cache_alloc
*
1026 sizeof(*sha1_size_cache
));
1028 sha1_size_cache_nr
++;
1029 if (first
< sha1_size_cache_nr
)
1030 memmove(sha1_size_cache
+ first
+ 1, sha1_size_cache
+ first
,
1031 (sha1_size_cache_nr
- first
- 1) *
1032 sizeof(*sha1_size_cache
));
1033 e
= xmalloc(sizeof(struct sha1_size_cache
));
1034 sha1_size_cache
[first
] = e
;
1035 memcpy(e
->sha1
, sha1
, 20);
1041 * While doing rename detection and pickaxe operation, we may need to
1042 * grab the data for the blob (or file) for our own in-core comparison.
1043 * diff_filespec has data and size fields for this purpose.
1045 int diff_populate_filespec(struct diff_filespec
*s
, int size_only
)
1048 if (!DIFF_FILE_VALID(s
))
1049 die("internal error: asking to populate invalid file.");
1050 if (S_ISDIR(s
->mode
))
1053 if (!use_size_cache
)
1058 if (!s
->sha1_valid
||
1059 work_tree_matches(s
->path
, s
->sha1
)) {
1062 if (lstat(s
->path
, &st
) < 0) {
1063 if (errno
== ENOENT
) {
1067 s
->data
= (char *)"";
1072 s
->size
= st
.st_size
;
1077 if (S_ISLNK(st
.st_mode
)) {
1079 s
->data
= xmalloc(s
->size
);
1081 ret
= readlink(s
->path
, s
->data
, s
->size
);
1088 fd
= open(s
->path
, O_RDONLY
);
1091 s
->data
= mmap(NULL
, s
->size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
1093 if (s
->data
== MAP_FAILED
)
1095 s
->should_munmap
= 1;
1099 struct sha1_size_cache
*e
;
1102 e
= locate_size_cache(s
->sha1
, 1, 0);
1107 if (!sha1_object_info(s
->sha1
, type
, &s
->size
))
1108 locate_size_cache(s
->sha1
, 0, s
->size
);
1111 s
->data
= read_sha1_file(s
->sha1
, type
, &s
->size
);
1118 void diff_free_filespec_data(struct diff_filespec
*s
)
1122 else if (s
->should_munmap
)
1123 munmap(s
->data
, s
->size
);
1124 s
->should_free
= s
->should_munmap
= 0;
1130 static void prep_temp_blob(struct diff_tempfile
*temp
,
1133 const unsigned char *sha1
,
1138 fd
= git_mkstemp(temp
->tmp_path
, TEMPFILE_PATH_LEN
, ".diff_XXXXXX");
1140 die("unable to create temp-file");
1141 if (write(fd
, blob
, size
) != size
)
1142 die("unable to write temp-file");
1144 temp
->name
= temp
->tmp_path
;
1145 strcpy(temp
->hex
, sha1_to_hex(sha1
));
1147 sprintf(temp
->mode
, "%06o", mode
);
1150 static void prepare_temp_file(const char *name
,
1151 struct diff_tempfile
*temp
,
1152 struct diff_filespec
*one
)
1154 if (!DIFF_FILE_VALID(one
)) {
1156 /* A '-' entry produces this for file-2, and
1157 * a '+' entry produces this for file-1.
1159 temp
->name
= "/dev/null";
1160 strcpy(temp
->hex
, ".");
1161 strcpy(temp
->mode
, ".");
1165 if (!one
->sha1_valid
||
1166 work_tree_matches(name
, one
->sha1
)) {
1168 if (lstat(name
, &st
) < 0) {
1169 if (errno
== ENOENT
)
1170 goto not_a_valid_file
;
1171 die("stat(%s): %s", name
, strerror(errno
));
1173 if (S_ISLNK(st
.st_mode
)) {
1175 char buf
[PATH_MAX
+ 1]; /* ought to be SYMLINK_MAX */
1176 if (sizeof(buf
) <= st
.st_size
)
1177 die("symlink too long: %s", name
);
1178 ret
= readlink(name
, buf
, st
.st_size
);
1180 die("readlink(%s)", name
);
1181 prep_temp_blob(temp
, buf
, st
.st_size
,
1183 one
->sha1
: null_sha1
),
1185 one
->mode
: S_IFLNK
));
1188 /* we can borrow from the file in the work tree */
1190 if (!one
->sha1_valid
)
1191 strcpy(temp
->hex
, sha1_to_hex(null_sha1
));
1193 strcpy(temp
->hex
, sha1_to_hex(one
->sha1
));
1194 /* Even though we may sometimes borrow the
1195 * contents from the work tree, we always want
1196 * one->mode. mode is trustworthy even when
1197 * !(one->sha1_valid), as long as
1198 * DIFF_FILE_VALID(one).
1200 sprintf(temp
->mode
, "%06o", one
->mode
);
1205 if (diff_populate_filespec(one
, 0))
1206 die("cannot read data blob for %s", one
->path
);
1207 prep_temp_blob(temp
, one
->data
, one
->size
,
1208 one
->sha1
, one
->mode
);
1212 static void remove_tempfile(void)
1216 for (i
= 0; i
< 2; i
++)
1217 if (diff_temp
[i
].name
== diff_temp
[i
].tmp_path
) {
1218 unlink(diff_temp
[i
].name
);
1219 diff_temp
[i
].name
= NULL
;
1223 static void remove_tempfile_on_signal(int signo
)
1226 signal(SIGINT
, SIG_DFL
);
1230 static int spawn_prog(const char *pgm
, const char **arg
)
1238 die("unable to fork");
1240 execvp(pgm
, (char *const*) arg
);
1244 while (waitpid(pid
, &status
, 0) < 0) {
1250 /* Earlier we did not check the exit status because
1251 * diff exits non-zero if files are different, and
1252 * we are not interested in knowing that. It was a
1253 * mistake which made it harder to quit a diff-*
1254 * session that uses the git-apply-patch-script as
1255 * the GIT_EXTERNAL_DIFF. A custom GIT_EXTERNAL_DIFF
1256 * should also exit non-zero only when it wants to
1257 * abort the entire diff-* session.
1259 if (WIFEXITED(status
) && !WEXITSTATUS(status
))
1264 /* An external diff command takes:
1266 * diff-cmd name infile1 infile1-sha1 infile1-mode \
1267 * infile2 infile2-sha1 infile2-mode [ rename-to ]
1270 static void run_external_diff(const char *pgm
,
1273 struct diff_filespec
*one
,
1274 struct diff_filespec
*two
,
1275 const char *xfrm_msg
,
1276 int complete_rewrite
)
1278 const char *spawn_arg
[10];
1279 struct diff_tempfile
*temp
= diff_temp
;
1281 static int atexit_asked
= 0;
1282 const char *othername
;
1283 const char **arg
= &spawn_arg
[0];
1285 othername
= (other
? other
: name
);
1287 prepare_temp_file(name
, &temp
[0], one
);
1288 prepare_temp_file(othername
, &temp
[1], two
);
1289 if (! atexit_asked
&&
1290 (temp
[0].name
== temp
[0].tmp_path
||
1291 temp
[1].name
== temp
[1].tmp_path
)) {
1293 atexit(remove_tempfile
);
1295 signal(SIGINT
, remove_tempfile_on_signal
);
1301 *arg
++ = temp
[0].name
;
1302 *arg
++ = temp
[0].hex
;
1303 *arg
++ = temp
[0].mode
;
1304 *arg
++ = temp
[1].name
;
1305 *arg
++ = temp
[1].hex
;
1306 *arg
++ = temp
[1].mode
;
1316 retval
= spawn_prog(pgm
, spawn_arg
);
1319 fprintf(stderr
, "external diff died, stopping at %s.\n", name
);
1324 static void run_diff_cmd(const char *pgm
,
1327 struct diff_filespec
*one
,
1328 struct diff_filespec
*two
,
1329 const char *xfrm_msg
,
1330 struct diff_options
*o
,
1331 int complete_rewrite
)
1334 run_external_diff(pgm
, name
, other
, one
, two
, xfrm_msg
,
1339 builtin_diff(name
, other
? other
: name
,
1340 one
, two
, xfrm_msg
, o
, complete_rewrite
);
1342 printf("* Unmerged path %s\n", name
);
1345 static void diff_fill_sha1_info(struct diff_filespec
*one
)
1347 if (DIFF_FILE_VALID(one
)) {
1348 if (!one
->sha1_valid
) {
1350 if (lstat(one
->path
, &st
) < 0)
1351 die("stat %s", one
->path
);
1352 if (index_path(one
->sha1
, one
->path
, &st
, 0))
1353 die("cannot hash %s\n", one
->path
);
1357 memset(one
->sha1
, 0, 20);
1360 static void run_diff(struct diff_filepair
*p
, struct diff_options
*o
)
1362 const char *pgm
= external_diff();
1363 char msg
[PATH_MAX
*2+300], *xfrm_msg
;
1364 struct diff_filespec
*one
;
1365 struct diff_filespec
*two
;
1368 char *name_munged
, *other_munged
;
1369 int complete_rewrite
= 0;
1372 if (DIFF_PAIR_UNMERGED(p
)) {
1374 run_diff_cmd(pgm
, p
->one
->path
, NULL
, NULL
, NULL
, NULL
, o
, 0);
1378 name
= p
->one
->path
;
1379 other
= (strcmp(name
, p
->two
->path
) ? p
->two
->path
: NULL
);
1380 name_munged
= quote_one(name
);
1381 other_munged
= quote_one(other
);
1382 one
= p
->one
; two
= p
->two
;
1384 diff_fill_sha1_info(one
);
1385 diff_fill_sha1_info(two
);
1388 switch (p
->status
) {
1389 case DIFF_STATUS_COPIED
:
1390 len
+= snprintf(msg
+ len
, sizeof(msg
) - len
,
1391 "similarity index %d%%\n"
1394 (int)(0.5 + p
->score
* 100.0/MAX_SCORE
),
1395 name_munged
, other_munged
);
1397 case DIFF_STATUS_RENAMED
:
1398 len
+= snprintf(msg
+ len
, sizeof(msg
) - len
,
1399 "similarity index %d%%\n"
1402 (int)(0.5 + p
->score
* 100.0/MAX_SCORE
),
1403 name_munged
, other_munged
);
1405 case DIFF_STATUS_MODIFIED
:
1407 len
+= snprintf(msg
+ len
, sizeof(msg
) - len
,
1408 "dissimilarity index %d%%\n",
1409 (int)(0.5 + p
->score
*
1411 complete_rewrite
= 1;
1420 if (memcmp(one
->sha1
, two
->sha1
, 20)) {
1421 int abbrev
= o
->full_index
? 40 : DEFAULT_ABBREV
;
1423 len
+= snprintf(msg
+ len
, sizeof(msg
) - len
,
1425 abbrev
, sha1_to_hex(one
->sha1
),
1426 abbrev
, sha1_to_hex(two
->sha1
));
1427 if (one
->mode
== two
->mode
)
1428 len
+= snprintf(msg
+ len
, sizeof(msg
) - len
,
1429 " %06o", one
->mode
);
1430 len
+= snprintf(msg
+ len
, sizeof(msg
) - len
, "\n");
1435 xfrm_msg
= len
? msg
: NULL
;
1438 DIFF_FILE_VALID(one
) && DIFF_FILE_VALID(two
) &&
1439 (S_IFMT
& one
->mode
) != (S_IFMT
& two
->mode
)) {
1440 /* a filepair that changes between file and symlink
1441 * needs to be split into deletion and creation.
1443 struct diff_filespec
*null
= alloc_filespec(two
->path
);
1444 run_diff_cmd(NULL
, name
, other
, one
, null
, xfrm_msg
, o
, 0);
1446 null
= alloc_filespec(one
->path
);
1447 run_diff_cmd(NULL
, name
, other
, null
, two
, xfrm_msg
, o
, 0);
1451 run_diff_cmd(pgm
, name
, other
, one
, two
, xfrm_msg
, o
,
1458 static void run_diffstat(struct diff_filepair
*p
, struct diff_options
*o
,
1459 struct diffstat_t
*diffstat
)
1463 int complete_rewrite
= 0;
1465 if (DIFF_PAIR_UNMERGED(p
)) {
1467 builtin_diffstat(p
->one
->path
, NULL
, NULL
, NULL
, diffstat
, o
, 0);
1471 name
= p
->one
->path
;
1472 other
= (strcmp(name
, p
->two
->path
) ? p
->two
->path
: NULL
);
1474 diff_fill_sha1_info(p
->one
);
1475 diff_fill_sha1_info(p
->two
);
1477 if (p
->status
== DIFF_STATUS_MODIFIED
&& p
->score
)
1478 complete_rewrite
= 1;
1479 builtin_diffstat(name
, other
, p
->one
, p
->two
, diffstat
, o
, complete_rewrite
);
1482 static void run_checkdiff(struct diff_filepair
*p
, struct diff_options
*o
)
1487 if (DIFF_PAIR_UNMERGED(p
)) {
1492 name
= p
->one
->path
;
1493 other
= (strcmp(name
, p
->two
->path
) ? p
->two
->path
: NULL
);
1495 diff_fill_sha1_info(p
->one
);
1496 diff_fill_sha1_info(p
->two
);
1498 builtin_checkdiff(name
, other
, p
->one
, p
->two
);
1501 void diff_setup(struct diff_options
*options
)
1503 memset(options
, 0, sizeof(*options
));
1504 options
->line_termination
= '\n';
1505 options
->break_opt
= -1;
1506 options
->rename_limit
= -1;
1507 options
->context
= 3;
1508 options
->msg_sep
= "";
1510 options
->change
= diff_change
;
1511 options
->add_remove
= diff_addremove
;
1512 options
->color_diff
= diff_use_color_default
;
1513 options
->detect_rename
= diff_detect_rename_default
;
1516 int diff_setup_done(struct diff_options
*options
)
1518 if ((options
->find_copies_harder
&&
1519 options
->detect_rename
!= DIFF_DETECT_COPY
) ||
1520 (0 <= options
->rename_limit
&& !options
->detect_rename
))
1523 if (options
->output_format
& (DIFF_FORMAT_NAME
|
1524 DIFF_FORMAT_NAME_STATUS
|
1525 DIFF_FORMAT_CHECKDIFF
|
1526 DIFF_FORMAT_NO_OUTPUT
))
1527 options
->output_format
&= ~(DIFF_FORMAT_RAW
|
1528 DIFF_FORMAT_DIFFSTAT
|
1529 DIFF_FORMAT_SUMMARY
|
1533 * These cases always need recursive; we do not drop caller-supplied
1534 * recursive bits for other formats here.
1536 if (options
->output_format
& (DIFF_FORMAT_PATCH
|
1537 DIFF_FORMAT_DIFFSTAT
|
1538 DIFF_FORMAT_CHECKDIFF
))
1539 options
->recursive
= 1;
1541 * Also pickaxe would not work very well if you do not say recursive
1543 if (options
->pickaxe
)
1544 options
->recursive
= 1;
1546 if (options
->detect_rename
&& options
->rename_limit
< 0)
1547 options
->rename_limit
= diff_rename_limit_default
;
1548 if (options
->setup
& DIFF_SETUP_USE_CACHE
) {
1550 /* read-cache does not die even when it fails
1551 * so it is safe for us to do this here. Also
1552 * it does not smudge active_cache or active_nr
1553 * when it fails, so we do not have to worry about
1554 * cleaning it up ourselves either.
1558 if (options
->setup
& DIFF_SETUP_USE_SIZE_CACHE
)
1560 if (options
->abbrev
<= 0 || 40 < options
->abbrev
)
1561 options
->abbrev
= 40; /* full */
1566 static int opt_arg(const char *arg
, int arg_short
, const char *arg_long
, int *val
)
1576 if (c
== arg_short
) {
1580 if (val
&& isdigit(c
)) {
1582 int n
= strtoul(arg
, &end
, 10);
1593 eq
= strchr(arg
, '=');
1598 if (!len
|| strncmp(arg
, arg_long
, len
))
1603 if (!isdigit(*++eq
))
1605 n
= strtoul(eq
, &end
, 10);
1613 int diff_opt_parse(struct diff_options
*options
, const char **av
, int ac
)
1615 const char *arg
= av
[0];
1616 if (!strcmp(arg
, "-p") || !strcmp(arg
, "-u"))
1617 options
->output_format
|= DIFF_FORMAT_PATCH
;
1618 else if (opt_arg(arg
, 'U', "unified", &options
->context
))
1619 options
->output_format
|= DIFF_FORMAT_PATCH
;
1620 else if (!strcmp(arg
, "--raw"))
1621 options
->output_format
|= DIFF_FORMAT_RAW
;
1622 else if (!strcmp(arg
, "--patch-with-raw")) {
1623 options
->output_format
|= DIFF_FORMAT_PATCH
| DIFF_FORMAT_RAW
;
1625 else if (!strcmp(arg
, "--stat"))
1626 options
->output_format
|= DIFF_FORMAT_DIFFSTAT
;
1627 else if (!strcmp(arg
, "--check"))
1628 options
->output_format
|= DIFF_FORMAT_CHECKDIFF
;
1629 else if (!strcmp(arg
, "--summary"))
1630 options
->output_format
|= DIFF_FORMAT_SUMMARY
;
1631 else if (!strcmp(arg
, "--patch-with-stat")) {
1632 options
->output_format
|= DIFF_FORMAT_PATCH
| DIFF_FORMAT_DIFFSTAT
;
1634 else if (!strcmp(arg
, "-z"))
1635 options
->line_termination
= 0;
1636 else if (!strncmp(arg
, "-l", 2))
1637 options
->rename_limit
= strtoul(arg
+2, NULL
, 10);
1638 else if (!strcmp(arg
, "--full-index"))
1639 options
->full_index
= 1;
1640 else if (!strcmp(arg
, "--binary")) {
1641 options
->output_format
|= DIFF_FORMAT_PATCH
;
1642 options
->full_index
= options
->binary
= 1;
1644 else if (!strcmp(arg
, "-a") || !strcmp(arg
, "--text")) {
1647 else if (!strcmp(arg
, "--name-only"))
1648 options
->output_format
|= DIFF_FORMAT_NAME
;
1649 else if (!strcmp(arg
, "--name-status"))
1650 options
->output_format
|= DIFF_FORMAT_NAME_STATUS
;
1651 else if (!strcmp(arg
, "-R"))
1652 options
->reverse_diff
= 1;
1653 else if (!strncmp(arg
, "-S", 2))
1654 options
->pickaxe
= arg
+ 2;
1655 else if (!strcmp(arg
, "-s")) {
1656 options
->output_format
|= DIFF_FORMAT_NO_OUTPUT
;
1658 else if (!strncmp(arg
, "-O", 2))
1659 options
->orderfile
= arg
+ 2;
1660 else if (!strncmp(arg
, "--diff-filter=", 14))
1661 options
->filter
= arg
+ 14;
1662 else if (!strcmp(arg
, "--pickaxe-all"))
1663 options
->pickaxe_opts
= DIFF_PICKAXE_ALL
;
1664 else if (!strcmp(arg
, "--pickaxe-regex"))
1665 options
->pickaxe_opts
= DIFF_PICKAXE_REGEX
;
1666 else if (!strncmp(arg
, "-B", 2)) {
1667 if ((options
->break_opt
=
1668 diff_scoreopt_parse(arg
)) == -1)
1671 else if (!strncmp(arg
, "-M", 2)) {
1672 if ((options
->rename_score
=
1673 diff_scoreopt_parse(arg
)) == -1)
1675 options
->detect_rename
= DIFF_DETECT_RENAME
;
1677 else if (!strncmp(arg
, "-C", 2)) {
1678 if ((options
->rename_score
=
1679 diff_scoreopt_parse(arg
)) == -1)
1681 options
->detect_rename
= DIFF_DETECT_COPY
;
1683 else if (!strcmp(arg
, "--find-copies-harder"))
1684 options
->find_copies_harder
= 1;
1685 else if (!strcmp(arg
, "--abbrev"))
1686 options
->abbrev
= DEFAULT_ABBREV
;
1687 else if (!strncmp(arg
, "--abbrev=", 9)) {
1688 options
->abbrev
= strtoul(arg
+ 9, NULL
, 10);
1689 if (options
->abbrev
< MINIMUM_ABBREV
)
1690 options
->abbrev
= MINIMUM_ABBREV
;
1691 else if (40 < options
->abbrev
)
1692 options
->abbrev
= 40;
1694 else if (!strcmp(arg
, "--color"))
1695 options
->color_diff
= 1;
1696 else if (!strcmp(arg
, "--no-color"))
1697 options
->color_diff
= 0;
1698 else if (!strcmp(arg
, "-w") || !strcmp(arg
, "--ignore-all-space"))
1699 options
->xdl_opts
|= XDF_IGNORE_WHITESPACE
;
1700 else if (!strcmp(arg
, "-b") || !strcmp(arg
, "--ignore-space-change"))
1701 options
->xdl_opts
|= XDF_IGNORE_WHITESPACE_CHANGE
;
1702 else if (!strcmp(arg
, "--no-renames"))
1703 options
->detect_rename
= 0;
1709 static int parse_num(const char **cp_p
)
1711 unsigned long num
, scale
;
1713 const char *cp
= *cp_p
;
1720 if ( !dot
&& ch
== '.' ) {
1723 } else if ( ch
== '%' ) {
1724 scale
= dot
? scale
*100 : 100;
1725 cp
++; /* % is always at the end */
1727 } else if ( ch
>= '0' && ch
<= '9' ) {
1728 if ( scale
< 100000 ) {
1730 num
= (num
*10) + (ch
-'0');
1739 /* user says num divided by scale and we say internally that
1740 * is MAX_SCORE * num / scale.
1742 return (num
>= scale
) ? MAX_SCORE
: (MAX_SCORE
* num
/ scale
);
1745 int diff_scoreopt_parse(const char *opt
)
1747 int opt1
, opt2
, cmd
;
1752 if (cmd
!= 'M' && cmd
!= 'C' && cmd
!= 'B')
1753 return -1; /* that is not a -M, -C nor -B option */
1755 opt1
= parse_num(&opt
);
1761 else if (*opt
!= '/')
1762 return -1; /* we expect -B80/99 or -B80 */
1765 opt2
= parse_num(&opt
);
1770 return opt1
| (opt2
<< 16);
1773 struct diff_queue_struct diff_queued_diff
;
1775 void diff_q(struct diff_queue_struct
*queue
, struct diff_filepair
*dp
)
1777 if (queue
->alloc
<= queue
->nr
) {
1778 queue
->alloc
= alloc_nr(queue
->alloc
);
1779 queue
->queue
= xrealloc(queue
->queue
,
1780 sizeof(dp
) * queue
->alloc
);
1782 queue
->queue
[queue
->nr
++] = dp
;
1785 struct diff_filepair
*diff_queue(struct diff_queue_struct
*queue
,
1786 struct diff_filespec
*one
,
1787 struct diff_filespec
*two
)
1789 struct diff_filepair
*dp
= xmalloc(sizeof(*dp
));
1794 dp
->source_stays
= 0;
1795 dp
->broken_pair
= 0;
1801 void diff_free_filepair(struct diff_filepair
*p
)
1803 diff_free_filespec_data(p
->one
);
1804 diff_free_filespec_data(p
->two
);
1810 /* This is different from find_unique_abbrev() in that
1811 * it stuffs the result with dots for alignment.
1813 const char *diff_unique_abbrev(const unsigned char *sha1
, int len
)
1818 return sha1_to_hex(sha1
);
1820 abbrev
= find_unique_abbrev(sha1
, len
);
1822 return sha1_to_hex(sha1
);
1823 abblen
= strlen(abbrev
);
1825 static char hex
[41];
1826 if (len
< abblen
&& abblen
<= len
+ 2)
1827 sprintf(hex
, "%s%.*s", abbrev
, len
+3-abblen
, "..");
1829 sprintf(hex
, "%s...", abbrev
);
1832 return sha1_to_hex(sha1
);
1835 static void diff_flush_raw(struct diff_filepair
*p
,
1836 struct diff_options
*options
)
1840 int abbrev
= options
->abbrev
;
1841 const char *path_one
, *path_two
;
1842 int inter_name_termination
= '\t';
1843 int line_termination
= options
->line_termination
;
1845 if (!line_termination
)
1846 inter_name_termination
= 0;
1848 path_one
= p
->one
->path
;
1849 path_two
= p
->two
->path
;
1850 if (line_termination
) {
1851 path_one
= quote_one(path_one
);
1852 path_two
= quote_one(path_two
);
1856 sprintf(status
, "%c%03d", p
->status
,
1857 (int)(0.5 + p
->score
* 100.0/MAX_SCORE
));
1859 status
[0] = p
->status
;
1862 switch (p
->status
) {
1863 case DIFF_STATUS_COPIED
:
1864 case DIFF_STATUS_RENAMED
:
1867 case DIFF_STATUS_ADDED
:
1868 case DIFF_STATUS_DELETED
:
1875 if (!(options
->output_format
& DIFF_FORMAT_NAME_STATUS
)) {
1876 printf(":%06o %06o %s ",
1877 p
->one
->mode
, p
->two
->mode
,
1878 diff_unique_abbrev(p
->one
->sha1
, abbrev
));
1880 diff_unique_abbrev(p
->two
->sha1
, abbrev
));
1882 printf("%s%c%s", status
, inter_name_termination
, path_one
);
1884 printf("%c%s", inter_name_termination
, path_two
);
1885 putchar(line_termination
);
1886 if (path_one
!= p
->one
->path
)
1887 free((void*)path_one
);
1888 if (path_two
!= p
->two
->path
)
1889 free((void*)path_two
);
1892 static void diff_flush_name(struct diff_filepair
*p
, int line_termination
)
1894 char *path
= p
->two
->path
;
1896 if (line_termination
)
1897 path
= quote_one(p
->two
->path
);
1898 printf("%s%c", path
, line_termination
);
1899 if (p
->two
->path
!= path
)
1903 int diff_unmodified_pair(struct diff_filepair
*p
)
1905 /* This function is written stricter than necessary to support
1906 * the currently implemented transformers, but the idea is to
1907 * let transformers to produce diff_filepairs any way they want,
1908 * and filter and clean them up here before producing the output.
1910 struct diff_filespec
*one
, *two
;
1912 if (DIFF_PAIR_UNMERGED(p
))
1913 return 0; /* unmerged is interesting */
1918 /* deletion, addition, mode or type change
1919 * and rename are all interesting.
1921 if (DIFF_FILE_VALID(one
) != DIFF_FILE_VALID(two
) ||
1922 DIFF_PAIR_MODE_CHANGED(p
) ||
1923 strcmp(one
->path
, two
->path
))
1926 /* both are valid and point at the same path. that is, we are
1927 * dealing with a change.
1929 if (one
->sha1_valid
&& two
->sha1_valid
&&
1930 !memcmp(one
->sha1
, two
->sha1
, sizeof(one
->sha1
)))
1931 return 1; /* no change */
1932 if (!one
->sha1_valid
&& !two
->sha1_valid
)
1933 return 1; /* both look at the same file on the filesystem. */
1937 static void diff_flush_patch(struct diff_filepair
*p
, struct diff_options
*o
)
1939 if (diff_unmodified_pair(p
))
1942 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
1943 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
1944 return; /* no tree diffs in patch format */
1949 static void diff_flush_stat(struct diff_filepair
*p
, struct diff_options
*o
,
1950 struct diffstat_t
*diffstat
)
1952 if (diff_unmodified_pair(p
))
1955 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
1956 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
1957 return; /* no tree diffs in patch format */
1959 run_diffstat(p
, o
, diffstat
);
1962 static void diff_flush_checkdiff(struct diff_filepair
*p
,
1963 struct diff_options
*o
)
1965 if (diff_unmodified_pair(p
))
1968 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
1969 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
1970 return; /* no tree diffs in patch format */
1972 run_checkdiff(p
, o
);
1975 int diff_queue_is_empty(void)
1977 struct diff_queue_struct
*q
= &diff_queued_diff
;
1979 for (i
= 0; i
< q
->nr
; i
++)
1980 if (!diff_unmodified_pair(q
->queue
[i
]))
1986 void diff_debug_filespec(struct diff_filespec
*s
, int x
, const char *one
)
1988 fprintf(stderr
, "queue[%d] %s (%s) %s %06o %s\n",
1991 DIFF_FILE_VALID(s
) ? "valid" : "invalid",
1993 s
->sha1_valid
? sha1_to_hex(s
->sha1
) : "");
1994 fprintf(stderr
, "queue[%d] %s size %lu flags %d\n",
1996 s
->size
, s
->xfrm_flags
);
1999 void diff_debug_filepair(const struct diff_filepair
*p
, int i
)
2001 diff_debug_filespec(p
->one
, i
, "one");
2002 diff_debug_filespec(p
->two
, i
, "two");
2003 fprintf(stderr
, "score %d, status %c stays %d broken %d\n",
2004 p
->score
, p
->status
? p
->status
: '?',
2005 p
->source_stays
, p
->broken_pair
);
2008 void diff_debug_queue(const char *msg
, struct diff_queue_struct
*q
)
2012 fprintf(stderr
, "%s\n", msg
);
2013 fprintf(stderr
, "q->nr = %d\n", q
->nr
);
2014 for (i
= 0; i
< q
->nr
; i
++) {
2015 struct diff_filepair
*p
= q
->queue
[i
];
2016 diff_debug_filepair(p
, i
);
2021 static void diff_resolve_rename_copy(void)
2024 struct diff_filepair
*p
, *pp
;
2025 struct diff_queue_struct
*q
= &diff_queued_diff
;
2027 diff_debug_queue("resolve-rename-copy", q
);
2029 for (i
= 0; i
< q
->nr
; i
++) {
2031 p
->status
= 0; /* undecided */
2032 if (DIFF_PAIR_UNMERGED(p
))
2033 p
->status
= DIFF_STATUS_UNMERGED
;
2034 else if (!DIFF_FILE_VALID(p
->one
))
2035 p
->status
= DIFF_STATUS_ADDED
;
2036 else if (!DIFF_FILE_VALID(p
->two
))
2037 p
->status
= DIFF_STATUS_DELETED
;
2038 else if (DIFF_PAIR_TYPE_CHANGED(p
))
2039 p
->status
= DIFF_STATUS_TYPE_CHANGED
;
2041 /* from this point on, we are dealing with a pair
2042 * whose both sides are valid and of the same type, i.e.
2043 * either in-place edit or rename/copy edit.
2045 else if (DIFF_PAIR_RENAME(p
)) {
2046 if (p
->source_stays
) {
2047 p
->status
= DIFF_STATUS_COPIED
;
2050 /* See if there is some other filepair that
2051 * copies from the same source as us. If so
2052 * we are a copy. Otherwise we are either a
2053 * copy if the path stays, or a rename if it
2054 * does not, but we already handled "stays" case.
2056 for (j
= i
+ 1; j
< q
->nr
; j
++) {
2058 if (strcmp(pp
->one
->path
, p
->one
->path
))
2059 continue; /* not us */
2060 if (!DIFF_PAIR_RENAME(pp
))
2061 continue; /* not a rename/copy */
2062 /* pp is a rename/copy from the same source */
2063 p
->status
= DIFF_STATUS_COPIED
;
2067 p
->status
= DIFF_STATUS_RENAMED
;
2069 else if (memcmp(p
->one
->sha1
, p
->two
->sha1
, 20) ||
2070 p
->one
->mode
!= p
->two
->mode
)
2071 p
->status
= DIFF_STATUS_MODIFIED
;
2073 /* This is a "no-change" entry and should not
2074 * happen anymore, but prepare for broken callers.
2076 error("feeding unmodified %s to diffcore",
2078 p
->status
= DIFF_STATUS_UNKNOWN
;
2081 diff_debug_queue("resolve-rename-copy done", q
);
2084 static int check_pair_status(struct diff_filepair
*p
)
2086 switch (p
->status
) {
2087 case DIFF_STATUS_UNKNOWN
:
2090 die("internal error in diff-resolve-rename-copy");
2096 static void flush_one_pair(struct diff_filepair
*p
, struct diff_options
*opt
)
2098 int fmt
= opt
->output_format
;
2100 if (fmt
& DIFF_FORMAT_CHECKDIFF
)
2101 diff_flush_checkdiff(p
, opt
);
2102 else if (fmt
& (DIFF_FORMAT_RAW
| DIFF_FORMAT_NAME_STATUS
))
2103 diff_flush_raw(p
, opt
);
2104 else if (fmt
& DIFF_FORMAT_NAME
)
2105 diff_flush_name(p
, opt
->line_termination
);
2108 static void show_file_mode_name(const char *newdelete
, struct diff_filespec
*fs
)
2111 printf(" %s mode %06o %s\n", newdelete
, fs
->mode
, fs
->path
);
2113 printf(" %s %s\n", newdelete
, fs
->path
);
2117 static void show_mode_change(struct diff_filepair
*p
, int show_name
)
2119 if (p
->one
->mode
&& p
->two
->mode
&& p
->one
->mode
!= p
->two
->mode
) {
2121 printf(" mode change %06o => %06o %s\n",
2122 p
->one
->mode
, p
->two
->mode
, p
->two
->path
);
2124 printf(" mode change %06o => %06o\n",
2125 p
->one
->mode
, p
->two
->mode
);
2129 static void show_rename_copy(const char *renamecopy
, struct diff_filepair
*p
)
2131 const char *old
, *new;
2133 /* Find common prefix */
2137 const char *slash_old
, *slash_new
;
2138 slash_old
= strchr(old
, '/');
2139 slash_new
= strchr(new, '/');
2142 slash_old
- old
!= slash_new
- new ||
2143 memcmp(old
, new, slash_new
- new))
2145 old
= slash_old
+ 1;
2146 new = slash_new
+ 1;
2148 /* p->one->path thru old is the common prefix, and old and new
2149 * through the end of names are renames
2151 if (old
!= p
->one
->path
)
2152 printf(" %s %.*s{%s => %s} (%d%%)\n", renamecopy
,
2153 (int)(old
- p
->one
->path
), p
->one
->path
,
2154 old
, new, (int)(0.5 + p
->score
* 100.0/MAX_SCORE
));
2156 printf(" %s %s => %s (%d%%)\n", renamecopy
,
2157 p
->one
->path
, p
->two
->path
,
2158 (int)(0.5 + p
->score
* 100.0/MAX_SCORE
));
2159 show_mode_change(p
, 0);
2162 static void diff_summary(struct diff_filepair
*p
)
2165 case DIFF_STATUS_DELETED
:
2166 show_file_mode_name("delete", p
->one
);
2168 case DIFF_STATUS_ADDED
:
2169 show_file_mode_name("create", p
->two
);
2171 case DIFF_STATUS_COPIED
:
2172 show_rename_copy("copy", p
);
2174 case DIFF_STATUS_RENAMED
:
2175 show_rename_copy("rename", p
);
2179 printf(" rewrite %s (%d%%)\n", p
->two
->path
,
2180 (int)(0.5 + p
->score
* 100.0/MAX_SCORE
));
2181 show_mode_change(p
, 0);
2182 } else show_mode_change(p
, 1);
2188 struct xdiff_emit_state xm
;
2193 static int remove_space(char *line
, int len
)
2199 for (i
= 0; i
< len
; i
++)
2200 if (!isspace((c
= line
[i
])))
2206 static void patch_id_consume(void *priv
, char *line
, unsigned long len
)
2208 struct patch_id_t
*data
= priv
;
2211 /* Ignore line numbers when computing the SHA1 of the patch */
2212 if (!strncmp(line
, "@@ -", 4))
2215 new_len
= remove_space(line
, len
);
2217 SHA1_Update(data
->ctx
, line
, new_len
);
2218 data
->patchlen
+= new_len
;
2221 /* returns 0 upon success, and writes result into sha1 */
2222 static int diff_get_patch_id(struct diff_options
*options
, unsigned char *sha1
)
2224 struct diff_queue_struct
*q
= &diff_queued_diff
;
2227 struct patch_id_t data
;
2228 char buffer
[PATH_MAX
* 4 + 20];
2231 memset(&data
, 0, sizeof(struct patch_id_t
));
2233 data
.xm
.consume
= patch_id_consume
;
2235 for (i
= 0; i
< q
->nr
; i
++) {
2240 struct diff_filepair
*p
= q
->queue
[i
];
2244 return error("internal diff status error");
2245 if (p
->status
== DIFF_STATUS_UNKNOWN
)
2247 if (diff_unmodified_pair(p
))
2249 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
2250 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
2252 if (DIFF_PAIR_UNMERGED(p
))
2255 diff_fill_sha1_info(p
->one
);
2256 diff_fill_sha1_info(p
->two
);
2257 if (fill_mmfile(&mf1
, p
->one
) < 0 ||
2258 fill_mmfile(&mf2
, p
->two
) < 0)
2259 return error("unable to read files to diff");
2261 /* Maybe hash p->two? into the patch id? */
2262 if (mmfile_is_binary(&mf2
))
2265 len1
= remove_space(p
->one
->path
, strlen(p
->one
->path
));
2266 len2
= remove_space(p
->two
->path
, strlen(p
->two
->path
));
2267 if (p
->one
->mode
== 0)
2268 len1
= snprintf(buffer
, sizeof(buffer
),
2269 "diff--gita/%.*sb/%.*s"
2276 len2
, p
->two
->path
);
2277 else if (p
->two
->mode
== 0)
2278 len1
= snprintf(buffer
, sizeof(buffer
),
2279 "diff--gita/%.*sb/%.*s"
2280 "deletedfilemode%06o"
2286 len1
, p
->one
->path
);
2288 len1
= snprintf(buffer
, sizeof(buffer
),
2289 "diff--gita/%.*sb/%.*s"
2295 len2
, p
->two
->path
);
2296 SHA1_Update(&ctx
, buffer
, len1
);
2298 xpp
.flags
= XDF_NEED_MINIMAL
;
2300 xecfg
.flags
= XDL_EMIT_FUNCNAMES
;
2301 ecb
.outf
= xdiff_outf
;
2303 xdl_diff(&mf1
, &mf2
, &xpp
, &xecfg
, &ecb
);
2306 SHA1_Final(sha1
, &ctx
);
2310 int diff_flush_patch_id(struct diff_options
*options
, unsigned char *sha1
)
2312 struct diff_queue_struct
*q
= &diff_queued_diff
;
2314 int result
= diff_get_patch_id(options
, sha1
);
2316 for (i
= 0; i
< q
->nr
; i
++)
2317 diff_free_filepair(q
->queue
[i
]);
2321 q
->nr
= q
->alloc
= 0;
2326 static int is_summary_empty(const struct diff_queue_struct
*q
)
2330 for (i
= 0; i
< q
->nr
; i
++) {
2331 const struct diff_filepair
*p
= q
->queue
[i
];
2333 switch (p
->status
) {
2334 case DIFF_STATUS_DELETED
:
2335 case DIFF_STATUS_ADDED
:
2336 case DIFF_STATUS_COPIED
:
2337 case DIFF_STATUS_RENAMED
:
2342 if (p
->one
->mode
&& p
->two
->mode
&&
2343 p
->one
->mode
!= p
->two
->mode
)
2351 void diff_flush(struct diff_options
*options
)
2353 struct diff_queue_struct
*q
= &diff_queued_diff
;
2354 int i
, output_format
= options
->output_format
;
2358 * Order: raw, stat, summary, patch
2359 * or: name/name-status/checkdiff (other bits clear)
2364 if (output_format
& (DIFF_FORMAT_RAW
|
2366 DIFF_FORMAT_NAME_STATUS
|
2367 DIFF_FORMAT_CHECKDIFF
)) {
2368 for (i
= 0; i
< q
->nr
; i
++) {
2369 struct diff_filepair
*p
= q
->queue
[i
];
2370 if (check_pair_status(p
))
2371 flush_one_pair(p
, options
);
2376 if (output_format
& DIFF_FORMAT_DIFFSTAT
) {
2377 struct diffstat_t diffstat
;
2379 memset(&diffstat
, 0, sizeof(struct diffstat_t
));
2380 diffstat
.xm
.consume
= diffstat_consume
;
2381 for (i
= 0; i
< q
->nr
; i
++) {
2382 struct diff_filepair
*p
= q
->queue
[i
];
2383 if (check_pair_status(p
))
2384 diff_flush_stat(p
, options
, &diffstat
);
2386 show_stats(&diffstat
);
2390 if (output_format
& DIFF_FORMAT_SUMMARY
&& !is_summary_empty(q
)) {
2391 for (i
= 0; i
< q
->nr
; i
++)
2392 diff_summary(q
->queue
[i
]);
2396 if (output_format
& DIFF_FORMAT_PATCH
) {
2398 if (options
->stat_sep
) {
2399 /* attach patch instead of inline */
2400 fputs(options
->stat_sep
, stdout
);
2402 putchar(options
->line_termination
);
2406 for (i
= 0; i
< q
->nr
; i
++) {
2407 struct diff_filepair
*p
= q
->queue
[i
];
2408 if (check_pair_status(p
))
2409 diff_flush_patch(p
, options
);
2413 for (i
= 0; i
< q
->nr
; i
++)
2414 diff_free_filepair(q
->queue
[i
]);
2418 q
->nr
= q
->alloc
= 0;
2421 static void diffcore_apply_filter(const char *filter
)
2424 struct diff_queue_struct
*q
= &diff_queued_diff
;
2425 struct diff_queue_struct outq
;
2427 outq
.nr
= outq
.alloc
= 0;
2432 if (strchr(filter
, DIFF_STATUS_FILTER_AON
)) {
2434 for (i
= found
= 0; !found
&& i
< q
->nr
; i
++) {
2435 struct diff_filepair
*p
= q
->queue
[i
];
2436 if (((p
->status
== DIFF_STATUS_MODIFIED
) &&
2438 strchr(filter
, DIFF_STATUS_FILTER_BROKEN
)) ||
2440 strchr(filter
, DIFF_STATUS_MODIFIED
)))) ||
2441 ((p
->status
!= DIFF_STATUS_MODIFIED
) &&
2442 strchr(filter
, p
->status
)))
2448 /* otherwise we will clear the whole queue
2449 * by copying the empty outq at the end of this
2450 * function, but first clear the current entries
2453 for (i
= 0; i
< q
->nr
; i
++)
2454 diff_free_filepair(q
->queue
[i
]);
2457 /* Only the matching ones */
2458 for (i
= 0; i
< q
->nr
; i
++) {
2459 struct diff_filepair
*p
= q
->queue
[i
];
2461 if (((p
->status
== DIFF_STATUS_MODIFIED
) &&
2463 strchr(filter
, DIFF_STATUS_FILTER_BROKEN
)) ||
2465 strchr(filter
, DIFF_STATUS_MODIFIED
)))) ||
2466 ((p
->status
!= DIFF_STATUS_MODIFIED
) &&
2467 strchr(filter
, p
->status
)))
2470 diff_free_filepair(p
);
2477 void diffcore_std(struct diff_options
*options
)
2479 if (options
->break_opt
!= -1)
2480 diffcore_break(options
->break_opt
);
2481 if (options
->detect_rename
)
2482 diffcore_rename(options
);
2483 if (options
->break_opt
!= -1)
2484 diffcore_merge_broken();
2485 if (options
->pickaxe
)
2486 diffcore_pickaxe(options
->pickaxe
, options
->pickaxe_opts
);
2487 if (options
->orderfile
)
2488 diffcore_order(options
->orderfile
);
2489 diff_resolve_rename_copy();
2490 diffcore_apply_filter(options
->filter
);
2494 void diffcore_std_no_resolve(struct diff_options
*options
)
2496 if (options
->pickaxe
)
2497 diffcore_pickaxe(options
->pickaxe
, options
->pickaxe_opts
);
2498 if (options
->orderfile
)
2499 diffcore_order(options
->orderfile
);
2500 diffcore_apply_filter(options
->filter
);
2503 void diff_addremove(struct diff_options
*options
,
2504 int addremove
, unsigned mode
,
2505 const unsigned char *sha1
,
2506 const char *base
, const char *path
)
2508 char concatpath
[PATH_MAX
];
2509 struct diff_filespec
*one
, *two
;
2511 /* This may look odd, but it is a preparation for
2512 * feeding "there are unchanged files which should
2513 * not produce diffs, but when you are doing copy
2514 * detection you would need them, so here they are"
2515 * entries to the diff-core. They will be prefixed
2516 * with something like '=' or '*' (I haven't decided
2517 * which but should not make any difference).
2518 * Feeding the same new and old to diff_change()
2519 * also has the same effect.
2520 * Before the final output happens, they are pruned after
2521 * merged into rename/copy pairs as appropriate.
2523 if (options
->reverse_diff
)
2524 addremove
= (addremove
== '+' ? '-' :
2525 addremove
== '-' ? '+' : addremove
);
2527 if (!path
) path
= "";
2528 sprintf(concatpath
, "%s%s", base
, path
);
2529 one
= alloc_filespec(concatpath
);
2530 two
= alloc_filespec(concatpath
);
2532 if (addremove
!= '+')
2533 fill_filespec(one
, sha1
, mode
);
2534 if (addremove
!= '-')
2535 fill_filespec(two
, sha1
, mode
);
2537 diff_queue(&diff_queued_diff
, one
, two
);
2540 void diff_change(struct diff_options
*options
,
2541 unsigned old_mode
, unsigned new_mode
,
2542 const unsigned char *old_sha1
,
2543 const unsigned char *new_sha1
,
2544 const char *base
, const char *path
)
2546 char concatpath
[PATH_MAX
];
2547 struct diff_filespec
*one
, *two
;
2549 if (options
->reverse_diff
) {
2551 const unsigned char *tmp_c
;
2552 tmp
= old_mode
; old_mode
= new_mode
; new_mode
= tmp
;
2553 tmp_c
= old_sha1
; old_sha1
= new_sha1
; new_sha1
= tmp_c
;
2555 if (!path
) path
= "";
2556 sprintf(concatpath
, "%s%s", base
, path
);
2557 one
= alloc_filespec(concatpath
);
2558 two
= alloc_filespec(concatpath
);
2559 fill_filespec(one
, old_sha1
, old_mode
);
2560 fill_filespec(two
, new_sha1
, new_mode
);
2562 diff_queue(&diff_queued_diff
, one
, two
);
2565 void diff_unmerge(struct diff_options
*options
,
2568 struct diff_filespec
*one
, *two
;
2569 one
= alloc_filespec(path
);
2570 two
= alloc_filespec(path
);
2571 diff_queue(&diff_queued_diff
, one
, two
);