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;
29 #define COLOR_NORMAL ""
30 #define COLOR_BOLD "\033[1m"
31 #define COLOR_DIM "\033[2m"
32 #define COLOR_UL "\033[4m"
33 #define COLOR_BLINK "\033[5m"
34 #define COLOR_REVERSE "\033[7m"
35 #define COLOR_RESET "\033[m"
37 #define COLOR_BLACK "\033[30m"
38 #define COLOR_RED "\033[31m"
39 #define COLOR_GREEN "\033[32m"
40 #define COLOR_YELLOW "\033[33m"
41 #define COLOR_BLUE "\033[34m"
42 #define COLOR_MAGENTA "\033[35m"
43 #define COLOR_CYAN "\033[36m"
44 #define COLOR_WHITE "\033[37m"
46 static const char *diff_colors
[] = {
47 [DIFF_RESET
] = COLOR_RESET
,
48 [DIFF_PLAIN
] = COLOR_NORMAL
,
49 [DIFF_METAINFO
] = COLOR_BOLD
,
50 [DIFF_FRAGINFO
] = COLOR_CYAN
,
51 [DIFF_FILE_OLD
] = COLOR_RED
,
52 [DIFF_FILE_NEW
] = COLOR_GREEN
,
55 static int parse_diff_color_slot(const char *var
, int ofs
)
57 if (!strcasecmp(var
+ofs
, "plain"))
59 if (!strcasecmp(var
+ofs
, "meta"))
61 if (!strcasecmp(var
+ofs
, "frag"))
63 if (!strcasecmp(var
+ofs
, "old"))
65 if (!strcasecmp(var
+ofs
, "new"))
67 die("bad config variable '%s'", var
);
70 static const char *parse_diff_color_value(const char *value
, const char *var
)
72 if (!strcasecmp(value
, "normal"))
74 if (!strcasecmp(value
, "bold"))
76 if (!strcasecmp(value
, "dim"))
78 if (!strcasecmp(value
, "ul"))
80 if (!strcasecmp(value
, "blink"))
82 if (!strcasecmp(value
, "reverse"))
84 if (!strcasecmp(value
, "reset"))
86 if (!strcasecmp(value
, "black"))
88 if (!strcasecmp(value
, "red"))
90 if (!strcasecmp(value
, "green"))
92 if (!strcasecmp(value
, "yellow"))
94 if (!strcasecmp(value
, "blue"))
96 if (!strcasecmp(value
, "magenta"))
98 if (!strcasecmp(value
, "cyan"))
100 if (!strcasecmp(value
, "white"))
102 die("bad config value '%s' for variable '%s'", value
, var
);
105 int git_diff_config(const char *var
, const char *value
)
107 if (!strcmp(var
, "diff.renamelimit")) {
108 diff_rename_limit_default
= git_config_int(var
, value
);
111 if (!strcmp(var
, "diff.color")) {
113 diff_use_color_default
= 1; /* bool */
114 else if (!strcasecmp(value
, "auto"))
115 diff_use_color_default
= isatty(1);
116 else if (!strcasecmp(value
, "never"))
117 diff_use_color_default
= 0;
118 else if (!strcasecmp(value
, "always"))
119 diff_use_color_default
= 1;
121 diff_use_color_default
= git_config_bool(var
, value
);
124 if (!strcmp(var
, "diff.renames")) {
126 diff_detect_rename_default
= DIFF_DETECT_RENAME
;
127 else if (!strcasecmp(value
, "copies") ||
128 !strcasecmp(value
, "copy"))
129 diff_detect_rename_default
= DIFF_DETECT_COPY
;
130 else if (git_config_bool(var
,value
))
131 diff_detect_rename_default
= DIFF_DETECT_RENAME
;
134 if (!strncmp(var
, "diff.color.", 11)) {
135 int slot
= parse_diff_color_slot(var
, 11);
136 diff_colors
[slot
] = parse_diff_color_value(value
, var
);
139 return git_default_config(var
, value
);
142 static char *quote_one(const char *str
)
149 needlen
= quote_c_style(str
, NULL
, NULL
, 0);
152 xp
= xmalloc(needlen
+ 1);
153 quote_c_style(str
, xp
, NULL
, 0);
157 static char *quote_two(const char *one
, const char *two
)
159 int need_one
= quote_c_style(one
, NULL
, NULL
, 1);
160 int need_two
= quote_c_style(two
, NULL
, NULL
, 1);
163 if (need_one
+ need_two
) {
164 if (!need_one
) need_one
= strlen(one
);
165 if (!need_two
) need_one
= strlen(two
);
167 xp
= xmalloc(need_one
+ need_two
+ 3);
169 quote_c_style(one
, xp
+ 1, NULL
, 1);
170 quote_c_style(two
, xp
+ need_one
+ 1, NULL
, 1);
171 strcpy(xp
+ need_one
+ need_two
+ 1, "\"");
174 need_one
= strlen(one
);
175 need_two
= strlen(two
);
176 xp
= xmalloc(need_one
+ need_two
+ 1);
178 strcpy(xp
+ need_one
, two
);
182 static const char *external_diff(void)
184 static const char *external_diff_cmd
= NULL
;
185 static int done_preparing
= 0;
188 return external_diff_cmd
;
189 external_diff_cmd
= getenv("GIT_EXTERNAL_DIFF");
191 return external_diff_cmd
;
194 #define TEMPFILE_PATH_LEN 50
196 static struct diff_tempfile
{
197 const char *name
; /* filename external diff should read from */
200 char tmp_path
[TEMPFILE_PATH_LEN
];
203 static int count_lines(const char *data
, int size
)
205 int count
, ch
, completely_empty
= 1, nl_just_seen
= 0;
212 completely_empty
= 0;
216 completely_empty
= 0;
219 if (completely_empty
)
222 count
++; /* no trailing newline */
226 static void print_line_count(int count
)
236 printf("1,%d", count
);
241 static void copy_file(int prefix
, const char *data
, int size
)
243 int ch
, nl_just_seen
= 1;
255 printf("\n\\ No newline at end of file\n");
258 static void emit_rewrite_diff(const char *name_a
,
260 struct diff_filespec
*one
,
261 struct diff_filespec
*two
)
264 diff_populate_filespec(one
, 0);
265 diff_populate_filespec(two
, 0);
266 lc_a
= count_lines(one
->data
, one
->size
);
267 lc_b
= count_lines(two
->data
, two
->size
);
268 printf("--- %s\n+++ %s\n@@ -", name_a
, name_b
);
269 print_line_count(lc_a
);
271 print_line_count(lc_b
);
274 copy_file('-', one
->data
, one
->size
);
276 copy_file('+', two
->data
, two
->size
);
279 static int fill_mmfile(mmfile_t
*mf
, struct diff_filespec
*one
)
281 if (!DIFF_FILE_VALID(one
)) {
282 mf
->ptr
= (char *)""; /* does not matter */
286 else if (diff_populate_filespec(one
, 0))
289 mf
->size
= one
->size
;
293 struct emit_callback
{
294 struct xdiff_emit_state xm
;
295 int nparents
, color_diff
;
296 const char **label_path
;
299 static inline const char *get_color(int diff_use_color
, enum color_diff ix
)
302 return diff_colors
[ix
];
306 static void fn_out_consume(void *priv
, char *line
, unsigned long len
)
309 struct emit_callback
*ecbdata
= priv
;
310 const char *set
= get_color(ecbdata
->color_diff
, DIFF_METAINFO
);
311 const char *reset
= get_color(ecbdata
->color_diff
, DIFF_RESET
);
313 if (ecbdata
->label_path
[0]) {
314 printf("%s--- %s%s\n", set
, ecbdata
->label_path
[0], reset
);
315 printf("%s+++ %s%s\n", set
, ecbdata
->label_path
[1], reset
);
316 ecbdata
->label_path
[0] = ecbdata
->label_path
[1] = NULL
;
319 /* This is not really necessary for now because
320 * this codepath only deals with two-way diffs.
322 for (i
= 0; i
< len
&& line
[i
] == '@'; i
++)
324 if (2 <= i
&& i
< len
&& line
[i
] == ' ') {
325 ecbdata
->nparents
= i
- 1;
326 set
= get_color(ecbdata
->color_diff
, DIFF_FRAGINFO
);
328 else if (len
< ecbdata
->nparents
)
331 int nparents
= ecbdata
->nparents
;
332 int color
= DIFF_PLAIN
;
333 for (i
= 0; i
< nparents
&& len
; i
++) {
335 color
= DIFF_FILE_OLD
;
336 else if (line
[i
] == '+')
337 color
= DIFF_FILE_NEW
;
339 set
= get_color(ecbdata
->color_diff
, color
);
341 if (len
> 0 && line
[len
-1] == '\n')
344 fwrite (line
, len
, 1, stdout
);
348 static char *pprint_rename(const char *a
, const char *b
)
353 int pfx_length
, sfx_length
;
354 int len_a
= strlen(a
);
355 int len_b
= strlen(b
);
357 /* Find common prefix */
359 while (*old
&& *new && *old
== *new) {
361 pfx_length
= old
- a
+ 1;
366 /* Find common suffix */
370 while (a
<= old
&& b
<= new && *old
== *new) {
372 sfx_length
= len_a
- (old
- a
);
378 * pfx{mid-a => mid-b}sfx
379 * {pfx-a => pfx-b}sfx
380 * pfx{sfx-a => sfx-b}
383 if (pfx_length
+ sfx_length
) {
384 int a_midlen
= len_a
- pfx_length
- sfx_length
;
385 int b_midlen
= len_b
- pfx_length
- sfx_length
;
386 if (a_midlen
< 0) a_midlen
= 0;
387 if (b_midlen
< 0) b_midlen
= 0;
389 name
= xmalloc(pfx_length
+ a_midlen
+ b_midlen
+ sfx_length
+ 7);
390 sprintf(name
, "%.*s{%.*s => %.*s}%s",
392 a_midlen
, a
+ pfx_length
,
393 b_midlen
, b
+ pfx_length
,
394 a
+ len_a
- sfx_length
);
397 name
= xmalloc(len_a
+ len_b
+ 5);
398 sprintf(name
, "%s => %s", a
, b
);
404 struct xdiff_emit_state xm
;
408 struct diffstat_file
{
410 unsigned is_unmerged
:1;
411 unsigned is_binary
:1;
412 unsigned is_renamed
:1;
413 unsigned int added
, deleted
;
417 static struct diffstat_file
*diffstat_add(struct diffstat_t
*diffstat
,
421 struct diffstat_file
*x
;
422 x
= xcalloc(sizeof (*x
), 1);
423 if (diffstat
->nr
== diffstat
->alloc
) {
424 diffstat
->alloc
= alloc_nr(diffstat
->alloc
);
425 diffstat
->files
= xrealloc(diffstat
->files
,
426 diffstat
->alloc
* sizeof(x
));
428 diffstat
->files
[diffstat
->nr
++] = x
;
430 x
->name
= pprint_rename(name_a
, name_b
);
434 x
->name
= strdup(name_a
);
438 static void diffstat_consume(void *priv
, char *line
, unsigned long len
)
440 struct diffstat_t
*diffstat
= priv
;
441 struct diffstat_file
*x
= diffstat
->files
[diffstat
->nr
- 1];
445 else if (line
[0] == '-')
449 static const char pluses
[] = "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++";
450 static const char minuses
[]= "----------------------------------------------------------------------";
451 const char mime_boundary_leader
[] = "------------";
453 static void show_stats(struct diffstat_t
* data
)
455 int i
, len
, add
, del
, total
, adds
= 0, dels
= 0;
456 int max
, max_change
= 0, max_len
= 0;
457 int total_files
= data
->nr
;
462 for (i
= 0; i
< data
->nr
; i
++) {
463 struct diffstat_file
*file
= data
->files
[i
];
465 len
= strlen(file
->name
);
469 if (file
->is_binary
|| file
->is_unmerged
)
471 if (max_change
< file
->added
+ file
->deleted
)
472 max_change
= file
->added
+ file
->deleted
;
475 for (i
= 0; i
< data
->nr
; i
++) {
476 const char *prefix
= "";
477 char *name
= data
->files
[i
]->name
;
478 int added
= data
->files
[i
]->added
;
479 int deleted
= data
->files
[i
]->deleted
;
481 if (0 < (len
= quote_c_style(name
, NULL
, NULL
, 0))) {
482 char *qname
= xmalloc(len
+ 1);
483 quote_c_style(name
, qname
, NULL
, 0);
485 data
->files
[i
]->name
= name
= qname
;
489 * "scale" the filename
500 slash
= strchr(name
, '/');
507 * scale the add/delete
513 if (data
->files
[i
]->is_binary
) {
514 printf(" %s%-*s | Bin\n", prefix
, len
, name
);
515 goto free_diffstat_file
;
517 else if (data
->files
[i
]->is_unmerged
) {
518 printf(" %s%-*s | Unmerged\n", prefix
, len
, name
);
519 goto free_diffstat_file
;
521 else if (!data
->files
[i
]->is_renamed
&&
522 (added
+ deleted
== 0)) {
524 goto free_diffstat_file
;
533 if (max_change
> 0) {
534 total
= (total
* max
+ max_change
/ 2) / max_change
;
535 add
= (add
* max
+ max_change
/ 2) / max_change
;
538 printf(" %s%-*s |%5d %.*s%.*s\n", prefix
,
539 len
, name
, added
+ deleted
,
540 add
, pluses
, del
, minuses
);
542 free(data
->files
[i
]->name
);
543 free(data
->files
[i
]);
546 printf(" %d files changed, %d insertions(+), %d deletions(-)\n",
547 total_files
, adds
, dels
);
551 struct xdiff_emit_state xm
;
552 const char *filename
;
556 static void checkdiff_consume(void *priv
, char *line
, unsigned long len
)
558 struct checkdiff_t
*data
= priv
;
560 if (line
[0] == '+') {
565 /* check space before tab */
566 for (i
= 1; i
< len
&& (line
[i
] == ' ' || line
[i
] == '\t'); i
++)
569 if (line
[i
- 1] == '\t' && spaces
)
570 printf("%s:%d: space before tab:%.*s\n",
571 data
->filename
, data
->lineno
, (int)len
, line
);
573 /* check white space at line end */
574 if (line
[len
- 1] == '\n')
576 if (isspace(line
[len
- 1]))
577 printf("%s:%d: white space at end: %.*s\n",
578 data
->filename
, data
->lineno
, (int)len
, line
);
579 } else if (line
[0] == ' ')
581 else if (line
[0] == '@') {
582 char *plus
= strchr(line
, '+');
584 data
->lineno
= strtol(plus
, NULL
, 10);
590 static unsigned char *deflate_it(char *data
,
592 unsigned long *result_size
)
595 unsigned char *deflated
;
598 memset(&stream
, 0, sizeof(stream
));
599 deflateInit(&stream
, zlib_compression_level
);
600 bound
= deflateBound(&stream
, size
);
601 deflated
= xmalloc(bound
);
602 stream
.next_out
= deflated
;
603 stream
.avail_out
= bound
;
605 stream
.next_in
= (unsigned char *)data
;
606 stream
.avail_in
= size
;
607 while (deflate(&stream
, Z_FINISH
) == Z_OK
)
610 *result_size
= stream
.total_out
;
614 static void emit_binary_diff(mmfile_t
*one
, mmfile_t
*two
)
620 unsigned long orig_size
;
621 unsigned long delta_size
;
622 unsigned long deflate_size
;
623 unsigned long data_size
;
625 printf("GIT binary patch\n");
626 /* We could do deflated delta, or we could do just deflated two,
627 * whichever is smaller.
630 deflated
= deflate_it(two
->ptr
, two
->size
, &deflate_size
);
631 if (one
->size
&& two
->size
) {
632 delta
= diff_delta(one
->ptr
, one
->size
,
634 &delta_size
, deflate_size
);
636 void *to_free
= delta
;
637 orig_size
= delta_size
;
638 delta
= deflate_it(delta
, delta_size
, &delta_size
);
643 if (delta
&& delta_size
< deflate_size
) {
644 printf("delta %lu\n", orig_size
);
647 data_size
= delta_size
;
650 printf("literal %lu\n", two
->size
);
653 data_size
= deflate_size
;
656 /* emit data encoded in base85 */
659 int bytes
= (52 < data_size
) ? 52 : data_size
;
663 line
[0] = bytes
+ 'A' - 1;
665 line
[0] = bytes
- 26 + 'a' - 1;
666 encode_85(line
+ 1, cp
, bytes
);
667 cp
= (char *) cp
+ bytes
;
674 #define FIRST_FEW_BYTES 8000
675 static int mmfile_is_binary(mmfile_t
*mf
)
678 if (FIRST_FEW_BYTES
< sz
)
679 sz
= FIRST_FEW_BYTES
;
680 if (memchr(mf
->ptr
, 0, sz
))
685 static void builtin_diff(const char *name_a
,
687 struct diff_filespec
*one
,
688 struct diff_filespec
*two
,
689 const char *xfrm_msg
,
690 struct diff_options
*o
,
691 int complete_rewrite
)
696 const char *set
= get_color(o
->color_diff
, DIFF_METAINFO
);
697 const char *reset
= get_color(o
->color_diff
, DIFF_RESET
);
699 a_one
= quote_two("a/", name_a
);
700 b_two
= quote_two("b/", name_b
);
701 lbl
[0] = DIFF_FILE_VALID(one
) ? a_one
: "/dev/null";
702 lbl
[1] = DIFF_FILE_VALID(two
) ? b_two
: "/dev/null";
703 printf("%sdiff --git %s %s%s\n", set
, a_one
, b_two
, reset
);
704 if (lbl
[0][0] == '/') {
706 printf("%snew file mode %06o%s\n", set
, two
->mode
, reset
);
707 if (xfrm_msg
&& xfrm_msg
[0])
708 printf("%s%s%s\n", set
, xfrm_msg
, reset
);
710 else if (lbl
[1][0] == '/') {
711 printf("%sdeleted file mode %06o%s\n", set
, one
->mode
, reset
);
712 if (xfrm_msg
&& xfrm_msg
[0])
713 printf("%s%s%s\n", set
, xfrm_msg
, reset
);
716 if (one
->mode
!= two
->mode
) {
717 printf("%sold mode %06o%s\n", set
, one
->mode
, reset
);
718 printf("%snew mode %06o%s\n", set
, two
->mode
, reset
);
720 if (xfrm_msg
&& xfrm_msg
[0])
721 printf("%s%s%s\n", set
, xfrm_msg
, reset
);
723 * we do not run diff between different kind
726 if ((one
->mode
^ two
->mode
) & S_IFMT
)
727 goto free_ab_and_return
;
728 if (complete_rewrite
) {
729 emit_rewrite_diff(name_a
, name_b
, one
, two
);
730 goto free_ab_and_return
;
734 if (fill_mmfile(&mf1
, one
) < 0 || fill_mmfile(&mf2
, two
) < 0)
735 die("unable to read files to diff");
737 if (!o
->text
&& (mmfile_is_binary(&mf1
) || mmfile_is_binary(&mf2
))) {
738 /* Quite common confusing case */
739 if (mf1
.size
== mf2
.size
&&
740 !memcmp(mf1
.ptr
, mf2
.ptr
, mf1
.size
))
741 goto free_ab_and_return
;
743 emit_binary_diff(&mf1
, &mf2
);
745 printf("Binary files %s and %s differ\n",
749 /* Crazy xdl interfaces.. */
750 const char *diffopts
= getenv("GIT_DIFF_OPTS");
754 struct emit_callback ecbdata
;
756 memset(&ecbdata
, 0, sizeof(ecbdata
));
757 ecbdata
.label_path
= lbl
;
758 ecbdata
.color_diff
= o
->color_diff
;
759 xpp
.flags
= XDF_NEED_MINIMAL
| o
->xdl_opts
;
760 xecfg
.ctxlen
= o
->context
;
761 xecfg
.flags
= XDL_EMIT_FUNCNAMES
;
764 else if (!strncmp(diffopts
, "--unified=", 10))
765 xecfg
.ctxlen
= strtoul(diffopts
+ 10, NULL
, 10);
766 else if (!strncmp(diffopts
, "-u", 2))
767 xecfg
.ctxlen
= strtoul(diffopts
+ 2, NULL
, 10);
768 ecb
.outf
= xdiff_outf
;
770 ecbdata
.xm
.consume
= fn_out_consume
;
771 xdl_diff(&mf1
, &mf2
, &xpp
, &xecfg
, &ecb
);
780 static void builtin_diffstat(const char *name_a
, const char *name_b
,
781 struct diff_filespec
*one
,
782 struct diff_filespec
*two
,
783 struct diffstat_t
*diffstat
,
784 struct diff_options
*o
,
785 int complete_rewrite
)
788 struct diffstat_file
*data
;
790 data
= diffstat_add(diffstat
, name_a
, name_b
);
793 data
->is_unmerged
= 1;
796 if (complete_rewrite
) {
797 diff_populate_filespec(one
, 0);
798 diff_populate_filespec(two
, 0);
799 data
->deleted
= count_lines(one
->data
, one
->size
);
800 data
->added
= count_lines(two
->data
, two
->size
);
803 if (fill_mmfile(&mf1
, one
) < 0 || fill_mmfile(&mf2
, two
) < 0)
804 die("unable to read files to diff");
806 if (mmfile_is_binary(&mf1
) || mmfile_is_binary(&mf2
))
809 /* Crazy xdl interfaces.. */
814 xpp
.flags
= XDF_NEED_MINIMAL
| o
->xdl_opts
;
817 ecb
.outf
= xdiff_outf
;
819 xdl_diff(&mf1
, &mf2
, &xpp
, &xecfg
, &ecb
);
823 static void builtin_checkdiff(const char *name_a
, const char *name_b
,
824 struct diff_filespec
*one
,
825 struct diff_filespec
*two
)
828 struct checkdiff_t data
;
833 memset(&data
, 0, sizeof(data
));
834 data
.xm
.consume
= checkdiff_consume
;
835 data
.filename
= name_b
? name_b
: name_a
;
838 if (fill_mmfile(&mf1
, one
) < 0 || fill_mmfile(&mf2
, two
) < 0)
839 die("unable to read files to diff");
841 if (mmfile_is_binary(&mf2
))
844 /* Crazy xdl interfaces.. */
849 xpp
.flags
= XDF_NEED_MINIMAL
;
852 ecb
.outf
= xdiff_outf
;
854 xdl_diff(&mf1
, &mf2
, &xpp
, &xecfg
, &ecb
);
858 struct diff_filespec
*alloc_filespec(const char *path
)
860 int namelen
= strlen(path
);
861 struct diff_filespec
*spec
= xmalloc(sizeof(*spec
) + namelen
+ 1);
863 memset(spec
, 0, sizeof(*spec
));
864 spec
->path
= (char *)(spec
+ 1);
865 memcpy(spec
->path
, path
, namelen
+1);
869 void fill_filespec(struct diff_filespec
*spec
, const unsigned char *sha1
,
873 spec
->mode
= canon_mode(mode
);
874 memcpy(spec
->sha1
, sha1
, 20);
875 spec
->sha1_valid
= !!memcmp(sha1
, null_sha1
, 20);
880 * Given a name and sha1 pair, if the dircache tells us the file in
881 * the work tree has that object contents, return true, so that
882 * prepare_temp_file() does not have to inflate and extract.
884 static int work_tree_matches(const char *name
, const unsigned char *sha1
)
886 struct cache_entry
*ce
;
890 /* We do not read the cache ourselves here, because the
891 * benchmark with my previous version that always reads cache
892 * shows that it makes things worse for diff-tree comparing
893 * two linux-2.6 kernel trees in an already checked out work
894 * tree. This is because most diff-tree comparisons deal with
895 * only a small number of files, while reading the cache is
896 * expensive for a large project, and its cost outweighs the
897 * savings we get by not inflating the object to a temporary
898 * file. Practically, this code only helps when we are used
899 * by diff-cache --cached, which does read the cache before
906 pos
= cache_name_pos(name
, len
);
909 ce
= active_cache
[pos
];
910 if ((lstat(name
, &st
) < 0) ||
911 !S_ISREG(st
.st_mode
) || /* careful! */
912 ce_match_stat(ce
, &st
, 0) ||
913 memcmp(sha1
, ce
->sha1
, 20))
915 /* we return 1 only when we can stat, it is a regular file,
916 * stat information matches, and sha1 recorded in the cache
917 * matches. I.e. we know the file in the work tree really is
918 * the same as the <name, sha1> pair.
923 static struct sha1_size_cache
{
924 unsigned char sha1
[20];
927 static int sha1_size_cache_nr
, sha1_size_cache_alloc
;
929 static struct sha1_size_cache
*locate_size_cache(unsigned char *sha1
,
934 struct sha1_size_cache
*e
;
937 last
= sha1_size_cache_nr
;
938 while (last
> first
) {
939 int cmp
, next
= (last
+ first
) >> 1;
940 e
= sha1_size_cache
[next
];
941 cmp
= memcmp(e
->sha1
, sha1
, 20);
953 /* insert to make it at "first" */
954 if (sha1_size_cache_alloc
<= sha1_size_cache_nr
) {
955 sha1_size_cache_alloc
= alloc_nr(sha1_size_cache_alloc
);
956 sha1_size_cache
= xrealloc(sha1_size_cache
,
957 sha1_size_cache_alloc
*
958 sizeof(*sha1_size_cache
));
960 sha1_size_cache_nr
++;
961 if (first
< sha1_size_cache_nr
)
962 memmove(sha1_size_cache
+ first
+ 1, sha1_size_cache
+ first
,
963 (sha1_size_cache_nr
- first
- 1) *
964 sizeof(*sha1_size_cache
));
965 e
= xmalloc(sizeof(struct sha1_size_cache
));
966 sha1_size_cache
[first
] = e
;
967 memcpy(e
->sha1
, sha1
, 20);
973 * While doing rename detection and pickaxe operation, we may need to
974 * grab the data for the blob (or file) for our own in-core comparison.
975 * diff_filespec has data and size fields for this purpose.
977 int diff_populate_filespec(struct diff_filespec
*s
, int size_only
)
980 if (!DIFF_FILE_VALID(s
))
981 die("internal error: asking to populate invalid file.");
982 if (S_ISDIR(s
->mode
))
990 if (!s
->sha1_valid
||
991 work_tree_matches(s
->path
, s
->sha1
)) {
994 if (lstat(s
->path
, &st
) < 0) {
995 if (errno
== ENOENT
) {
999 s
->data
= (char *)"";
1004 s
->size
= st
.st_size
;
1009 if (S_ISLNK(st
.st_mode
)) {
1011 s
->data
= xmalloc(s
->size
);
1013 ret
= readlink(s
->path
, s
->data
, s
->size
);
1020 fd
= open(s
->path
, O_RDONLY
);
1023 s
->data
= mmap(NULL
, s
->size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
1025 if (s
->data
== MAP_FAILED
)
1027 s
->should_munmap
= 1;
1031 struct sha1_size_cache
*e
;
1034 e
= locate_size_cache(s
->sha1
, 1, 0);
1039 if (!sha1_object_info(s
->sha1
, type
, &s
->size
))
1040 locate_size_cache(s
->sha1
, 0, s
->size
);
1043 s
->data
= read_sha1_file(s
->sha1
, type
, &s
->size
);
1050 void diff_free_filespec_data(struct diff_filespec
*s
)
1054 else if (s
->should_munmap
)
1055 munmap(s
->data
, s
->size
);
1056 s
->should_free
= s
->should_munmap
= 0;
1062 static void prep_temp_blob(struct diff_tempfile
*temp
,
1065 const unsigned char *sha1
,
1070 fd
= git_mkstemp(temp
->tmp_path
, TEMPFILE_PATH_LEN
, ".diff_XXXXXX");
1072 die("unable to create temp-file");
1073 if (write(fd
, blob
, size
) != size
)
1074 die("unable to write temp-file");
1076 temp
->name
= temp
->tmp_path
;
1077 strcpy(temp
->hex
, sha1_to_hex(sha1
));
1079 sprintf(temp
->mode
, "%06o", mode
);
1082 static void prepare_temp_file(const char *name
,
1083 struct diff_tempfile
*temp
,
1084 struct diff_filespec
*one
)
1086 if (!DIFF_FILE_VALID(one
)) {
1088 /* A '-' entry produces this for file-2, and
1089 * a '+' entry produces this for file-1.
1091 temp
->name
= "/dev/null";
1092 strcpy(temp
->hex
, ".");
1093 strcpy(temp
->mode
, ".");
1097 if (!one
->sha1_valid
||
1098 work_tree_matches(name
, one
->sha1
)) {
1100 if (lstat(name
, &st
) < 0) {
1101 if (errno
== ENOENT
)
1102 goto not_a_valid_file
;
1103 die("stat(%s): %s", name
, strerror(errno
));
1105 if (S_ISLNK(st
.st_mode
)) {
1107 char buf
[PATH_MAX
+ 1]; /* ought to be SYMLINK_MAX */
1108 if (sizeof(buf
) <= st
.st_size
)
1109 die("symlink too long: %s", name
);
1110 ret
= readlink(name
, buf
, st
.st_size
);
1112 die("readlink(%s)", name
);
1113 prep_temp_blob(temp
, buf
, st
.st_size
,
1115 one
->sha1
: null_sha1
),
1117 one
->mode
: S_IFLNK
));
1120 /* we can borrow from the file in the work tree */
1122 if (!one
->sha1_valid
)
1123 strcpy(temp
->hex
, sha1_to_hex(null_sha1
));
1125 strcpy(temp
->hex
, sha1_to_hex(one
->sha1
));
1126 /* Even though we may sometimes borrow the
1127 * contents from the work tree, we always want
1128 * one->mode. mode is trustworthy even when
1129 * !(one->sha1_valid), as long as
1130 * DIFF_FILE_VALID(one).
1132 sprintf(temp
->mode
, "%06o", one
->mode
);
1137 if (diff_populate_filespec(one
, 0))
1138 die("cannot read data blob for %s", one
->path
);
1139 prep_temp_blob(temp
, one
->data
, one
->size
,
1140 one
->sha1
, one
->mode
);
1144 static void remove_tempfile(void)
1148 for (i
= 0; i
< 2; i
++)
1149 if (diff_temp
[i
].name
== diff_temp
[i
].tmp_path
) {
1150 unlink(diff_temp
[i
].name
);
1151 diff_temp
[i
].name
= NULL
;
1155 static void remove_tempfile_on_signal(int signo
)
1158 signal(SIGINT
, SIG_DFL
);
1162 static int spawn_prog(const char *pgm
, const char **arg
)
1170 die("unable to fork");
1172 execvp(pgm
, (char *const*) arg
);
1176 while (waitpid(pid
, &status
, 0) < 0) {
1182 /* Earlier we did not check the exit status because
1183 * diff exits non-zero if files are different, and
1184 * we are not interested in knowing that. It was a
1185 * mistake which made it harder to quit a diff-*
1186 * session that uses the git-apply-patch-script as
1187 * the GIT_EXTERNAL_DIFF. A custom GIT_EXTERNAL_DIFF
1188 * should also exit non-zero only when it wants to
1189 * abort the entire diff-* session.
1191 if (WIFEXITED(status
) && !WEXITSTATUS(status
))
1196 /* An external diff command takes:
1198 * diff-cmd name infile1 infile1-sha1 infile1-mode \
1199 * infile2 infile2-sha1 infile2-mode [ rename-to ]
1202 static void run_external_diff(const char *pgm
,
1205 struct diff_filespec
*one
,
1206 struct diff_filespec
*two
,
1207 const char *xfrm_msg
,
1208 int complete_rewrite
)
1210 const char *spawn_arg
[10];
1211 struct diff_tempfile
*temp
= diff_temp
;
1213 static int atexit_asked
= 0;
1214 const char *othername
;
1215 const char **arg
= &spawn_arg
[0];
1217 othername
= (other
? other
: name
);
1219 prepare_temp_file(name
, &temp
[0], one
);
1220 prepare_temp_file(othername
, &temp
[1], two
);
1221 if (! atexit_asked
&&
1222 (temp
[0].name
== temp
[0].tmp_path
||
1223 temp
[1].name
== temp
[1].tmp_path
)) {
1225 atexit(remove_tempfile
);
1227 signal(SIGINT
, remove_tempfile_on_signal
);
1233 *arg
++ = temp
[0].name
;
1234 *arg
++ = temp
[0].hex
;
1235 *arg
++ = temp
[0].mode
;
1236 *arg
++ = temp
[1].name
;
1237 *arg
++ = temp
[1].hex
;
1238 *arg
++ = temp
[1].mode
;
1248 retval
= spawn_prog(pgm
, spawn_arg
);
1251 fprintf(stderr
, "external diff died, stopping at %s.\n", name
);
1256 static void run_diff_cmd(const char *pgm
,
1259 struct diff_filespec
*one
,
1260 struct diff_filespec
*two
,
1261 const char *xfrm_msg
,
1262 struct diff_options
*o
,
1263 int complete_rewrite
)
1266 run_external_diff(pgm
, name
, other
, one
, two
, xfrm_msg
,
1271 builtin_diff(name
, other
? other
: name
,
1272 one
, two
, xfrm_msg
, o
, complete_rewrite
);
1274 printf("* Unmerged path %s\n", name
);
1277 static void diff_fill_sha1_info(struct diff_filespec
*one
)
1279 if (DIFF_FILE_VALID(one
)) {
1280 if (!one
->sha1_valid
) {
1282 if (lstat(one
->path
, &st
) < 0)
1283 die("stat %s", one
->path
);
1284 if (index_path(one
->sha1
, one
->path
, &st
, 0))
1285 die("cannot hash %s\n", one
->path
);
1289 memset(one
->sha1
, 0, 20);
1292 static void run_diff(struct diff_filepair
*p
, struct diff_options
*o
)
1294 const char *pgm
= external_diff();
1295 char msg
[PATH_MAX
*2+300], *xfrm_msg
;
1296 struct diff_filespec
*one
;
1297 struct diff_filespec
*two
;
1300 char *name_munged
, *other_munged
;
1301 int complete_rewrite
= 0;
1304 if (DIFF_PAIR_UNMERGED(p
)) {
1306 run_diff_cmd(pgm
, p
->one
->path
, NULL
, NULL
, NULL
, NULL
, o
, 0);
1310 name
= p
->one
->path
;
1311 other
= (strcmp(name
, p
->two
->path
) ? p
->two
->path
: NULL
);
1312 name_munged
= quote_one(name
);
1313 other_munged
= quote_one(other
);
1314 one
= p
->one
; two
= p
->two
;
1316 diff_fill_sha1_info(one
);
1317 diff_fill_sha1_info(two
);
1320 switch (p
->status
) {
1321 case DIFF_STATUS_COPIED
:
1322 len
+= snprintf(msg
+ len
, sizeof(msg
) - len
,
1323 "similarity index %d%%\n"
1326 (int)(0.5 + p
->score
* 100.0/MAX_SCORE
),
1327 name_munged
, other_munged
);
1329 case DIFF_STATUS_RENAMED
:
1330 len
+= snprintf(msg
+ len
, sizeof(msg
) - len
,
1331 "similarity index %d%%\n"
1334 (int)(0.5 + p
->score
* 100.0/MAX_SCORE
),
1335 name_munged
, other_munged
);
1337 case DIFF_STATUS_MODIFIED
:
1339 len
+= snprintf(msg
+ len
, sizeof(msg
) - len
,
1340 "dissimilarity index %d%%\n",
1341 (int)(0.5 + p
->score
*
1343 complete_rewrite
= 1;
1352 if (memcmp(one
->sha1
, two
->sha1
, 20)) {
1353 int abbrev
= o
->full_index
? 40 : DEFAULT_ABBREV
;
1355 len
+= snprintf(msg
+ len
, sizeof(msg
) - len
,
1357 abbrev
, sha1_to_hex(one
->sha1
),
1358 abbrev
, sha1_to_hex(two
->sha1
));
1359 if (one
->mode
== two
->mode
)
1360 len
+= snprintf(msg
+ len
, sizeof(msg
) - len
,
1361 " %06o", one
->mode
);
1362 len
+= snprintf(msg
+ len
, sizeof(msg
) - len
, "\n");
1367 xfrm_msg
= len
? msg
: NULL
;
1370 DIFF_FILE_VALID(one
) && DIFF_FILE_VALID(two
) &&
1371 (S_IFMT
& one
->mode
) != (S_IFMT
& two
->mode
)) {
1372 /* a filepair that changes between file and symlink
1373 * needs to be split into deletion and creation.
1375 struct diff_filespec
*null
= alloc_filespec(two
->path
);
1376 run_diff_cmd(NULL
, name
, other
, one
, null
, xfrm_msg
, o
, 0);
1378 null
= alloc_filespec(one
->path
);
1379 run_diff_cmd(NULL
, name
, other
, null
, two
, xfrm_msg
, o
, 0);
1383 run_diff_cmd(pgm
, name
, other
, one
, two
, xfrm_msg
, o
,
1390 static void run_diffstat(struct diff_filepair
*p
, struct diff_options
*o
,
1391 struct diffstat_t
*diffstat
)
1395 int complete_rewrite
= 0;
1397 if (DIFF_PAIR_UNMERGED(p
)) {
1399 builtin_diffstat(p
->one
->path
, NULL
, NULL
, NULL
, diffstat
, o
, 0);
1403 name
= p
->one
->path
;
1404 other
= (strcmp(name
, p
->two
->path
) ? p
->two
->path
: NULL
);
1406 diff_fill_sha1_info(p
->one
);
1407 diff_fill_sha1_info(p
->two
);
1409 if (p
->status
== DIFF_STATUS_MODIFIED
&& p
->score
)
1410 complete_rewrite
= 1;
1411 builtin_diffstat(name
, other
, p
->one
, p
->two
, diffstat
, o
, complete_rewrite
);
1414 static void run_checkdiff(struct diff_filepair
*p
, struct diff_options
*o
)
1419 if (DIFF_PAIR_UNMERGED(p
)) {
1424 name
= p
->one
->path
;
1425 other
= (strcmp(name
, p
->two
->path
) ? p
->two
->path
: NULL
);
1427 diff_fill_sha1_info(p
->one
);
1428 diff_fill_sha1_info(p
->two
);
1430 builtin_checkdiff(name
, other
, p
->one
, p
->two
);
1433 void diff_setup(struct diff_options
*options
)
1435 memset(options
, 0, sizeof(*options
));
1436 options
->line_termination
= '\n';
1437 options
->break_opt
= -1;
1438 options
->rename_limit
= -1;
1439 options
->context
= 3;
1440 options
->msg_sep
= "";
1442 options
->change
= diff_change
;
1443 options
->add_remove
= diff_addremove
;
1444 options
->color_diff
= diff_use_color_default
;
1445 options
->detect_rename
= diff_detect_rename_default
;
1448 int diff_setup_done(struct diff_options
*options
)
1450 if ((options
->find_copies_harder
&&
1451 options
->detect_rename
!= DIFF_DETECT_COPY
) ||
1452 (0 <= options
->rename_limit
&& !options
->detect_rename
))
1455 if (options
->output_format
& (DIFF_FORMAT_NAME
|
1456 DIFF_FORMAT_NAME_STATUS
|
1457 DIFF_FORMAT_CHECKDIFF
|
1458 DIFF_FORMAT_NO_OUTPUT
))
1459 options
->output_format
&= ~(DIFF_FORMAT_RAW
|
1460 DIFF_FORMAT_DIFFSTAT
|
1461 DIFF_FORMAT_SUMMARY
|
1465 * These cases always need recursive; we do not drop caller-supplied
1466 * recursive bits for other formats here.
1468 if (options
->output_format
& (DIFF_FORMAT_PATCH
|
1469 DIFF_FORMAT_DIFFSTAT
|
1470 DIFF_FORMAT_CHECKDIFF
))
1471 options
->recursive
= 1;
1473 * Also pickaxe would not work very well if you do not say recursive
1475 if (options
->pickaxe
)
1476 options
->recursive
= 1;
1478 if (options
->detect_rename
&& options
->rename_limit
< 0)
1479 options
->rename_limit
= diff_rename_limit_default
;
1480 if (options
->setup
& DIFF_SETUP_USE_CACHE
) {
1482 /* read-cache does not die even when it fails
1483 * so it is safe for us to do this here. Also
1484 * it does not smudge active_cache or active_nr
1485 * when it fails, so we do not have to worry about
1486 * cleaning it up ourselves either.
1490 if (options
->setup
& DIFF_SETUP_USE_SIZE_CACHE
)
1492 if (options
->abbrev
<= 0 || 40 < options
->abbrev
)
1493 options
->abbrev
= 40; /* full */
1498 static int opt_arg(const char *arg
, int arg_short
, const char *arg_long
, int *val
)
1508 if (c
== arg_short
) {
1512 if (val
&& isdigit(c
)) {
1514 int n
= strtoul(arg
, &end
, 10);
1525 eq
= strchr(arg
, '=');
1530 if (!len
|| strncmp(arg
, arg_long
, len
))
1535 if (!isdigit(*++eq
))
1537 n
= strtoul(eq
, &end
, 10);
1545 int diff_opt_parse(struct diff_options
*options
, const char **av
, int ac
)
1547 const char *arg
= av
[0];
1548 if (!strcmp(arg
, "-p") || !strcmp(arg
, "-u"))
1549 options
->output_format
|= DIFF_FORMAT_PATCH
;
1550 else if (opt_arg(arg
, 'U', "unified", &options
->context
))
1551 options
->output_format
|= DIFF_FORMAT_PATCH
;
1552 else if (!strcmp(arg
, "--raw"))
1553 options
->output_format
|= DIFF_FORMAT_RAW
;
1554 else if (!strcmp(arg
, "--patch-with-raw")) {
1555 options
->output_format
|= DIFF_FORMAT_PATCH
| DIFF_FORMAT_RAW
;
1557 else if (!strcmp(arg
, "--stat"))
1558 options
->output_format
|= DIFF_FORMAT_DIFFSTAT
;
1559 else if (!strcmp(arg
, "--check"))
1560 options
->output_format
|= DIFF_FORMAT_CHECKDIFF
;
1561 else if (!strcmp(arg
, "--summary"))
1562 options
->output_format
|= DIFF_FORMAT_SUMMARY
;
1563 else if (!strcmp(arg
, "--patch-with-stat")) {
1564 options
->output_format
|= DIFF_FORMAT_PATCH
| DIFF_FORMAT_DIFFSTAT
;
1566 else if (!strcmp(arg
, "-z"))
1567 options
->line_termination
= 0;
1568 else if (!strncmp(arg
, "-l", 2))
1569 options
->rename_limit
= strtoul(arg
+2, NULL
, 10);
1570 else if (!strcmp(arg
, "--full-index"))
1571 options
->full_index
= 1;
1572 else if (!strcmp(arg
, "--binary")) {
1573 options
->output_format
|= DIFF_FORMAT_PATCH
;
1574 options
->full_index
= options
->binary
= 1;
1576 else if (!strcmp(arg
, "-a") || !strcmp(arg
, "--text")) {
1579 else if (!strcmp(arg
, "--name-only"))
1580 options
->output_format
|= DIFF_FORMAT_NAME
;
1581 else if (!strcmp(arg
, "--name-status"))
1582 options
->output_format
|= DIFF_FORMAT_NAME_STATUS
;
1583 else if (!strcmp(arg
, "-R"))
1584 options
->reverse_diff
= 1;
1585 else if (!strncmp(arg
, "-S", 2))
1586 options
->pickaxe
= arg
+ 2;
1587 else if (!strcmp(arg
, "-s")) {
1588 options
->output_format
|= DIFF_FORMAT_NO_OUTPUT
;
1590 else if (!strncmp(arg
, "-O", 2))
1591 options
->orderfile
= arg
+ 2;
1592 else if (!strncmp(arg
, "--diff-filter=", 14))
1593 options
->filter
= arg
+ 14;
1594 else if (!strcmp(arg
, "--pickaxe-all"))
1595 options
->pickaxe_opts
= DIFF_PICKAXE_ALL
;
1596 else if (!strcmp(arg
, "--pickaxe-regex"))
1597 options
->pickaxe_opts
= DIFF_PICKAXE_REGEX
;
1598 else if (!strncmp(arg
, "-B", 2)) {
1599 if ((options
->break_opt
=
1600 diff_scoreopt_parse(arg
)) == -1)
1603 else if (!strncmp(arg
, "-M", 2)) {
1604 if ((options
->rename_score
=
1605 diff_scoreopt_parse(arg
)) == -1)
1607 options
->detect_rename
= DIFF_DETECT_RENAME
;
1609 else if (!strncmp(arg
, "-C", 2)) {
1610 if ((options
->rename_score
=
1611 diff_scoreopt_parse(arg
)) == -1)
1613 options
->detect_rename
= DIFF_DETECT_COPY
;
1615 else if (!strcmp(arg
, "--find-copies-harder"))
1616 options
->find_copies_harder
= 1;
1617 else if (!strcmp(arg
, "--abbrev"))
1618 options
->abbrev
= DEFAULT_ABBREV
;
1619 else if (!strncmp(arg
, "--abbrev=", 9)) {
1620 options
->abbrev
= strtoul(arg
+ 9, NULL
, 10);
1621 if (options
->abbrev
< MINIMUM_ABBREV
)
1622 options
->abbrev
= MINIMUM_ABBREV
;
1623 else if (40 < options
->abbrev
)
1624 options
->abbrev
= 40;
1626 else if (!strcmp(arg
, "--color"))
1627 options
->color_diff
= 1;
1628 else if (!strcmp(arg
, "--no-color"))
1629 options
->color_diff
= 0;
1630 else if (!strcmp(arg
, "-w") || !strcmp(arg
, "--ignore-all-space"))
1631 options
->xdl_opts
|= XDF_IGNORE_WHITESPACE
;
1632 else if (!strcmp(arg
, "-b") || !strcmp(arg
, "--ignore-space-change"))
1633 options
->xdl_opts
|= XDF_IGNORE_WHITESPACE_CHANGE
;
1634 else if (!strcmp(arg
, "--no-renames"))
1635 options
->detect_rename
= 0;
1641 static int parse_num(const char **cp_p
)
1643 unsigned long num
, scale
;
1645 const char *cp
= *cp_p
;
1652 if ( !dot
&& ch
== '.' ) {
1655 } else if ( ch
== '%' ) {
1656 scale
= dot
? scale
*100 : 100;
1657 cp
++; /* % is always at the end */
1659 } else if ( ch
>= '0' && ch
<= '9' ) {
1660 if ( scale
< 100000 ) {
1662 num
= (num
*10) + (ch
-'0');
1671 /* user says num divided by scale and we say internally that
1672 * is MAX_SCORE * num / scale.
1674 return (num
>= scale
) ? MAX_SCORE
: (MAX_SCORE
* num
/ scale
);
1677 int diff_scoreopt_parse(const char *opt
)
1679 int opt1
, opt2
, cmd
;
1684 if (cmd
!= 'M' && cmd
!= 'C' && cmd
!= 'B')
1685 return -1; /* that is not a -M, -C nor -B option */
1687 opt1
= parse_num(&opt
);
1693 else if (*opt
!= '/')
1694 return -1; /* we expect -B80/99 or -B80 */
1697 opt2
= parse_num(&opt
);
1702 return opt1
| (opt2
<< 16);
1705 struct diff_queue_struct diff_queued_diff
;
1707 void diff_q(struct diff_queue_struct
*queue
, struct diff_filepair
*dp
)
1709 if (queue
->alloc
<= queue
->nr
) {
1710 queue
->alloc
= alloc_nr(queue
->alloc
);
1711 queue
->queue
= xrealloc(queue
->queue
,
1712 sizeof(dp
) * queue
->alloc
);
1714 queue
->queue
[queue
->nr
++] = dp
;
1717 struct diff_filepair
*diff_queue(struct diff_queue_struct
*queue
,
1718 struct diff_filespec
*one
,
1719 struct diff_filespec
*two
)
1721 struct diff_filepair
*dp
= xmalloc(sizeof(*dp
));
1726 dp
->source_stays
= 0;
1727 dp
->broken_pair
= 0;
1733 void diff_free_filepair(struct diff_filepair
*p
)
1735 diff_free_filespec_data(p
->one
);
1736 diff_free_filespec_data(p
->two
);
1742 /* This is different from find_unique_abbrev() in that
1743 * it stuffs the result with dots for alignment.
1745 const char *diff_unique_abbrev(const unsigned char *sha1
, int len
)
1750 return sha1_to_hex(sha1
);
1752 abbrev
= find_unique_abbrev(sha1
, len
);
1754 return sha1_to_hex(sha1
);
1755 abblen
= strlen(abbrev
);
1757 static char hex
[41];
1758 if (len
< abblen
&& abblen
<= len
+ 2)
1759 sprintf(hex
, "%s%.*s", abbrev
, len
+3-abblen
, "..");
1761 sprintf(hex
, "%s...", abbrev
);
1764 return sha1_to_hex(sha1
);
1767 static void diff_flush_raw(struct diff_filepair
*p
,
1768 struct diff_options
*options
)
1772 int abbrev
= options
->abbrev
;
1773 const char *path_one
, *path_two
;
1774 int inter_name_termination
= '\t';
1775 int line_termination
= options
->line_termination
;
1777 if (!line_termination
)
1778 inter_name_termination
= 0;
1780 path_one
= p
->one
->path
;
1781 path_two
= p
->two
->path
;
1782 if (line_termination
) {
1783 path_one
= quote_one(path_one
);
1784 path_two
= quote_one(path_two
);
1788 sprintf(status
, "%c%03d", p
->status
,
1789 (int)(0.5 + p
->score
* 100.0/MAX_SCORE
));
1791 status
[0] = p
->status
;
1794 switch (p
->status
) {
1795 case DIFF_STATUS_COPIED
:
1796 case DIFF_STATUS_RENAMED
:
1799 case DIFF_STATUS_ADDED
:
1800 case DIFF_STATUS_DELETED
:
1807 if (!(options
->output_format
& DIFF_FORMAT_NAME_STATUS
)) {
1808 printf(":%06o %06o %s ",
1809 p
->one
->mode
, p
->two
->mode
,
1810 diff_unique_abbrev(p
->one
->sha1
, abbrev
));
1812 diff_unique_abbrev(p
->two
->sha1
, abbrev
));
1814 printf("%s%c%s", status
, inter_name_termination
, path_one
);
1816 printf("%c%s", inter_name_termination
, path_two
);
1817 putchar(line_termination
);
1818 if (path_one
!= p
->one
->path
)
1819 free((void*)path_one
);
1820 if (path_two
!= p
->two
->path
)
1821 free((void*)path_two
);
1824 static void diff_flush_name(struct diff_filepair
*p
, int line_termination
)
1826 char *path
= p
->two
->path
;
1828 if (line_termination
)
1829 path
= quote_one(p
->two
->path
);
1830 printf("%s%c", path
, line_termination
);
1831 if (p
->two
->path
!= path
)
1835 int diff_unmodified_pair(struct diff_filepair
*p
)
1837 /* This function is written stricter than necessary to support
1838 * the currently implemented transformers, but the idea is to
1839 * let transformers to produce diff_filepairs any way they want,
1840 * and filter and clean them up here before producing the output.
1842 struct diff_filespec
*one
, *two
;
1844 if (DIFF_PAIR_UNMERGED(p
))
1845 return 0; /* unmerged is interesting */
1850 /* deletion, addition, mode or type change
1851 * and rename are all interesting.
1853 if (DIFF_FILE_VALID(one
) != DIFF_FILE_VALID(two
) ||
1854 DIFF_PAIR_MODE_CHANGED(p
) ||
1855 strcmp(one
->path
, two
->path
))
1858 /* both are valid and point at the same path. that is, we are
1859 * dealing with a change.
1861 if (one
->sha1_valid
&& two
->sha1_valid
&&
1862 !memcmp(one
->sha1
, two
->sha1
, sizeof(one
->sha1
)))
1863 return 1; /* no change */
1864 if (!one
->sha1_valid
&& !two
->sha1_valid
)
1865 return 1; /* both look at the same file on the filesystem. */
1869 static void diff_flush_patch(struct diff_filepair
*p
, struct diff_options
*o
)
1871 if (diff_unmodified_pair(p
))
1874 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
1875 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
1876 return; /* no tree diffs in patch format */
1881 static void diff_flush_stat(struct diff_filepair
*p
, struct diff_options
*o
,
1882 struct diffstat_t
*diffstat
)
1884 if (diff_unmodified_pair(p
))
1887 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
1888 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
1889 return; /* no tree diffs in patch format */
1891 run_diffstat(p
, o
, diffstat
);
1894 static void diff_flush_checkdiff(struct diff_filepair
*p
,
1895 struct diff_options
*o
)
1897 if (diff_unmodified_pair(p
))
1900 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
1901 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
1902 return; /* no tree diffs in patch format */
1904 run_checkdiff(p
, o
);
1907 int diff_queue_is_empty(void)
1909 struct diff_queue_struct
*q
= &diff_queued_diff
;
1911 for (i
= 0; i
< q
->nr
; i
++)
1912 if (!diff_unmodified_pair(q
->queue
[i
]))
1918 void diff_debug_filespec(struct diff_filespec
*s
, int x
, const char *one
)
1920 fprintf(stderr
, "queue[%d] %s (%s) %s %06o %s\n",
1923 DIFF_FILE_VALID(s
) ? "valid" : "invalid",
1925 s
->sha1_valid
? sha1_to_hex(s
->sha1
) : "");
1926 fprintf(stderr
, "queue[%d] %s size %lu flags %d\n",
1928 s
->size
, s
->xfrm_flags
);
1931 void diff_debug_filepair(const struct diff_filepair
*p
, int i
)
1933 diff_debug_filespec(p
->one
, i
, "one");
1934 diff_debug_filespec(p
->two
, i
, "two");
1935 fprintf(stderr
, "score %d, status %c stays %d broken %d\n",
1936 p
->score
, p
->status
? p
->status
: '?',
1937 p
->source_stays
, p
->broken_pair
);
1940 void diff_debug_queue(const char *msg
, struct diff_queue_struct
*q
)
1944 fprintf(stderr
, "%s\n", msg
);
1945 fprintf(stderr
, "q->nr = %d\n", q
->nr
);
1946 for (i
= 0; i
< q
->nr
; i
++) {
1947 struct diff_filepair
*p
= q
->queue
[i
];
1948 diff_debug_filepair(p
, i
);
1953 static void diff_resolve_rename_copy(void)
1956 struct diff_filepair
*p
, *pp
;
1957 struct diff_queue_struct
*q
= &diff_queued_diff
;
1959 diff_debug_queue("resolve-rename-copy", q
);
1961 for (i
= 0; i
< q
->nr
; i
++) {
1963 p
->status
= 0; /* undecided */
1964 if (DIFF_PAIR_UNMERGED(p
))
1965 p
->status
= DIFF_STATUS_UNMERGED
;
1966 else if (!DIFF_FILE_VALID(p
->one
))
1967 p
->status
= DIFF_STATUS_ADDED
;
1968 else if (!DIFF_FILE_VALID(p
->two
))
1969 p
->status
= DIFF_STATUS_DELETED
;
1970 else if (DIFF_PAIR_TYPE_CHANGED(p
))
1971 p
->status
= DIFF_STATUS_TYPE_CHANGED
;
1973 /* from this point on, we are dealing with a pair
1974 * whose both sides are valid and of the same type, i.e.
1975 * either in-place edit or rename/copy edit.
1977 else if (DIFF_PAIR_RENAME(p
)) {
1978 if (p
->source_stays
) {
1979 p
->status
= DIFF_STATUS_COPIED
;
1982 /* See if there is some other filepair that
1983 * copies from the same source as us. If so
1984 * we are a copy. Otherwise we are either a
1985 * copy if the path stays, or a rename if it
1986 * does not, but we already handled "stays" case.
1988 for (j
= i
+ 1; j
< q
->nr
; j
++) {
1990 if (strcmp(pp
->one
->path
, p
->one
->path
))
1991 continue; /* not us */
1992 if (!DIFF_PAIR_RENAME(pp
))
1993 continue; /* not a rename/copy */
1994 /* pp is a rename/copy from the same source */
1995 p
->status
= DIFF_STATUS_COPIED
;
1999 p
->status
= DIFF_STATUS_RENAMED
;
2001 else if (memcmp(p
->one
->sha1
, p
->two
->sha1
, 20) ||
2002 p
->one
->mode
!= p
->two
->mode
)
2003 p
->status
= DIFF_STATUS_MODIFIED
;
2005 /* This is a "no-change" entry and should not
2006 * happen anymore, but prepare for broken callers.
2008 error("feeding unmodified %s to diffcore",
2010 p
->status
= DIFF_STATUS_UNKNOWN
;
2013 diff_debug_queue("resolve-rename-copy done", q
);
2016 static int check_pair_status(struct diff_filepair
*p
)
2018 switch (p
->status
) {
2019 case DIFF_STATUS_UNKNOWN
:
2022 die("internal error in diff-resolve-rename-copy");
2028 static void flush_one_pair(struct diff_filepair
*p
, struct diff_options
*opt
)
2030 int fmt
= opt
->output_format
;
2032 if (fmt
& DIFF_FORMAT_CHECKDIFF
)
2033 diff_flush_checkdiff(p
, opt
);
2034 else if (fmt
& (DIFF_FORMAT_RAW
| DIFF_FORMAT_NAME_STATUS
))
2035 diff_flush_raw(p
, opt
);
2036 else if (fmt
& DIFF_FORMAT_NAME
)
2037 diff_flush_name(p
, opt
->line_termination
);
2040 static void show_file_mode_name(const char *newdelete
, struct diff_filespec
*fs
)
2043 printf(" %s mode %06o %s\n", newdelete
, fs
->mode
, fs
->path
);
2045 printf(" %s %s\n", newdelete
, fs
->path
);
2049 static void show_mode_change(struct diff_filepair
*p
, int show_name
)
2051 if (p
->one
->mode
&& p
->two
->mode
&& p
->one
->mode
!= p
->two
->mode
) {
2053 printf(" mode change %06o => %06o %s\n",
2054 p
->one
->mode
, p
->two
->mode
, p
->two
->path
);
2056 printf(" mode change %06o => %06o\n",
2057 p
->one
->mode
, p
->two
->mode
);
2061 static void show_rename_copy(const char *renamecopy
, struct diff_filepair
*p
)
2063 const char *old
, *new;
2065 /* Find common prefix */
2069 const char *slash_old
, *slash_new
;
2070 slash_old
= strchr(old
, '/');
2071 slash_new
= strchr(new, '/');
2074 slash_old
- old
!= slash_new
- new ||
2075 memcmp(old
, new, slash_new
- new))
2077 old
= slash_old
+ 1;
2078 new = slash_new
+ 1;
2080 /* p->one->path thru old is the common prefix, and old and new
2081 * through the end of names are renames
2083 if (old
!= p
->one
->path
)
2084 printf(" %s %.*s{%s => %s} (%d%%)\n", renamecopy
,
2085 (int)(old
- p
->one
->path
), p
->one
->path
,
2086 old
, new, (int)(0.5 + p
->score
* 100.0/MAX_SCORE
));
2088 printf(" %s %s => %s (%d%%)\n", renamecopy
,
2089 p
->one
->path
, p
->two
->path
,
2090 (int)(0.5 + p
->score
* 100.0/MAX_SCORE
));
2091 show_mode_change(p
, 0);
2094 static void diff_summary(struct diff_filepair
*p
)
2097 case DIFF_STATUS_DELETED
:
2098 show_file_mode_name("delete", p
->one
);
2100 case DIFF_STATUS_ADDED
:
2101 show_file_mode_name("create", p
->two
);
2103 case DIFF_STATUS_COPIED
:
2104 show_rename_copy("copy", p
);
2106 case DIFF_STATUS_RENAMED
:
2107 show_rename_copy("rename", p
);
2111 printf(" rewrite %s (%d%%)\n", p
->two
->path
,
2112 (int)(0.5 + p
->score
* 100.0/MAX_SCORE
));
2113 show_mode_change(p
, 0);
2114 } else show_mode_change(p
, 1);
2120 struct xdiff_emit_state xm
;
2125 static int remove_space(char *line
, int len
)
2131 for (i
= 0; i
< len
; i
++)
2132 if (!isspace((c
= line
[i
])))
2138 static void patch_id_consume(void *priv
, char *line
, unsigned long len
)
2140 struct patch_id_t
*data
= priv
;
2143 /* Ignore line numbers when computing the SHA1 of the patch */
2144 if (!strncmp(line
, "@@ -", 4))
2147 new_len
= remove_space(line
, len
);
2149 SHA1_Update(data
->ctx
, line
, new_len
);
2150 data
->patchlen
+= new_len
;
2153 /* returns 0 upon success, and writes result into sha1 */
2154 static int diff_get_patch_id(struct diff_options
*options
, unsigned char *sha1
)
2156 struct diff_queue_struct
*q
= &diff_queued_diff
;
2159 struct patch_id_t data
;
2160 char buffer
[PATH_MAX
* 4 + 20];
2163 memset(&data
, 0, sizeof(struct patch_id_t
));
2165 data
.xm
.consume
= patch_id_consume
;
2167 for (i
= 0; i
< q
->nr
; i
++) {
2172 struct diff_filepair
*p
= q
->queue
[i
];
2176 return error("internal diff status error");
2177 if (p
->status
== DIFF_STATUS_UNKNOWN
)
2179 if (diff_unmodified_pair(p
))
2181 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
2182 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
2184 if (DIFF_PAIR_UNMERGED(p
))
2187 diff_fill_sha1_info(p
->one
);
2188 diff_fill_sha1_info(p
->two
);
2189 if (fill_mmfile(&mf1
, p
->one
) < 0 ||
2190 fill_mmfile(&mf2
, p
->two
) < 0)
2191 return error("unable to read files to diff");
2193 /* Maybe hash p->two? into the patch id? */
2194 if (mmfile_is_binary(&mf2
))
2197 len1
= remove_space(p
->one
->path
, strlen(p
->one
->path
));
2198 len2
= remove_space(p
->two
->path
, strlen(p
->two
->path
));
2199 if (p
->one
->mode
== 0)
2200 len1
= snprintf(buffer
, sizeof(buffer
),
2201 "diff--gita/%.*sb/%.*s"
2208 len2
, p
->two
->path
);
2209 else if (p
->two
->mode
== 0)
2210 len1
= snprintf(buffer
, sizeof(buffer
),
2211 "diff--gita/%.*sb/%.*s"
2212 "deletedfilemode%06o"
2218 len1
, p
->one
->path
);
2220 len1
= snprintf(buffer
, sizeof(buffer
),
2221 "diff--gita/%.*sb/%.*s"
2227 len2
, p
->two
->path
);
2228 SHA1_Update(&ctx
, buffer
, len1
);
2230 xpp
.flags
= XDF_NEED_MINIMAL
;
2232 xecfg
.flags
= XDL_EMIT_FUNCNAMES
;
2233 ecb
.outf
= xdiff_outf
;
2235 xdl_diff(&mf1
, &mf2
, &xpp
, &xecfg
, &ecb
);
2238 SHA1_Final(sha1
, &ctx
);
2242 int diff_flush_patch_id(struct diff_options
*options
, unsigned char *sha1
)
2244 struct diff_queue_struct
*q
= &diff_queued_diff
;
2246 int result
= diff_get_patch_id(options
, sha1
);
2248 for (i
= 0; i
< q
->nr
; i
++)
2249 diff_free_filepair(q
->queue
[i
]);
2253 q
->nr
= q
->alloc
= 0;
2258 static int is_summary_empty(const struct diff_queue_struct
*q
)
2262 for (i
= 0; i
< q
->nr
; i
++) {
2263 const struct diff_filepair
*p
= q
->queue
[i
];
2265 switch (p
->status
) {
2266 case DIFF_STATUS_DELETED
:
2267 case DIFF_STATUS_ADDED
:
2268 case DIFF_STATUS_COPIED
:
2269 case DIFF_STATUS_RENAMED
:
2274 if (p
->one
->mode
&& p
->two
->mode
&&
2275 p
->one
->mode
!= p
->two
->mode
)
2283 void diff_flush(struct diff_options
*options
)
2285 struct diff_queue_struct
*q
= &diff_queued_diff
;
2286 int i
, output_format
= options
->output_format
;
2290 * Order: raw, stat, summary, patch
2291 * or: name/name-status/checkdiff (other bits clear)
2296 if (output_format
& (DIFF_FORMAT_RAW
|
2298 DIFF_FORMAT_NAME_STATUS
|
2299 DIFF_FORMAT_CHECKDIFF
)) {
2300 for (i
= 0; i
< q
->nr
; i
++) {
2301 struct diff_filepair
*p
= q
->queue
[i
];
2302 if (check_pair_status(p
))
2303 flush_one_pair(p
, options
);
2308 if (output_format
& DIFF_FORMAT_DIFFSTAT
) {
2309 struct diffstat_t diffstat
;
2311 memset(&diffstat
, 0, sizeof(struct diffstat_t
));
2312 diffstat
.xm
.consume
= diffstat_consume
;
2313 for (i
= 0; i
< q
->nr
; i
++) {
2314 struct diff_filepair
*p
= q
->queue
[i
];
2315 if (check_pair_status(p
))
2316 diff_flush_stat(p
, options
, &diffstat
);
2318 show_stats(&diffstat
);
2322 if (output_format
& DIFF_FORMAT_SUMMARY
&& !is_summary_empty(q
)) {
2323 for (i
= 0; i
< q
->nr
; i
++)
2324 diff_summary(q
->queue
[i
]);
2328 if (output_format
& DIFF_FORMAT_PATCH
) {
2330 if (options
->stat_sep
) {
2331 /* attach patch instead of inline */
2332 fputs(options
->stat_sep
, stdout
);
2334 putchar(options
->line_termination
);
2338 for (i
= 0; i
< q
->nr
; i
++) {
2339 struct diff_filepair
*p
= q
->queue
[i
];
2340 if (check_pair_status(p
))
2341 diff_flush_patch(p
, options
);
2345 for (i
= 0; i
< q
->nr
; i
++)
2346 diff_free_filepair(q
->queue
[i
]);
2350 q
->nr
= q
->alloc
= 0;
2353 static void diffcore_apply_filter(const char *filter
)
2356 struct diff_queue_struct
*q
= &diff_queued_diff
;
2357 struct diff_queue_struct outq
;
2359 outq
.nr
= outq
.alloc
= 0;
2364 if (strchr(filter
, DIFF_STATUS_FILTER_AON
)) {
2366 for (i
= found
= 0; !found
&& i
< q
->nr
; i
++) {
2367 struct diff_filepair
*p
= q
->queue
[i
];
2368 if (((p
->status
== DIFF_STATUS_MODIFIED
) &&
2370 strchr(filter
, DIFF_STATUS_FILTER_BROKEN
)) ||
2372 strchr(filter
, DIFF_STATUS_MODIFIED
)))) ||
2373 ((p
->status
!= DIFF_STATUS_MODIFIED
) &&
2374 strchr(filter
, p
->status
)))
2380 /* otherwise we will clear the whole queue
2381 * by copying the empty outq at the end of this
2382 * function, but first clear the current entries
2385 for (i
= 0; i
< q
->nr
; i
++)
2386 diff_free_filepair(q
->queue
[i
]);
2389 /* Only the matching ones */
2390 for (i
= 0; i
< q
->nr
; i
++) {
2391 struct diff_filepair
*p
= q
->queue
[i
];
2393 if (((p
->status
== DIFF_STATUS_MODIFIED
) &&
2395 strchr(filter
, DIFF_STATUS_FILTER_BROKEN
)) ||
2397 strchr(filter
, DIFF_STATUS_MODIFIED
)))) ||
2398 ((p
->status
!= DIFF_STATUS_MODIFIED
) &&
2399 strchr(filter
, p
->status
)))
2402 diff_free_filepair(p
);
2409 void diffcore_std(struct diff_options
*options
)
2411 if (options
->break_opt
!= -1)
2412 diffcore_break(options
->break_opt
);
2413 if (options
->detect_rename
)
2414 diffcore_rename(options
);
2415 if (options
->break_opt
!= -1)
2416 diffcore_merge_broken();
2417 if (options
->pickaxe
)
2418 diffcore_pickaxe(options
->pickaxe
, options
->pickaxe_opts
);
2419 if (options
->orderfile
)
2420 diffcore_order(options
->orderfile
);
2421 diff_resolve_rename_copy();
2422 diffcore_apply_filter(options
->filter
);
2426 void diffcore_std_no_resolve(struct diff_options
*options
)
2428 if (options
->pickaxe
)
2429 diffcore_pickaxe(options
->pickaxe
, options
->pickaxe_opts
);
2430 if (options
->orderfile
)
2431 diffcore_order(options
->orderfile
);
2432 diffcore_apply_filter(options
->filter
);
2435 void diff_addremove(struct diff_options
*options
,
2436 int addremove
, unsigned mode
,
2437 const unsigned char *sha1
,
2438 const char *base
, const char *path
)
2440 char concatpath
[PATH_MAX
];
2441 struct diff_filespec
*one
, *two
;
2443 /* This may look odd, but it is a preparation for
2444 * feeding "there are unchanged files which should
2445 * not produce diffs, but when you are doing copy
2446 * detection you would need them, so here they are"
2447 * entries to the diff-core. They will be prefixed
2448 * with something like '=' or '*' (I haven't decided
2449 * which but should not make any difference).
2450 * Feeding the same new and old to diff_change()
2451 * also has the same effect.
2452 * Before the final output happens, they are pruned after
2453 * merged into rename/copy pairs as appropriate.
2455 if (options
->reverse_diff
)
2456 addremove
= (addremove
== '+' ? '-' :
2457 addremove
== '-' ? '+' : addremove
);
2459 if (!path
) path
= "";
2460 sprintf(concatpath
, "%s%s", base
, path
);
2461 one
= alloc_filespec(concatpath
);
2462 two
= alloc_filespec(concatpath
);
2464 if (addremove
!= '+')
2465 fill_filespec(one
, sha1
, mode
);
2466 if (addremove
!= '-')
2467 fill_filespec(two
, sha1
, mode
);
2469 diff_queue(&diff_queued_diff
, one
, two
);
2472 void diff_change(struct diff_options
*options
,
2473 unsigned old_mode
, unsigned new_mode
,
2474 const unsigned char *old_sha1
,
2475 const unsigned char *new_sha1
,
2476 const char *base
, const char *path
)
2478 char concatpath
[PATH_MAX
];
2479 struct diff_filespec
*one
, *two
;
2481 if (options
->reverse_diff
) {
2483 const unsigned char *tmp_c
;
2484 tmp
= old_mode
; old_mode
= new_mode
; new_mode
= tmp
;
2485 tmp_c
= old_sha1
; old_sha1
= new_sha1
; new_sha1
= tmp_c
;
2487 if (!path
) path
= "";
2488 sprintf(concatpath
, "%s%s", base
, path
);
2489 one
= alloc_filespec(concatpath
);
2490 two
= alloc_filespec(concatpath
);
2491 fill_filespec(one
, old_sha1
, old_mode
);
2492 fill_filespec(two
, new_sha1
, new_mode
);
2494 diff_queue(&diff_queued_diff
, one
, two
);
2497 void diff_unmerge(struct diff_options
*options
,
2500 struct diff_filespec
*one
, *two
;
2501 one
= alloc_filespec(path
);
2502 two
= alloc_filespec(path
);
2503 diff_queue(&diff_queued_diff
, one
, two
);