2 * Copyright (C) 2005 Junio C Hamano
12 #include "xdiff-interface.h"
14 static int use_size_cache
;
16 int diff_rename_limit_default
= -1;
18 int git_diff_config(const char *var
, const char *value
)
20 if (!strcmp(var
, "diff.renamelimit")) {
21 diff_rename_limit_default
= git_config_int(var
, value
);
25 return git_default_config(var
, value
);
35 static const char *diff_colors
[] = {
42 static char *quote_one(const char *str
)
49 needlen
= quote_c_style(str
, NULL
, NULL
, 0);
52 xp
= xmalloc(needlen
+ 1);
53 quote_c_style(str
, xp
, NULL
, 0);
57 static char *quote_two(const char *one
, const char *two
)
59 int need_one
= quote_c_style(one
, NULL
, NULL
, 1);
60 int need_two
= quote_c_style(two
, NULL
, NULL
, 1);
63 if (need_one
+ need_two
) {
64 if (!need_one
) need_one
= strlen(one
);
65 if (!need_two
) need_one
= strlen(two
);
67 xp
= xmalloc(need_one
+ need_two
+ 3);
69 quote_c_style(one
, xp
+ 1, NULL
, 1);
70 quote_c_style(two
, xp
+ need_one
+ 1, NULL
, 1);
71 strcpy(xp
+ need_one
+ need_two
+ 1, "\"");
74 need_one
= strlen(one
);
75 need_two
= strlen(two
);
76 xp
= xmalloc(need_one
+ need_two
+ 1);
78 strcpy(xp
+ need_one
, two
);
82 static const char *external_diff(void)
84 static const char *external_diff_cmd
= NULL
;
85 static int done_preparing
= 0;
88 return external_diff_cmd
;
89 external_diff_cmd
= getenv("GIT_EXTERNAL_DIFF");
91 return external_diff_cmd
;
94 #define TEMPFILE_PATH_LEN 50
96 static struct diff_tempfile
{
97 const char *name
; /* filename external diff should read from */
100 char tmp_path
[TEMPFILE_PATH_LEN
];
103 static int count_lines(const char *data
, int size
)
105 int count
, ch
, completely_empty
= 1, nl_just_seen
= 0;
112 completely_empty
= 0;
116 completely_empty
= 0;
119 if (completely_empty
)
122 count
++; /* no trailing newline */
126 static void print_line_count(int count
)
136 printf("1,%d", count
);
141 static void copy_file(int prefix
, const char *data
, int size
)
143 int ch
, nl_just_seen
= 1;
155 printf("\n\\ No newline at end of file\n");
158 static void emit_rewrite_diff(const char *name_a
,
160 struct diff_filespec
*one
,
161 struct diff_filespec
*two
)
164 diff_populate_filespec(one
, 0);
165 diff_populate_filespec(two
, 0);
166 lc_a
= count_lines(one
->data
, one
->size
);
167 lc_b
= count_lines(two
->data
, two
->size
);
168 printf("--- %s\n+++ %s\n@@ -", name_a
, name_b
);
169 print_line_count(lc_a
);
171 print_line_count(lc_b
);
174 copy_file('-', one
->data
, one
->size
);
176 copy_file('+', two
->data
, two
->size
);
179 static int fill_mmfile(mmfile_t
*mf
, struct diff_filespec
*one
)
181 if (!DIFF_FILE_VALID(one
)) {
182 mf
->ptr
= ""; /* does not matter */
186 else if (diff_populate_filespec(one
, 0))
189 mf
->size
= one
->size
;
193 struct emit_callback
{
194 struct xdiff_emit_state xm
;
195 int nparents
, color_diff
;
196 const char **label_path
;
199 static inline void color_diff(int diff_use_color
, enum color_diff ix
)
202 fputs(diff_colors
[ix
], stdout
);
205 static void fn_out_consume(void *priv
, char *line
, unsigned long len
)
208 struct emit_callback
*ecbdata
= priv
;
210 if (ecbdata
->label_path
[0]) {
211 color_diff(ecbdata
->color_diff
, DIFF_METAINFO
);
212 printf("--- %s\n", ecbdata
->label_path
[0]);
213 color_diff(ecbdata
->color_diff
, DIFF_METAINFO
);
214 printf("+++ %s\n", ecbdata
->label_path
[1]);
215 ecbdata
->label_path
[0] = ecbdata
->label_path
[1] = NULL
;
218 /* This is not really necessary for now because
219 * this codepath only deals with two-way diffs.
221 for (i
= 0; i
< len
&& line
[i
] == '@'; i
++)
223 if (2 <= i
&& i
< len
&& line
[i
] == ' ') {
224 ecbdata
->nparents
= i
- 1;
225 color_diff(ecbdata
->color_diff
, DIFF_METAINFO
);
227 else if (len
< ecbdata
->nparents
)
228 color_diff(ecbdata
->color_diff
, DIFF_PLAIN
);
230 int nparents
= ecbdata
->nparents
;
231 int color
= DIFF_PLAIN
;
232 for (i
= 0; i
< nparents
&& len
; i
++) {
234 color
= DIFF_FILE_OLD
;
235 else if (line
[i
] == '+')
236 color
= DIFF_FILE_NEW
;
238 color_diff(ecbdata
->color_diff
, color
);
240 fwrite(line
, len
, 1, stdout
);
241 color_diff(ecbdata
->color_diff
, DIFF_PLAIN
);
244 static char *pprint_rename(const char *a
, const char *b
)
249 int pfx_length
, sfx_length
;
250 int len_a
= strlen(a
);
251 int len_b
= strlen(b
);
253 /* Find common prefix */
255 while (*old
&& *new && *old
== *new) {
257 pfx_length
= old
- a
+ 1;
262 /* Find common suffix */
266 while (a
<= old
&& b
<= new && *old
== *new) {
268 sfx_length
= len_a
- (old
- a
);
274 * pfx{mid-a => mid-b}sfx
275 * {pfx-a => pfx-b}sfx
276 * pfx{sfx-a => sfx-b}
279 if (pfx_length
+ sfx_length
) {
280 int a_midlen
= len_a
- pfx_length
- sfx_length
;
281 int b_midlen
= len_b
- pfx_length
- sfx_length
;
282 if (a_midlen
< 0) a_midlen
= 0;
283 if (b_midlen
< 0) b_midlen
= 0;
285 name
= xmalloc(pfx_length
+ a_midlen
+ b_midlen
+ sfx_length
+ 7);
286 sprintf(name
, "%.*s{%.*s => %.*s}%s",
288 a_midlen
, a
+ pfx_length
,
289 b_midlen
, b
+ pfx_length
,
290 a
+ len_a
- sfx_length
);
293 name
= xmalloc(len_a
+ len_b
+ 5);
294 sprintf(name
, "%s => %s", a
, b
);
300 struct xdiff_emit_state xm
;
304 struct diffstat_file
{
306 unsigned is_unmerged
:1;
307 unsigned is_binary
:1;
308 unsigned is_renamed
:1;
309 unsigned int added
, deleted
;
313 static struct diffstat_file
*diffstat_add(struct diffstat_t
*diffstat
,
317 struct diffstat_file
*x
;
318 x
= xcalloc(sizeof (*x
), 1);
319 if (diffstat
->nr
== diffstat
->alloc
) {
320 diffstat
->alloc
= alloc_nr(diffstat
->alloc
);
321 diffstat
->files
= xrealloc(diffstat
->files
,
322 diffstat
->alloc
* sizeof(x
));
324 diffstat
->files
[diffstat
->nr
++] = x
;
326 x
->name
= pprint_rename(name_a
, name_b
);
330 x
->name
= strdup(name_a
);
334 static void diffstat_consume(void *priv
, char *line
, unsigned long len
)
336 struct diffstat_t
*diffstat
= priv
;
337 struct diffstat_file
*x
= diffstat
->files
[diffstat
->nr
- 1];
341 else if (line
[0] == '-')
345 static const char pluses
[] = "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++";
346 static const char minuses
[]= "----------------------------------------------------------------------";
347 const char mime_boundary_leader
[] = "------------";
349 static void show_stats(struct diffstat_t
* data
)
351 int i
, len
, add
, del
, total
, adds
= 0, dels
= 0;
352 int max
, max_change
= 0, max_len
= 0;
353 int total_files
= data
->nr
;
358 for (i
= 0; i
< data
->nr
; i
++) {
359 struct diffstat_file
*file
= data
->files
[i
];
361 len
= strlen(file
->name
);
365 if (file
->is_binary
|| file
->is_unmerged
)
367 if (max_change
< file
->added
+ file
->deleted
)
368 max_change
= file
->added
+ file
->deleted
;
371 for (i
= 0; i
< data
->nr
; i
++) {
373 char *name
= data
->files
[i
]->name
;
374 int added
= data
->files
[i
]->added
;
375 int deleted
= data
->files
[i
]->deleted
;
377 if (0 < (len
= quote_c_style(name
, NULL
, NULL
, 0))) {
378 char *qname
= xmalloc(len
+ 1);
379 quote_c_style(name
, qname
, NULL
, 0);
381 data
->files
[i
]->name
= name
= qname
;
385 * "scale" the filename
396 slash
= strchr(name
, '/');
403 * scale the add/delete
409 if (data
->files
[i
]->is_binary
) {
410 printf(" %s%-*s | Bin\n", prefix
, len
, name
);
411 goto free_diffstat_file
;
413 else if (data
->files
[i
]->is_unmerged
) {
414 printf(" %s%-*s | Unmerged\n", prefix
, len
, name
);
415 goto free_diffstat_file
;
417 else if (!data
->files
[i
]->is_renamed
&&
418 (added
+ deleted
== 0)) {
420 goto free_diffstat_file
;
429 if (max_change
> 0) {
430 total
= (total
* max
+ max_change
/ 2) / max_change
;
431 add
= (add
* max
+ max_change
/ 2) / max_change
;
434 printf(" %s%-*s |%5d %.*s%.*s\n", prefix
,
435 len
, name
, added
+ deleted
,
436 add
, pluses
, del
, minuses
);
438 free(data
->files
[i
]->name
);
439 free(data
->files
[i
]);
442 printf(" %d files changed, %d insertions(+), %d deletions(-)\n",
443 total_files
, adds
, dels
);
447 struct xdiff_emit_state xm
;
448 const char *filename
;
452 static void checkdiff_consume(void *priv
, char *line
, unsigned long len
)
454 struct checkdiff_t
*data
= priv
;
456 if (line
[0] == '+') {
461 /* check space before tab */
462 for (i
= 1; i
< len
&& (line
[i
] == ' ' || line
[i
] == '\t'); i
++)
465 if (line
[i
- 1] == '\t' && spaces
)
466 printf("%s:%d: space before tab:%.*s\n",
467 data
->filename
, data
->lineno
, (int)len
, line
);
469 /* check white space at line end */
470 if (line
[len
- 1] == '\n')
472 if (isspace(line
[len
- 1]))
473 printf("%s:%d: white space at end: %.*s\n",
474 data
->filename
, data
->lineno
, (int)len
, line
);
475 } else if (line
[0] == ' ')
477 else if (line
[0] == '@') {
478 char *plus
= strchr(line
, '+');
480 data
->lineno
= strtol(plus
, NULL
, 10);
486 static unsigned char *deflate_it(char *data
,
488 unsigned long *result_size
)
491 unsigned char *deflated
;
494 memset(&stream
, 0, sizeof(stream
));
495 deflateInit(&stream
, Z_BEST_COMPRESSION
);
496 bound
= deflateBound(&stream
, size
);
497 deflated
= xmalloc(bound
);
498 stream
.next_out
= deflated
;
499 stream
.avail_out
= bound
;
501 stream
.next_in
= (unsigned char *)data
;
502 stream
.avail_in
= size
;
503 while (deflate(&stream
, Z_FINISH
) == Z_OK
)
506 *result_size
= stream
.total_out
;
510 static void emit_binary_diff(mmfile_t
*one
, mmfile_t
*two
)
516 unsigned long orig_size
;
517 unsigned long delta_size
;
518 unsigned long deflate_size
;
519 unsigned long data_size
;
521 printf("GIT binary patch\n");
522 /* We could do deflated delta, or we could do just deflated two,
523 * whichever is smaller.
526 deflated
= deflate_it(two
->ptr
, two
->size
, &deflate_size
);
527 if (one
->size
&& two
->size
) {
528 delta
= diff_delta(one
->ptr
, one
->size
,
530 &delta_size
, deflate_size
);
532 void *to_free
= delta
;
533 orig_size
= delta_size
;
534 delta
= deflate_it(delta
, delta_size
, &delta_size
);
539 if (delta
&& delta_size
< deflate_size
) {
540 printf("delta %lu\n", orig_size
);
543 data_size
= delta_size
;
546 printf("literal %lu\n", two
->size
);
549 data_size
= deflate_size
;
552 /* emit data encoded in base85 */
555 int bytes
= (52 < data_size
) ? 52 : data_size
;
559 line
[0] = bytes
+ 'A' - 1;
561 line
[0] = bytes
- 26 + 'a' - 1;
562 encode_85(line
+ 1, cp
, bytes
);
570 #define FIRST_FEW_BYTES 8000
571 static int mmfile_is_binary(mmfile_t
*mf
)
574 if (FIRST_FEW_BYTES
< sz
)
575 sz
= FIRST_FEW_BYTES
;
576 if (memchr(mf
->ptr
, 0, sz
))
581 static void builtin_diff(const char *name_a
,
583 struct diff_filespec
*one
,
584 struct diff_filespec
*two
,
585 const char *xfrm_msg
,
586 struct diff_options
*o
,
587 int complete_rewrite
)
593 a_one
= quote_two("a/", name_a
);
594 b_two
= quote_two("b/", name_b
);
595 lbl
[0] = DIFF_FILE_VALID(one
) ? a_one
: "/dev/null";
596 lbl
[1] = DIFF_FILE_VALID(two
) ? b_two
: "/dev/null";
597 color_diff(o
->color_diff
, DIFF_METAINFO
);
598 printf("diff --git %s %s\n", a_one
, b_two
);
599 if (lbl
[0][0] == '/') {
601 color_diff(o
->color_diff
, DIFF_METAINFO
);
602 printf("new file mode %06o\n", two
->mode
);
603 if (xfrm_msg
&& xfrm_msg
[0]) {
604 color_diff(o
->color_diff
, DIFF_METAINFO
);
608 else if (lbl
[1][0] == '/') {
609 printf("deleted file mode %06o\n", one
->mode
);
610 if (xfrm_msg
&& xfrm_msg
[0]) {
611 color_diff(o
->color_diff
, DIFF_METAINFO
);
616 if (one
->mode
!= two
->mode
) {
617 color_diff(o
->color_diff
, DIFF_METAINFO
);
618 printf("old mode %06o\n", one
->mode
);
619 color_diff(o
->color_diff
, DIFF_METAINFO
);
620 printf("new mode %06o\n", two
->mode
);
622 if (xfrm_msg
&& xfrm_msg
[0]) {
623 color_diff(o
->color_diff
, DIFF_METAINFO
);
627 * we do not run diff between different kind
630 if ((one
->mode
^ two
->mode
) & S_IFMT
)
631 goto free_ab_and_return
;
632 if (complete_rewrite
) {
633 color_diff(o
->color_diff
, DIFF_PLAIN
);
634 emit_rewrite_diff(name_a
, name_b
, one
, two
);
635 goto free_ab_and_return
;
639 if (fill_mmfile(&mf1
, one
) < 0 || fill_mmfile(&mf2
, two
) < 0)
640 die("unable to read files to diff");
642 if (mmfile_is_binary(&mf1
) || mmfile_is_binary(&mf2
)) {
643 /* Quite common confusing case */
644 if (mf1
.size
== mf2
.size
&&
645 !memcmp(mf1
.ptr
, mf2
.ptr
, mf1
.size
))
646 goto free_ab_and_return
;
648 emit_binary_diff(&mf1
, &mf2
);
650 printf("Binary files %s and %s differ\n",
654 /* Crazy xdl interfaces.. */
655 const char *diffopts
= getenv("GIT_DIFF_OPTS");
659 struct emit_callback ecbdata
;
661 memset(&ecbdata
, 0, sizeof(ecbdata
));
662 ecbdata
.label_path
= lbl
;
663 ecbdata
.color_diff
= o
->color_diff
;
664 xpp
.flags
= XDF_NEED_MINIMAL
;
665 xecfg
.ctxlen
= o
->context
;
666 xecfg
.flags
= XDL_EMIT_FUNCNAMES
;
669 else if (!strncmp(diffopts
, "--unified=", 10))
670 xecfg
.ctxlen
= strtoul(diffopts
+ 10, NULL
, 10);
671 else if (!strncmp(diffopts
, "-u", 2))
672 xecfg
.ctxlen
= strtoul(diffopts
+ 2, NULL
, 10);
673 ecb
.outf
= xdiff_outf
;
675 ecbdata
.xm
.consume
= fn_out_consume
;
676 xdl_diff(&mf1
, &mf2
, &xpp
, &xecfg
, &ecb
);
685 static void builtin_diffstat(const char *name_a
, const char *name_b
,
686 struct diff_filespec
*one
,
687 struct diff_filespec
*two
,
688 struct diffstat_t
*diffstat
,
689 int complete_rewrite
)
692 struct diffstat_file
*data
;
694 data
= diffstat_add(diffstat
, name_a
, name_b
);
697 data
->is_unmerged
= 1;
700 if (complete_rewrite
) {
701 diff_populate_filespec(one
, 0);
702 diff_populate_filespec(two
, 0);
703 data
->deleted
= count_lines(one
->data
, one
->size
);
704 data
->added
= count_lines(two
->data
, two
->size
);
707 if (fill_mmfile(&mf1
, one
) < 0 || fill_mmfile(&mf2
, two
) < 0)
708 die("unable to read files to diff");
710 if (mmfile_is_binary(&mf1
) || mmfile_is_binary(&mf2
))
713 /* Crazy xdl interfaces.. */
718 xpp
.flags
= XDF_NEED_MINIMAL
;
721 ecb
.outf
= xdiff_outf
;
723 xdl_diff(&mf1
, &mf2
, &xpp
, &xecfg
, &ecb
);
727 static void builtin_checkdiff(const char *name_a
, const char *name_b
,
728 struct diff_filespec
*one
,
729 struct diff_filespec
*two
)
732 struct checkdiff_t data
;
737 memset(&data
, 0, sizeof(data
));
738 data
.xm
.consume
= checkdiff_consume
;
739 data
.filename
= name_b
? name_b
: name_a
;
742 if (fill_mmfile(&mf1
, one
) < 0 || fill_mmfile(&mf2
, two
) < 0)
743 die("unable to read files to diff");
745 if (mmfile_is_binary(&mf2
))
748 /* Crazy xdl interfaces.. */
753 xpp
.flags
= XDF_NEED_MINIMAL
;
756 ecb
.outf
= xdiff_outf
;
758 xdl_diff(&mf1
, &mf2
, &xpp
, &xecfg
, &ecb
);
762 struct diff_filespec
*alloc_filespec(const char *path
)
764 int namelen
= strlen(path
);
765 struct diff_filespec
*spec
= xmalloc(sizeof(*spec
) + namelen
+ 1);
767 memset(spec
, 0, sizeof(*spec
));
768 spec
->path
= (char *)(spec
+ 1);
769 memcpy(spec
->path
, path
, namelen
+1);
773 void fill_filespec(struct diff_filespec
*spec
, const unsigned char *sha1
,
777 spec
->mode
= canon_mode(mode
);
778 memcpy(spec
->sha1
, sha1
, 20);
779 spec
->sha1_valid
= !!memcmp(sha1
, null_sha1
, 20);
784 * Given a name and sha1 pair, if the dircache tells us the file in
785 * the work tree has that object contents, return true, so that
786 * prepare_temp_file() does not have to inflate and extract.
788 static int work_tree_matches(const char *name
, const unsigned char *sha1
)
790 struct cache_entry
*ce
;
794 /* We do not read the cache ourselves here, because the
795 * benchmark with my previous version that always reads cache
796 * shows that it makes things worse for diff-tree comparing
797 * two linux-2.6 kernel trees in an already checked out work
798 * tree. This is because most diff-tree comparisons deal with
799 * only a small number of files, while reading the cache is
800 * expensive for a large project, and its cost outweighs the
801 * savings we get by not inflating the object to a temporary
802 * file. Practically, this code only helps when we are used
803 * by diff-cache --cached, which does read the cache before
810 pos
= cache_name_pos(name
, len
);
813 ce
= active_cache
[pos
];
814 if ((lstat(name
, &st
) < 0) ||
815 !S_ISREG(st
.st_mode
) || /* careful! */
816 ce_match_stat(ce
, &st
, 0) ||
817 memcmp(sha1
, ce
->sha1
, 20))
819 /* we return 1 only when we can stat, it is a regular file,
820 * stat information matches, and sha1 recorded in the cache
821 * matches. I.e. we know the file in the work tree really is
822 * the same as the <name, sha1> pair.
827 static struct sha1_size_cache
{
828 unsigned char sha1
[20];
831 static int sha1_size_cache_nr
, sha1_size_cache_alloc
;
833 static struct sha1_size_cache
*locate_size_cache(unsigned char *sha1
,
838 struct sha1_size_cache
*e
;
841 last
= sha1_size_cache_nr
;
842 while (last
> first
) {
843 int cmp
, next
= (last
+ first
) >> 1;
844 e
= sha1_size_cache
[next
];
845 cmp
= memcmp(e
->sha1
, sha1
, 20);
857 /* insert to make it at "first" */
858 if (sha1_size_cache_alloc
<= sha1_size_cache_nr
) {
859 sha1_size_cache_alloc
= alloc_nr(sha1_size_cache_alloc
);
860 sha1_size_cache
= xrealloc(sha1_size_cache
,
861 sha1_size_cache_alloc
*
862 sizeof(*sha1_size_cache
));
864 sha1_size_cache_nr
++;
865 if (first
< sha1_size_cache_nr
)
866 memmove(sha1_size_cache
+ first
+ 1, sha1_size_cache
+ first
,
867 (sha1_size_cache_nr
- first
- 1) *
868 sizeof(*sha1_size_cache
));
869 e
= xmalloc(sizeof(struct sha1_size_cache
));
870 sha1_size_cache
[first
] = e
;
871 memcpy(e
->sha1
, sha1
, 20);
877 * While doing rename detection and pickaxe operation, we may need to
878 * grab the data for the blob (or file) for our own in-core comparison.
879 * diff_filespec has data and size fields for this purpose.
881 int diff_populate_filespec(struct diff_filespec
*s
, int size_only
)
884 if (!DIFF_FILE_VALID(s
))
885 die("internal error: asking to populate invalid file.");
886 if (S_ISDIR(s
->mode
))
894 if (!s
->sha1_valid
||
895 work_tree_matches(s
->path
, s
->sha1
)) {
898 if (lstat(s
->path
, &st
) < 0) {
899 if (errno
== ENOENT
) {
908 s
->size
= st
.st_size
;
913 if (S_ISLNK(st
.st_mode
)) {
915 s
->data
= xmalloc(s
->size
);
917 ret
= readlink(s
->path
, s
->data
, s
->size
);
924 fd
= open(s
->path
, O_RDONLY
);
927 s
->data
= mmap(NULL
, s
->size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
929 if (s
->data
== MAP_FAILED
)
931 s
->should_munmap
= 1;
935 struct sha1_size_cache
*e
;
938 e
= locate_size_cache(s
->sha1
, 1, 0);
943 if (!sha1_object_info(s
->sha1
, type
, &s
->size
))
944 locate_size_cache(s
->sha1
, 0, s
->size
);
947 s
->data
= read_sha1_file(s
->sha1
, type
, &s
->size
);
954 void diff_free_filespec_data(struct diff_filespec
*s
)
958 else if (s
->should_munmap
)
959 munmap(s
->data
, s
->size
);
960 s
->should_free
= s
->should_munmap
= 0;
966 static void prep_temp_blob(struct diff_tempfile
*temp
,
969 const unsigned char *sha1
,
974 fd
= git_mkstemp(temp
->tmp_path
, TEMPFILE_PATH_LEN
, ".diff_XXXXXX");
976 die("unable to create temp-file");
977 if (write(fd
, blob
, size
) != size
)
978 die("unable to write temp-file");
980 temp
->name
= temp
->tmp_path
;
981 strcpy(temp
->hex
, sha1_to_hex(sha1
));
983 sprintf(temp
->mode
, "%06o", mode
);
986 static void prepare_temp_file(const char *name
,
987 struct diff_tempfile
*temp
,
988 struct diff_filespec
*one
)
990 if (!DIFF_FILE_VALID(one
)) {
992 /* A '-' entry produces this for file-2, and
993 * a '+' entry produces this for file-1.
995 temp
->name
= "/dev/null";
996 strcpy(temp
->hex
, ".");
997 strcpy(temp
->mode
, ".");
1001 if (!one
->sha1_valid
||
1002 work_tree_matches(name
, one
->sha1
)) {
1004 if (lstat(name
, &st
) < 0) {
1005 if (errno
== ENOENT
)
1006 goto not_a_valid_file
;
1007 die("stat(%s): %s", name
, strerror(errno
));
1009 if (S_ISLNK(st
.st_mode
)) {
1011 char buf
[PATH_MAX
+ 1]; /* ought to be SYMLINK_MAX */
1012 if (sizeof(buf
) <= st
.st_size
)
1013 die("symlink too long: %s", name
);
1014 ret
= readlink(name
, buf
, st
.st_size
);
1016 die("readlink(%s)", name
);
1017 prep_temp_blob(temp
, buf
, st
.st_size
,
1019 one
->sha1
: null_sha1
),
1021 one
->mode
: S_IFLNK
));
1024 /* we can borrow from the file in the work tree */
1026 if (!one
->sha1_valid
)
1027 strcpy(temp
->hex
, sha1_to_hex(null_sha1
));
1029 strcpy(temp
->hex
, sha1_to_hex(one
->sha1
));
1030 /* Even though we may sometimes borrow the
1031 * contents from the work tree, we always want
1032 * one->mode. mode is trustworthy even when
1033 * !(one->sha1_valid), as long as
1034 * DIFF_FILE_VALID(one).
1036 sprintf(temp
->mode
, "%06o", one
->mode
);
1041 if (diff_populate_filespec(one
, 0))
1042 die("cannot read data blob for %s", one
->path
);
1043 prep_temp_blob(temp
, one
->data
, one
->size
,
1044 one
->sha1
, one
->mode
);
1048 static void remove_tempfile(void)
1052 for (i
= 0; i
< 2; i
++)
1053 if (diff_temp
[i
].name
== diff_temp
[i
].tmp_path
) {
1054 unlink(diff_temp
[i
].name
);
1055 diff_temp
[i
].name
= NULL
;
1059 static void remove_tempfile_on_signal(int signo
)
1062 signal(SIGINT
, SIG_DFL
);
1066 static int spawn_prog(const char *pgm
, const char **arg
)
1074 die("unable to fork");
1076 execvp(pgm
, (char *const*) arg
);
1080 while (waitpid(pid
, &status
, 0) < 0) {
1086 /* Earlier we did not check the exit status because
1087 * diff exits non-zero if files are different, and
1088 * we are not interested in knowing that. It was a
1089 * mistake which made it harder to quit a diff-*
1090 * session that uses the git-apply-patch-script as
1091 * the GIT_EXTERNAL_DIFF. A custom GIT_EXTERNAL_DIFF
1092 * should also exit non-zero only when it wants to
1093 * abort the entire diff-* session.
1095 if (WIFEXITED(status
) && !WEXITSTATUS(status
))
1100 /* An external diff command takes:
1102 * diff-cmd name infile1 infile1-sha1 infile1-mode \
1103 * infile2 infile2-sha1 infile2-mode [ rename-to ]
1106 static void run_external_diff(const char *pgm
,
1109 struct diff_filespec
*one
,
1110 struct diff_filespec
*two
,
1111 const char *xfrm_msg
,
1112 int complete_rewrite
)
1114 const char *spawn_arg
[10];
1115 struct diff_tempfile
*temp
= diff_temp
;
1117 static int atexit_asked
= 0;
1118 const char *othername
;
1119 const char **arg
= &spawn_arg
[0];
1121 othername
= (other
? other
: name
);
1123 prepare_temp_file(name
, &temp
[0], one
);
1124 prepare_temp_file(othername
, &temp
[1], two
);
1125 if (! atexit_asked
&&
1126 (temp
[0].name
== temp
[0].tmp_path
||
1127 temp
[1].name
== temp
[1].tmp_path
)) {
1129 atexit(remove_tempfile
);
1131 signal(SIGINT
, remove_tempfile_on_signal
);
1137 *arg
++ = temp
[0].name
;
1138 *arg
++ = temp
[0].hex
;
1139 *arg
++ = temp
[0].mode
;
1140 *arg
++ = temp
[1].name
;
1141 *arg
++ = temp
[1].hex
;
1142 *arg
++ = temp
[1].mode
;
1152 retval
= spawn_prog(pgm
, spawn_arg
);
1155 fprintf(stderr
, "external diff died, stopping at %s.\n", name
);
1160 static void run_diff_cmd(const char *pgm
,
1163 struct diff_filespec
*one
,
1164 struct diff_filespec
*two
,
1165 const char *xfrm_msg
,
1166 struct diff_options
*o
,
1167 int complete_rewrite
)
1170 run_external_diff(pgm
, name
, other
, one
, two
, xfrm_msg
,
1175 builtin_diff(name
, other
? other
: name
,
1176 one
, two
, xfrm_msg
, o
, complete_rewrite
);
1178 printf("* Unmerged path %s\n", name
);
1181 static void diff_fill_sha1_info(struct diff_filespec
*one
)
1183 if (DIFF_FILE_VALID(one
)) {
1184 if (!one
->sha1_valid
) {
1186 if (lstat(one
->path
, &st
) < 0)
1187 die("stat %s", one
->path
);
1188 if (index_path(one
->sha1
, one
->path
, &st
, 0))
1189 die("cannot hash %s\n", one
->path
);
1193 memset(one
->sha1
, 0, 20);
1196 static void run_diff(struct diff_filepair
*p
, struct diff_options
*o
)
1198 const char *pgm
= external_diff();
1199 char msg
[PATH_MAX
*2+300], *xfrm_msg
;
1200 struct diff_filespec
*one
;
1201 struct diff_filespec
*two
;
1204 char *name_munged
, *other_munged
;
1205 int complete_rewrite
= 0;
1208 if (DIFF_PAIR_UNMERGED(p
)) {
1210 run_diff_cmd(pgm
, p
->one
->path
, NULL
, NULL
, NULL
, NULL
, o
, 0);
1214 name
= p
->one
->path
;
1215 other
= (strcmp(name
, p
->two
->path
) ? p
->two
->path
: NULL
);
1216 name_munged
= quote_one(name
);
1217 other_munged
= quote_one(other
);
1218 one
= p
->one
; two
= p
->two
;
1220 diff_fill_sha1_info(one
);
1221 diff_fill_sha1_info(two
);
1224 switch (p
->status
) {
1225 case DIFF_STATUS_COPIED
:
1226 len
+= snprintf(msg
+ len
, sizeof(msg
) - len
,
1227 "similarity index %d%%\n"
1230 (int)(0.5 + p
->score
* 100.0/MAX_SCORE
),
1231 name_munged
, other_munged
);
1233 case DIFF_STATUS_RENAMED
:
1234 len
+= snprintf(msg
+ len
, sizeof(msg
) - len
,
1235 "similarity index %d%%\n"
1238 (int)(0.5 + p
->score
* 100.0/MAX_SCORE
),
1239 name_munged
, other_munged
);
1241 case DIFF_STATUS_MODIFIED
:
1243 len
+= snprintf(msg
+ len
, sizeof(msg
) - len
,
1244 "dissimilarity index %d%%\n",
1245 (int)(0.5 + p
->score
*
1247 complete_rewrite
= 1;
1256 if (memcmp(one
->sha1
, two
->sha1
, 20)) {
1257 int abbrev
= o
->full_index
? 40 : DEFAULT_ABBREV
;
1259 len
+= snprintf(msg
+ len
, sizeof(msg
) - len
,
1261 abbrev
, sha1_to_hex(one
->sha1
),
1262 abbrev
, sha1_to_hex(two
->sha1
));
1263 if (one
->mode
== two
->mode
)
1264 len
+= snprintf(msg
+ len
, sizeof(msg
) - len
,
1265 " %06o", one
->mode
);
1266 len
+= snprintf(msg
+ len
, sizeof(msg
) - len
, "\n");
1271 xfrm_msg
= len
? msg
: NULL
;
1274 DIFF_FILE_VALID(one
) && DIFF_FILE_VALID(two
) &&
1275 (S_IFMT
& one
->mode
) != (S_IFMT
& two
->mode
)) {
1276 /* a filepair that changes between file and symlink
1277 * needs to be split into deletion and creation.
1279 struct diff_filespec
*null
= alloc_filespec(two
->path
);
1280 run_diff_cmd(NULL
, name
, other
, one
, null
, xfrm_msg
, o
, 0);
1282 null
= alloc_filespec(one
->path
);
1283 run_diff_cmd(NULL
, name
, other
, null
, two
, xfrm_msg
, o
, 0);
1287 run_diff_cmd(pgm
, name
, other
, one
, two
, xfrm_msg
, o
,
1294 static void run_diffstat(struct diff_filepair
*p
, struct diff_options
*o
,
1295 struct diffstat_t
*diffstat
)
1299 int complete_rewrite
= 0;
1301 if (DIFF_PAIR_UNMERGED(p
)) {
1303 builtin_diffstat(p
->one
->path
, NULL
, NULL
, NULL
, diffstat
, 0);
1307 name
= p
->one
->path
;
1308 other
= (strcmp(name
, p
->two
->path
) ? p
->two
->path
: NULL
);
1310 diff_fill_sha1_info(p
->one
);
1311 diff_fill_sha1_info(p
->two
);
1313 if (p
->status
== DIFF_STATUS_MODIFIED
&& p
->score
)
1314 complete_rewrite
= 1;
1315 builtin_diffstat(name
, other
, p
->one
, p
->two
, diffstat
, complete_rewrite
);
1318 static void run_checkdiff(struct diff_filepair
*p
, struct diff_options
*o
)
1323 if (DIFF_PAIR_UNMERGED(p
)) {
1328 name
= p
->one
->path
;
1329 other
= (strcmp(name
, p
->two
->path
) ? p
->two
->path
: NULL
);
1331 diff_fill_sha1_info(p
->one
);
1332 diff_fill_sha1_info(p
->two
);
1334 builtin_checkdiff(name
, other
, p
->one
, p
->two
);
1337 void diff_setup(struct diff_options
*options
)
1339 memset(options
, 0, sizeof(*options
));
1340 options
->output_format
= DIFF_FORMAT_RAW
;
1341 options
->line_termination
= '\n';
1342 options
->break_opt
= -1;
1343 options
->rename_limit
= -1;
1344 options
->context
= 3;
1346 options
->change
= diff_change
;
1347 options
->add_remove
= diff_addremove
;
1350 int diff_setup_done(struct diff_options
*options
)
1352 if ((options
->find_copies_harder
&&
1353 options
->detect_rename
!= DIFF_DETECT_COPY
) ||
1354 (0 <= options
->rename_limit
&& !options
->detect_rename
))
1358 * These cases always need recursive; we do not drop caller-supplied
1359 * recursive bits for other formats here.
1361 if ((options
->output_format
== DIFF_FORMAT_PATCH
) ||
1362 (options
->output_format
== DIFF_FORMAT_DIFFSTAT
) ||
1363 (options
->output_format
== DIFF_FORMAT_CHECKDIFF
))
1364 options
->recursive
= 1;
1367 * These combinations do not make sense.
1369 if (options
->output_format
== DIFF_FORMAT_RAW
)
1370 options
->with_raw
= 0;
1371 if (options
->output_format
== DIFF_FORMAT_DIFFSTAT
)
1372 options
->with_stat
= 0;
1374 if (options
->detect_rename
&& options
->rename_limit
< 0)
1375 options
->rename_limit
= diff_rename_limit_default
;
1376 if (options
->setup
& DIFF_SETUP_USE_CACHE
) {
1378 /* read-cache does not die even when it fails
1379 * so it is safe for us to do this here. Also
1380 * it does not smudge active_cache or active_nr
1381 * when it fails, so we do not have to worry about
1382 * cleaning it up ourselves either.
1386 if (options
->setup
& DIFF_SETUP_USE_SIZE_CACHE
)
1388 if (options
->abbrev
<= 0 || 40 < options
->abbrev
)
1389 options
->abbrev
= 40; /* full */
1394 int opt_arg(const char *arg
, int arg_short
, const char *arg_long
, int *val
)
1404 if (c
== arg_short
) {
1408 if (val
&& isdigit(c
)) {
1410 int n
= strtoul(arg
, &end
, 10);
1421 eq
= strchr(arg
, '=');
1426 if (!len
|| strncmp(arg
, arg_long
, len
))
1431 if (!isdigit(*++eq
))
1433 n
= strtoul(eq
, &end
, 10);
1441 int diff_opt_parse(struct diff_options
*options
, const char **av
, int ac
)
1443 const char *arg
= av
[0];
1444 if (!strcmp(arg
, "-p") || !strcmp(arg
, "-u"))
1445 options
->output_format
= DIFF_FORMAT_PATCH
;
1446 else if (opt_arg(arg
, 'U', "unified", &options
->context
))
1447 options
->output_format
= DIFF_FORMAT_PATCH
;
1448 else if (!strcmp(arg
, "--patch-with-raw")) {
1449 options
->output_format
= DIFF_FORMAT_PATCH
;
1450 options
->with_raw
= 1;
1452 else if (!strcmp(arg
, "--stat"))
1453 options
->output_format
= DIFF_FORMAT_DIFFSTAT
;
1454 else if (!strcmp(arg
, "--check"))
1455 options
->output_format
= DIFF_FORMAT_CHECKDIFF
;
1456 else if (!strcmp(arg
, "--summary"))
1457 options
->summary
= 1;
1458 else if (!strcmp(arg
, "--patch-with-stat")) {
1459 options
->output_format
= DIFF_FORMAT_PATCH
;
1460 options
->with_stat
= 1;
1462 else if (!strcmp(arg
, "-z"))
1463 options
->line_termination
= 0;
1464 else if (!strncmp(arg
, "-l", 2))
1465 options
->rename_limit
= strtoul(arg
+2, NULL
, 10);
1466 else if (!strcmp(arg
, "--full-index"))
1467 options
->full_index
= 1;
1468 else if (!strcmp(arg
, "--binary")) {
1469 options
->output_format
= DIFF_FORMAT_PATCH
;
1470 options
->full_index
= options
->binary
= 1;
1472 else if (!strcmp(arg
, "--name-only"))
1473 options
->output_format
= DIFF_FORMAT_NAME
;
1474 else if (!strcmp(arg
, "--name-status"))
1475 options
->output_format
= DIFF_FORMAT_NAME_STATUS
;
1476 else if (!strcmp(arg
, "-R"))
1477 options
->reverse_diff
= 1;
1478 else if (!strncmp(arg
, "-S", 2))
1479 options
->pickaxe
= arg
+ 2;
1480 else if (!strcmp(arg
, "-s"))
1481 options
->output_format
= DIFF_FORMAT_NO_OUTPUT
;
1482 else if (!strncmp(arg
, "-O", 2))
1483 options
->orderfile
= arg
+ 2;
1484 else if (!strncmp(arg
, "--diff-filter=", 14))
1485 options
->filter
= arg
+ 14;
1486 else if (!strcmp(arg
, "--pickaxe-all"))
1487 options
->pickaxe_opts
= DIFF_PICKAXE_ALL
;
1488 else if (!strcmp(arg
, "--pickaxe-regex"))
1489 options
->pickaxe_opts
= DIFF_PICKAXE_REGEX
;
1490 else if (!strncmp(arg
, "-B", 2)) {
1491 if ((options
->break_opt
=
1492 diff_scoreopt_parse(arg
)) == -1)
1495 else if (!strncmp(arg
, "-M", 2)) {
1496 if ((options
->rename_score
=
1497 diff_scoreopt_parse(arg
)) == -1)
1499 options
->detect_rename
= DIFF_DETECT_RENAME
;
1501 else if (!strncmp(arg
, "-C", 2)) {
1502 if ((options
->rename_score
=
1503 diff_scoreopt_parse(arg
)) == -1)
1505 options
->detect_rename
= DIFF_DETECT_COPY
;
1507 else if (!strcmp(arg
, "--find-copies-harder"))
1508 options
->find_copies_harder
= 1;
1509 else if (!strcmp(arg
, "--abbrev"))
1510 options
->abbrev
= DEFAULT_ABBREV
;
1511 else if (!strncmp(arg
, "--abbrev=", 9)) {
1512 options
->abbrev
= strtoul(arg
+ 9, NULL
, 10);
1513 if (options
->abbrev
< MINIMUM_ABBREV
)
1514 options
->abbrev
= MINIMUM_ABBREV
;
1515 else if (40 < options
->abbrev
)
1516 options
->abbrev
= 40;
1518 else if (!strcmp(arg
, "--color"))
1519 options
->color_diff
= 1;
1525 static int parse_num(const char **cp_p
)
1527 unsigned long num
, scale
;
1529 const char *cp
= *cp_p
;
1536 if ( !dot
&& ch
== '.' ) {
1539 } else if ( ch
== '%' ) {
1540 scale
= dot
? scale
*100 : 100;
1541 cp
++; /* % is always at the end */
1543 } else if ( ch
>= '0' && ch
<= '9' ) {
1544 if ( scale
< 100000 ) {
1546 num
= (num
*10) + (ch
-'0');
1555 /* user says num divided by scale and we say internally that
1556 * is MAX_SCORE * num / scale.
1558 return (num
>= scale
) ? MAX_SCORE
: (MAX_SCORE
* num
/ scale
);
1561 int diff_scoreopt_parse(const char *opt
)
1563 int opt1
, opt2
, cmd
;
1568 if (cmd
!= 'M' && cmd
!= 'C' && cmd
!= 'B')
1569 return -1; /* that is not a -M, -C nor -B option */
1571 opt1
= parse_num(&opt
);
1577 else if (*opt
!= '/')
1578 return -1; /* we expect -B80/99 or -B80 */
1581 opt2
= parse_num(&opt
);
1586 return opt1
| (opt2
<< 16);
1589 struct diff_queue_struct diff_queued_diff
;
1591 void diff_q(struct diff_queue_struct
*queue
, struct diff_filepair
*dp
)
1593 if (queue
->alloc
<= queue
->nr
) {
1594 queue
->alloc
= alloc_nr(queue
->alloc
);
1595 queue
->queue
= xrealloc(queue
->queue
,
1596 sizeof(dp
) * queue
->alloc
);
1598 queue
->queue
[queue
->nr
++] = dp
;
1601 struct diff_filepair
*diff_queue(struct diff_queue_struct
*queue
,
1602 struct diff_filespec
*one
,
1603 struct diff_filespec
*two
)
1605 struct diff_filepair
*dp
= xmalloc(sizeof(*dp
));
1610 dp
->source_stays
= 0;
1611 dp
->broken_pair
= 0;
1617 void diff_free_filepair(struct diff_filepair
*p
)
1619 diff_free_filespec_data(p
->one
);
1620 diff_free_filespec_data(p
->two
);
1626 /* This is different from find_unique_abbrev() in that
1627 * it stuffs the result with dots for alignment.
1629 const char *diff_unique_abbrev(const unsigned char *sha1
, int len
)
1634 return sha1_to_hex(sha1
);
1636 abbrev
= find_unique_abbrev(sha1
, len
);
1638 return sha1_to_hex(sha1
);
1639 abblen
= strlen(abbrev
);
1641 static char hex
[41];
1642 if (len
< abblen
&& abblen
<= len
+ 2)
1643 sprintf(hex
, "%s%.*s", abbrev
, len
+3-abblen
, "..");
1645 sprintf(hex
, "%s...", abbrev
);
1648 return sha1_to_hex(sha1
);
1651 static void diff_flush_raw(struct diff_filepair
*p
,
1652 int line_termination
,
1653 int inter_name_termination
,
1654 struct diff_options
*options
,
1659 int abbrev
= options
->abbrev
;
1660 const char *path_one
, *path_two
;
1662 path_one
= p
->one
->path
;
1663 path_two
= p
->two
->path
;
1664 if (line_termination
) {
1665 path_one
= quote_one(path_one
);
1666 path_two
= quote_one(path_two
);
1670 sprintf(status
, "%c%03d", p
->status
,
1671 (int)(0.5 + p
->score
* 100.0/MAX_SCORE
));
1673 status
[0] = p
->status
;
1676 switch (p
->status
) {
1677 case DIFF_STATUS_COPIED
:
1678 case DIFF_STATUS_RENAMED
:
1681 case DIFF_STATUS_ADDED
:
1682 case DIFF_STATUS_DELETED
:
1689 if (output_format
!= DIFF_FORMAT_NAME_STATUS
) {
1690 printf(":%06o %06o %s ",
1691 p
->one
->mode
, p
->two
->mode
,
1692 diff_unique_abbrev(p
->one
->sha1
, abbrev
));
1694 diff_unique_abbrev(p
->two
->sha1
, abbrev
));
1696 printf("%s%c%s", status
, inter_name_termination
, path_one
);
1698 printf("%c%s", inter_name_termination
, path_two
);
1699 putchar(line_termination
);
1700 if (path_one
!= p
->one
->path
)
1701 free((void*)path_one
);
1702 if (path_two
!= p
->two
->path
)
1703 free((void*)path_two
);
1706 static void diff_flush_name(struct diff_filepair
*p
,
1707 int inter_name_termination
,
1708 int line_termination
)
1710 char *path
= p
->two
->path
;
1712 if (line_termination
)
1713 path
= quote_one(p
->two
->path
);
1715 path
= p
->two
->path
;
1716 printf("%s%c", path
, line_termination
);
1717 if (p
->two
->path
!= path
)
1721 int diff_unmodified_pair(struct diff_filepair
*p
)
1723 /* This function is written stricter than necessary to support
1724 * the currently implemented transformers, but the idea is to
1725 * let transformers to produce diff_filepairs any way they want,
1726 * and filter and clean them up here before producing the output.
1728 struct diff_filespec
*one
, *two
;
1730 if (DIFF_PAIR_UNMERGED(p
))
1731 return 0; /* unmerged is interesting */
1736 /* deletion, addition, mode or type change
1737 * and rename are all interesting.
1739 if (DIFF_FILE_VALID(one
) != DIFF_FILE_VALID(two
) ||
1740 DIFF_PAIR_MODE_CHANGED(p
) ||
1741 strcmp(one
->path
, two
->path
))
1744 /* both are valid and point at the same path. that is, we are
1745 * dealing with a change.
1747 if (one
->sha1_valid
&& two
->sha1_valid
&&
1748 !memcmp(one
->sha1
, two
->sha1
, sizeof(one
->sha1
)))
1749 return 1; /* no change */
1750 if (!one
->sha1_valid
&& !two
->sha1_valid
)
1751 return 1; /* both look at the same file on the filesystem. */
1755 static void diff_flush_patch(struct diff_filepair
*p
, struct diff_options
*o
)
1757 if (diff_unmodified_pair(p
))
1760 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
1761 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
1762 return; /* no tree diffs in patch format */
1767 static void diff_flush_stat(struct diff_filepair
*p
, struct diff_options
*o
,
1768 struct diffstat_t
*diffstat
)
1770 if (diff_unmodified_pair(p
))
1773 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
1774 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
1775 return; /* no tree diffs in patch format */
1777 run_diffstat(p
, o
, diffstat
);
1780 static void diff_flush_checkdiff(struct diff_filepair
*p
,
1781 struct diff_options
*o
)
1783 if (diff_unmodified_pair(p
))
1786 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
1787 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
1788 return; /* no tree diffs in patch format */
1790 run_checkdiff(p
, o
);
1793 int diff_queue_is_empty(void)
1795 struct diff_queue_struct
*q
= &diff_queued_diff
;
1797 for (i
= 0; i
< q
->nr
; i
++)
1798 if (!diff_unmodified_pair(q
->queue
[i
]))
1804 void diff_debug_filespec(struct diff_filespec
*s
, int x
, const char *one
)
1806 fprintf(stderr
, "queue[%d] %s (%s) %s %06o %s\n",
1809 DIFF_FILE_VALID(s
) ? "valid" : "invalid",
1811 s
->sha1_valid
? sha1_to_hex(s
->sha1
) : "");
1812 fprintf(stderr
, "queue[%d] %s size %lu flags %d\n",
1814 s
->size
, s
->xfrm_flags
);
1817 void diff_debug_filepair(const struct diff_filepair
*p
, int i
)
1819 diff_debug_filespec(p
->one
, i
, "one");
1820 diff_debug_filespec(p
->two
, i
, "two");
1821 fprintf(stderr
, "score %d, status %c stays %d broken %d\n",
1822 p
->score
, p
->status
? p
->status
: '?',
1823 p
->source_stays
, p
->broken_pair
);
1826 void diff_debug_queue(const char *msg
, struct diff_queue_struct
*q
)
1830 fprintf(stderr
, "%s\n", msg
);
1831 fprintf(stderr
, "q->nr = %d\n", q
->nr
);
1832 for (i
= 0; i
< q
->nr
; i
++) {
1833 struct diff_filepair
*p
= q
->queue
[i
];
1834 diff_debug_filepair(p
, i
);
1839 static void diff_resolve_rename_copy(void)
1842 struct diff_filepair
*p
, *pp
;
1843 struct diff_queue_struct
*q
= &diff_queued_diff
;
1845 diff_debug_queue("resolve-rename-copy", q
);
1847 for (i
= 0; i
< q
->nr
; i
++) {
1849 p
->status
= 0; /* undecided */
1850 if (DIFF_PAIR_UNMERGED(p
))
1851 p
->status
= DIFF_STATUS_UNMERGED
;
1852 else if (!DIFF_FILE_VALID(p
->one
))
1853 p
->status
= DIFF_STATUS_ADDED
;
1854 else if (!DIFF_FILE_VALID(p
->two
))
1855 p
->status
= DIFF_STATUS_DELETED
;
1856 else if (DIFF_PAIR_TYPE_CHANGED(p
))
1857 p
->status
= DIFF_STATUS_TYPE_CHANGED
;
1859 /* from this point on, we are dealing with a pair
1860 * whose both sides are valid and of the same type, i.e.
1861 * either in-place edit or rename/copy edit.
1863 else if (DIFF_PAIR_RENAME(p
)) {
1864 if (p
->source_stays
) {
1865 p
->status
= DIFF_STATUS_COPIED
;
1868 /* See if there is some other filepair that
1869 * copies from the same source as us. If so
1870 * we are a copy. Otherwise we are either a
1871 * copy if the path stays, or a rename if it
1872 * does not, but we already handled "stays" case.
1874 for (j
= i
+ 1; j
< q
->nr
; j
++) {
1876 if (strcmp(pp
->one
->path
, p
->one
->path
))
1877 continue; /* not us */
1878 if (!DIFF_PAIR_RENAME(pp
))
1879 continue; /* not a rename/copy */
1880 /* pp is a rename/copy from the same source */
1881 p
->status
= DIFF_STATUS_COPIED
;
1885 p
->status
= DIFF_STATUS_RENAMED
;
1887 else if (memcmp(p
->one
->sha1
, p
->two
->sha1
, 20) ||
1888 p
->one
->mode
!= p
->two
->mode
)
1889 p
->status
= DIFF_STATUS_MODIFIED
;
1891 /* This is a "no-change" entry and should not
1892 * happen anymore, but prepare for broken callers.
1894 error("feeding unmodified %s to diffcore",
1896 p
->status
= DIFF_STATUS_UNKNOWN
;
1899 diff_debug_queue("resolve-rename-copy done", q
);
1902 static void flush_one_pair(struct diff_filepair
*p
,
1903 int diff_output_format
,
1904 struct diff_options
*options
,
1905 struct diffstat_t
*diffstat
)
1907 int inter_name_termination
= '\t';
1908 int line_termination
= options
->line_termination
;
1909 if (!line_termination
)
1910 inter_name_termination
= 0;
1912 switch (p
->status
) {
1913 case DIFF_STATUS_UNKNOWN
:
1916 die("internal error in diff-resolve-rename-copy");
1919 switch (diff_output_format
) {
1920 case DIFF_FORMAT_DIFFSTAT
:
1921 diff_flush_stat(p
, options
, diffstat
);
1923 case DIFF_FORMAT_CHECKDIFF
:
1924 diff_flush_checkdiff(p
, options
);
1926 case DIFF_FORMAT_PATCH
:
1927 diff_flush_patch(p
, options
);
1929 case DIFF_FORMAT_RAW
:
1930 case DIFF_FORMAT_NAME_STATUS
:
1931 diff_flush_raw(p
, line_termination
,
1932 inter_name_termination
,
1933 options
, diff_output_format
);
1935 case DIFF_FORMAT_NAME
:
1937 inter_name_termination
,
1940 case DIFF_FORMAT_NO_OUTPUT
:
1946 static void show_file_mode_name(const char *newdelete
, struct diff_filespec
*fs
)
1949 printf(" %s mode %06o %s\n", newdelete
, fs
->mode
, fs
->path
);
1951 printf(" %s %s\n", newdelete
, fs
->path
);
1955 static void show_mode_change(struct diff_filepair
*p
, int show_name
)
1957 if (p
->one
->mode
&& p
->two
->mode
&& p
->one
->mode
!= p
->two
->mode
) {
1959 printf(" mode change %06o => %06o %s\n",
1960 p
->one
->mode
, p
->two
->mode
, p
->two
->path
);
1962 printf(" mode change %06o => %06o\n",
1963 p
->one
->mode
, p
->two
->mode
);
1967 static void show_rename_copy(const char *renamecopy
, struct diff_filepair
*p
)
1969 const char *old
, *new;
1971 /* Find common prefix */
1975 const char *slash_old
, *slash_new
;
1976 slash_old
= strchr(old
, '/');
1977 slash_new
= strchr(new, '/');
1980 slash_old
- old
!= slash_new
- new ||
1981 memcmp(old
, new, slash_new
- new))
1983 old
= slash_old
+ 1;
1984 new = slash_new
+ 1;
1986 /* p->one->path thru old is the common prefix, and old and new
1987 * through the end of names are renames
1989 if (old
!= p
->one
->path
)
1990 printf(" %s %.*s{%s => %s} (%d%%)\n", renamecopy
,
1991 (int)(old
- p
->one
->path
), p
->one
->path
,
1992 old
, new, (int)(0.5 + p
->score
* 100.0/MAX_SCORE
));
1994 printf(" %s %s => %s (%d%%)\n", renamecopy
,
1995 p
->one
->path
, p
->two
->path
,
1996 (int)(0.5 + p
->score
* 100.0/MAX_SCORE
));
1997 show_mode_change(p
, 0);
2000 static void diff_summary(struct diff_filepair
*p
)
2003 case DIFF_STATUS_DELETED
:
2004 show_file_mode_name("delete", p
->one
);
2006 case DIFF_STATUS_ADDED
:
2007 show_file_mode_name("create", p
->two
);
2009 case DIFF_STATUS_COPIED
:
2010 show_rename_copy("copy", p
);
2012 case DIFF_STATUS_RENAMED
:
2013 show_rename_copy("rename", p
);
2017 printf(" rewrite %s (%d%%)\n", p
->two
->path
,
2018 (int)(0.5 + p
->score
* 100.0/MAX_SCORE
));
2019 show_mode_change(p
, 0);
2020 } else show_mode_change(p
, 1);
2025 void diff_flush(struct diff_options
*options
)
2027 struct diff_queue_struct
*q
= &diff_queued_diff
;
2029 int diff_output_format
= options
->output_format
;
2030 struct diffstat_t
*diffstat
= NULL
;
2032 if (diff_output_format
== DIFF_FORMAT_DIFFSTAT
|| options
->with_stat
) {
2033 diffstat
= xcalloc(sizeof (struct diffstat_t
), 1);
2034 diffstat
->xm
.consume
= diffstat_consume
;
2037 if (options
->with_raw
) {
2038 for (i
= 0; i
< q
->nr
; i
++) {
2039 struct diff_filepair
*p
= q
->queue
[i
];
2040 flush_one_pair(p
, DIFF_FORMAT_RAW
, options
, NULL
);
2042 putchar(options
->line_termination
);
2044 if (options
->with_stat
) {
2045 for (i
= 0; i
< q
->nr
; i
++) {
2046 struct diff_filepair
*p
= q
->queue
[i
];
2047 flush_one_pair(p
, DIFF_FORMAT_DIFFSTAT
, options
,
2050 show_stats(diffstat
);
2053 if (options
->summary
)
2054 for (i
= 0; i
< q
->nr
; i
++)
2055 diff_summary(q
->queue
[i
]);
2056 if (options
->stat_sep
)
2057 fputs(options
->stat_sep
, stdout
);
2059 putchar(options
->line_termination
);
2061 for (i
= 0; i
< q
->nr
; i
++) {
2062 struct diff_filepair
*p
= q
->queue
[i
];
2063 flush_one_pair(p
, diff_output_format
, options
, diffstat
);
2067 show_stats(diffstat
);
2071 for (i
= 0; i
< q
->nr
; i
++) {
2072 if (diffstat
&& options
->summary
)
2073 diff_summary(q
->queue
[i
]);
2074 diff_free_filepair(q
->queue
[i
]);
2079 q
->nr
= q
->alloc
= 0;
2082 static void diffcore_apply_filter(const char *filter
)
2085 struct diff_queue_struct
*q
= &diff_queued_diff
;
2086 struct diff_queue_struct outq
;
2088 outq
.nr
= outq
.alloc
= 0;
2093 if (strchr(filter
, DIFF_STATUS_FILTER_AON
)) {
2095 for (i
= found
= 0; !found
&& i
< q
->nr
; i
++) {
2096 struct diff_filepair
*p
= q
->queue
[i
];
2097 if (((p
->status
== DIFF_STATUS_MODIFIED
) &&
2099 strchr(filter
, DIFF_STATUS_FILTER_BROKEN
)) ||
2101 strchr(filter
, DIFF_STATUS_MODIFIED
)))) ||
2102 ((p
->status
!= DIFF_STATUS_MODIFIED
) &&
2103 strchr(filter
, p
->status
)))
2109 /* otherwise we will clear the whole queue
2110 * by copying the empty outq at the end of this
2111 * function, but first clear the current entries
2114 for (i
= 0; i
< q
->nr
; i
++)
2115 diff_free_filepair(q
->queue
[i
]);
2118 /* Only the matching ones */
2119 for (i
= 0; i
< q
->nr
; i
++) {
2120 struct diff_filepair
*p
= q
->queue
[i
];
2122 if (((p
->status
== DIFF_STATUS_MODIFIED
) &&
2124 strchr(filter
, DIFF_STATUS_FILTER_BROKEN
)) ||
2126 strchr(filter
, DIFF_STATUS_MODIFIED
)))) ||
2127 ((p
->status
!= DIFF_STATUS_MODIFIED
) &&
2128 strchr(filter
, p
->status
)))
2131 diff_free_filepair(p
);
2138 void diffcore_std(struct diff_options
*options
)
2140 if (options
->break_opt
!= -1)
2141 diffcore_break(options
->break_opt
);
2142 if (options
->detect_rename
)
2143 diffcore_rename(options
);
2144 if (options
->break_opt
!= -1)
2145 diffcore_merge_broken();
2146 if (options
->pickaxe
)
2147 diffcore_pickaxe(options
->pickaxe
, options
->pickaxe_opts
);
2148 if (options
->orderfile
)
2149 diffcore_order(options
->orderfile
);
2150 diff_resolve_rename_copy();
2151 diffcore_apply_filter(options
->filter
);
2155 void diffcore_std_no_resolve(struct diff_options
*options
)
2157 if (options
->pickaxe
)
2158 diffcore_pickaxe(options
->pickaxe
, options
->pickaxe_opts
);
2159 if (options
->orderfile
)
2160 diffcore_order(options
->orderfile
);
2161 diffcore_apply_filter(options
->filter
);
2164 void diff_addremove(struct diff_options
*options
,
2165 int addremove
, unsigned mode
,
2166 const unsigned char *sha1
,
2167 const char *base
, const char *path
)
2169 char concatpath
[PATH_MAX
];
2170 struct diff_filespec
*one
, *two
;
2172 /* This may look odd, but it is a preparation for
2173 * feeding "there are unchanged files which should
2174 * not produce diffs, but when you are doing copy
2175 * detection you would need them, so here they are"
2176 * entries to the diff-core. They will be prefixed
2177 * with something like '=' or '*' (I haven't decided
2178 * which but should not make any difference).
2179 * Feeding the same new and old to diff_change()
2180 * also has the same effect.
2181 * Before the final output happens, they are pruned after
2182 * merged into rename/copy pairs as appropriate.
2184 if (options
->reverse_diff
)
2185 addremove
= (addremove
== '+' ? '-' :
2186 addremove
== '-' ? '+' : addremove
);
2188 if (!path
) path
= "";
2189 sprintf(concatpath
, "%s%s", base
, path
);
2190 one
= alloc_filespec(concatpath
);
2191 two
= alloc_filespec(concatpath
);
2193 if (addremove
!= '+')
2194 fill_filespec(one
, sha1
, mode
);
2195 if (addremove
!= '-')
2196 fill_filespec(two
, sha1
, mode
);
2198 diff_queue(&diff_queued_diff
, one
, two
);
2201 void diff_change(struct diff_options
*options
,
2202 unsigned old_mode
, unsigned new_mode
,
2203 const unsigned char *old_sha1
,
2204 const unsigned char *new_sha1
,
2205 const char *base
, const char *path
)
2207 char concatpath
[PATH_MAX
];
2208 struct diff_filespec
*one
, *two
;
2210 if (options
->reverse_diff
) {
2212 const unsigned char *tmp_c
;
2213 tmp
= old_mode
; old_mode
= new_mode
; new_mode
= tmp
;
2214 tmp_c
= old_sha1
; old_sha1
= new_sha1
; new_sha1
= tmp_c
;
2216 if (!path
) path
= "";
2217 sprintf(concatpath
, "%s%s", base
, path
);
2218 one
= alloc_filespec(concatpath
);
2219 two
= alloc_filespec(concatpath
);
2220 fill_filespec(one
, old_sha1
, old_mode
);
2221 fill_filespec(two
, new_sha1
, new_mode
);
2223 diff_queue(&diff_queued_diff
, one
, two
);
2226 void diff_unmerge(struct diff_options
*options
,
2229 struct diff_filespec
*one
, *two
;
2230 one
= alloc_filespec(path
);
2231 two
= alloc_filespec(path
);
2232 diff_queue(&diff_queued_diff
, one
, two
);