2 * Copyright (C) 2005 Junio C Hamano
12 #include "xdiff-interface.h"
14 static int use_size_cache
;
16 static int diff_rename_limit_default
= -1;
17 static int diff_use_color_default
= 0;
28 #define COLOR_NORMAL ""
29 #define COLOR_BOLD "\033[1m"
30 #define COLOR_DIM "\033[2m"
31 #define COLOR_UL "\033[4m"
32 #define COLOR_BLINK "\033[5m"
33 #define COLOR_REVERSE "\033[7m"
34 #define COLOR_RESET "\033[m"
36 #define COLOR_BLACK "\033[30m"
37 #define COLOR_RED "\033[31m"
38 #define COLOR_GREEN "\033[32m"
39 #define COLOR_YELLOW "\033[33m"
40 #define COLOR_BLUE "\033[34m"
41 #define COLOR_MAGENTA "\033[35m"
42 #define COLOR_CYAN "\033[36m"
43 #define COLOR_WHITE "\033[37m"
45 static const char *diff_colors
[] = {
46 [DIFF_RESET
] = COLOR_RESET
,
47 [DIFF_PLAIN
] = COLOR_NORMAL
,
48 [DIFF_METAINFO
] = COLOR_BOLD
,
49 [DIFF_FRAGINFO
] = COLOR_CYAN
,
50 [DIFF_FILE_OLD
] = COLOR_RED
,
51 [DIFF_FILE_NEW
] = COLOR_GREEN
,
54 static int parse_diff_color_slot(const char *var
, int ofs
)
56 if (!strcasecmp(var
+ofs
, "plain"))
58 if (!strcasecmp(var
+ofs
, "meta"))
60 if (!strcasecmp(var
+ofs
, "frag"))
62 if (!strcasecmp(var
+ofs
, "old"))
64 if (!strcasecmp(var
+ofs
, "new"))
66 die("bad config variable '%s'", var
);
69 static const char *parse_diff_color_value(const char *value
, const char *var
)
71 if (!strcasecmp(value
, "normal"))
73 if (!strcasecmp(value
, "bold"))
75 if (!strcasecmp(value
, "dim"))
77 if (!strcasecmp(value
, "ul"))
79 if (!strcasecmp(value
, "blink"))
81 if (!strcasecmp(value
, "reverse"))
83 if (!strcasecmp(value
, "reset"))
85 if (!strcasecmp(value
, "black"))
87 if (!strcasecmp(value
, "red"))
89 if (!strcasecmp(value
, "green"))
91 if (!strcasecmp(value
, "yellow"))
93 if (!strcasecmp(value
, "blue"))
95 if (!strcasecmp(value
, "magenta"))
97 if (!strcasecmp(value
, "cyan"))
99 if (!strcasecmp(value
, "white"))
101 die("bad config value '%s' for variable '%s'", value
, var
);
104 int git_diff_config(const char *var
, const char *value
)
106 if (!strcmp(var
, "diff.renamelimit")) {
107 diff_rename_limit_default
= git_config_int(var
, value
);
110 if (!strcmp(var
, "diff.color")) {
112 diff_use_color_default
= 1; /* bool */
113 else if (!strcasecmp(value
, "auto")) {
114 diff_use_color_default
= 0;
116 char *term
= getenv("TERM");
117 if (term
&& strcmp(term
, "dumb"))
118 diff_use_color_default
= 1;
121 else if (!strcasecmp(value
, "never"))
122 diff_use_color_default
= 0;
123 else if (!strcasecmp(value
, "always"))
124 diff_use_color_default
= 1;
126 diff_use_color_default
= git_config_bool(var
, value
);
129 if (!strncmp(var
, "diff.color.", 11)) {
130 int slot
= parse_diff_color_slot(var
, 11);
131 diff_colors
[slot
] = parse_diff_color_value(value
, var
);
134 return git_default_config(var
, value
);
137 static char *quote_one(const char *str
)
144 needlen
= quote_c_style(str
, NULL
, NULL
, 0);
147 xp
= xmalloc(needlen
+ 1);
148 quote_c_style(str
, xp
, NULL
, 0);
152 static char *quote_two(const char *one
, const char *two
)
154 int need_one
= quote_c_style(one
, NULL
, NULL
, 1);
155 int need_two
= quote_c_style(two
, NULL
, NULL
, 1);
158 if (need_one
+ need_two
) {
159 if (!need_one
) need_one
= strlen(one
);
160 if (!need_two
) need_one
= strlen(two
);
162 xp
= xmalloc(need_one
+ need_two
+ 3);
164 quote_c_style(one
, xp
+ 1, NULL
, 1);
165 quote_c_style(two
, xp
+ need_one
+ 1, NULL
, 1);
166 strcpy(xp
+ need_one
+ need_two
+ 1, "\"");
169 need_one
= strlen(one
);
170 need_two
= strlen(two
);
171 xp
= xmalloc(need_one
+ need_two
+ 1);
173 strcpy(xp
+ need_one
, two
);
177 static const char *external_diff(void)
179 static const char *external_diff_cmd
= NULL
;
180 static int done_preparing
= 0;
183 return external_diff_cmd
;
184 external_diff_cmd
= getenv("GIT_EXTERNAL_DIFF");
186 return external_diff_cmd
;
189 #define TEMPFILE_PATH_LEN 50
191 static struct diff_tempfile
{
192 const char *name
; /* filename external diff should read from */
195 char tmp_path
[TEMPFILE_PATH_LEN
];
198 static int count_lines(const char *data
, int size
)
200 int count
, ch
, completely_empty
= 1, nl_just_seen
= 0;
207 completely_empty
= 0;
211 completely_empty
= 0;
214 if (completely_empty
)
217 count
++; /* no trailing newline */
221 static void print_line_count(int count
)
231 printf("1,%d", count
);
236 static void copy_file(int prefix
, const char *data
, int size
)
238 int ch
, nl_just_seen
= 1;
250 printf("\n\\ No newline at end of file\n");
253 static void emit_rewrite_diff(const char *name_a
,
255 struct diff_filespec
*one
,
256 struct diff_filespec
*two
)
259 diff_populate_filespec(one
, 0);
260 diff_populate_filespec(two
, 0);
261 lc_a
= count_lines(one
->data
, one
->size
);
262 lc_b
= count_lines(two
->data
, two
->size
);
263 printf("--- %s\n+++ %s\n@@ -", name_a
, name_b
);
264 print_line_count(lc_a
);
266 print_line_count(lc_b
);
269 copy_file('-', one
->data
, one
->size
);
271 copy_file('+', two
->data
, two
->size
);
274 static int fill_mmfile(mmfile_t
*mf
, struct diff_filespec
*one
)
276 if (!DIFF_FILE_VALID(one
)) {
277 mf
->ptr
= (char *)""; /* does not matter */
281 else if (diff_populate_filespec(one
, 0))
284 mf
->size
= one
->size
;
288 struct emit_callback
{
289 struct xdiff_emit_state xm
;
290 int nparents
, color_diff
;
291 const char **label_path
;
294 static inline const char *get_color(int diff_use_color
, enum color_diff ix
)
297 return diff_colors
[ix
];
301 static void fn_out_consume(void *priv
, char *line
, unsigned long len
)
304 struct emit_callback
*ecbdata
= priv
;
305 const char *set
= get_color(ecbdata
->color_diff
, DIFF_METAINFO
);
306 const char *reset
= get_color(ecbdata
->color_diff
, DIFF_RESET
);
308 if (ecbdata
->label_path
[0]) {
309 printf("%s--- %s%s\n", set
, ecbdata
->label_path
[0], reset
);
310 printf("%s+++ %s%s\n", set
, ecbdata
->label_path
[1], reset
);
311 ecbdata
->label_path
[0] = ecbdata
->label_path
[1] = NULL
;
314 /* This is not really necessary for now because
315 * this codepath only deals with two-way diffs.
317 for (i
= 0; i
< len
&& line
[i
] == '@'; i
++)
319 if (2 <= i
&& i
< len
&& line
[i
] == ' ') {
320 ecbdata
->nparents
= i
- 1;
321 set
= get_color(ecbdata
->color_diff
, DIFF_FRAGINFO
);
323 else if (len
< ecbdata
->nparents
)
326 int nparents
= ecbdata
->nparents
;
327 int color
= DIFF_PLAIN
;
328 for (i
= 0; i
< nparents
&& len
; i
++) {
330 color
= DIFF_FILE_OLD
;
331 else if (line
[i
] == '+')
332 color
= DIFF_FILE_NEW
;
334 set
= get_color(ecbdata
->color_diff
, color
);
336 if (len
> 0 && line
[len
-1] == '\n')
339 fwrite (line
, len
, 1, stdout
);
343 static char *pprint_rename(const char *a
, const char *b
)
348 int pfx_length
, sfx_length
;
349 int len_a
= strlen(a
);
350 int len_b
= strlen(b
);
352 /* Find common prefix */
354 while (*old
&& *new && *old
== *new) {
356 pfx_length
= old
- a
+ 1;
361 /* Find common suffix */
365 while (a
<= old
&& b
<= new && *old
== *new) {
367 sfx_length
= len_a
- (old
- a
);
373 * pfx{mid-a => mid-b}sfx
374 * {pfx-a => pfx-b}sfx
375 * pfx{sfx-a => sfx-b}
378 if (pfx_length
+ sfx_length
) {
379 int a_midlen
= len_a
- pfx_length
- sfx_length
;
380 int b_midlen
= len_b
- pfx_length
- sfx_length
;
381 if (a_midlen
< 0) a_midlen
= 0;
382 if (b_midlen
< 0) b_midlen
= 0;
384 name
= xmalloc(pfx_length
+ a_midlen
+ b_midlen
+ sfx_length
+ 7);
385 sprintf(name
, "%.*s{%.*s => %.*s}%s",
387 a_midlen
, a
+ pfx_length
,
388 b_midlen
, b
+ pfx_length
,
389 a
+ len_a
- sfx_length
);
392 name
= xmalloc(len_a
+ len_b
+ 5);
393 sprintf(name
, "%s => %s", a
, b
);
399 struct xdiff_emit_state xm
;
403 struct diffstat_file
{
405 unsigned is_unmerged
:1;
406 unsigned is_binary
:1;
407 unsigned is_renamed
:1;
408 unsigned int added
, deleted
;
412 static struct diffstat_file
*diffstat_add(struct diffstat_t
*diffstat
,
416 struct diffstat_file
*x
;
417 x
= xcalloc(sizeof (*x
), 1);
418 if (diffstat
->nr
== diffstat
->alloc
) {
419 diffstat
->alloc
= alloc_nr(diffstat
->alloc
);
420 diffstat
->files
= xrealloc(diffstat
->files
,
421 diffstat
->alloc
* sizeof(x
));
423 diffstat
->files
[diffstat
->nr
++] = x
;
425 x
->name
= pprint_rename(name_a
, name_b
);
429 x
->name
= strdup(name_a
);
433 static void diffstat_consume(void *priv
, char *line
, unsigned long len
)
435 struct diffstat_t
*diffstat
= priv
;
436 struct diffstat_file
*x
= diffstat
->files
[diffstat
->nr
- 1];
440 else if (line
[0] == '-')
444 static const char pluses
[] = "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++";
445 static const char minuses
[]= "----------------------------------------------------------------------";
446 const char mime_boundary_leader
[] = "------------";
448 static void show_stats(struct diffstat_t
* data
)
450 int i
, len
, add
, del
, total
, adds
= 0, dels
= 0;
451 int max
, max_change
= 0, max_len
= 0;
452 int total_files
= data
->nr
;
457 for (i
= 0; i
< data
->nr
; i
++) {
458 struct diffstat_file
*file
= data
->files
[i
];
460 len
= strlen(file
->name
);
464 if (file
->is_binary
|| file
->is_unmerged
)
466 if (max_change
< file
->added
+ file
->deleted
)
467 max_change
= file
->added
+ file
->deleted
;
470 for (i
= 0; i
< data
->nr
; i
++) {
471 const char *prefix
= "";
472 char *name
= data
->files
[i
]->name
;
473 int added
= data
->files
[i
]->added
;
474 int deleted
= data
->files
[i
]->deleted
;
476 if (0 < (len
= quote_c_style(name
, NULL
, NULL
, 0))) {
477 char *qname
= xmalloc(len
+ 1);
478 quote_c_style(name
, qname
, NULL
, 0);
480 data
->files
[i
]->name
= name
= qname
;
484 * "scale" the filename
495 slash
= strchr(name
, '/');
502 * scale the add/delete
508 if (data
->files
[i
]->is_binary
) {
509 printf(" %s%-*s | Bin\n", prefix
, len
, name
);
510 goto free_diffstat_file
;
512 else if (data
->files
[i
]->is_unmerged
) {
513 printf(" %s%-*s | Unmerged\n", prefix
, len
, name
);
514 goto free_diffstat_file
;
516 else if (!data
->files
[i
]->is_renamed
&&
517 (added
+ deleted
== 0)) {
519 goto free_diffstat_file
;
528 if (max_change
> 0) {
529 total
= (total
* max
+ max_change
/ 2) / max_change
;
530 add
= (add
* max
+ max_change
/ 2) / max_change
;
533 printf(" %s%-*s |%5d %.*s%.*s\n", prefix
,
534 len
, name
, added
+ deleted
,
535 add
, pluses
, del
, minuses
);
537 free(data
->files
[i
]->name
);
538 free(data
->files
[i
]);
541 printf(" %d files changed, %d insertions(+), %d deletions(-)\n",
542 total_files
, adds
, dels
);
546 struct xdiff_emit_state xm
;
547 const char *filename
;
551 static void checkdiff_consume(void *priv
, char *line
, unsigned long len
)
553 struct checkdiff_t
*data
= priv
;
555 if (line
[0] == '+') {
560 /* check space before tab */
561 for (i
= 1; i
< len
&& (line
[i
] == ' ' || line
[i
] == '\t'); i
++)
564 if (line
[i
- 1] == '\t' && spaces
)
565 printf("%s:%d: space before tab:%.*s\n",
566 data
->filename
, data
->lineno
, (int)len
, line
);
568 /* check white space at line end */
569 if (line
[len
- 1] == '\n')
571 if (isspace(line
[len
- 1]))
572 printf("%s:%d: white space at end: %.*s\n",
573 data
->filename
, data
->lineno
, (int)len
, line
);
574 } else if (line
[0] == ' ')
576 else if (line
[0] == '@') {
577 char *plus
= strchr(line
, '+');
579 data
->lineno
= strtol(plus
, NULL
, 10);
585 static unsigned char *deflate_it(char *data
,
587 unsigned long *result_size
)
590 unsigned char *deflated
;
593 memset(&stream
, 0, sizeof(stream
));
594 deflateInit(&stream
, zlib_compression_level
);
595 bound
= deflateBound(&stream
, size
);
596 deflated
= xmalloc(bound
);
597 stream
.next_out
= deflated
;
598 stream
.avail_out
= bound
;
600 stream
.next_in
= (unsigned char *)data
;
601 stream
.avail_in
= size
;
602 while (deflate(&stream
, Z_FINISH
) == Z_OK
)
605 *result_size
= stream
.total_out
;
609 static void emit_binary_diff(mmfile_t
*one
, mmfile_t
*two
)
615 unsigned long orig_size
;
616 unsigned long delta_size
;
617 unsigned long deflate_size
;
618 unsigned long data_size
;
620 printf("GIT binary patch\n");
621 /* We could do deflated delta, or we could do just deflated two,
622 * whichever is smaller.
625 deflated
= deflate_it(two
->ptr
, two
->size
, &deflate_size
);
626 if (one
->size
&& two
->size
) {
627 delta
= diff_delta(one
->ptr
, one
->size
,
629 &delta_size
, deflate_size
);
631 void *to_free
= delta
;
632 orig_size
= delta_size
;
633 delta
= deflate_it(delta
, delta_size
, &delta_size
);
638 if (delta
&& delta_size
< deflate_size
) {
639 printf("delta %lu\n", orig_size
);
642 data_size
= delta_size
;
645 printf("literal %lu\n", two
->size
);
648 data_size
= deflate_size
;
651 /* emit data encoded in base85 */
654 int bytes
= (52 < data_size
) ? 52 : data_size
;
658 line
[0] = bytes
+ 'A' - 1;
660 line
[0] = bytes
- 26 + 'a' - 1;
661 encode_85(line
+ 1, cp
, bytes
);
662 cp
= (char *) cp
+ bytes
;
669 #define FIRST_FEW_BYTES 8000
670 static int mmfile_is_binary(mmfile_t
*mf
)
673 if (FIRST_FEW_BYTES
< sz
)
674 sz
= FIRST_FEW_BYTES
;
675 if (memchr(mf
->ptr
, 0, sz
))
680 static void builtin_diff(const char *name_a
,
682 struct diff_filespec
*one
,
683 struct diff_filespec
*two
,
684 const char *xfrm_msg
,
685 struct diff_options
*o
,
686 int complete_rewrite
)
691 const char *set
= get_color(o
->color_diff
, DIFF_METAINFO
);
692 const char *reset
= get_color(o
->color_diff
, DIFF_RESET
);
694 a_one
= quote_two("a/", name_a
);
695 b_two
= quote_two("b/", name_b
);
696 lbl
[0] = DIFF_FILE_VALID(one
) ? a_one
: "/dev/null";
697 lbl
[1] = DIFF_FILE_VALID(two
) ? b_two
: "/dev/null";
698 printf("%sdiff --git %s %s%s\n", set
, a_one
, b_two
, reset
);
699 if (lbl
[0][0] == '/') {
701 printf("%snew file mode %06o%s\n", set
, two
->mode
, reset
);
702 if (xfrm_msg
&& xfrm_msg
[0])
703 printf("%s%s%s\n", set
, xfrm_msg
, reset
);
705 else if (lbl
[1][0] == '/') {
706 printf("%sdeleted file mode %06o%s\n", set
, one
->mode
, reset
);
707 if (xfrm_msg
&& xfrm_msg
[0])
708 printf("%s%s%s\n", set
, xfrm_msg
, reset
);
711 if (one
->mode
!= two
->mode
) {
712 printf("%sold mode %06o%s\n", set
, one
->mode
, reset
);
713 printf("%snew mode %06o%s\n", set
, two
->mode
, reset
);
715 if (xfrm_msg
&& xfrm_msg
[0])
716 printf("%s%s%s\n", set
, xfrm_msg
, reset
);
718 * we do not run diff between different kind
721 if ((one
->mode
^ two
->mode
) & S_IFMT
)
722 goto free_ab_and_return
;
723 if (complete_rewrite
) {
724 emit_rewrite_diff(name_a
, name_b
, one
, two
);
725 goto free_ab_and_return
;
729 if (fill_mmfile(&mf1
, one
) < 0 || fill_mmfile(&mf2
, two
) < 0)
730 die("unable to read files to diff");
732 if (mmfile_is_binary(&mf1
) || mmfile_is_binary(&mf2
)) {
733 /* Quite common confusing case */
734 if (mf1
.size
== mf2
.size
&&
735 !memcmp(mf1
.ptr
, mf2
.ptr
, mf1
.size
))
736 goto free_ab_and_return
;
738 emit_binary_diff(&mf1
, &mf2
);
740 printf("Binary files %s and %s differ\n",
744 /* Crazy xdl interfaces.. */
745 const char *diffopts
= getenv("GIT_DIFF_OPTS");
749 struct emit_callback ecbdata
;
751 memset(&ecbdata
, 0, sizeof(ecbdata
));
752 ecbdata
.label_path
= lbl
;
753 ecbdata
.color_diff
= o
->color_diff
;
754 xpp
.flags
= XDF_NEED_MINIMAL
| o
->xdl_opts
;
755 xecfg
.ctxlen
= o
->context
;
756 xecfg
.flags
= XDL_EMIT_FUNCNAMES
;
759 else if (!strncmp(diffopts
, "--unified=", 10))
760 xecfg
.ctxlen
= strtoul(diffopts
+ 10, NULL
, 10);
761 else if (!strncmp(diffopts
, "-u", 2))
762 xecfg
.ctxlen
= strtoul(diffopts
+ 2, NULL
, 10);
763 ecb
.outf
= xdiff_outf
;
765 ecbdata
.xm
.consume
= fn_out_consume
;
766 xdl_diff(&mf1
, &mf2
, &xpp
, &xecfg
, &ecb
);
775 static void builtin_diffstat(const char *name_a
, const char *name_b
,
776 struct diff_filespec
*one
,
777 struct diff_filespec
*two
,
778 struct diffstat_t
*diffstat
,
779 struct diff_options
*o
,
780 int complete_rewrite
)
783 struct diffstat_file
*data
;
785 data
= diffstat_add(diffstat
, name_a
, name_b
);
788 data
->is_unmerged
= 1;
791 if (complete_rewrite
) {
792 diff_populate_filespec(one
, 0);
793 diff_populate_filespec(two
, 0);
794 data
->deleted
= count_lines(one
->data
, one
->size
);
795 data
->added
= count_lines(two
->data
, two
->size
);
798 if (fill_mmfile(&mf1
, one
) < 0 || fill_mmfile(&mf2
, two
) < 0)
799 die("unable to read files to diff");
801 if (mmfile_is_binary(&mf1
) || mmfile_is_binary(&mf2
))
804 /* Crazy xdl interfaces.. */
809 xpp
.flags
= XDF_NEED_MINIMAL
| o
->xdl_opts
;
812 ecb
.outf
= xdiff_outf
;
814 xdl_diff(&mf1
, &mf2
, &xpp
, &xecfg
, &ecb
);
818 static void builtin_checkdiff(const char *name_a
, const char *name_b
,
819 struct diff_filespec
*one
,
820 struct diff_filespec
*two
)
823 struct checkdiff_t data
;
828 memset(&data
, 0, sizeof(data
));
829 data
.xm
.consume
= checkdiff_consume
;
830 data
.filename
= name_b
? name_b
: name_a
;
833 if (fill_mmfile(&mf1
, one
) < 0 || fill_mmfile(&mf2
, two
) < 0)
834 die("unable to read files to diff");
836 if (mmfile_is_binary(&mf2
))
839 /* Crazy xdl interfaces.. */
844 xpp
.flags
= XDF_NEED_MINIMAL
;
847 ecb
.outf
= xdiff_outf
;
849 xdl_diff(&mf1
, &mf2
, &xpp
, &xecfg
, &ecb
);
853 struct diff_filespec
*alloc_filespec(const char *path
)
855 int namelen
= strlen(path
);
856 struct diff_filespec
*spec
= xmalloc(sizeof(*spec
) + namelen
+ 1);
858 memset(spec
, 0, sizeof(*spec
));
859 spec
->path
= (char *)(spec
+ 1);
860 memcpy(spec
->path
, path
, namelen
+1);
864 void fill_filespec(struct diff_filespec
*spec
, const unsigned char *sha1
,
868 spec
->mode
= canon_mode(mode
);
869 memcpy(spec
->sha1
, sha1
, 20);
870 spec
->sha1_valid
= !!memcmp(sha1
, null_sha1
, 20);
875 * Given a name and sha1 pair, if the dircache tells us the file in
876 * the work tree has that object contents, return true, so that
877 * prepare_temp_file() does not have to inflate and extract.
879 static int work_tree_matches(const char *name
, const unsigned char *sha1
)
881 struct cache_entry
*ce
;
885 /* We do not read the cache ourselves here, because the
886 * benchmark with my previous version that always reads cache
887 * shows that it makes things worse for diff-tree comparing
888 * two linux-2.6 kernel trees in an already checked out work
889 * tree. This is because most diff-tree comparisons deal with
890 * only a small number of files, while reading the cache is
891 * expensive for a large project, and its cost outweighs the
892 * savings we get by not inflating the object to a temporary
893 * file. Practically, this code only helps when we are used
894 * by diff-cache --cached, which does read the cache before
901 pos
= cache_name_pos(name
, len
);
904 ce
= active_cache
[pos
];
905 if ((lstat(name
, &st
) < 0) ||
906 !S_ISREG(st
.st_mode
) || /* careful! */
907 ce_match_stat(ce
, &st
, 0) ||
908 memcmp(sha1
, ce
->sha1
, 20))
910 /* we return 1 only when we can stat, it is a regular file,
911 * stat information matches, and sha1 recorded in the cache
912 * matches. I.e. we know the file in the work tree really is
913 * the same as the <name, sha1> pair.
918 static struct sha1_size_cache
{
919 unsigned char sha1
[20];
922 static int sha1_size_cache_nr
, sha1_size_cache_alloc
;
924 static struct sha1_size_cache
*locate_size_cache(unsigned char *sha1
,
929 struct sha1_size_cache
*e
;
932 last
= sha1_size_cache_nr
;
933 while (last
> first
) {
934 int cmp
, next
= (last
+ first
) >> 1;
935 e
= sha1_size_cache
[next
];
936 cmp
= memcmp(e
->sha1
, sha1
, 20);
948 /* insert to make it at "first" */
949 if (sha1_size_cache_alloc
<= sha1_size_cache_nr
) {
950 sha1_size_cache_alloc
= alloc_nr(sha1_size_cache_alloc
);
951 sha1_size_cache
= xrealloc(sha1_size_cache
,
952 sha1_size_cache_alloc
*
953 sizeof(*sha1_size_cache
));
955 sha1_size_cache_nr
++;
956 if (first
< sha1_size_cache_nr
)
957 memmove(sha1_size_cache
+ first
+ 1, sha1_size_cache
+ first
,
958 (sha1_size_cache_nr
- first
- 1) *
959 sizeof(*sha1_size_cache
));
960 e
= xmalloc(sizeof(struct sha1_size_cache
));
961 sha1_size_cache
[first
] = e
;
962 memcpy(e
->sha1
, sha1
, 20);
968 * While doing rename detection and pickaxe operation, we may need to
969 * grab the data for the blob (or file) for our own in-core comparison.
970 * diff_filespec has data and size fields for this purpose.
972 int diff_populate_filespec(struct diff_filespec
*s
, int size_only
)
975 if (!DIFF_FILE_VALID(s
))
976 die("internal error: asking to populate invalid file.");
977 if (S_ISDIR(s
->mode
))
985 if (!s
->sha1_valid
||
986 work_tree_matches(s
->path
, s
->sha1
)) {
989 if (lstat(s
->path
, &st
) < 0) {
990 if (errno
== ENOENT
) {
994 s
->data
= (char *)"";
999 s
->size
= st
.st_size
;
1004 if (S_ISLNK(st
.st_mode
)) {
1006 s
->data
= xmalloc(s
->size
);
1008 ret
= readlink(s
->path
, s
->data
, s
->size
);
1015 fd
= open(s
->path
, O_RDONLY
);
1018 s
->data
= mmap(NULL
, s
->size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
1020 if (s
->data
== MAP_FAILED
)
1022 s
->should_munmap
= 1;
1026 struct sha1_size_cache
*e
;
1029 e
= locate_size_cache(s
->sha1
, 1, 0);
1034 if (!sha1_object_info(s
->sha1
, type
, &s
->size
))
1035 locate_size_cache(s
->sha1
, 0, s
->size
);
1038 s
->data
= read_sha1_file(s
->sha1
, type
, &s
->size
);
1045 void diff_free_filespec_data(struct diff_filespec
*s
)
1049 else if (s
->should_munmap
)
1050 munmap(s
->data
, s
->size
);
1051 s
->should_free
= s
->should_munmap
= 0;
1057 static void prep_temp_blob(struct diff_tempfile
*temp
,
1060 const unsigned char *sha1
,
1065 fd
= git_mkstemp(temp
->tmp_path
, TEMPFILE_PATH_LEN
, ".diff_XXXXXX");
1067 die("unable to create temp-file");
1068 if (write(fd
, blob
, size
) != size
)
1069 die("unable to write temp-file");
1071 temp
->name
= temp
->tmp_path
;
1072 strcpy(temp
->hex
, sha1_to_hex(sha1
));
1074 sprintf(temp
->mode
, "%06o", mode
);
1077 static void prepare_temp_file(const char *name
,
1078 struct diff_tempfile
*temp
,
1079 struct diff_filespec
*one
)
1081 if (!DIFF_FILE_VALID(one
)) {
1083 /* A '-' entry produces this for file-2, and
1084 * a '+' entry produces this for file-1.
1086 temp
->name
= "/dev/null";
1087 strcpy(temp
->hex
, ".");
1088 strcpy(temp
->mode
, ".");
1092 if (!one
->sha1_valid
||
1093 work_tree_matches(name
, one
->sha1
)) {
1095 if (lstat(name
, &st
) < 0) {
1096 if (errno
== ENOENT
)
1097 goto not_a_valid_file
;
1098 die("stat(%s): %s", name
, strerror(errno
));
1100 if (S_ISLNK(st
.st_mode
)) {
1102 char buf
[PATH_MAX
+ 1]; /* ought to be SYMLINK_MAX */
1103 if (sizeof(buf
) <= st
.st_size
)
1104 die("symlink too long: %s", name
);
1105 ret
= readlink(name
, buf
, st
.st_size
);
1107 die("readlink(%s)", name
);
1108 prep_temp_blob(temp
, buf
, st
.st_size
,
1110 one
->sha1
: null_sha1
),
1112 one
->mode
: S_IFLNK
));
1115 /* we can borrow from the file in the work tree */
1117 if (!one
->sha1_valid
)
1118 strcpy(temp
->hex
, sha1_to_hex(null_sha1
));
1120 strcpy(temp
->hex
, sha1_to_hex(one
->sha1
));
1121 /* Even though we may sometimes borrow the
1122 * contents from the work tree, we always want
1123 * one->mode. mode is trustworthy even when
1124 * !(one->sha1_valid), as long as
1125 * DIFF_FILE_VALID(one).
1127 sprintf(temp
->mode
, "%06o", one
->mode
);
1132 if (diff_populate_filespec(one
, 0))
1133 die("cannot read data blob for %s", one
->path
);
1134 prep_temp_blob(temp
, one
->data
, one
->size
,
1135 one
->sha1
, one
->mode
);
1139 static void remove_tempfile(void)
1143 for (i
= 0; i
< 2; i
++)
1144 if (diff_temp
[i
].name
== diff_temp
[i
].tmp_path
) {
1145 unlink(diff_temp
[i
].name
);
1146 diff_temp
[i
].name
= NULL
;
1150 static void remove_tempfile_on_signal(int signo
)
1153 signal(SIGINT
, SIG_DFL
);
1157 static int spawn_prog(const char *pgm
, const char **arg
)
1165 die("unable to fork");
1167 execvp(pgm
, (char *const*) arg
);
1171 while (waitpid(pid
, &status
, 0) < 0) {
1177 /* Earlier we did not check the exit status because
1178 * diff exits non-zero if files are different, and
1179 * we are not interested in knowing that. It was a
1180 * mistake which made it harder to quit a diff-*
1181 * session that uses the git-apply-patch-script as
1182 * the GIT_EXTERNAL_DIFF. A custom GIT_EXTERNAL_DIFF
1183 * should also exit non-zero only when it wants to
1184 * abort the entire diff-* session.
1186 if (WIFEXITED(status
) && !WEXITSTATUS(status
))
1191 /* An external diff command takes:
1193 * diff-cmd name infile1 infile1-sha1 infile1-mode \
1194 * infile2 infile2-sha1 infile2-mode [ rename-to ]
1197 static void run_external_diff(const char *pgm
,
1200 struct diff_filespec
*one
,
1201 struct diff_filespec
*two
,
1202 const char *xfrm_msg
,
1203 int complete_rewrite
)
1205 const char *spawn_arg
[10];
1206 struct diff_tempfile
*temp
= diff_temp
;
1208 static int atexit_asked
= 0;
1209 const char *othername
;
1210 const char **arg
= &spawn_arg
[0];
1212 othername
= (other
? other
: name
);
1214 prepare_temp_file(name
, &temp
[0], one
);
1215 prepare_temp_file(othername
, &temp
[1], two
);
1216 if (! atexit_asked
&&
1217 (temp
[0].name
== temp
[0].tmp_path
||
1218 temp
[1].name
== temp
[1].tmp_path
)) {
1220 atexit(remove_tempfile
);
1222 signal(SIGINT
, remove_tempfile_on_signal
);
1228 *arg
++ = temp
[0].name
;
1229 *arg
++ = temp
[0].hex
;
1230 *arg
++ = temp
[0].mode
;
1231 *arg
++ = temp
[1].name
;
1232 *arg
++ = temp
[1].hex
;
1233 *arg
++ = temp
[1].mode
;
1243 retval
= spawn_prog(pgm
, spawn_arg
);
1246 fprintf(stderr
, "external diff died, stopping at %s.\n", name
);
1251 static void run_diff_cmd(const char *pgm
,
1254 struct diff_filespec
*one
,
1255 struct diff_filespec
*two
,
1256 const char *xfrm_msg
,
1257 struct diff_options
*o
,
1258 int complete_rewrite
)
1261 run_external_diff(pgm
, name
, other
, one
, two
, xfrm_msg
,
1266 builtin_diff(name
, other
? other
: name
,
1267 one
, two
, xfrm_msg
, o
, complete_rewrite
);
1269 printf("* Unmerged path %s\n", name
);
1272 static void diff_fill_sha1_info(struct diff_filespec
*one
)
1274 if (DIFF_FILE_VALID(one
)) {
1275 if (!one
->sha1_valid
) {
1277 if (lstat(one
->path
, &st
) < 0)
1278 die("stat %s", one
->path
);
1279 if (index_path(one
->sha1
, one
->path
, &st
, 0))
1280 die("cannot hash %s\n", one
->path
);
1284 memset(one
->sha1
, 0, 20);
1287 static void run_diff(struct diff_filepair
*p
, struct diff_options
*o
)
1289 const char *pgm
= external_diff();
1290 char msg
[PATH_MAX
*2+300], *xfrm_msg
;
1291 struct diff_filespec
*one
;
1292 struct diff_filespec
*two
;
1295 char *name_munged
, *other_munged
;
1296 int complete_rewrite
= 0;
1299 if (DIFF_PAIR_UNMERGED(p
)) {
1301 run_diff_cmd(pgm
, p
->one
->path
, NULL
, NULL
, NULL
, NULL
, o
, 0);
1305 name
= p
->one
->path
;
1306 other
= (strcmp(name
, p
->two
->path
) ? p
->two
->path
: NULL
);
1307 name_munged
= quote_one(name
);
1308 other_munged
= quote_one(other
);
1309 one
= p
->one
; two
= p
->two
;
1311 diff_fill_sha1_info(one
);
1312 diff_fill_sha1_info(two
);
1315 switch (p
->status
) {
1316 case DIFF_STATUS_COPIED
:
1317 len
+= snprintf(msg
+ len
, sizeof(msg
) - len
,
1318 "similarity index %d%%\n"
1321 (int)(0.5 + p
->score
* 100.0/MAX_SCORE
),
1322 name_munged
, other_munged
);
1324 case DIFF_STATUS_RENAMED
:
1325 len
+= snprintf(msg
+ len
, sizeof(msg
) - len
,
1326 "similarity index %d%%\n"
1329 (int)(0.5 + p
->score
* 100.0/MAX_SCORE
),
1330 name_munged
, other_munged
);
1332 case DIFF_STATUS_MODIFIED
:
1334 len
+= snprintf(msg
+ len
, sizeof(msg
) - len
,
1335 "dissimilarity index %d%%\n",
1336 (int)(0.5 + p
->score
*
1338 complete_rewrite
= 1;
1347 if (memcmp(one
->sha1
, two
->sha1
, 20)) {
1348 int abbrev
= o
->full_index
? 40 : DEFAULT_ABBREV
;
1350 len
+= snprintf(msg
+ len
, sizeof(msg
) - len
,
1352 abbrev
, sha1_to_hex(one
->sha1
),
1353 abbrev
, sha1_to_hex(two
->sha1
));
1354 if (one
->mode
== two
->mode
)
1355 len
+= snprintf(msg
+ len
, sizeof(msg
) - len
,
1356 " %06o", one
->mode
);
1357 len
+= snprintf(msg
+ len
, sizeof(msg
) - len
, "\n");
1362 xfrm_msg
= len
? msg
: NULL
;
1365 DIFF_FILE_VALID(one
) && DIFF_FILE_VALID(two
) &&
1366 (S_IFMT
& one
->mode
) != (S_IFMT
& two
->mode
)) {
1367 /* a filepair that changes between file and symlink
1368 * needs to be split into deletion and creation.
1370 struct diff_filespec
*null
= alloc_filespec(two
->path
);
1371 run_diff_cmd(NULL
, name
, other
, one
, null
, xfrm_msg
, o
, 0);
1373 null
= alloc_filespec(one
->path
);
1374 run_diff_cmd(NULL
, name
, other
, null
, two
, xfrm_msg
, o
, 0);
1378 run_diff_cmd(pgm
, name
, other
, one
, two
, xfrm_msg
, o
,
1385 static void run_diffstat(struct diff_filepair
*p
, struct diff_options
*o
,
1386 struct diffstat_t
*diffstat
)
1390 int complete_rewrite
= 0;
1392 if (DIFF_PAIR_UNMERGED(p
)) {
1394 builtin_diffstat(p
->one
->path
, NULL
, NULL
, NULL
, diffstat
, o
, 0);
1398 name
= p
->one
->path
;
1399 other
= (strcmp(name
, p
->two
->path
) ? p
->two
->path
: NULL
);
1401 diff_fill_sha1_info(p
->one
);
1402 diff_fill_sha1_info(p
->two
);
1404 if (p
->status
== DIFF_STATUS_MODIFIED
&& p
->score
)
1405 complete_rewrite
= 1;
1406 builtin_diffstat(name
, other
, p
->one
, p
->two
, diffstat
, o
, complete_rewrite
);
1409 static void run_checkdiff(struct diff_filepair
*p
, struct diff_options
*o
)
1414 if (DIFF_PAIR_UNMERGED(p
)) {
1419 name
= p
->one
->path
;
1420 other
= (strcmp(name
, p
->two
->path
) ? p
->two
->path
: NULL
);
1422 diff_fill_sha1_info(p
->one
);
1423 diff_fill_sha1_info(p
->two
);
1425 builtin_checkdiff(name
, other
, p
->one
, p
->two
);
1428 void diff_setup(struct diff_options
*options
)
1430 memset(options
, 0, sizeof(*options
));
1431 options
->line_termination
= '\n';
1432 options
->break_opt
= -1;
1433 options
->rename_limit
= -1;
1434 options
->context
= 3;
1435 options
->msg_sep
= "";
1437 options
->change
= diff_change
;
1438 options
->add_remove
= diff_addremove
;
1439 options
->color_diff
= diff_use_color_default
;
1442 int diff_setup_done(struct diff_options
*options
)
1444 if ((options
->find_copies_harder
&&
1445 options
->detect_rename
!= DIFF_DETECT_COPY
) ||
1446 (0 <= options
->rename_limit
&& !options
->detect_rename
))
1449 if (options
->output_format
& (DIFF_FORMAT_NAME
|
1450 DIFF_FORMAT_NAME_STATUS
|
1451 DIFF_FORMAT_CHECKDIFF
|
1452 DIFF_FORMAT_NO_OUTPUT
))
1453 options
->output_format
&= ~(DIFF_FORMAT_RAW
|
1454 DIFF_FORMAT_DIFFSTAT
|
1455 DIFF_FORMAT_SUMMARY
|
1459 * These cases always need recursive; we do not drop caller-supplied
1460 * recursive bits for other formats here.
1462 if (options
->output_format
& (DIFF_FORMAT_PATCH
|
1463 DIFF_FORMAT_DIFFSTAT
|
1464 DIFF_FORMAT_CHECKDIFF
))
1465 options
->recursive
= 1;
1467 * Also pickaxe would not work very well if you do not say recursive
1469 if (options
->pickaxe
)
1470 options
->recursive
= 1;
1472 if (options
->detect_rename
&& options
->rename_limit
< 0)
1473 options
->rename_limit
= diff_rename_limit_default
;
1474 if (options
->setup
& DIFF_SETUP_USE_CACHE
) {
1476 /* read-cache does not die even when it fails
1477 * so it is safe for us to do this here. Also
1478 * it does not smudge active_cache or active_nr
1479 * when it fails, so we do not have to worry about
1480 * cleaning it up ourselves either.
1484 if (options
->setup
& DIFF_SETUP_USE_SIZE_CACHE
)
1486 if (options
->abbrev
<= 0 || 40 < options
->abbrev
)
1487 options
->abbrev
= 40; /* full */
1492 static int opt_arg(const char *arg
, int arg_short
, const char *arg_long
, int *val
)
1502 if (c
== arg_short
) {
1506 if (val
&& isdigit(c
)) {
1508 int n
= strtoul(arg
, &end
, 10);
1519 eq
= strchr(arg
, '=');
1524 if (!len
|| strncmp(arg
, arg_long
, len
))
1529 if (!isdigit(*++eq
))
1531 n
= strtoul(eq
, &end
, 10);
1539 int diff_opt_parse(struct diff_options
*options
, const char **av
, int ac
)
1541 const char *arg
= av
[0];
1542 if (!strcmp(arg
, "-p") || !strcmp(arg
, "-u"))
1543 options
->output_format
|= DIFF_FORMAT_PATCH
;
1544 else if (opt_arg(arg
, 'U', "unified", &options
->context
))
1545 options
->output_format
|= DIFF_FORMAT_PATCH
;
1546 else if (!strcmp(arg
, "--raw"))
1547 options
->output_format
|= DIFF_FORMAT_RAW
;
1548 else if (!strcmp(arg
, "--patch-with-raw")) {
1549 options
->output_format
|= DIFF_FORMAT_PATCH
| DIFF_FORMAT_RAW
;
1551 else if (!strcmp(arg
, "--stat"))
1552 options
->output_format
|= DIFF_FORMAT_DIFFSTAT
;
1553 else if (!strcmp(arg
, "--check"))
1554 options
->output_format
|= DIFF_FORMAT_CHECKDIFF
;
1555 else if (!strcmp(arg
, "--summary"))
1556 options
->output_format
|= DIFF_FORMAT_SUMMARY
;
1557 else if (!strcmp(arg
, "--patch-with-stat")) {
1558 options
->output_format
|= DIFF_FORMAT_PATCH
| DIFF_FORMAT_DIFFSTAT
;
1560 else if (!strcmp(arg
, "-z"))
1561 options
->line_termination
= 0;
1562 else if (!strncmp(arg
, "-l", 2))
1563 options
->rename_limit
= strtoul(arg
+2, NULL
, 10);
1564 else if (!strcmp(arg
, "--full-index"))
1565 options
->full_index
= 1;
1566 else if (!strcmp(arg
, "--binary")) {
1567 options
->output_format
|= DIFF_FORMAT_PATCH
;
1568 options
->full_index
= options
->binary
= 1;
1570 else if (!strcmp(arg
, "--name-only"))
1571 options
->output_format
|= DIFF_FORMAT_NAME
;
1572 else if (!strcmp(arg
, "--name-status"))
1573 options
->output_format
|= DIFF_FORMAT_NAME_STATUS
;
1574 else if (!strcmp(arg
, "-R"))
1575 options
->reverse_diff
= 1;
1576 else if (!strncmp(arg
, "-S", 2))
1577 options
->pickaxe
= arg
+ 2;
1578 else if (!strcmp(arg
, "-s")) {
1579 options
->output_format
|= DIFF_FORMAT_NO_OUTPUT
;
1581 else if (!strncmp(arg
, "-O", 2))
1582 options
->orderfile
= arg
+ 2;
1583 else if (!strncmp(arg
, "--diff-filter=", 14))
1584 options
->filter
= arg
+ 14;
1585 else if (!strcmp(arg
, "--pickaxe-all"))
1586 options
->pickaxe_opts
= DIFF_PICKAXE_ALL
;
1587 else if (!strcmp(arg
, "--pickaxe-regex"))
1588 options
->pickaxe_opts
= DIFF_PICKAXE_REGEX
;
1589 else if (!strncmp(arg
, "-B", 2)) {
1590 if ((options
->break_opt
=
1591 diff_scoreopt_parse(arg
)) == -1)
1594 else if (!strncmp(arg
, "-M", 2)) {
1595 if ((options
->rename_score
=
1596 diff_scoreopt_parse(arg
)) == -1)
1598 options
->detect_rename
= DIFF_DETECT_RENAME
;
1600 else if (!strncmp(arg
, "-C", 2)) {
1601 if ((options
->rename_score
=
1602 diff_scoreopt_parse(arg
)) == -1)
1604 options
->detect_rename
= DIFF_DETECT_COPY
;
1606 else if (!strcmp(arg
, "--find-copies-harder"))
1607 options
->find_copies_harder
= 1;
1608 else if (!strcmp(arg
, "--abbrev"))
1609 options
->abbrev
= DEFAULT_ABBREV
;
1610 else if (!strncmp(arg
, "--abbrev=", 9)) {
1611 options
->abbrev
= strtoul(arg
+ 9, NULL
, 10);
1612 if (options
->abbrev
< MINIMUM_ABBREV
)
1613 options
->abbrev
= MINIMUM_ABBREV
;
1614 else if (40 < options
->abbrev
)
1615 options
->abbrev
= 40;
1617 else if (!strcmp(arg
, "--color"))
1618 options
->color_diff
= 1;
1619 else if (!strcmp(arg
, "-w") || !strcmp(arg
, "--ignore-all-space"))
1620 options
->xdl_opts
|= XDF_IGNORE_WHITESPACE
;
1621 else if (!strcmp(arg
, "-b") || !strcmp(arg
, "--ignore-space-change"))
1622 options
->xdl_opts
|= XDF_IGNORE_WHITESPACE_CHANGE
;
1628 static int parse_num(const char **cp_p
)
1630 unsigned long num
, scale
;
1632 const char *cp
= *cp_p
;
1639 if ( !dot
&& ch
== '.' ) {
1642 } else if ( ch
== '%' ) {
1643 scale
= dot
? scale
*100 : 100;
1644 cp
++; /* % is always at the end */
1646 } else if ( ch
>= '0' && ch
<= '9' ) {
1647 if ( scale
< 100000 ) {
1649 num
= (num
*10) + (ch
-'0');
1658 /* user says num divided by scale and we say internally that
1659 * is MAX_SCORE * num / scale.
1661 return (num
>= scale
) ? MAX_SCORE
: (MAX_SCORE
* num
/ scale
);
1664 int diff_scoreopt_parse(const char *opt
)
1666 int opt1
, opt2
, cmd
;
1671 if (cmd
!= 'M' && cmd
!= 'C' && cmd
!= 'B')
1672 return -1; /* that is not a -M, -C nor -B option */
1674 opt1
= parse_num(&opt
);
1680 else if (*opt
!= '/')
1681 return -1; /* we expect -B80/99 or -B80 */
1684 opt2
= parse_num(&opt
);
1689 return opt1
| (opt2
<< 16);
1692 struct diff_queue_struct diff_queued_diff
;
1694 void diff_q(struct diff_queue_struct
*queue
, struct diff_filepair
*dp
)
1696 if (queue
->alloc
<= queue
->nr
) {
1697 queue
->alloc
= alloc_nr(queue
->alloc
);
1698 queue
->queue
= xrealloc(queue
->queue
,
1699 sizeof(dp
) * queue
->alloc
);
1701 queue
->queue
[queue
->nr
++] = dp
;
1704 struct diff_filepair
*diff_queue(struct diff_queue_struct
*queue
,
1705 struct diff_filespec
*one
,
1706 struct diff_filespec
*two
)
1708 struct diff_filepair
*dp
= xmalloc(sizeof(*dp
));
1713 dp
->source_stays
= 0;
1714 dp
->broken_pair
= 0;
1720 void diff_free_filepair(struct diff_filepair
*p
)
1722 diff_free_filespec_data(p
->one
);
1723 diff_free_filespec_data(p
->two
);
1729 /* This is different from find_unique_abbrev() in that
1730 * it stuffs the result with dots for alignment.
1732 const char *diff_unique_abbrev(const unsigned char *sha1
, int len
)
1737 return sha1_to_hex(sha1
);
1739 abbrev
= find_unique_abbrev(sha1
, len
);
1741 return sha1_to_hex(sha1
);
1742 abblen
= strlen(abbrev
);
1744 static char hex
[41];
1745 if (len
< abblen
&& abblen
<= len
+ 2)
1746 sprintf(hex
, "%s%.*s", abbrev
, len
+3-abblen
, "..");
1748 sprintf(hex
, "%s...", abbrev
);
1751 return sha1_to_hex(sha1
);
1754 static void diff_flush_raw(struct diff_filepair
*p
,
1755 struct diff_options
*options
)
1759 int abbrev
= options
->abbrev
;
1760 const char *path_one
, *path_two
;
1761 int inter_name_termination
= '\t';
1762 int line_termination
= options
->line_termination
;
1764 if (!line_termination
)
1765 inter_name_termination
= 0;
1767 path_one
= p
->one
->path
;
1768 path_two
= p
->two
->path
;
1769 if (line_termination
) {
1770 path_one
= quote_one(path_one
);
1771 path_two
= quote_one(path_two
);
1775 sprintf(status
, "%c%03d", p
->status
,
1776 (int)(0.5 + p
->score
* 100.0/MAX_SCORE
));
1778 status
[0] = p
->status
;
1781 switch (p
->status
) {
1782 case DIFF_STATUS_COPIED
:
1783 case DIFF_STATUS_RENAMED
:
1786 case DIFF_STATUS_ADDED
:
1787 case DIFF_STATUS_DELETED
:
1794 if (!(options
->output_format
& DIFF_FORMAT_NAME_STATUS
)) {
1795 printf(":%06o %06o %s ",
1796 p
->one
->mode
, p
->two
->mode
,
1797 diff_unique_abbrev(p
->one
->sha1
, abbrev
));
1799 diff_unique_abbrev(p
->two
->sha1
, abbrev
));
1801 printf("%s%c%s", status
, inter_name_termination
, path_one
);
1803 printf("%c%s", inter_name_termination
, path_two
);
1804 putchar(line_termination
);
1805 if (path_one
!= p
->one
->path
)
1806 free((void*)path_one
);
1807 if (path_two
!= p
->two
->path
)
1808 free((void*)path_two
);
1811 static void diff_flush_name(struct diff_filepair
*p
, int line_termination
)
1813 char *path
= p
->two
->path
;
1815 if (line_termination
)
1816 path
= quote_one(p
->two
->path
);
1817 printf("%s%c", path
, line_termination
);
1818 if (p
->two
->path
!= path
)
1822 int diff_unmodified_pair(struct diff_filepair
*p
)
1824 /* This function is written stricter than necessary to support
1825 * the currently implemented transformers, but the idea is to
1826 * let transformers to produce diff_filepairs any way they want,
1827 * and filter and clean them up here before producing the output.
1829 struct diff_filespec
*one
, *two
;
1831 if (DIFF_PAIR_UNMERGED(p
))
1832 return 0; /* unmerged is interesting */
1837 /* deletion, addition, mode or type change
1838 * and rename are all interesting.
1840 if (DIFF_FILE_VALID(one
) != DIFF_FILE_VALID(two
) ||
1841 DIFF_PAIR_MODE_CHANGED(p
) ||
1842 strcmp(one
->path
, two
->path
))
1845 /* both are valid and point at the same path. that is, we are
1846 * dealing with a change.
1848 if (one
->sha1_valid
&& two
->sha1_valid
&&
1849 !memcmp(one
->sha1
, two
->sha1
, sizeof(one
->sha1
)))
1850 return 1; /* no change */
1851 if (!one
->sha1_valid
&& !two
->sha1_valid
)
1852 return 1; /* both look at the same file on the filesystem. */
1856 static void diff_flush_patch(struct diff_filepair
*p
, struct diff_options
*o
)
1858 if (diff_unmodified_pair(p
))
1861 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
1862 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
1863 return; /* no tree diffs in patch format */
1868 static void diff_flush_stat(struct diff_filepair
*p
, struct diff_options
*o
,
1869 struct diffstat_t
*diffstat
)
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 */
1878 run_diffstat(p
, o
, diffstat
);
1881 static void diff_flush_checkdiff(struct diff_filepair
*p
,
1882 struct diff_options
*o
)
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_checkdiff(p
, o
);
1894 int diff_queue_is_empty(void)
1896 struct diff_queue_struct
*q
= &diff_queued_diff
;
1898 for (i
= 0; i
< q
->nr
; i
++)
1899 if (!diff_unmodified_pair(q
->queue
[i
]))
1905 void diff_debug_filespec(struct diff_filespec
*s
, int x
, const char *one
)
1907 fprintf(stderr
, "queue[%d] %s (%s) %s %06o %s\n",
1910 DIFF_FILE_VALID(s
) ? "valid" : "invalid",
1912 s
->sha1_valid
? sha1_to_hex(s
->sha1
) : "");
1913 fprintf(stderr
, "queue[%d] %s size %lu flags %d\n",
1915 s
->size
, s
->xfrm_flags
);
1918 void diff_debug_filepair(const struct diff_filepair
*p
, int i
)
1920 diff_debug_filespec(p
->one
, i
, "one");
1921 diff_debug_filespec(p
->two
, i
, "two");
1922 fprintf(stderr
, "score %d, status %c stays %d broken %d\n",
1923 p
->score
, p
->status
? p
->status
: '?',
1924 p
->source_stays
, p
->broken_pair
);
1927 void diff_debug_queue(const char *msg
, struct diff_queue_struct
*q
)
1931 fprintf(stderr
, "%s\n", msg
);
1932 fprintf(stderr
, "q->nr = %d\n", q
->nr
);
1933 for (i
= 0; i
< q
->nr
; i
++) {
1934 struct diff_filepair
*p
= q
->queue
[i
];
1935 diff_debug_filepair(p
, i
);
1940 static void diff_resolve_rename_copy(void)
1943 struct diff_filepair
*p
, *pp
;
1944 struct diff_queue_struct
*q
= &diff_queued_diff
;
1946 diff_debug_queue("resolve-rename-copy", q
);
1948 for (i
= 0; i
< q
->nr
; i
++) {
1950 p
->status
= 0; /* undecided */
1951 if (DIFF_PAIR_UNMERGED(p
))
1952 p
->status
= DIFF_STATUS_UNMERGED
;
1953 else if (!DIFF_FILE_VALID(p
->one
))
1954 p
->status
= DIFF_STATUS_ADDED
;
1955 else if (!DIFF_FILE_VALID(p
->two
))
1956 p
->status
= DIFF_STATUS_DELETED
;
1957 else if (DIFF_PAIR_TYPE_CHANGED(p
))
1958 p
->status
= DIFF_STATUS_TYPE_CHANGED
;
1960 /* from this point on, we are dealing with a pair
1961 * whose both sides are valid and of the same type, i.e.
1962 * either in-place edit or rename/copy edit.
1964 else if (DIFF_PAIR_RENAME(p
)) {
1965 if (p
->source_stays
) {
1966 p
->status
= DIFF_STATUS_COPIED
;
1969 /* See if there is some other filepair that
1970 * copies from the same source as us. If so
1971 * we are a copy. Otherwise we are either a
1972 * copy if the path stays, or a rename if it
1973 * does not, but we already handled "stays" case.
1975 for (j
= i
+ 1; j
< q
->nr
; j
++) {
1977 if (strcmp(pp
->one
->path
, p
->one
->path
))
1978 continue; /* not us */
1979 if (!DIFF_PAIR_RENAME(pp
))
1980 continue; /* not a rename/copy */
1981 /* pp is a rename/copy from the same source */
1982 p
->status
= DIFF_STATUS_COPIED
;
1986 p
->status
= DIFF_STATUS_RENAMED
;
1988 else if (memcmp(p
->one
->sha1
, p
->two
->sha1
, 20) ||
1989 p
->one
->mode
!= p
->two
->mode
)
1990 p
->status
= DIFF_STATUS_MODIFIED
;
1992 /* This is a "no-change" entry and should not
1993 * happen anymore, but prepare for broken callers.
1995 error("feeding unmodified %s to diffcore",
1997 p
->status
= DIFF_STATUS_UNKNOWN
;
2000 diff_debug_queue("resolve-rename-copy done", q
);
2003 static int check_pair_status(struct diff_filepair
*p
)
2005 switch (p
->status
) {
2006 case DIFF_STATUS_UNKNOWN
:
2009 die("internal error in diff-resolve-rename-copy");
2015 static void flush_one_pair(struct diff_filepair
*p
, struct diff_options
*opt
)
2017 int fmt
= opt
->output_format
;
2019 if (fmt
& DIFF_FORMAT_CHECKDIFF
)
2020 diff_flush_checkdiff(p
, opt
);
2021 else if (fmt
& (DIFF_FORMAT_RAW
| DIFF_FORMAT_NAME_STATUS
))
2022 diff_flush_raw(p
, opt
);
2023 else if (fmt
& DIFF_FORMAT_NAME
)
2024 diff_flush_name(p
, opt
->line_termination
);
2027 static void show_file_mode_name(const char *newdelete
, struct diff_filespec
*fs
)
2030 printf(" %s mode %06o %s\n", newdelete
, fs
->mode
, fs
->path
);
2032 printf(" %s %s\n", newdelete
, fs
->path
);
2036 static void show_mode_change(struct diff_filepair
*p
, int show_name
)
2038 if (p
->one
->mode
&& p
->two
->mode
&& p
->one
->mode
!= p
->two
->mode
) {
2040 printf(" mode change %06o => %06o %s\n",
2041 p
->one
->mode
, p
->two
->mode
, p
->two
->path
);
2043 printf(" mode change %06o => %06o\n",
2044 p
->one
->mode
, p
->two
->mode
);
2048 static void show_rename_copy(const char *renamecopy
, struct diff_filepair
*p
)
2050 const char *old
, *new;
2052 /* Find common prefix */
2056 const char *slash_old
, *slash_new
;
2057 slash_old
= strchr(old
, '/');
2058 slash_new
= strchr(new, '/');
2061 slash_old
- old
!= slash_new
- new ||
2062 memcmp(old
, new, slash_new
- new))
2064 old
= slash_old
+ 1;
2065 new = slash_new
+ 1;
2067 /* p->one->path thru old is the common prefix, and old and new
2068 * through the end of names are renames
2070 if (old
!= p
->one
->path
)
2071 printf(" %s %.*s{%s => %s} (%d%%)\n", renamecopy
,
2072 (int)(old
- p
->one
->path
), p
->one
->path
,
2073 old
, new, (int)(0.5 + p
->score
* 100.0/MAX_SCORE
));
2075 printf(" %s %s => %s (%d%%)\n", renamecopy
,
2076 p
->one
->path
, p
->two
->path
,
2077 (int)(0.5 + p
->score
* 100.0/MAX_SCORE
));
2078 show_mode_change(p
, 0);
2081 static void diff_summary(struct diff_filepair
*p
)
2084 case DIFF_STATUS_DELETED
:
2085 show_file_mode_name("delete", p
->one
);
2087 case DIFF_STATUS_ADDED
:
2088 show_file_mode_name("create", p
->two
);
2090 case DIFF_STATUS_COPIED
:
2091 show_rename_copy("copy", p
);
2093 case DIFF_STATUS_RENAMED
:
2094 show_rename_copy("rename", p
);
2098 printf(" rewrite %s (%d%%)\n", p
->two
->path
,
2099 (int)(0.5 + p
->score
* 100.0/MAX_SCORE
));
2100 show_mode_change(p
, 0);
2101 } else show_mode_change(p
, 1);
2107 struct xdiff_emit_state xm
;
2112 static int remove_space(char *line
, int len
)
2118 for (i
= 0; i
< len
; i
++)
2119 if (!isspace((c
= line
[i
])))
2125 static void patch_id_consume(void *priv
, char *line
, unsigned long len
)
2127 struct patch_id_t
*data
= priv
;
2130 /* Ignore line numbers when computing the SHA1 of the patch */
2131 if (!strncmp(line
, "@@ -", 4))
2134 new_len
= remove_space(line
, len
);
2136 SHA1_Update(data
->ctx
, line
, new_len
);
2137 data
->patchlen
+= new_len
;
2140 /* returns 0 upon success, and writes result into sha1 */
2141 static int diff_get_patch_id(struct diff_options
*options
, unsigned char *sha1
)
2143 struct diff_queue_struct
*q
= &diff_queued_diff
;
2146 struct patch_id_t data
;
2147 char buffer
[PATH_MAX
* 4 + 20];
2150 memset(&data
, 0, sizeof(struct patch_id_t
));
2152 data
.xm
.consume
= patch_id_consume
;
2154 for (i
= 0; i
< q
->nr
; i
++) {
2159 struct diff_filepair
*p
= q
->queue
[i
];
2163 return error("internal diff status error");
2164 if (p
->status
== DIFF_STATUS_UNKNOWN
)
2166 if (diff_unmodified_pair(p
))
2168 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
2169 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
2171 if (DIFF_PAIR_UNMERGED(p
))
2174 diff_fill_sha1_info(p
->one
);
2175 diff_fill_sha1_info(p
->two
);
2176 if (fill_mmfile(&mf1
, p
->one
) < 0 ||
2177 fill_mmfile(&mf2
, p
->two
) < 0)
2178 return error("unable to read files to diff");
2180 /* Maybe hash p->two? into the patch id? */
2181 if (mmfile_is_binary(&mf2
))
2184 len1
= remove_space(p
->one
->path
, strlen(p
->one
->path
));
2185 len2
= remove_space(p
->two
->path
, strlen(p
->two
->path
));
2186 if (p
->one
->mode
== 0)
2187 len1
= snprintf(buffer
, sizeof(buffer
),
2188 "diff--gita/%.*sb/%.*s"
2195 len2
, p
->two
->path
);
2196 else if (p
->two
->mode
== 0)
2197 len1
= snprintf(buffer
, sizeof(buffer
),
2198 "diff--gita/%.*sb/%.*s"
2199 "deletedfilemode%06o"
2205 len1
, p
->one
->path
);
2207 len1
= snprintf(buffer
, sizeof(buffer
),
2208 "diff--gita/%.*sb/%.*s"
2214 len2
, p
->two
->path
);
2215 SHA1_Update(&ctx
, buffer
, len1
);
2217 xpp
.flags
= XDF_NEED_MINIMAL
;
2219 xecfg
.flags
= XDL_EMIT_FUNCNAMES
;
2220 ecb
.outf
= xdiff_outf
;
2222 xdl_diff(&mf1
, &mf2
, &xpp
, &xecfg
, &ecb
);
2225 SHA1_Final(sha1
, &ctx
);
2229 int diff_flush_patch_id(struct diff_options
*options
, unsigned char *sha1
)
2231 struct diff_queue_struct
*q
= &diff_queued_diff
;
2233 int result
= diff_get_patch_id(options
, sha1
);
2235 for (i
= 0; i
< q
->nr
; i
++)
2236 diff_free_filepair(q
->queue
[i
]);
2240 q
->nr
= q
->alloc
= 0;
2245 static int is_summary_empty(const struct diff_queue_struct
*q
)
2249 for (i
= 0; i
< q
->nr
; i
++) {
2250 const struct diff_filepair
*p
= q
->queue
[i
];
2252 switch (p
->status
) {
2253 case DIFF_STATUS_DELETED
:
2254 case DIFF_STATUS_ADDED
:
2255 case DIFF_STATUS_COPIED
:
2256 case DIFF_STATUS_RENAMED
:
2261 if (p
->one
->mode
&& p
->two
->mode
&&
2262 p
->one
->mode
!= p
->two
->mode
)
2270 void diff_flush(struct diff_options
*options
)
2272 struct diff_queue_struct
*q
= &diff_queued_diff
;
2273 int i
, output_format
= options
->output_format
;
2277 * Order: raw, stat, summary, patch
2278 * or: name/name-status/checkdiff (other bits clear)
2283 if (output_format
& (DIFF_FORMAT_RAW
|
2285 DIFF_FORMAT_NAME_STATUS
|
2286 DIFF_FORMAT_CHECKDIFF
)) {
2287 for (i
= 0; i
< q
->nr
; i
++) {
2288 struct diff_filepair
*p
= q
->queue
[i
];
2289 if (check_pair_status(p
))
2290 flush_one_pair(p
, options
);
2295 if (output_format
& DIFF_FORMAT_DIFFSTAT
) {
2296 struct diffstat_t diffstat
;
2298 memset(&diffstat
, 0, sizeof(struct diffstat_t
));
2299 diffstat
.xm
.consume
= diffstat_consume
;
2300 for (i
= 0; i
< q
->nr
; i
++) {
2301 struct diff_filepair
*p
= q
->queue
[i
];
2302 if (check_pair_status(p
))
2303 diff_flush_stat(p
, options
, &diffstat
);
2305 show_stats(&diffstat
);
2309 if (output_format
& DIFF_FORMAT_SUMMARY
&& !is_summary_empty(q
)) {
2310 for (i
= 0; i
< q
->nr
; i
++)
2311 diff_summary(q
->queue
[i
]);
2315 if (output_format
& DIFF_FORMAT_PATCH
) {
2317 if (options
->stat_sep
) {
2318 /* attach patch instead of inline */
2319 fputs(options
->stat_sep
, stdout
);
2321 putchar(options
->line_termination
);
2325 for (i
= 0; i
< q
->nr
; i
++) {
2326 struct diff_filepair
*p
= q
->queue
[i
];
2327 if (check_pair_status(p
))
2328 diff_flush_patch(p
, options
);
2332 for (i
= 0; i
< q
->nr
; i
++)
2333 diff_free_filepair(q
->queue
[i
]);
2337 q
->nr
= q
->alloc
= 0;
2340 static void diffcore_apply_filter(const char *filter
)
2343 struct diff_queue_struct
*q
= &diff_queued_diff
;
2344 struct diff_queue_struct outq
;
2346 outq
.nr
= outq
.alloc
= 0;
2351 if (strchr(filter
, DIFF_STATUS_FILTER_AON
)) {
2353 for (i
= found
= 0; !found
&& i
< q
->nr
; i
++) {
2354 struct diff_filepair
*p
= q
->queue
[i
];
2355 if (((p
->status
== DIFF_STATUS_MODIFIED
) &&
2357 strchr(filter
, DIFF_STATUS_FILTER_BROKEN
)) ||
2359 strchr(filter
, DIFF_STATUS_MODIFIED
)))) ||
2360 ((p
->status
!= DIFF_STATUS_MODIFIED
) &&
2361 strchr(filter
, p
->status
)))
2367 /* otherwise we will clear the whole queue
2368 * by copying the empty outq at the end of this
2369 * function, but first clear the current entries
2372 for (i
= 0; i
< q
->nr
; i
++)
2373 diff_free_filepair(q
->queue
[i
]);
2376 /* Only the matching ones */
2377 for (i
= 0; i
< q
->nr
; i
++) {
2378 struct diff_filepair
*p
= q
->queue
[i
];
2380 if (((p
->status
== DIFF_STATUS_MODIFIED
) &&
2382 strchr(filter
, DIFF_STATUS_FILTER_BROKEN
)) ||
2384 strchr(filter
, DIFF_STATUS_MODIFIED
)))) ||
2385 ((p
->status
!= DIFF_STATUS_MODIFIED
) &&
2386 strchr(filter
, p
->status
)))
2389 diff_free_filepair(p
);
2396 void diffcore_std(struct diff_options
*options
)
2398 if (options
->break_opt
!= -1)
2399 diffcore_break(options
->break_opt
);
2400 if (options
->detect_rename
)
2401 diffcore_rename(options
);
2402 if (options
->break_opt
!= -1)
2403 diffcore_merge_broken();
2404 if (options
->pickaxe
)
2405 diffcore_pickaxe(options
->pickaxe
, options
->pickaxe_opts
);
2406 if (options
->orderfile
)
2407 diffcore_order(options
->orderfile
);
2408 diff_resolve_rename_copy();
2409 diffcore_apply_filter(options
->filter
);
2413 void diffcore_std_no_resolve(struct diff_options
*options
)
2415 if (options
->pickaxe
)
2416 diffcore_pickaxe(options
->pickaxe
, options
->pickaxe_opts
);
2417 if (options
->orderfile
)
2418 diffcore_order(options
->orderfile
);
2419 diffcore_apply_filter(options
->filter
);
2422 void diff_addremove(struct diff_options
*options
,
2423 int addremove
, unsigned mode
,
2424 const unsigned char *sha1
,
2425 const char *base
, const char *path
)
2427 char concatpath
[PATH_MAX
];
2428 struct diff_filespec
*one
, *two
;
2430 /* This may look odd, but it is a preparation for
2431 * feeding "there are unchanged files which should
2432 * not produce diffs, but when you are doing copy
2433 * detection you would need them, so here they are"
2434 * entries to the diff-core. They will be prefixed
2435 * with something like '=' or '*' (I haven't decided
2436 * which but should not make any difference).
2437 * Feeding the same new and old to diff_change()
2438 * also has the same effect.
2439 * Before the final output happens, they are pruned after
2440 * merged into rename/copy pairs as appropriate.
2442 if (options
->reverse_diff
)
2443 addremove
= (addremove
== '+' ? '-' :
2444 addremove
== '-' ? '+' : addremove
);
2446 if (!path
) path
= "";
2447 sprintf(concatpath
, "%s%s", base
, path
);
2448 one
= alloc_filespec(concatpath
);
2449 two
= alloc_filespec(concatpath
);
2451 if (addremove
!= '+')
2452 fill_filespec(one
, sha1
, mode
);
2453 if (addremove
!= '-')
2454 fill_filespec(two
, sha1
, mode
);
2456 diff_queue(&diff_queued_diff
, one
, two
);
2459 void diff_change(struct diff_options
*options
,
2460 unsigned old_mode
, unsigned new_mode
,
2461 const unsigned char *old_sha1
,
2462 const unsigned char *new_sha1
,
2463 const char *base
, const char *path
)
2465 char concatpath
[PATH_MAX
];
2466 struct diff_filespec
*one
, *two
;
2468 if (options
->reverse_diff
) {
2470 const unsigned char *tmp_c
;
2471 tmp
= old_mode
; old_mode
= new_mode
; new_mode
= tmp
;
2472 tmp_c
= old_sha1
; old_sha1
= new_sha1
; new_sha1
= tmp_c
;
2474 if (!path
) path
= "";
2475 sprintf(concatpath
, "%s%s", base
, path
);
2476 one
= alloc_filespec(concatpath
);
2477 two
= alloc_filespec(concatpath
);
2478 fill_filespec(one
, old_sha1
, old_mode
);
2479 fill_filespec(two
, new_sha1
, new_mode
);
2481 diff_queue(&diff_queued_diff
, one
, two
);
2484 void diff_unmerge(struct diff_options
*options
,
2487 struct diff_filespec
*one
, *two
;
2488 one
= alloc_filespec(path
);
2489 two
= alloc_filespec(path
);
2490 diff_queue(&diff_queued_diff
, one
, two
);