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
);
106 * These are to give UI layer defaults.
107 * The core-level commands such as git-diff-files should
108 * never be affected by the setting of diff.renames
109 * the user happens to have in the configuration file.
111 int git_diff_ui_config(const char *var
, const char *value
)
113 if (!strcmp(var
, "diff.renamelimit")) {
114 diff_rename_limit_default
= git_config_int(var
, value
);
117 if (!strcmp(var
, "diff.color")) {
119 diff_use_color_default
= 1; /* bool */
120 else if (!strcasecmp(value
, "auto"))
121 diff_use_color_default
= isatty(1);
122 else if (!strcasecmp(value
, "never"))
123 diff_use_color_default
= 0;
124 else if (!strcasecmp(value
, "always"))
125 diff_use_color_default
= 1;
127 diff_use_color_default
= git_config_bool(var
, value
);
130 if (!strcmp(var
, "diff.renames")) {
132 diff_detect_rename_default
= DIFF_DETECT_RENAME
;
133 else if (!strcasecmp(value
, "copies") ||
134 !strcasecmp(value
, "copy"))
135 diff_detect_rename_default
= DIFF_DETECT_COPY
;
136 else if (git_config_bool(var
,value
))
137 diff_detect_rename_default
= DIFF_DETECT_RENAME
;
140 if (!strncmp(var
, "diff.color.", 11)) {
141 int slot
= parse_diff_color_slot(var
, 11);
142 diff_colors
[slot
] = parse_diff_color_value(value
, var
);
145 return git_default_config(var
, value
);
148 static char *quote_one(const char *str
)
155 needlen
= quote_c_style(str
, NULL
, NULL
, 0);
158 xp
= xmalloc(needlen
+ 1);
159 quote_c_style(str
, xp
, NULL
, 0);
163 static char *quote_two(const char *one
, const char *two
)
165 int need_one
= quote_c_style(one
, NULL
, NULL
, 1);
166 int need_two
= quote_c_style(two
, NULL
, NULL
, 1);
169 if (need_one
+ need_two
) {
170 if (!need_one
) need_one
= strlen(one
);
171 if (!need_two
) need_one
= strlen(two
);
173 xp
= xmalloc(need_one
+ need_two
+ 3);
175 quote_c_style(one
, xp
+ 1, NULL
, 1);
176 quote_c_style(two
, xp
+ need_one
+ 1, NULL
, 1);
177 strcpy(xp
+ need_one
+ need_two
+ 1, "\"");
180 need_one
= strlen(one
);
181 need_two
= strlen(two
);
182 xp
= xmalloc(need_one
+ need_two
+ 1);
184 strcpy(xp
+ need_one
, two
);
188 static const char *external_diff(void)
190 static const char *external_diff_cmd
= NULL
;
191 static int done_preparing
= 0;
194 return external_diff_cmd
;
195 external_diff_cmd
= getenv("GIT_EXTERNAL_DIFF");
197 return external_diff_cmd
;
200 #define TEMPFILE_PATH_LEN 50
202 static struct diff_tempfile
{
203 const char *name
; /* filename external diff should read from */
206 char tmp_path
[TEMPFILE_PATH_LEN
];
209 static int count_lines(const char *data
, int size
)
211 int count
, ch
, completely_empty
= 1, nl_just_seen
= 0;
218 completely_empty
= 0;
222 completely_empty
= 0;
225 if (completely_empty
)
228 count
++; /* no trailing newline */
232 static void print_line_count(int count
)
242 printf("1,%d", count
);
247 static void copy_file(int prefix
, const char *data
, int size
)
249 int ch
, nl_just_seen
= 1;
261 printf("\n\\ No newline at end of file\n");
264 static void emit_rewrite_diff(const char *name_a
,
266 struct diff_filespec
*one
,
267 struct diff_filespec
*two
)
270 diff_populate_filespec(one
, 0);
271 diff_populate_filespec(two
, 0);
272 lc_a
= count_lines(one
->data
, one
->size
);
273 lc_b
= count_lines(two
->data
, two
->size
);
274 printf("--- %s\n+++ %s\n@@ -", name_a
, name_b
);
275 print_line_count(lc_a
);
277 print_line_count(lc_b
);
280 copy_file('-', one
->data
, one
->size
);
282 copy_file('+', two
->data
, two
->size
);
285 static int fill_mmfile(mmfile_t
*mf
, struct diff_filespec
*one
)
287 if (!DIFF_FILE_VALID(one
)) {
288 mf
->ptr
= (char *)""; /* does not matter */
292 else if (diff_populate_filespec(one
, 0))
295 mf
->size
= one
->size
;
299 struct emit_callback
{
300 struct xdiff_emit_state xm
;
301 int nparents
, color_diff
;
302 const char **label_path
;
305 static inline const char *get_color(int diff_use_color
, enum color_diff ix
)
308 return diff_colors
[ix
];
312 static void fn_out_consume(void *priv
, char *line
, unsigned long len
)
315 struct emit_callback
*ecbdata
= priv
;
316 const char *set
= get_color(ecbdata
->color_diff
, DIFF_METAINFO
);
317 const char *reset
= get_color(ecbdata
->color_diff
, DIFF_RESET
);
319 if (ecbdata
->label_path
[0]) {
320 printf("%s--- %s%s\n", set
, ecbdata
->label_path
[0], reset
);
321 printf("%s+++ %s%s\n", set
, ecbdata
->label_path
[1], reset
);
322 ecbdata
->label_path
[0] = ecbdata
->label_path
[1] = NULL
;
325 /* This is not really necessary for now because
326 * this codepath only deals with two-way diffs.
328 for (i
= 0; i
< len
&& line
[i
] == '@'; i
++)
330 if (2 <= i
&& i
< len
&& line
[i
] == ' ') {
331 ecbdata
->nparents
= i
- 1;
332 set
= get_color(ecbdata
->color_diff
, DIFF_FRAGINFO
);
334 else if (len
< ecbdata
->nparents
)
337 int nparents
= ecbdata
->nparents
;
338 int color
= DIFF_PLAIN
;
339 for (i
= 0; i
< nparents
&& len
; i
++) {
341 color
= DIFF_FILE_OLD
;
342 else if (line
[i
] == '+')
343 color
= DIFF_FILE_NEW
;
345 set
= get_color(ecbdata
->color_diff
, color
);
347 if (len
> 0 && line
[len
-1] == '\n')
350 fwrite (line
, len
, 1, stdout
);
354 static char *pprint_rename(const char *a
, const char *b
)
359 int pfx_length
, sfx_length
;
360 int len_a
= strlen(a
);
361 int len_b
= strlen(b
);
363 /* Find common prefix */
365 while (*old
&& *new && *old
== *new) {
367 pfx_length
= old
- a
+ 1;
372 /* Find common suffix */
376 while (a
<= old
&& b
<= new && *old
== *new) {
378 sfx_length
= len_a
- (old
- a
);
384 * pfx{mid-a => mid-b}sfx
385 * {pfx-a => pfx-b}sfx
386 * pfx{sfx-a => sfx-b}
389 if (pfx_length
+ sfx_length
) {
390 int a_midlen
= len_a
- pfx_length
- sfx_length
;
391 int b_midlen
= len_b
- pfx_length
- sfx_length
;
392 if (a_midlen
< 0) a_midlen
= 0;
393 if (b_midlen
< 0) b_midlen
= 0;
395 name
= xmalloc(pfx_length
+ a_midlen
+ b_midlen
+ sfx_length
+ 7);
396 sprintf(name
, "%.*s{%.*s => %.*s}%s",
398 a_midlen
, a
+ pfx_length
,
399 b_midlen
, b
+ pfx_length
,
400 a
+ len_a
- sfx_length
);
403 name
= xmalloc(len_a
+ len_b
+ 5);
404 sprintf(name
, "%s => %s", a
, b
);
410 struct xdiff_emit_state xm
;
414 struct diffstat_file
{
416 unsigned is_unmerged
:1;
417 unsigned is_binary
:1;
418 unsigned is_renamed
:1;
419 unsigned int added
, deleted
;
423 static struct diffstat_file
*diffstat_add(struct diffstat_t
*diffstat
,
427 struct diffstat_file
*x
;
428 x
= xcalloc(sizeof (*x
), 1);
429 if (diffstat
->nr
== diffstat
->alloc
) {
430 diffstat
->alloc
= alloc_nr(diffstat
->alloc
);
431 diffstat
->files
= xrealloc(diffstat
->files
,
432 diffstat
->alloc
* sizeof(x
));
434 diffstat
->files
[diffstat
->nr
++] = x
;
436 x
->name
= pprint_rename(name_a
, name_b
);
440 x
->name
= strdup(name_a
);
444 static void diffstat_consume(void *priv
, char *line
, unsigned long len
)
446 struct diffstat_t
*diffstat
= priv
;
447 struct diffstat_file
*x
= diffstat
->files
[diffstat
->nr
- 1];
451 else if (line
[0] == '-')
455 static const char pluses
[] = "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++";
456 static const char minuses
[]= "----------------------------------------------------------------------";
457 const char mime_boundary_leader
[] = "------------";
459 static void show_stats(struct diffstat_t
* data
)
461 int i
, len
, add
, del
, total
, adds
= 0, dels
= 0;
462 int max
, max_change
= 0, max_len
= 0;
463 int total_files
= data
->nr
;
468 for (i
= 0; i
< data
->nr
; i
++) {
469 struct diffstat_file
*file
= data
->files
[i
];
471 len
= strlen(file
->name
);
475 if (file
->is_binary
|| file
->is_unmerged
)
477 if (max_change
< file
->added
+ file
->deleted
)
478 max_change
= file
->added
+ file
->deleted
;
481 for (i
= 0; i
< data
->nr
; i
++) {
482 const char *prefix
= "";
483 char *name
= data
->files
[i
]->name
;
484 int added
= data
->files
[i
]->added
;
485 int deleted
= data
->files
[i
]->deleted
;
487 if (0 < (len
= quote_c_style(name
, NULL
, NULL
, 0))) {
488 char *qname
= xmalloc(len
+ 1);
489 quote_c_style(name
, qname
, NULL
, 0);
491 data
->files
[i
]->name
= name
= qname
;
495 * "scale" the filename
506 slash
= strchr(name
, '/');
513 * scale the add/delete
519 if (data
->files
[i
]->is_binary
) {
520 printf(" %s%-*s | Bin\n", prefix
, len
, name
);
521 goto free_diffstat_file
;
523 else if (data
->files
[i
]->is_unmerged
) {
524 printf(" %s%-*s | Unmerged\n", prefix
, len
, name
);
525 goto free_diffstat_file
;
527 else if (!data
->files
[i
]->is_renamed
&&
528 (added
+ deleted
== 0)) {
530 goto free_diffstat_file
;
539 if (max_change
> 0) {
540 total
= (total
* max
+ max_change
/ 2) / max_change
;
541 add
= (add
* max
+ max_change
/ 2) / max_change
;
544 printf(" %s%-*s |%5d %.*s%.*s\n", prefix
,
545 len
, name
, added
+ deleted
,
546 add
, pluses
, del
, minuses
);
548 free(data
->files
[i
]->name
);
549 free(data
->files
[i
]);
552 printf(" %d files changed, %d insertions(+), %d deletions(-)\n",
553 total_files
, adds
, dels
);
557 struct xdiff_emit_state xm
;
558 const char *filename
;
562 static void checkdiff_consume(void *priv
, char *line
, unsigned long len
)
564 struct checkdiff_t
*data
= priv
;
566 if (line
[0] == '+') {
571 /* check space before tab */
572 for (i
= 1; i
< len
&& (line
[i
] == ' ' || line
[i
] == '\t'); i
++)
575 if (line
[i
- 1] == '\t' && spaces
)
576 printf("%s:%d: space before tab:%.*s\n",
577 data
->filename
, data
->lineno
, (int)len
, line
);
579 /* check white space at line end */
580 if (line
[len
- 1] == '\n')
582 if (isspace(line
[len
- 1]))
583 printf("%s:%d: white space at end: %.*s\n",
584 data
->filename
, data
->lineno
, (int)len
, line
);
585 } else if (line
[0] == ' ')
587 else if (line
[0] == '@') {
588 char *plus
= strchr(line
, '+');
590 data
->lineno
= strtol(plus
, NULL
, 10);
596 static unsigned char *deflate_it(char *data
,
598 unsigned long *result_size
)
601 unsigned char *deflated
;
604 memset(&stream
, 0, sizeof(stream
));
605 deflateInit(&stream
, zlib_compression_level
);
606 bound
= deflateBound(&stream
, size
);
607 deflated
= xmalloc(bound
);
608 stream
.next_out
= deflated
;
609 stream
.avail_out
= bound
;
611 stream
.next_in
= (unsigned char *)data
;
612 stream
.avail_in
= size
;
613 while (deflate(&stream
, Z_FINISH
) == Z_OK
)
616 *result_size
= stream
.total_out
;
620 static void emit_binary_diff(mmfile_t
*one
, mmfile_t
*two
)
626 unsigned long orig_size
;
627 unsigned long delta_size
;
628 unsigned long deflate_size
;
629 unsigned long data_size
;
631 printf("GIT binary patch\n");
632 /* We could do deflated delta, or we could do just deflated two,
633 * whichever is smaller.
636 deflated
= deflate_it(two
->ptr
, two
->size
, &deflate_size
);
637 if (one
->size
&& two
->size
) {
638 delta
= diff_delta(one
->ptr
, one
->size
,
640 &delta_size
, deflate_size
);
642 void *to_free
= delta
;
643 orig_size
= delta_size
;
644 delta
= deflate_it(delta
, delta_size
, &delta_size
);
649 if (delta
&& delta_size
< deflate_size
) {
650 printf("delta %lu\n", orig_size
);
653 data_size
= delta_size
;
656 printf("literal %lu\n", two
->size
);
659 data_size
= deflate_size
;
662 /* emit data encoded in base85 */
665 int bytes
= (52 < data_size
) ? 52 : data_size
;
669 line
[0] = bytes
+ 'A' - 1;
671 line
[0] = bytes
- 26 + 'a' - 1;
672 encode_85(line
+ 1, cp
, bytes
);
673 cp
= (char *) cp
+ bytes
;
680 #define FIRST_FEW_BYTES 8000
681 static int mmfile_is_binary(mmfile_t
*mf
)
684 if (FIRST_FEW_BYTES
< sz
)
685 sz
= FIRST_FEW_BYTES
;
686 if (memchr(mf
->ptr
, 0, sz
))
691 static void builtin_diff(const char *name_a
,
693 struct diff_filespec
*one
,
694 struct diff_filespec
*two
,
695 const char *xfrm_msg
,
696 struct diff_options
*o
,
697 int complete_rewrite
)
702 const char *set
= get_color(o
->color_diff
, DIFF_METAINFO
);
703 const char *reset
= get_color(o
->color_diff
, DIFF_RESET
);
705 a_one
= quote_two("a/", name_a
);
706 b_two
= quote_two("b/", name_b
);
707 lbl
[0] = DIFF_FILE_VALID(one
) ? a_one
: "/dev/null";
708 lbl
[1] = DIFF_FILE_VALID(two
) ? b_two
: "/dev/null";
709 printf("%sdiff --git %s %s%s\n", set
, a_one
, b_two
, reset
);
710 if (lbl
[0][0] == '/') {
712 printf("%snew file mode %06o%s\n", set
, two
->mode
, reset
);
713 if (xfrm_msg
&& xfrm_msg
[0])
714 printf("%s%s%s\n", set
, xfrm_msg
, reset
);
716 else if (lbl
[1][0] == '/') {
717 printf("%sdeleted file mode %06o%s\n", set
, one
->mode
, reset
);
718 if (xfrm_msg
&& xfrm_msg
[0])
719 printf("%s%s%s\n", set
, xfrm_msg
, reset
);
722 if (one
->mode
!= two
->mode
) {
723 printf("%sold mode %06o%s\n", set
, one
->mode
, reset
);
724 printf("%snew mode %06o%s\n", set
, two
->mode
, reset
);
726 if (xfrm_msg
&& xfrm_msg
[0])
727 printf("%s%s%s\n", set
, xfrm_msg
, reset
);
729 * we do not run diff between different kind
732 if ((one
->mode
^ two
->mode
) & S_IFMT
)
733 goto free_ab_and_return
;
734 if (complete_rewrite
) {
735 emit_rewrite_diff(name_a
, name_b
, one
, two
);
736 goto free_ab_and_return
;
740 if (fill_mmfile(&mf1
, one
) < 0 || fill_mmfile(&mf2
, two
) < 0)
741 die("unable to read files to diff");
743 if (!o
->text
&& (mmfile_is_binary(&mf1
) || mmfile_is_binary(&mf2
))) {
744 /* Quite common confusing case */
745 if (mf1
.size
== mf2
.size
&&
746 !memcmp(mf1
.ptr
, mf2
.ptr
, mf1
.size
))
747 goto free_ab_and_return
;
749 emit_binary_diff(&mf1
, &mf2
);
751 printf("Binary files %s and %s differ\n",
755 /* Crazy xdl interfaces.. */
756 const char *diffopts
= getenv("GIT_DIFF_OPTS");
760 struct emit_callback ecbdata
;
762 memset(&ecbdata
, 0, sizeof(ecbdata
));
763 ecbdata
.label_path
= lbl
;
764 ecbdata
.color_diff
= o
->color_diff
;
765 xpp
.flags
= XDF_NEED_MINIMAL
| o
->xdl_opts
;
766 xecfg
.ctxlen
= o
->context
;
767 xecfg
.flags
= XDL_EMIT_FUNCNAMES
;
770 else if (!strncmp(diffopts
, "--unified=", 10))
771 xecfg
.ctxlen
= strtoul(diffopts
+ 10, NULL
, 10);
772 else if (!strncmp(diffopts
, "-u", 2))
773 xecfg
.ctxlen
= strtoul(diffopts
+ 2, NULL
, 10);
774 ecb
.outf
= xdiff_outf
;
776 ecbdata
.xm
.consume
= fn_out_consume
;
777 xdl_diff(&mf1
, &mf2
, &xpp
, &xecfg
, &ecb
);
786 static void builtin_diffstat(const char *name_a
, const char *name_b
,
787 struct diff_filespec
*one
,
788 struct diff_filespec
*two
,
789 struct diffstat_t
*diffstat
,
790 struct diff_options
*o
,
791 int complete_rewrite
)
794 struct diffstat_file
*data
;
796 data
= diffstat_add(diffstat
, name_a
, name_b
);
799 data
->is_unmerged
= 1;
802 if (complete_rewrite
) {
803 diff_populate_filespec(one
, 0);
804 diff_populate_filespec(two
, 0);
805 data
->deleted
= count_lines(one
->data
, one
->size
);
806 data
->added
= count_lines(two
->data
, two
->size
);
809 if (fill_mmfile(&mf1
, one
) < 0 || fill_mmfile(&mf2
, two
) < 0)
810 die("unable to read files to diff");
812 if (mmfile_is_binary(&mf1
) || mmfile_is_binary(&mf2
))
815 /* Crazy xdl interfaces.. */
820 xpp
.flags
= XDF_NEED_MINIMAL
| o
->xdl_opts
;
823 ecb
.outf
= xdiff_outf
;
825 xdl_diff(&mf1
, &mf2
, &xpp
, &xecfg
, &ecb
);
829 static void builtin_checkdiff(const char *name_a
, const char *name_b
,
830 struct diff_filespec
*one
,
831 struct diff_filespec
*two
)
834 struct checkdiff_t data
;
839 memset(&data
, 0, sizeof(data
));
840 data
.xm
.consume
= checkdiff_consume
;
841 data
.filename
= name_b
? name_b
: name_a
;
844 if (fill_mmfile(&mf1
, one
) < 0 || fill_mmfile(&mf2
, two
) < 0)
845 die("unable to read files to diff");
847 if (mmfile_is_binary(&mf2
))
850 /* Crazy xdl interfaces.. */
855 xpp
.flags
= XDF_NEED_MINIMAL
;
858 ecb
.outf
= xdiff_outf
;
860 xdl_diff(&mf1
, &mf2
, &xpp
, &xecfg
, &ecb
);
864 struct diff_filespec
*alloc_filespec(const char *path
)
866 int namelen
= strlen(path
);
867 struct diff_filespec
*spec
= xmalloc(sizeof(*spec
) + namelen
+ 1);
869 memset(spec
, 0, sizeof(*spec
));
870 spec
->path
= (char *)(spec
+ 1);
871 memcpy(spec
->path
, path
, namelen
+1);
875 void fill_filespec(struct diff_filespec
*spec
, const unsigned char *sha1
,
879 spec
->mode
= canon_mode(mode
);
880 memcpy(spec
->sha1
, sha1
, 20);
881 spec
->sha1_valid
= !!memcmp(sha1
, null_sha1
, 20);
886 * Given a name and sha1 pair, if the dircache tells us the file in
887 * the work tree has that object contents, return true, so that
888 * prepare_temp_file() does not have to inflate and extract.
890 static int work_tree_matches(const char *name
, const unsigned char *sha1
)
892 struct cache_entry
*ce
;
896 /* We do not read the cache ourselves here, because the
897 * benchmark with my previous version that always reads cache
898 * shows that it makes things worse for diff-tree comparing
899 * two linux-2.6 kernel trees in an already checked out work
900 * tree. This is because most diff-tree comparisons deal with
901 * only a small number of files, while reading the cache is
902 * expensive for a large project, and its cost outweighs the
903 * savings we get by not inflating the object to a temporary
904 * file. Practically, this code only helps when we are used
905 * by diff-cache --cached, which does read the cache before
912 pos
= cache_name_pos(name
, len
);
915 ce
= active_cache
[pos
];
916 if ((lstat(name
, &st
) < 0) ||
917 !S_ISREG(st
.st_mode
) || /* careful! */
918 ce_match_stat(ce
, &st
, 0) ||
919 memcmp(sha1
, ce
->sha1
, 20))
921 /* we return 1 only when we can stat, it is a regular file,
922 * stat information matches, and sha1 recorded in the cache
923 * matches. I.e. we know the file in the work tree really is
924 * the same as the <name, sha1> pair.
929 static struct sha1_size_cache
{
930 unsigned char sha1
[20];
933 static int sha1_size_cache_nr
, sha1_size_cache_alloc
;
935 static struct sha1_size_cache
*locate_size_cache(unsigned char *sha1
,
940 struct sha1_size_cache
*e
;
943 last
= sha1_size_cache_nr
;
944 while (last
> first
) {
945 int cmp
, next
= (last
+ first
) >> 1;
946 e
= sha1_size_cache
[next
];
947 cmp
= memcmp(e
->sha1
, sha1
, 20);
959 /* insert to make it at "first" */
960 if (sha1_size_cache_alloc
<= sha1_size_cache_nr
) {
961 sha1_size_cache_alloc
= alloc_nr(sha1_size_cache_alloc
);
962 sha1_size_cache
= xrealloc(sha1_size_cache
,
963 sha1_size_cache_alloc
*
964 sizeof(*sha1_size_cache
));
966 sha1_size_cache_nr
++;
967 if (first
< sha1_size_cache_nr
)
968 memmove(sha1_size_cache
+ first
+ 1, sha1_size_cache
+ first
,
969 (sha1_size_cache_nr
- first
- 1) *
970 sizeof(*sha1_size_cache
));
971 e
= xmalloc(sizeof(struct sha1_size_cache
));
972 sha1_size_cache
[first
] = e
;
973 memcpy(e
->sha1
, sha1
, 20);
979 * While doing rename detection and pickaxe operation, we may need to
980 * grab the data for the blob (or file) for our own in-core comparison.
981 * diff_filespec has data and size fields for this purpose.
983 int diff_populate_filespec(struct diff_filespec
*s
, int size_only
)
986 if (!DIFF_FILE_VALID(s
))
987 die("internal error: asking to populate invalid file.");
988 if (S_ISDIR(s
->mode
))
996 if (!s
->sha1_valid
||
997 work_tree_matches(s
->path
, s
->sha1
)) {
1000 if (lstat(s
->path
, &st
) < 0) {
1001 if (errno
== ENOENT
) {
1005 s
->data
= (char *)"";
1010 s
->size
= st
.st_size
;
1015 if (S_ISLNK(st
.st_mode
)) {
1017 s
->data
= xmalloc(s
->size
);
1019 ret
= readlink(s
->path
, s
->data
, s
->size
);
1026 fd
= open(s
->path
, O_RDONLY
);
1029 s
->data
= mmap(NULL
, s
->size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
1031 if (s
->data
== MAP_FAILED
)
1033 s
->should_munmap
= 1;
1037 struct sha1_size_cache
*e
;
1040 e
= locate_size_cache(s
->sha1
, 1, 0);
1045 if (!sha1_object_info(s
->sha1
, type
, &s
->size
))
1046 locate_size_cache(s
->sha1
, 0, s
->size
);
1049 s
->data
= read_sha1_file(s
->sha1
, type
, &s
->size
);
1056 void diff_free_filespec_data(struct diff_filespec
*s
)
1060 else if (s
->should_munmap
)
1061 munmap(s
->data
, s
->size
);
1062 s
->should_free
= s
->should_munmap
= 0;
1068 static void prep_temp_blob(struct diff_tempfile
*temp
,
1071 const unsigned char *sha1
,
1076 fd
= git_mkstemp(temp
->tmp_path
, TEMPFILE_PATH_LEN
, ".diff_XXXXXX");
1078 die("unable to create temp-file");
1079 if (write(fd
, blob
, size
) != size
)
1080 die("unable to write temp-file");
1082 temp
->name
= temp
->tmp_path
;
1083 strcpy(temp
->hex
, sha1_to_hex(sha1
));
1085 sprintf(temp
->mode
, "%06o", mode
);
1088 static void prepare_temp_file(const char *name
,
1089 struct diff_tempfile
*temp
,
1090 struct diff_filespec
*one
)
1092 if (!DIFF_FILE_VALID(one
)) {
1094 /* A '-' entry produces this for file-2, and
1095 * a '+' entry produces this for file-1.
1097 temp
->name
= "/dev/null";
1098 strcpy(temp
->hex
, ".");
1099 strcpy(temp
->mode
, ".");
1103 if (!one
->sha1_valid
||
1104 work_tree_matches(name
, one
->sha1
)) {
1106 if (lstat(name
, &st
) < 0) {
1107 if (errno
== ENOENT
)
1108 goto not_a_valid_file
;
1109 die("stat(%s): %s", name
, strerror(errno
));
1111 if (S_ISLNK(st
.st_mode
)) {
1113 char buf
[PATH_MAX
+ 1]; /* ought to be SYMLINK_MAX */
1114 if (sizeof(buf
) <= st
.st_size
)
1115 die("symlink too long: %s", name
);
1116 ret
= readlink(name
, buf
, st
.st_size
);
1118 die("readlink(%s)", name
);
1119 prep_temp_blob(temp
, buf
, st
.st_size
,
1121 one
->sha1
: null_sha1
),
1123 one
->mode
: S_IFLNK
));
1126 /* we can borrow from the file in the work tree */
1128 if (!one
->sha1_valid
)
1129 strcpy(temp
->hex
, sha1_to_hex(null_sha1
));
1131 strcpy(temp
->hex
, sha1_to_hex(one
->sha1
));
1132 /* Even though we may sometimes borrow the
1133 * contents from the work tree, we always want
1134 * one->mode. mode is trustworthy even when
1135 * !(one->sha1_valid), as long as
1136 * DIFF_FILE_VALID(one).
1138 sprintf(temp
->mode
, "%06o", one
->mode
);
1143 if (diff_populate_filespec(one
, 0))
1144 die("cannot read data blob for %s", one
->path
);
1145 prep_temp_blob(temp
, one
->data
, one
->size
,
1146 one
->sha1
, one
->mode
);
1150 static void remove_tempfile(void)
1154 for (i
= 0; i
< 2; i
++)
1155 if (diff_temp
[i
].name
== diff_temp
[i
].tmp_path
) {
1156 unlink(diff_temp
[i
].name
);
1157 diff_temp
[i
].name
= NULL
;
1161 static void remove_tempfile_on_signal(int signo
)
1164 signal(SIGINT
, SIG_DFL
);
1168 static int spawn_prog(const char *pgm
, const char **arg
)
1176 die("unable to fork");
1178 execvp(pgm
, (char *const*) arg
);
1182 while (waitpid(pid
, &status
, 0) < 0) {
1188 /* Earlier we did not check the exit status because
1189 * diff exits non-zero if files are different, and
1190 * we are not interested in knowing that. It was a
1191 * mistake which made it harder to quit a diff-*
1192 * session that uses the git-apply-patch-script as
1193 * the GIT_EXTERNAL_DIFF. A custom GIT_EXTERNAL_DIFF
1194 * should also exit non-zero only when it wants to
1195 * abort the entire diff-* session.
1197 if (WIFEXITED(status
) && !WEXITSTATUS(status
))
1202 /* An external diff command takes:
1204 * diff-cmd name infile1 infile1-sha1 infile1-mode \
1205 * infile2 infile2-sha1 infile2-mode [ rename-to ]
1208 static void run_external_diff(const char *pgm
,
1211 struct diff_filespec
*one
,
1212 struct diff_filespec
*two
,
1213 const char *xfrm_msg
,
1214 int complete_rewrite
)
1216 const char *spawn_arg
[10];
1217 struct diff_tempfile
*temp
= diff_temp
;
1219 static int atexit_asked
= 0;
1220 const char *othername
;
1221 const char **arg
= &spawn_arg
[0];
1223 othername
= (other
? other
: name
);
1225 prepare_temp_file(name
, &temp
[0], one
);
1226 prepare_temp_file(othername
, &temp
[1], two
);
1227 if (! atexit_asked
&&
1228 (temp
[0].name
== temp
[0].tmp_path
||
1229 temp
[1].name
== temp
[1].tmp_path
)) {
1231 atexit(remove_tempfile
);
1233 signal(SIGINT
, remove_tempfile_on_signal
);
1239 *arg
++ = temp
[0].name
;
1240 *arg
++ = temp
[0].hex
;
1241 *arg
++ = temp
[0].mode
;
1242 *arg
++ = temp
[1].name
;
1243 *arg
++ = temp
[1].hex
;
1244 *arg
++ = temp
[1].mode
;
1254 retval
= spawn_prog(pgm
, spawn_arg
);
1257 fprintf(stderr
, "external diff died, stopping at %s.\n", name
);
1262 static void run_diff_cmd(const char *pgm
,
1265 struct diff_filespec
*one
,
1266 struct diff_filespec
*two
,
1267 const char *xfrm_msg
,
1268 struct diff_options
*o
,
1269 int complete_rewrite
)
1272 run_external_diff(pgm
, name
, other
, one
, two
, xfrm_msg
,
1277 builtin_diff(name
, other
? other
: name
,
1278 one
, two
, xfrm_msg
, o
, complete_rewrite
);
1280 printf("* Unmerged path %s\n", name
);
1283 static void diff_fill_sha1_info(struct diff_filespec
*one
)
1285 if (DIFF_FILE_VALID(one
)) {
1286 if (!one
->sha1_valid
) {
1288 if (lstat(one
->path
, &st
) < 0)
1289 die("stat %s", one
->path
);
1290 if (index_path(one
->sha1
, one
->path
, &st
, 0))
1291 die("cannot hash %s\n", one
->path
);
1295 memset(one
->sha1
, 0, 20);
1298 static void run_diff(struct diff_filepair
*p
, struct diff_options
*o
)
1300 const char *pgm
= external_diff();
1301 char msg
[PATH_MAX
*2+300], *xfrm_msg
;
1302 struct diff_filespec
*one
;
1303 struct diff_filespec
*two
;
1306 char *name_munged
, *other_munged
;
1307 int complete_rewrite
= 0;
1310 if (DIFF_PAIR_UNMERGED(p
)) {
1312 run_diff_cmd(pgm
, p
->one
->path
, NULL
, NULL
, NULL
, NULL
, o
, 0);
1316 name
= p
->one
->path
;
1317 other
= (strcmp(name
, p
->two
->path
) ? p
->two
->path
: NULL
);
1318 name_munged
= quote_one(name
);
1319 other_munged
= quote_one(other
);
1320 one
= p
->one
; two
= p
->two
;
1322 diff_fill_sha1_info(one
);
1323 diff_fill_sha1_info(two
);
1326 switch (p
->status
) {
1327 case DIFF_STATUS_COPIED
:
1328 len
+= snprintf(msg
+ len
, sizeof(msg
) - len
,
1329 "similarity index %d%%\n"
1332 (int)(0.5 + p
->score
* 100.0/MAX_SCORE
),
1333 name_munged
, other_munged
);
1335 case DIFF_STATUS_RENAMED
:
1336 len
+= snprintf(msg
+ len
, sizeof(msg
) - len
,
1337 "similarity index %d%%\n"
1340 (int)(0.5 + p
->score
* 100.0/MAX_SCORE
),
1341 name_munged
, other_munged
);
1343 case DIFF_STATUS_MODIFIED
:
1345 len
+= snprintf(msg
+ len
, sizeof(msg
) - len
,
1346 "dissimilarity index %d%%\n",
1347 (int)(0.5 + p
->score
*
1349 complete_rewrite
= 1;
1358 if (memcmp(one
->sha1
, two
->sha1
, 20)) {
1359 int abbrev
= o
->full_index
? 40 : DEFAULT_ABBREV
;
1361 len
+= snprintf(msg
+ len
, sizeof(msg
) - len
,
1363 abbrev
, sha1_to_hex(one
->sha1
),
1364 abbrev
, sha1_to_hex(two
->sha1
));
1365 if (one
->mode
== two
->mode
)
1366 len
+= snprintf(msg
+ len
, sizeof(msg
) - len
,
1367 " %06o", one
->mode
);
1368 len
+= snprintf(msg
+ len
, sizeof(msg
) - len
, "\n");
1373 xfrm_msg
= len
? msg
: NULL
;
1376 DIFF_FILE_VALID(one
) && DIFF_FILE_VALID(two
) &&
1377 (S_IFMT
& one
->mode
) != (S_IFMT
& two
->mode
)) {
1378 /* a filepair that changes between file and symlink
1379 * needs to be split into deletion and creation.
1381 struct diff_filespec
*null
= alloc_filespec(two
->path
);
1382 run_diff_cmd(NULL
, name
, other
, one
, null
, xfrm_msg
, o
, 0);
1384 null
= alloc_filespec(one
->path
);
1385 run_diff_cmd(NULL
, name
, other
, null
, two
, xfrm_msg
, o
, 0);
1389 run_diff_cmd(pgm
, name
, other
, one
, two
, xfrm_msg
, o
,
1396 static void run_diffstat(struct diff_filepair
*p
, struct diff_options
*o
,
1397 struct diffstat_t
*diffstat
)
1401 int complete_rewrite
= 0;
1403 if (DIFF_PAIR_UNMERGED(p
)) {
1405 builtin_diffstat(p
->one
->path
, NULL
, NULL
, NULL
, diffstat
, o
, 0);
1409 name
= p
->one
->path
;
1410 other
= (strcmp(name
, p
->two
->path
) ? p
->two
->path
: NULL
);
1412 diff_fill_sha1_info(p
->one
);
1413 diff_fill_sha1_info(p
->two
);
1415 if (p
->status
== DIFF_STATUS_MODIFIED
&& p
->score
)
1416 complete_rewrite
= 1;
1417 builtin_diffstat(name
, other
, p
->one
, p
->two
, diffstat
, o
, complete_rewrite
);
1420 static void run_checkdiff(struct diff_filepair
*p
, struct diff_options
*o
)
1425 if (DIFF_PAIR_UNMERGED(p
)) {
1430 name
= p
->one
->path
;
1431 other
= (strcmp(name
, p
->two
->path
) ? p
->two
->path
: NULL
);
1433 diff_fill_sha1_info(p
->one
);
1434 diff_fill_sha1_info(p
->two
);
1436 builtin_checkdiff(name
, other
, p
->one
, p
->two
);
1439 void diff_setup(struct diff_options
*options
)
1441 memset(options
, 0, sizeof(*options
));
1442 options
->line_termination
= '\n';
1443 options
->break_opt
= -1;
1444 options
->rename_limit
= -1;
1445 options
->context
= 3;
1446 options
->msg_sep
= "";
1448 options
->change
= diff_change
;
1449 options
->add_remove
= diff_addremove
;
1450 options
->color_diff
= diff_use_color_default
;
1451 options
->detect_rename
= diff_detect_rename_default
;
1454 int diff_setup_done(struct diff_options
*options
)
1456 if ((options
->find_copies_harder
&&
1457 options
->detect_rename
!= DIFF_DETECT_COPY
) ||
1458 (0 <= options
->rename_limit
&& !options
->detect_rename
))
1461 if (options
->output_format
& (DIFF_FORMAT_NAME
|
1462 DIFF_FORMAT_NAME_STATUS
|
1463 DIFF_FORMAT_CHECKDIFF
|
1464 DIFF_FORMAT_NO_OUTPUT
))
1465 options
->output_format
&= ~(DIFF_FORMAT_RAW
|
1466 DIFF_FORMAT_DIFFSTAT
|
1467 DIFF_FORMAT_SUMMARY
|
1471 * These cases always need recursive; we do not drop caller-supplied
1472 * recursive bits for other formats here.
1474 if (options
->output_format
& (DIFF_FORMAT_PATCH
|
1475 DIFF_FORMAT_DIFFSTAT
|
1476 DIFF_FORMAT_CHECKDIFF
))
1477 options
->recursive
= 1;
1479 * Also pickaxe would not work very well if you do not say recursive
1481 if (options
->pickaxe
)
1482 options
->recursive
= 1;
1484 if (options
->detect_rename
&& options
->rename_limit
< 0)
1485 options
->rename_limit
= diff_rename_limit_default
;
1486 if (options
->setup
& DIFF_SETUP_USE_CACHE
) {
1488 /* read-cache does not die even when it fails
1489 * so it is safe for us to do this here. Also
1490 * it does not smudge active_cache or active_nr
1491 * when it fails, so we do not have to worry about
1492 * cleaning it up ourselves either.
1496 if (options
->setup
& DIFF_SETUP_USE_SIZE_CACHE
)
1498 if (options
->abbrev
<= 0 || 40 < options
->abbrev
)
1499 options
->abbrev
= 40; /* full */
1504 static int opt_arg(const char *arg
, int arg_short
, const char *arg_long
, int *val
)
1514 if (c
== arg_short
) {
1518 if (val
&& isdigit(c
)) {
1520 int n
= strtoul(arg
, &end
, 10);
1531 eq
= strchr(arg
, '=');
1536 if (!len
|| strncmp(arg
, arg_long
, len
))
1541 if (!isdigit(*++eq
))
1543 n
= strtoul(eq
, &end
, 10);
1551 int diff_opt_parse(struct diff_options
*options
, const char **av
, int ac
)
1553 const char *arg
= av
[0];
1554 if (!strcmp(arg
, "-p") || !strcmp(arg
, "-u"))
1555 options
->output_format
|= DIFF_FORMAT_PATCH
;
1556 else if (opt_arg(arg
, 'U', "unified", &options
->context
))
1557 options
->output_format
|= DIFF_FORMAT_PATCH
;
1558 else if (!strcmp(arg
, "--raw"))
1559 options
->output_format
|= DIFF_FORMAT_RAW
;
1560 else if (!strcmp(arg
, "--patch-with-raw")) {
1561 options
->output_format
|= DIFF_FORMAT_PATCH
| DIFF_FORMAT_RAW
;
1563 else if (!strcmp(arg
, "--stat"))
1564 options
->output_format
|= DIFF_FORMAT_DIFFSTAT
;
1565 else if (!strcmp(arg
, "--check"))
1566 options
->output_format
|= DIFF_FORMAT_CHECKDIFF
;
1567 else if (!strcmp(arg
, "--summary"))
1568 options
->output_format
|= DIFF_FORMAT_SUMMARY
;
1569 else if (!strcmp(arg
, "--patch-with-stat")) {
1570 options
->output_format
|= DIFF_FORMAT_PATCH
| DIFF_FORMAT_DIFFSTAT
;
1572 else if (!strcmp(arg
, "-z"))
1573 options
->line_termination
= 0;
1574 else if (!strncmp(arg
, "-l", 2))
1575 options
->rename_limit
= strtoul(arg
+2, NULL
, 10);
1576 else if (!strcmp(arg
, "--full-index"))
1577 options
->full_index
= 1;
1578 else if (!strcmp(arg
, "--binary")) {
1579 options
->output_format
|= DIFF_FORMAT_PATCH
;
1580 options
->full_index
= options
->binary
= 1;
1582 else if (!strcmp(arg
, "-a") || !strcmp(arg
, "--text")) {
1585 else if (!strcmp(arg
, "--name-only"))
1586 options
->output_format
|= DIFF_FORMAT_NAME
;
1587 else if (!strcmp(arg
, "--name-status"))
1588 options
->output_format
|= DIFF_FORMAT_NAME_STATUS
;
1589 else if (!strcmp(arg
, "-R"))
1590 options
->reverse_diff
= 1;
1591 else if (!strncmp(arg
, "-S", 2))
1592 options
->pickaxe
= arg
+ 2;
1593 else if (!strcmp(arg
, "-s")) {
1594 options
->output_format
|= DIFF_FORMAT_NO_OUTPUT
;
1596 else if (!strncmp(arg
, "-O", 2))
1597 options
->orderfile
= arg
+ 2;
1598 else if (!strncmp(arg
, "--diff-filter=", 14))
1599 options
->filter
= arg
+ 14;
1600 else if (!strcmp(arg
, "--pickaxe-all"))
1601 options
->pickaxe_opts
= DIFF_PICKAXE_ALL
;
1602 else if (!strcmp(arg
, "--pickaxe-regex"))
1603 options
->pickaxe_opts
= DIFF_PICKAXE_REGEX
;
1604 else if (!strncmp(arg
, "-B", 2)) {
1605 if ((options
->break_opt
=
1606 diff_scoreopt_parse(arg
)) == -1)
1609 else if (!strncmp(arg
, "-M", 2)) {
1610 if ((options
->rename_score
=
1611 diff_scoreopt_parse(arg
)) == -1)
1613 options
->detect_rename
= DIFF_DETECT_RENAME
;
1615 else if (!strncmp(arg
, "-C", 2)) {
1616 if ((options
->rename_score
=
1617 diff_scoreopt_parse(arg
)) == -1)
1619 options
->detect_rename
= DIFF_DETECT_COPY
;
1621 else if (!strcmp(arg
, "--find-copies-harder"))
1622 options
->find_copies_harder
= 1;
1623 else if (!strcmp(arg
, "--abbrev"))
1624 options
->abbrev
= DEFAULT_ABBREV
;
1625 else if (!strncmp(arg
, "--abbrev=", 9)) {
1626 options
->abbrev
= strtoul(arg
+ 9, NULL
, 10);
1627 if (options
->abbrev
< MINIMUM_ABBREV
)
1628 options
->abbrev
= MINIMUM_ABBREV
;
1629 else if (40 < options
->abbrev
)
1630 options
->abbrev
= 40;
1632 else if (!strcmp(arg
, "--color"))
1633 options
->color_diff
= 1;
1634 else if (!strcmp(arg
, "--no-color"))
1635 options
->color_diff
= 0;
1636 else if (!strcmp(arg
, "-w") || !strcmp(arg
, "--ignore-all-space"))
1637 options
->xdl_opts
|= XDF_IGNORE_WHITESPACE
;
1638 else if (!strcmp(arg
, "-b") || !strcmp(arg
, "--ignore-space-change"))
1639 options
->xdl_opts
|= XDF_IGNORE_WHITESPACE_CHANGE
;
1640 else if (!strcmp(arg
, "--no-renames"))
1641 options
->detect_rename
= 0;
1647 static int parse_num(const char **cp_p
)
1649 unsigned long num
, scale
;
1651 const char *cp
= *cp_p
;
1658 if ( !dot
&& ch
== '.' ) {
1661 } else if ( ch
== '%' ) {
1662 scale
= dot
? scale
*100 : 100;
1663 cp
++; /* % is always at the end */
1665 } else if ( ch
>= '0' && ch
<= '9' ) {
1666 if ( scale
< 100000 ) {
1668 num
= (num
*10) + (ch
-'0');
1677 /* user says num divided by scale and we say internally that
1678 * is MAX_SCORE * num / scale.
1680 return (num
>= scale
) ? MAX_SCORE
: (MAX_SCORE
* num
/ scale
);
1683 int diff_scoreopt_parse(const char *opt
)
1685 int opt1
, opt2
, cmd
;
1690 if (cmd
!= 'M' && cmd
!= 'C' && cmd
!= 'B')
1691 return -1; /* that is not a -M, -C nor -B option */
1693 opt1
= parse_num(&opt
);
1699 else if (*opt
!= '/')
1700 return -1; /* we expect -B80/99 or -B80 */
1703 opt2
= parse_num(&opt
);
1708 return opt1
| (opt2
<< 16);
1711 struct diff_queue_struct diff_queued_diff
;
1713 void diff_q(struct diff_queue_struct
*queue
, struct diff_filepair
*dp
)
1715 if (queue
->alloc
<= queue
->nr
) {
1716 queue
->alloc
= alloc_nr(queue
->alloc
);
1717 queue
->queue
= xrealloc(queue
->queue
,
1718 sizeof(dp
) * queue
->alloc
);
1720 queue
->queue
[queue
->nr
++] = dp
;
1723 struct diff_filepair
*diff_queue(struct diff_queue_struct
*queue
,
1724 struct diff_filespec
*one
,
1725 struct diff_filespec
*two
)
1727 struct diff_filepair
*dp
= xmalloc(sizeof(*dp
));
1732 dp
->source_stays
= 0;
1733 dp
->broken_pair
= 0;
1739 void diff_free_filepair(struct diff_filepair
*p
)
1741 diff_free_filespec_data(p
->one
);
1742 diff_free_filespec_data(p
->two
);
1748 /* This is different from find_unique_abbrev() in that
1749 * it stuffs the result with dots for alignment.
1751 const char *diff_unique_abbrev(const unsigned char *sha1
, int len
)
1756 return sha1_to_hex(sha1
);
1758 abbrev
= find_unique_abbrev(sha1
, len
);
1760 return sha1_to_hex(sha1
);
1761 abblen
= strlen(abbrev
);
1763 static char hex
[41];
1764 if (len
< abblen
&& abblen
<= len
+ 2)
1765 sprintf(hex
, "%s%.*s", abbrev
, len
+3-abblen
, "..");
1767 sprintf(hex
, "%s...", abbrev
);
1770 return sha1_to_hex(sha1
);
1773 static void diff_flush_raw(struct diff_filepair
*p
,
1774 struct diff_options
*options
)
1778 int abbrev
= options
->abbrev
;
1779 const char *path_one
, *path_two
;
1780 int inter_name_termination
= '\t';
1781 int line_termination
= options
->line_termination
;
1783 if (!line_termination
)
1784 inter_name_termination
= 0;
1786 path_one
= p
->one
->path
;
1787 path_two
= p
->two
->path
;
1788 if (line_termination
) {
1789 path_one
= quote_one(path_one
);
1790 path_two
= quote_one(path_two
);
1794 sprintf(status
, "%c%03d", p
->status
,
1795 (int)(0.5 + p
->score
* 100.0/MAX_SCORE
));
1797 status
[0] = p
->status
;
1800 switch (p
->status
) {
1801 case DIFF_STATUS_COPIED
:
1802 case DIFF_STATUS_RENAMED
:
1805 case DIFF_STATUS_ADDED
:
1806 case DIFF_STATUS_DELETED
:
1813 if (!(options
->output_format
& DIFF_FORMAT_NAME_STATUS
)) {
1814 printf(":%06o %06o %s ",
1815 p
->one
->mode
, p
->two
->mode
,
1816 diff_unique_abbrev(p
->one
->sha1
, abbrev
));
1818 diff_unique_abbrev(p
->two
->sha1
, abbrev
));
1820 printf("%s%c%s", status
, inter_name_termination
, path_one
);
1822 printf("%c%s", inter_name_termination
, path_two
);
1823 putchar(line_termination
);
1824 if (path_one
!= p
->one
->path
)
1825 free((void*)path_one
);
1826 if (path_two
!= p
->two
->path
)
1827 free((void*)path_two
);
1830 static void diff_flush_name(struct diff_filepair
*p
, int line_termination
)
1832 char *path
= p
->two
->path
;
1834 if (line_termination
)
1835 path
= quote_one(p
->two
->path
);
1836 printf("%s%c", path
, line_termination
);
1837 if (p
->two
->path
!= path
)
1841 int diff_unmodified_pair(struct diff_filepair
*p
)
1843 /* This function is written stricter than necessary to support
1844 * the currently implemented transformers, but the idea is to
1845 * let transformers to produce diff_filepairs any way they want,
1846 * and filter and clean them up here before producing the output.
1848 struct diff_filespec
*one
, *two
;
1850 if (DIFF_PAIR_UNMERGED(p
))
1851 return 0; /* unmerged is interesting */
1856 /* deletion, addition, mode or type change
1857 * and rename are all interesting.
1859 if (DIFF_FILE_VALID(one
) != DIFF_FILE_VALID(two
) ||
1860 DIFF_PAIR_MODE_CHANGED(p
) ||
1861 strcmp(one
->path
, two
->path
))
1864 /* both are valid and point at the same path. that is, we are
1865 * dealing with a change.
1867 if (one
->sha1_valid
&& two
->sha1_valid
&&
1868 !memcmp(one
->sha1
, two
->sha1
, sizeof(one
->sha1
)))
1869 return 1; /* no change */
1870 if (!one
->sha1_valid
&& !two
->sha1_valid
)
1871 return 1; /* both look at the same file on the filesystem. */
1875 static void diff_flush_patch(struct diff_filepair
*p
, struct diff_options
*o
)
1877 if (diff_unmodified_pair(p
))
1880 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
1881 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
1882 return; /* no tree diffs in patch format */
1887 static void diff_flush_stat(struct diff_filepair
*p
, struct diff_options
*o
,
1888 struct diffstat_t
*diffstat
)
1890 if (diff_unmodified_pair(p
))
1893 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
1894 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
1895 return; /* no tree diffs in patch format */
1897 run_diffstat(p
, o
, diffstat
);
1900 static void diff_flush_checkdiff(struct diff_filepair
*p
,
1901 struct diff_options
*o
)
1903 if (diff_unmodified_pair(p
))
1906 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
1907 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
1908 return; /* no tree diffs in patch format */
1910 run_checkdiff(p
, o
);
1913 int diff_queue_is_empty(void)
1915 struct diff_queue_struct
*q
= &diff_queued_diff
;
1917 for (i
= 0; i
< q
->nr
; i
++)
1918 if (!diff_unmodified_pair(q
->queue
[i
]))
1924 void diff_debug_filespec(struct diff_filespec
*s
, int x
, const char *one
)
1926 fprintf(stderr
, "queue[%d] %s (%s) %s %06o %s\n",
1929 DIFF_FILE_VALID(s
) ? "valid" : "invalid",
1931 s
->sha1_valid
? sha1_to_hex(s
->sha1
) : "");
1932 fprintf(stderr
, "queue[%d] %s size %lu flags %d\n",
1934 s
->size
, s
->xfrm_flags
);
1937 void diff_debug_filepair(const struct diff_filepair
*p
, int i
)
1939 diff_debug_filespec(p
->one
, i
, "one");
1940 diff_debug_filespec(p
->two
, i
, "two");
1941 fprintf(stderr
, "score %d, status %c stays %d broken %d\n",
1942 p
->score
, p
->status
? p
->status
: '?',
1943 p
->source_stays
, p
->broken_pair
);
1946 void diff_debug_queue(const char *msg
, struct diff_queue_struct
*q
)
1950 fprintf(stderr
, "%s\n", msg
);
1951 fprintf(stderr
, "q->nr = %d\n", q
->nr
);
1952 for (i
= 0; i
< q
->nr
; i
++) {
1953 struct diff_filepair
*p
= q
->queue
[i
];
1954 diff_debug_filepair(p
, i
);
1959 static void diff_resolve_rename_copy(void)
1962 struct diff_filepair
*p
, *pp
;
1963 struct diff_queue_struct
*q
= &diff_queued_diff
;
1965 diff_debug_queue("resolve-rename-copy", q
);
1967 for (i
= 0; i
< q
->nr
; i
++) {
1969 p
->status
= 0; /* undecided */
1970 if (DIFF_PAIR_UNMERGED(p
))
1971 p
->status
= DIFF_STATUS_UNMERGED
;
1972 else if (!DIFF_FILE_VALID(p
->one
))
1973 p
->status
= DIFF_STATUS_ADDED
;
1974 else if (!DIFF_FILE_VALID(p
->two
))
1975 p
->status
= DIFF_STATUS_DELETED
;
1976 else if (DIFF_PAIR_TYPE_CHANGED(p
))
1977 p
->status
= DIFF_STATUS_TYPE_CHANGED
;
1979 /* from this point on, we are dealing with a pair
1980 * whose both sides are valid and of the same type, i.e.
1981 * either in-place edit or rename/copy edit.
1983 else if (DIFF_PAIR_RENAME(p
)) {
1984 if (p
->source_stays
) {
1985 p
->status
= DIFF_STATUS_COPIED
;
1988 /* See if there is some other filepair that
1989 * copies from the same source as us. If so
1990 * we are a copy. Otherwise we are either a
1991 * copy if the path stays, or a rename if it
1992 * does not, but we already handled "stays" case.
1994 for (j
= i
+ 1; j
< q
->nr
; j
++) {
1996 if (strcmp(pp
->one
->path
, p
->one
->path
))
1997 continue; /* not us */
1998 if (!DIFF_PAIR_RENAME(pp
))
1999 continue; /* not a rename/copy */
2000 /* pp is a rename/copy from the same source */
2001 p
->status
= DIFF_STATUS_COPIED
;
2005 p
->status
= DIFF_STATUS_RENAMED
;
2007 else if (memcmp(p
->one
->sha1
, p
->two
->sha1
, 20) ||
2008 p
->one
->mode
!= p
->two
->mode
)
2009 p
->status
= DIFF_STATUS_MODIFIED
;
2011 /* This is a "no-change" entry and should not
2012 * happen anymore, but prepare for broken callers.
2014 error("feeding unmodified %s to diffcore",
2016 p
->status
= DIFF_STATUS_UNKNOWN
;
2019 diff_debug_queue("resolve-rename-copy done", q
);
2022 static int check_pair_status(struct diff_filepair
*p
)
2024 switch (p
->status
) {
2025 case DIFF_STATUS_UNKNOWN
:
2028 die("internal error in diff-resolve-rename-copy");
2034 static void flush_one_pair(struct diff_filepair
*p
, struct diff_options
*opt
)
2036 int fmt
= opt
->output_format
;
2038 if (fmt
& DIFF_FORMAT_CHECKDIFF
)
2039 diff_flush_checkdiff(p
, opt
);
2040 else if (fmt
& (DIFF_FORMAT_RAW
| DIFF_FORMAT_NAME_STATUS
))
2041 diff_flush_raw(p
, opt
);
2042 else if (fmt
& DIFF_FORMAT_NAME
)
2043 diff_flush_name(p
, opt
->line_termination
);
2046 static void show_file_mode_name(const char *newdelete
, struct diff_filespec
*fs
)
2049 printf(" %s mode %06o %s\n", newdelete
, fs
->mode
, fs
->path
);
2051 printf(" %s %s\n", newdelete
, fs
->path
);
2055 static void show_mode_change(struct diff_filepair
*p
, int show_name
)
2057 if (p
->one
->mode
&& p
->two
->mode
&& p
->one
->mode
!= p
->two
->mode
) {
2059 printf(" mode change %06o => %06o %s\n",
2060 p
->one
->mode
, p
->two
->mode
, p
->two
->path
);
2062 printf(" mode change %06o => %06o\n",
2063 p
->one
->mode
, p
->two
->mode
);
2067 static void show_rename_copy(const char *renamecopy
, struct diff_filepair
*p
)
2069 const char *old
, *new;
2071 /* Find common prefix */
2075 const char *slash_old
, *slash_new
;
2076 slash_old
= strchr(old
, '/');
2077 slash_new
= strchr(new, '/');
2080 slash_old
- old
!= slash_new
- new ||
2081 memcmp(old
, new, slash_new
- new))
2083 old
= slash_old
+ 1;
2084 new = slash_new
+ 1;
2086 /* p->one->path thru old is the common prefix, and old and new
2087 * through the end of names are renames
2089 if (old
!= p
->one
->path
)
2090 printf(" %s %.*s{%s => %s} (%d%%)\n", renamecopy
,
2091 (int)(old
- p
->one
->path
), p
->one
->path
,
2092 old
, new, (int)(0.5 + p
->score
* 100.0/MAX_SCORE
));
2094 printf(" %s %s => %s (%d%%)\n", renamecopy
,
2095 p
->one
->path
, p
->two
->path
,
2096 (int)(0.5 + p
->score
* 100.0/MAX_SCORE
));
2097 show_mode_change(p
, 0);
2100 static void diff_summary(struct diff_filepair
*p
)
2103 case DIFF_STATUS_DELETED
:
2104 show_file_mode_name("delete", p
->one
);
2106 case DIFF_STATUS_ADDED
:
2107 show_file_mode_name("create", p
->two
);
2109 case DIFF_STATUS_COPIED
:
2110 show_rename_copy("copy", p
);
2112 case DIFF_STATUS_RENAMED
:
2113 show_rename_copy("rename", p
);
2117 printf(" rewrite %s (%d%%)\n", p
->two
->path
,
2118 (int)(0.5 + p
->score
* 100.0/MAX_SCORE
));
2119 show_mode_change(p
, 0);
2120 } else show_mode_change(p
, 1);
2126 struct xdiff_emit_state xm
;
2131 static int remove_space(char *line
, int len
)
2137 for (i
= 0; i
< len
; i
++)
2138 if (!isspace((c
= line
[i
])))
2144 static void patch_id_consume(void *priv
, char *line
, unsigned long len
)
2146 struct patch_id_t
*data
= priv
;
2149 /* Ignore line numbers when computing the SHA1 of the patch */
2150 if (!strncmp(line
, "@@ -", 4))
2153 new_len
= remove_space(line
, len
);
2155 SHA1_Update(data
->ctx
, line
, new_len
);
2156 data
->patchlen
+= new_len
;
2159 /* returns 0 upon success, and writes result into sha1 */
2160 static int diff_get_patch_id(struct diff_options
*options
, unsigned char *sha1
)
2162 struct diff_queue_struct
*q
= &diff_queued_diff
;
2165 struct patch_id_t data
;
2166 char buffer
[PATH_MAX
* 4 + 20];
2169 memset(&data
, 0, sizeof(struct patch_id_t
));
2171 data
.xm
.consume
= patch_id_consume
;
2173 for (i
= 0; i
< q
->nr
; i
++) {
2178 struct diff_filepair
*p
= q
->queue
[i
];
2182 return error("internal diff status error");
2183 if (p
->status
== DIFF_STATUS_UNKNOWN
)
2185 if (diff_unmodified_pair(p
))
2187 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
2188 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
2190 if (DIFF_PAIR_UNMERGED(p
))
2193 diff_fill_sha1_info(p
->one
);
2194 diff_fill_sha1_info(p
->two
);
2195 if (fill_mmfile(&mf1
, p
->one
) < 0 ||
2196 fill_mmfile(&mf2
, p
->two
) < 0)
2197 return error("unable to read files to diff");
2199 /* Maybe hash p->two? into the patch id? */
2200 if (mmfile_is_binary(&mf2
))
2203 len1
= remove_space(p
->one
->path
, strlen(p
->one
->path
));
2204 len2
= remove_space(p
->two
->path
, strlen(p
->two
->path
));
2205 if (p
->one
->mode
== 0)
2206 len1
= snprintf(buffer
, sizeof(buffer
),
2207 "diff--gita/%.*sb/%.*s"
2214 len2
, p
->two
->path
);
2215 else if (p
->two
->mode
== 0)
2216 len1
= snprintf(buffer
, sizeof(buffer
),
2217 "diff--gita/%.*sb/%.*s"
2218 "deletedfilemode%06o"
2224 len1
, p
->one
->path
);
2226 len1
= snprintf(buffer
, sizeof(buffer
),
2227 "diff--gita/%.*sb/%.*s"
2233 len2
, p
->two
->path
);
2234 SHA1_Update(&ctx
, buffer
, len1
);
2236 xpp
.flags
= XDF_NEED_MINIMAL
;
2238 xecfg
.flags
= XDL_EMIT_FUNCNAMES
;
2239 ecb
.outf
= xdiff_outf
;
2241 xdl_diff(&mf1
, &mf2
, &xpp
, &xecfg
, &ecb
);
2244 SHA1_Final(sha1
, &ctx
);
2248 int diff_flush_patch_id(struct diff_options
*options
, unsigned char *sha1
)
2250 struct diff_queue_struct
*q
= &diff_queued_diff
;
2252 int result
= diff_get_patch_id(options
, sha1
);
2254 for (i
= 0; i
< q
->nr
; i
++)
2255 diff_free_filepair(q
->queue
[i
]);
2259 q
->nr
= q
->alloc
= 0;
2264 static int is_summary_empty(const struct diff_queue_struct
*q
)
2268 for (i
= 0; i
< q
->nr
; i
++) {
2269 const struct diff_filepair
*p
= q
->queue
[i
];
2271 switch (p
->status
) {
2272 case DIFF_STATUS_DELETED
:
2273 case DIFF_STATUS_ADDED
:
2274 case DIFF_STATUS_COPIED
:
2275 case DIFF_STATUS_RENAMED
:
2280 if (p
->one
->mode
&& p
->two
->mode
&&
2281 p
->one
->mode
!= p
->two
->mode
)
2289 void diff_flush(struct diff_options
*options
)
2291 struct diff_queue_struct
*q
= &diff_queued_diff
;
2292 int i
, output_format
= options
->output_format
;
2296 * Order: raw, stat, summary, patch
2297 * or: name/name-status/checkdiff (other bits clear)
2302 if (output_format
& (DIFF_FORMAT_RAW
|
2304 DIFF_FORMAT_NAME_STATUS
|
2305 DIFF_FORMAT_CHECKDIFF
)) {
2306 for (i
= 0; i
< q
->nr
; i
++) {
2307 struct diff_filepair
*p
= q
->queue
[i
];
2308 if (check_pair_status(p
))
2309 flush_one_pair(p
, options
);
2314 if (output_format
& DIFF_FORMAT_DIFFSTAT
) {
2315 struct diffstat_t diffstat
;
2317 memset(&diffstat
, 0, sizeof(struct diffstat_t
));
2318 diffstat
.xm
.consume
= diffstat_consume
;
2319 for (i
= 0; i
< q
->nr
; i
++) {
2320 struct diff_filepair
*p
= q
->queue
[i
];
2321 if (check_pair_status(p
))
2322 diff_flush_stat(p
, options
, &diffstat
);
2324 show_stats(&diffstat
);
2328 if (output_format
& DIFF_FORMAT_SUMMARY
&& !is_summary_empty(q
)) {
2329 for (i
= 0; i
< q
->nr
; i
++)
2330 diff_summary(q
->queue
[i
]);
2334 if (output_format
& DIFF_FORMAT_PATCH
) {
2336 if (options
->stat_sep
) {
2337 /* attach patch instead of inline */
2338 fputs(options
->stat_sep
, stdout
);
2340 putchar(options
->line_termination
);
2344 for (i
= 0; i
< q
->nr
; i
++) {
2345 struct diff_filepair
*p
= q
->queue
[i
];
2346 if (check_pair_status(p
))
2347 diff_flush_patch(p
, options
);
2351 for (i
= 0; i
< q
->nr
; i
++)
2352 diff_free_filepair(q
->queue
[i
]);
2356 q
->nr
= q
->alloc
= 0;
2359 static void diffcore_apply_filter(const char *filter
)
2362 struct diff_queue_struct
*q
= &diff_queued_diff
;
2363 struct diff_queue_struct outq
;
2365 outq
.nr
= outq
.alloc
= 0;
2370 if (strchr(filter
, DIFF_STATUS_FILTER_AON
)) {
2372 for (i
= found
= 0; !found
&& i
< q
->nr
; i
++) {
2373 struct diff_filepair
*p
= q
->queue
[i
];
2374 if (((p
->status
== DIFF_STATUS_MODIFIED
) &&
2376 strchr(filter
, DIFF_STATUS_FILTER_BROKEN
)) ||
2378 strchr(filter
, DIFF_STATUS_MODIFIED
)))) ||
2379 ((p
->status
!= DIFF_STATUS_MODIFIED
) &&
2380 strchr(filter
, p
->status
)))
2386 /* otherwise we will clear the whole queue
2387 * by copying the empty outq at the end of this
2388 * function, but first clear the current entries
2391 for (i
= 0; i
< q
->nr
; i
++)
2392 diff_free_filepair(q
->queue
[i
]);
2395 /* Only the matching ones */
2396 for (i
= 0; i
< q
->nr
; i
++) {
2397 struct diff_filepair
*p
= q
->queue
[i
];
2399 if (((p
->status
== DIFF_STATUS_MODIFIED
) &&
2401 strchr(filter
, DIFF_STATUS_FILTER_BROKEN
)) ||
2403 strchr(filter
, DIFF_STATUS_MODIFIED
)))) ||
2404 ((p
->status
!= DIFF_STATUS_MODIFIED
) &&
2405 strchr(filter
, p
->status
)))
2408 diff_free_filepair(p
);
2415 void diffcore_std(struct diff_options
*options
)
2417 if (options
->break_opt
!= -1)
2418 diffcore_break(options
->break_opt
);
2419 if (options
->detect_rename
)
2420 diffcore_rename(options
);
2421 if (options
->break_opt
!= -1)
2422 diffcore_merge_broken();
2423 if (options
->pickaxe
)
2424 diffcore_pickaxe(options
->pickaxe
, options
->pickaxe_opts
);
2425 if (options
->orderfile
)
2426 diffcore_order(options
->orderfile
);
2427 diff_resolve_rename_copy();
2428 diffcore_apply_filter(options
->filter
);
2432 void diffcore_std_no_resolve(struct diff_options
*options
)
2434 if (options
->pickaxe
)
2435 diffcore_pickaxe(options
->pickaxe
, options
->pickaxe_opts
);
2436 if (options
->orderfile
)
2437 diffcore_order(options
->orderfile
);
2438 diffcore_apply_filter(options
->filter
);
2441 void diff_addremove(struct diff_options
*options
,
2442 int addremove
, unsigned mode
,
2443 const unsigned char *sha1
,
2444 const char *base
, const char *path
)
2446 char concatpath
[PATH_MAX
];
2447 struct diff_filespec
*one
, *two
;
2449 /* This may look odd, but it is a preparation for
2450 * feeding "there are unchanged files which should
2451 * not produce diffs, but when you are doing copy
2452 * detection you would need them, so here they are"
2453 * entries to the diff-core. They will be prefixed
2454 * with something like '=' or '*' (I haven't decided
2455 * which but should not make any difference).
2456 * Feeding the same new and old to diff_change()
2457 * also has the same effect.
2458 * Before the final output happens, they are pruned after
2459 * merged into rename/copy pairs as appropriate.
2461 if (options
->reverse_diff
)
2462 addremove
= (addremove
== '+' ? '-' :
2463 addremove
== '-' ? '+' : addremove
);
2465 if (!path
) path
= "";
2466 sprintf(concatpath
, "%s%s", base
, path
);
2467 one
= alloc_filespec(concatpath
);
2468 two
= alloc_filespec(concatpath
);
2470 if (addremove
!= '+')
2471 fill_filespec(one
, sha1
, mode
);
2472 if (addremove
!= '-')
2473 fill_filespec(two
, sha1
, mode
);
2475 diff_queue(&diff_queued_diff
, one
, two
);
2478 void diff_change(struct diff_options
*options
,
2479 unsigned old_mode
, unsigned new_mode
,
2480 const unsigned char *old_sha1
,
2481 const unsigned char *new_sha1
,
2482 const char *base
, const char *path
)
2484 char concatpath
[PATH_MAX
];
2485 struct diff_filespec
*one
, *two
;
2487 if (options
->reverse_diff
) {
2489 const unsigned char *tmp_c
;
2490 tmp
= old_mode
; old_mode
= new_mode
; new_mode
= tmp
;
2491 tmp_c
= old_sha1
; old_sha1
= new_sha1
; new_sha1
= tmp_c
;
2493 if (!path
) path
= "";
2494 sprintf(concatpath
, "%s%s", base
, path
);
2495 one
= alloc_filespec(concatpath
);
2496 two
= alloc_filespec(concatpath
);
2497 fill_filespec(one
, old_sha1
, old_mode
);
2498 fill_filespec(two
, new_sha1
, new_mode
);
2500 diff_queue(&diff_queued_diff
, one
, two
);
2503 void diff_unmerge(struct diff_options
*options
,
2506 struct diff_filespec
*one
, *two
;
2507 one
= alloc_filespec(path
);
2508 two
= alloc_filespec(path
);
2509 diff_queue(&diff_queued_diff
, one
, two
);