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
= isatty(1);
115 else if (!strcasecmp(value
, "never"))
116 diff_use_color_default
= 0;
117 else if (!strcasecmp(value
, "always"))
118 diff_use_color_default
= 1;
120 diff_use_color_default
= git_config_bool(var
, value
);
123 if (!strncmp(var
, "diff.color.", 11)) {
124 int slot
= parse_diff_color_slot(var
, 11);
125 diff_colors
[slot
] = parse_diff_color_value(value
, var
);
128 return git_default_config(var
, value
);
131 static char *quote_one(const char *str
)
138 needlen
= quote_c_style(str
, NULL
, NULL
, 0);
141 xp
= xmalloc(needlen
+ 1);
142 quote_c_style(str
, xp
, NULL
, 0);
146 static char *quote_two(const char *one
, const char *two
)
148 int need_one
= quote_c_style(one
, NULL
, NULL
, 1);
149 int need_two
= quote_c_style(two
, NULL
, NULL
, 1);
152 if (need_one
+ need_two
) {
153 if (!need_one
) need_one
= strlen(one
);
154 if (!need_two
) need_one
= strlen(two
);
156 xp
= xmalloc(need_one
+ need_two
+ 3);
158 quote_c_style(one
, xp
+ 1, NULL
, 1);
159 quote_c_style(two
, xp
+ need_one
+ 1, NULL
, 1);
160 strcpy(xp
+ need_one
+ need_two
+ 1, "\"");
163 need_one
= strlen(one
);
164 need_two
= strlen(two
);
165 xp
= xmalloc(need_one
+ need_two
+ 1);
167 strcpy(xp
+ need_one
, two
);
171 static const char *external_diff(void)
173 static const char *external_diff_cmd
= NULL
;
174 static int done_preparing
= 0;
177 return external_diff_cmd
;
178 external_diff_cmd
= getenv("GIT_EXTERNAL_DIFF");
180 return external_diff_cmd
;
183 #define TEMPFILE_PATH_LEN 50
185 static struct diff_tempfile
{
186 const char *name
; /* filename external diff should read from */
189 char tmp_path
[TEMPFILE_PATH_LEN
];
192 static int count_lines(const char *data
, int size
)
194 int count
, ch
, completely_empty
= 1, nl_just_seen
= 0;
201 completely_empty
= 0;
205 completely_empty
= 0;
208 if (completely_empty
)
211 count
++; /* no trailing newline */
215 static void print_line_count(int count
)
225 printf("1,%d", count
);
230 static void copy_file(int prefix
, const char *data
, int size
)
232 int ch
, nl_just_seen
= 1;
244 printf("\n\\ No newline at end of file\n");
247 static void emit_rewrite_diff(const char *name_a
,
249 struct diff_filespec
*one
,
250 struct diff_filespec
*two
)
253 diff_populate_filespec(one
, 0);
254 diff_populate_filespec(two
, 0);
255 lc_a
= count_lines(one
->data
, one
->size
);
256 lc_b
= count_lines(two
->data
, two
->size
);
257 printf("--- %s\n+++ %s\n@@ -", name_a
, name_b
);
258 print_line_count(lc_a
);
260 print_line_count(lc_b
);
263 copy_file('-', one
->data
, one
->size
);
265 copy_file('+', two
->data
, two
->size
);
268 static int fill_mmfile(mmfile_t
*mf
, struct diff_filespec
*one
)
270 if (!DIFF_FILE_VALID(one
)) {
271 mf
->ptr
= (char *)""; /* does not matter */
275 else if (diff_populate_filespec(one
, 0))
278 mf
->size
= one
->size
;
282 struct emit_callback
{
283 struct xdiff_emit_state xm
;
284 int nparents
, color_diff
;
285 const char **label_path
;
288 static inline const char *get_color(int diff_use_color
, enum color_diff ix
)
291 return diff_colors
[ix
];
295 static void fn_out_consume(void *priv
, char *line
, unsigned long len
)
298 struct emit_callback
*ecbdata
= priv
;
299 const char *set
= get_color(ecbdata
->color_diff
, DIFF_METAINFO
);
300 const char *reset
= get_color(ecbdata
->color_diff
, DIFF_RESET
);
302 if (ecbdata
->label_path
[0]) {
303 printf("%s--- %s%s\n", set
, ecbdata
->label_path
[0], reset
);
304 printf("%s+++ %s%s\n", set
, ecbdata
->label_path
[1], reset
);
305 ecbdata
->label_path
[0] = ecbdata
->label_path
[1] = NULL
;
308 /* This is not really necessary for now because
309 * this codepath only deals with two-way diffs.
311 for (i
= 0; i
< len
&& line
[i
] == '@'; i
++)
313 if (2 <= i
&& i
< len
&& line
[i
] == ' ') {
314 ecbdata
->nparents
= i
- 1;
315 set
= get_color(ecbdata
->color_diff
, DIFF_FRAGINFO
);
317 else if (len
< ecbdata
->nparents
)
320 int nparents
= ecbdata
->nparents
;
321 int color
= DIFF_PLAIN
;
322 for (i
= 0; i
< nparents
&& len
; i
++) {
324 color
= DIFF_FILE_OLD
;
325 else if (line
[i
] == '+')
326 color
= DIFF_FILE_NEW
;
328 set
= get_color(ecbdata
->color_diff
, color
);
330 if (len
> 0 && line
[len
-1] == '\n')
333 fwrite (line
, len
, 1, stdout
);
337 static char *pprint_rename(const char *a
, const char *b
)
342 int pfx_length
, sfx_length
;
343 int len_a
= strlen(a
);
344 int len_b
= strlen(b
);
346 /* Find common prefix */
348 while (*old
&& *new && *old
== *new) {
350 pfx_length
= old
- a
+ 1;
355 /* Find common suffix */
359 while (a
<= old
&& b
<= new && *old
== *new) {
361 sfx_length
= len_a
- (old
- a
);
367 * pfx{mid-a => mid-b}sfx
368 * {pfx-a => pfx-b}sfx
369 * pfx{sfx-a => sfx-b}
372 if (pfx_length
+ sfx_length
) {
373 int a_midlen
= len_a
- pfx_length
- sfx_length
;
374 int b_midlen
= len_b
- pfx_length
- sfx_length
;
375 if (a_midlen
< 0) a_midlen
= 0;
376 if (b_midlen
< 0) b_midlen
= 0;
378 name
= xmalloc(pfx_length
+ a_midlen
+ b_midlen
+ sfx_length
+ 7);
379 sprintf(name
, "%.*s{%.*s => %.*s}%s",
381 a_midlen
, a
+ pfx_length
,
382 b_midlen
, b
+ pfx_length
,
383 a
+ len_a
- sfx_length
);
386 name
= xmalloc(len_a
+ len_b
+ 5);
387 sprintf(name
, "%s => %s", a
, b
);
393 struct xdiff_emit_state xm
;
397 struct diffstat_file
{
399 unsigned is_unmerged
:1;
400 unsigned is_binary
:1;
401 unsigned is_renamed
:1;
402 unsigned int added
, deleted
;
406 static struct diffstat_file
*diffstat_add(struct diffstat_t
*diffstat
,
410 struct diffstat_file
*x
;
411 x
= xcalloc(sizeof (*x
), 1);
412 if (diffstat
->nr
== diffstat
->alloc
) {
413 diffstat
->alloc
= alloc_nr(diffstat
->alloc
);
414 diffstat
->files
= xrealloc(diffstat
->files
,
415 diffstat
->alloc
* sizeof(x
));
417 diffstat
->files
[diffstat
->nr
++] = x
;
419 x
->name
= pprint_rename(name_a
, name_b
);
423 x
->name
= strdup(name_a
);
427 static void diffstat_consume(void *priv
, char *line
, unsigned long len
)
429 struct diffstat_t
*diffstat
= priv
;
430 struct diffstat_file
*x
= diffstat
->files
[diffstat
->nr
- 1];
434 else if (line
[0] == '-')
438 static const char pluses
[] = "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++";
439 static const char minuses
[]= "----------------------------------------------------------------------";
440 const char mime_boundary_leader
[] = "------------";
442 static void show_stats(struct diffstat_t
* data
)
444 int i
, len
, add
, del
, total
, adds
= 0, dels
= 0;
445 int max
, max_change
= 0, max_len
= 0;
446 int total_files
= data
->nr
;
451 for (i
= 0; i
< data
->nr
; i
++) {
452 struct diffstat_file
*file
= data
->files
[i
];
454 len
= strlen(file
->name
);
458 if (file
->is_binary
|| file
->is_unmerged
)
460 if (max_change
< file
->added
+ file
->deleted
)
461 max_change
= file
->added
+ file
->deleted
;
464 for (i
= 0; i
< data
->nr
; i
++) {
465 const char *prefix
= "";
466 char *name
= data
->files
[i
]->name
;
467 int added
= data
->files
[i
]->added
;
468 int deleted
= data
->files
[i
]->deleted
;
470 if (0 < (len
= quote_c_style(name
, NULL
, NULL
, 0))) {
471 char *qname
= xmalloc(len
+ 1);
472 quote_c_style(name
, qname
, NULL
, 0);
474 data
->files
[i
]->name
= name
= qname
;
478 * "scale" the filename
489 slash
= strchr(name
, '/');
496 * scale the add/delete
502 if (data
->files
[i
]->is_binary
) {
503 printf(" %s%-*s | Bin\n", prefix
, len
, name
);
504 goto free_diffstat_file
;
506 else if (data
->files
[i
]->is_unmerged
) {
507 printf(" %s%-*s | Unmerged\n", prefix
, len
, name
);
508 goto free_diffstat_file
;
510 else if (!data
->files
[i
]->is_renamed
&&
511 (added
+ deleted
== 0)) {
513 goto free_diffstat_file
;
522 if (max_change
> 0) {
523 total
= (total
* max
+ max_change
/ 2) / max_change
;
524 add
= (add
* max
+ max_change
/ 2) / max_change
;
527 printf(" %s%-*s |%5d %.*s%.*s\n", prefix
,
528 len
, name
, added
+ deleted
,
529 add
, pluses
, del
, minuses
);
531 free(data
->files
[i
]->name
);
532 free(data
->files
[i
]);
535 printf(" %d files changed, %d insertions(+), %d deletions(-)\n",
536 total_files
, adds
, dels
);
540 struct xdiff_emit_state xm
;
541 const char *filename
;
545 static void checkdiff_consume(void *priv
, char *line
, unsigned long len
)
547 struct checkdiff_t
*data
= priv
;
549 if (line
[0] == '+') {
554 /* check space before tab */
555 for (i
= 1; i
< len
&& (line
[i
] == ' ' || line
[i
] == '\t'); i
++)
558 if (line
[i
- 1] == '\t' && spaces
)
559 printf("%s:%d: space before tab:%.*s\n",
560 data
->filename
, data
->lineno
, (int)len
, line
);
562 /* check white space at line end */
563 if (line
[len
- 1] == '\n')
565 if (isspace(line
[len
- 1]))
566 printf("%s:%d: white space at end: %.*s\n",
567 data
->filename
, data
->lineno
, (int)len
, line
);
568 } else if (line
[0] == ' ')
570 else if (line
[0] == '@') {
571 char *plus
= strchr(line
, '+');
573 data
->lineno
= strtol(plus
, NULL
, 10);
579 static unsigned char *deflate_it(char *data
,
581 unsigned long *result_size
)
584 unsigned char *deflated
;
587 memset(&stream
, 0, sizeof(stream
));
588 deflateInit(&stream
, zlib_compression_level
);
589 bound
= deflateBound(&stream
, size
);
590 deflated
= xmalloc(bound
);
591 stream
.next_out
= deflated
;
592 stream
.avail_out
= bound
;
594 stream
.next_in
= (unsigned char *)data
;
595 stream
.avail_in
= size
;
596 while (deflate(&stream
, Z_FINISH
) == Z_OK
)
599 *result_size
= stream
.total_out
;
603 static void emit_binary_diff(mmfile_t
*one
, mmfile_t
*two
)
609 unsigned long orig_size
;
610 unsigned long delta_size
;
611 unsigned long deflate_size
;
612 unsigned long data_size
;
614 printf("GIT binary patch\n");
615 /* We could do deflated delta, or we could do just deflated two,
616 * whichever is smaller.
619 deflated
= deflate_it(two
->ptr
, two
->size
, &deflate_size
);
620 if (one
->size
&& two
->size
) {
621 delta
= diff_delta(one
->ptr
, one
->size
,
623 &delta_size
, deflate_size
);
625 void *to_free
= delta
;
626 orig_size
= delta_size
;
627 delta
= deflate_it(delta
, delta_size
, &delta_size
);
632 if (delta
&& delta_size
< deflate_size
) {
633 printf("delta %lu\n", orig_size
);
636 data_size
= delta_size
;
639 printf("literal %lu\n", two
->size
);
642 data_size
= deflate_size
;
645 /* emit data encoded in base85 */
648 int bytes
= (52 < data_size
) ? 52 : data_size
;
652 line
[0] = bytes
+ 'A' - 1;
654 line
[0] = bytes
- 26 + 'a' - 1;
655 encode_85(line
+ 1, cp
, bytes
);
656 cp
= (char *) cp
+ bytes
;
663 #define FIRST_FEW_BYTES 8000
664 static int mmfile_is_binary(mmfile_t
*mf
)
667 if (FIRST_FEW_BYTES
< sz
)
668 sz
= FIRST_FEW_BYTES
;
669 if (memchr(mf
->ptr
, 0, sz
))
674 static void builtin_diff(const char *name_a
,
676 struct diff_filespec
*one
,
677 struct diff_filespec
*two
,
678 const char *xfrm_msg
,
679 struct diff_options
*o
,
680 int complete_rewrite
)
685 const char *set
= get_color(o
->color_diff
, DIFF_METAINFO
);
686 const char *reset
= get_color(o
->color_diff
, DIFF_RESET
);
688 a_one
= quote_two("a/", name_a
);
689 b_two
= quote_two("b/", name_b
);
690 lbl
[0] = DIFF_FILE_VALID(one
) ? a_one
: "/dev/null";
691 lbl
[1] = DIFF_FILE_VALID(two
) ? b_two
: "/dev/null";
692 printf("%sdiff --git %s %s%s\n", set
, a_one
, b_two
, reset
);
693 if (lbl
[0][0] == '/') {
695 printf("%snew file mode %06o%s\n", set
, two
->mode
, reset
);
696 if (xfrm_msg
&& xfrm_msg
[0])
697 printf("%s%s%s\n", set
, xfrm_msg
, reset
);
699 else if (lbl
[1][0] == '/') {
700 printf("%sdeleted file mode %06o%s\n", set
, one
->mode
, reset
);
701 if (xfrm_msg
&& xfrm_msg
[0])
702 printf("%s%s%s\n", set
, xfrm_msg
, reset
);
705 if (one
->mode
!= two
->mode
) {
706 printf("%sold mode %06o%s\n", set
, one
->mode
, reset
);
707 printf("%snew mode %06o%s\n", set
, two
->mode
, reset
);
709 if (xfrm_msg
&& xfrm_msg
[0])
710 printf("%s%s%s\n", set
, xfrm_msg
, reset
);
712 * we do not run diff between different kind
715 if ((one
->mode
^ two
->mode
) & S_IFMT
)
716 goto free_ab_and_return
;
717 if (complete_rewrite
) {
718 emit_rewrite_diff(name_a
, name_b
, one
, two
);
719 goto free_ab_and_return
;
723 if (fill_mmfile(&mf1
, one
) < 0 || fill_mmfile(&mf2
, two
) < 0)
724 die("unable to read files to diff");
726 if (!o
->text
&& (mmfile_is_binary(&mf1
) || mmfile_is_binary(&mf2
))) {
727 /* Quite common confusing case */
728 if (mf1
.size
== mf2
.size
&&
729 !memcmp(mf1
.ptr
, mf2
.ptr
, mf1
.size
))
730 goto free_ab_and_return
;
732 emit_binary_diff(&mf1
, &mf2
);
734 printf("Binary files %s and %s differ\n",
738 /* Crazy xdl interfaces.. */
739 const char *diffopts
= getenv("GIT_DIFF_OPTS");
743 struct emit_callback ecbdata
;
745 memset(&ecbdata
, 0, sizeof(ecbdata
));
746 ecbdata
.label_path
= lbl
;
747 ecbdata
.color_diff
= o
->color_diff
;
748 xpp
.flags
= XDF_NEED_MINIMAL
| o
->xdl_opts
;
749 xecfg
.ctxlen
= o
->context
;
750 xecfg
.flags
= XDL_EMIT_FUNCNAMES
;
753 else if (!strncmp(diffopts
, "--unified=", 10))
754 xecfg
.ctxlen
= strtoul(diffopts
+ 10, NULL
, 10);
755 else if (!strncmp(diffopts
, "-u", 2))
756 xecfg
.ctxlen
= strtoul(diffopts
+ 2, NULL
, 10);
757 ecb
.outf
= xdiff_outf
;
759 ecbdata
.xm
.consume
= fn_out_consume
;
760 xdl_diff(&mf1
, &mf2
, &xpp
, &xecfg
, &ecb
);
769 static void builtin_diffstat(const char *name_a
, const char *name_b
,
770 struct diff_filespec
*one
,
771 struct diff_filespec
*two
,
772 struct diffstat_t
*diffstat
,
773 struct diff_options
*o
,
774 int complete_rewrite
)
777 struct diffstat_file
*data
;
779 data
= diffstat_add(diffstat
, name_a
, name_b
);
782 data
->is_unmerged
= 1;
785 if (complete_rewrite
) {
786 diff_populate_filespec(one
, 0);
787 diff_populate_filespec(two
, 0);
788 data
->deleted
= count_lines(one
->data
, one
->size
);
789 data
->added
= count_lines(two
->data
, two
->size
);
792 if (fill_mmfile(&mf1
, one
) < 0 || fill_mmfile(&mf2
, two
) < 0)
793 die("unable to read files to diff");
795 if (mmfile_is_binary(&mf1
) || mmfile_is_binary(&mf2
))
798 /* Crazy xdl interfaces.. */
803 xpp
.flags
= XDF_NEED_MINIMAL
| o
->xdl_opts
;
806 ecb
.outf
= xdiff_outf
;
808 xdl_diff(&mf1
, &mf2
, &xpp
, &xecfg
, &ecb
);
812 static void builtin_checkdiff(const char *name_a
, const char *name_b
,
813 struct diff_filespec
*one
,
814 struct diff_filespec
*two
)
817 struct checkdiff_t data
;
822 memset(&data
, 0, sizeof(data
));
823 data
.xm
.consume
= checkdiff_consume
;
824 data
.filename
= name_b
? name_b
: name_a
;
827 if (fill_mmfile(&mf1
, one
) < 0 || fill_mmfile(&mf2
, two
) < 0)
828 die("unable to read files to diff");
830 if (mmfile_is_binary(&mf2
))
833 /* Crazy xdl interfaces.. */
838 xpp
.flags
= XDF_NEED_MINIMAL
;
841 ecb
.outf
= xdiff_outf
;
843 xdl_diff(&mf1
, &mf2
, &xpp
, &xecfg
, &ecb
);
847 struct diff_filespec
*alloc_filespec(const char *path
)
849 int namelen
= strlen(path
);
850 struct diff_filespec
*spec
= xmalloc(sizeof(*spec
) + namelen
+ 1);
852 memset(spec
, 0, sizeof(*spec
));
853 spec
->path
= (char *)(spec
+ 1);
854 memcpy(spec
->path
, path
, namelen
+1);
858 void fill_filespec(struct diff_filespec
*spec
, const unsigned char *sha1
,
862 spec
->mode
= canon_mode(mode
);
863 memcpy(spec
->sha1
, sha1
, 20);
864 spec
->sha1_valid
= !!memcmp(sha1
, null_sha1
, 20);
869 * Given a name and sha1 pair, if the dircache tells us the file in
870 * the work tree has that object contents, return true, so that
871 * prepare_temp_file() does not have to inflate and extract.
873 static int work_tree_matches(const char *name
, const unsigned char *sha1
)
875 struct cache_entry
*ce
;
879 /* We do not read the cache ourselves here, because the
880 * benchmark with my previous version that always reads cache
881 * shows that it makes things worse for diff-tree comparing
882 * two linux-2.6 kernel trees in an already checked out work
883 * tree. This is because most diff-tree comparisons deal with
884 * only a small number of files, while reading the cache is
885 * expensive for a large project, and its cost outweighs the
886 * savings we get by not inflating the object to a temporary
887 * file. Practically, this code only helps when we are used
888 * by diff-cache --cached, which does read the cache before
895 pos
= cache_name_pos(name
, len
);
898 ce
= active_cache
[pos
];
899 if ((lstat(name
, &st
) < 0) ||
900 !S_ISREG(st
.st_mode
) || /* careful! */
901 ce_match_stat(ce
, &st
, 0) ||
902 memcmp(sha1
, ce
->sha1
, 20))
904 /* we return 1 only when we can stat, it is a regular file,
905 * stat information matches, and sha1 recorded in the cache
906 * matches. I.e. we know the file in the work tree really is
907 * the same as the <name, sha1> pair.
912 static struct sha1_size_cache
{
913 unsigned char sha1
[20];
916 static int sha1_size_cache_nr
, sha1_size_cache_alloc
;
918 static struct sha1_size_cache
*locate_size_cache(unsigned char *sha1
,
923 struct sha1_size_cache
*e
;
926 last
= sha1_size_cache_nr
;
927 while (last
> first
) {
928 int cmp
, next
= (last
+ first
) >> 1;
929 e
= sha1_size_cache
[next
];
930 cmp
= memcmp(e
->sha1
, sha1
, 20);
942 /* insert to make it at "first" */
943 if (sha1_size_cache_alloc
<= sha1_size_cache_nr
) {
944 sha1_size_cache_alloc
= alloc_nr(sha1_size_cache_alloc
);
945 sha1_size_cache
= xrealloc(sha1_size_cache
,
946 sha1_size_cache_alloc
*
947 sizeof(*sha1_size_cache
));
949 sha1_size_cache_nr
++;
950 if (first
< sha1_size_cache_nr
)
951 memmove(sha1_size_cache
+ first
+ 1, sha1_size_cache
+ first
,
952 (sha1_size_cache_nr
- first
- 1) *
953 sizeof(*sha1_size_cache
));
954 e
= xmalloc(sizeof(struct sha1_size_cache
));
955 sha1_size_cache
[first
] = e
;
956 memcpy(e
->sha1
, sha1
, 20);
962 * While doing rename detection and pickaxe operation, we may need to
963 * grab the data for the blob (or file) for our own in-core comparison.
964 * diff_filespec has data and size fields for this purpose.
966 int diff_populate_filespec(struct diff_filespec
*s
, int size_only
)
969 if (!DIFF_FILE_VALID(s
))
970 die("internal error: asking to populate invalid file.");
971 if (S_ISDIR(s
->mode
))
979 if (!s
->sha1_valid
||
980 work_tree_matches(s
->path
, s
->sha1
)) {
983 if (lstat(s
->path
, &st
) < 0) {
984 if (errno
== ENOENT
) {
988 s
->data
= (char *)"";
993 s
->size
= st
.st_size
;
998 if (S_ISLNK(st
.st_mode
)) {
1000 s
->data
= xmalloc(s
->size
);
1002 ret
= readlink(s
->path
, s
->data
, s
->size
);
1009 fd
= open(s
->path
, O_RDONLY
);
1012 s
->data
= mmap(NULL
, s
->size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
1014 if (s
->data
== MAP_FAILED
)
1016 s
->should_munmap
= 1;
1020 struct sha1_size_cache
*e
;
1023 e
= locate_size_cache(s
->sha1
, 1, 0);
1028 if (!sha1_object_info(s
->sha1
, type
, &s
->size
))
1029 locate_size_cache(s
->sha1
, 0, s
->size
);
1032 s
->data
= read_sha1_file(s
->sha1
, type
, &s
->size
);
1039 void diff_free_filespec_data(struct diff_filespec
*s
)
1043 else if (s
->should_munmap
)
1044 munmap(s
->data
, s
->size
);
1045 s
->should_free
= s
->should_munmap
= 0;
1051 static void prep_temp_blob(struct diff_tempfile
*temp
,
1054 const unsigned char *sha1
,
1059 fd
= git_mkstemp(temp
->tmp_path
, TEMPFILE_PATH_LEN
, ".diff_XXXXXX");
1061 die("unable to create temp-file");
1062 if (write(fd
, blob
, size
) != size
)
1063 die("unable to write temp-file");
1065 temp
->name
= temp
->tmp_path
;
1066 strcpy(temp
->hex
, sha1_to_hex(sha1
));
1068 sprintf(temp
->mode
, "%06o", mode
);
1071 static void prepare_temp_file(const char *name
,
1072 struct diff_tempfile
*temp
,
1073 struct diff_filespec
*one
)
1075 if (!DIFF_FILE_VALID(one
)) {
1077 /* A '-' entry produces this for file-2, and
1078 * a '+' entry produces this for file-1.
1080 temp
->name
= "/dev/null";
1081 strcpy(temp
->hex
, ".");
1082 strcpy(temp
->mode
, ".");
1086 if (!one
->sha1_valid
||
1087 work_tree_matches(name
, one
->sha1
)) {
1089 if (lstat(name
, &st
) < 0) {
1090 if (errno
== ENOENT
)
1091 goto not_a_valid_file
;
1092 die("stat(%s): %s", name
, strerror(errno
));
1094 if (S_ISLNK(st
.st_mode
)) {
1096 char buf
[PATH_MAX
+ 1]; /* ought to be SYMLINK_MAX */
1097 if (sizeof(buf
) <= st
.st_size
)
1098 die("symlink too long: %s", name
);
1099 ret
= readlink(name
, buf
, st
.st_size
);
1101 die("readlink(%s)", name
);
1102 prep_temp_blob(temp
, buf
, st
.st_size
,
1104 one
->sha1
: null_sha1
),
1106 one
->mode
: S_IFLNK
));
1109 /* we can borrow from the file in the work tree */
1111 if (!one
->sha1_valid
)
1112 strcpy(temp
->hex
, sha1_to_hex(null_sha1
));
1114 strcpy(temp
->hex
, sha1_to_hex(one
->sha1
));
1115 /* Even though we may sometimes borrow the
1116 * contents from the work tree, we always want
1117 * one->mode. mode is trustworthy even when
1118 * !(one->sha1_valid), as long as
1119 * DIFF_FILE_VALID(one).
1121 sprintf(temp
->mode
, "%06o", one
->mode
);
1126 if (diff_populate_filespec(one
, 0))
1127 die("cannot read data blob for %s", one
->path
);
1128 prep_temp_blob(temp
, one
->data
, one
->size
,
1129 one
->sha1
, one
->mode
);
1133 static void remove_tempfile(void)
1137 for (i
= 0; i
< 2; i
++)
1138 if (diff_temp
[i
].name
== diff_temp
[i
].tmp_path
) {
1139 unlink(diff_temp
[i
].name
);
1140 diff_temp
[i
].name
= NULL
;
1144 static void remove_tempfile_on_signal(int signo
)
1147 signal(SIGINT
, SIG_DFL
);
1151 static int spawn_prog(const char *pgm
, const char **arg
)
1159 die("unable to fork");
1161 execvp(pgm
, (char *const*) arg
);
1165 while (waitpid(pid
, &status
, 0) < 0) {
1171 /* Earlier we did not check the exit status because
1172 * diff exits non-zero if files are different, and
1173 * we are not interested in knowing that. It was a
1174 * mistake which made it harder to quit a diff-*
1175 * session that uses the git-apply-patch-script as
1176 * the GIT_EXTERNAL_DIFF. A custom GIT_EXTERNAL_DIFF
1177 * should also exit non-zero only when it wants to
1178 * abort the entire diff-* session.
1180 if (WIFEXITED(status
) && !WEXITSTATUS(status
))
1185 /* An external diff command takes:
1187 * diff-cmd name infile1 infile1-sha1 infile1-mode \
1188 * infile2 infile2-sha1 infile2-mode [ rename-to ]
1191 static void run_external_diff(const char *pgm
,
1194 struct diff_filespec
*one
,
1195 struct diff_filespec
*two
,
1196 const char *xfrm_msg
,
1197 int complete_rewrite
)
1199 const char *spawn_arg
[10];
1200 struct diff_tempfile
*temp
= diff_temp
;
1202 static int atexit_asked
= 0;
1203 const char *othername
;
1204 const char **arg
= &spawn_arg
[0];
1206 othername
= (other
? other
: name
);
1208 prepare_temp_file(name
, &temp
[0], one
);
1209 prepare_temp_file(othername
, &temp
[1], two
);
1210 if (! atexit_asked
&&
1211 (temp
[0].name
== temp
[0].tmp_path
||
1212 temp
[1].name
== temp
[1].tmp_path
)) {
1214 atexit(remove_tempfile
);
1216 signal(SIGINT
, remove_tempfile_on_signal
);
1222 *arg
++ = temp
[0].name
;
1223 *arg
++ = temp
[0].hex
;
1224 *arg
++ = temp
[0].mode
;
1225 *arg
++ = temp
[1].name
;
1226 *arg
++ = temp
[1].hex
;
1227 *arg
++ = temp
[1].mode
;
1237 retval
= spawn_prog(pgm
, spawn_arg
);
1240 fprintf(stderr
, "external diff died, stopping at %s.\n", name
);
1245 static void run_diff_cmd(const char *pgm
,
1248 struct diff_filespec
*one
,
1249 struct diff_filespec
*two
,
1250 const char *xfrm_msg
,
1251 struct diff_options
*o
,
1252 int complete_rewrite
)
1255 run_external_diff(pgm
, name
, other
, one
, two
, xfrm_msg
,
1260 builtin_diff(name
, other
? other
: name
,
1261 one
, two
, xfrm_msg
, o
, complete_rewrite
);
1263 printf("* Unmerged path %s\n", name
);
1266 static void diff_fill_sha1_info(struct diff_filespec
*one
)
1268 if (DIFF_FILE_VALID(one
)) {
1269 if (!one
->sha1_valid
) {
1271 if (lstat(one
->path
, &st
) < 0)
1272 die("stat %s", one
->path
);
1273 if (index_path(one
->sha1
, one
->path
, &st
, 0))
1274 die("cannot hash %s\n", one
->path
);
1278 memset(one
->sha1
, 0, 20);
1281 static void run_diff(struct diff_filepair
*p
, struct diff_options
*o
)
1283 const char *pgm
= external_diff();
1284 char msg
[PATH_MAX
*2+300], *xfrm_msg
;
1285 struct diff_filespec
*one
;
1286 struct diff_filespec
*two
;
1289 char *name_munged
, *other_munged
;
1290 int complete_rewrite
= 0;
1293 if (DIFF_PAIR_UNMERGED(p
)) {
1295 run_diff_cmd(pgm
, p
->one
->path
, NULL
, NULL
, NULL
, NULL
, o
, 0);
1299 name
= p
->one
->path
;
1300 other
= (strcmp(name
, p
->two
->path
) ? p
->two
->path
: NULL
);
1301 name_munged
= quote_one(name
);
1302 other_munged
= quote_one(other
);
1303 one
= p
->one
; two
= p
->two
;
1305 diff_fill_sha1_info(one
);
1306 diff_fill_sha1_info(two
);
1309 switch (p
->status
) {
1310 case DIFF_STATUS_COPIED
:
1311 len
+= snprintf(msg
+ len
, sizeof(msg
) - len
,
1312 "similarity index %d%%\n"
1315 (int)(0.5 + p
->score
* 100.0/MAX_SCORE
),
1316 name_munged
, other_munged
);
1318 case DIFF_STATUS_RENAMED
:
1319 len
+= snprintf(msg
+ len
, sizeof(msg
) - len
,
1320 "similarity index %d%%\n"
1323 (int)(0.5 + p
->score
* 100.0/MAX_SCORE
),
1324 name_munged
, other_munged
);
1326 case DIFF_STATUS_MODIFIED
:
1328 len
+= snprintf(msg
+ len
, sizeof(msg
) - len
,
1329 "dissimilarity index %d%%\n",
1330 (int)(0.5 + p
->score
*
1332 complete_rewrite
= 1;
1341 if (memcmp(one
->sha1
, two
->sha1
, 20)) {
1342 int abbrev
= o
->full_index
? 40 : DEFAULT_ABBREV
;
1344 len
+= snprintf(msg
+ len
, sizeof(msg
) - len
,
1346 abbrev
, sha1_to_hex(one
->sha1
),
1347 abbrev
, sha1_to_hex(two
->sha1
));
1348 if (one
->mode
== two
->mode
)
1349 len
+= snprintf(msg
+ len
, sizeof(msg
) - len
,
1350 " %06o", one
->mode
);
1351 len
+= snprintf(msg
+ len
, sizeof(msg
) - len
, "\n");
1356 xfrm_msg
= len
? msg
: NULL
;
1359 DIFF_FILE_VALID(one
) && DIFF_FILE_VALID(two
) &&
1360 (S_IFMT
& one
->mode
) != (S_IFMT
& two
->mode
)) {
1361 /* a filepair that changes between file and symlink
1362 * needs to be split into deletion and creation.
1364 struct diff_filespec
*null
= alloc_filespec(two
->path
);
1365 run_diff_cmd(NULL
, name
, other
, one
, null
, xfrm_msg
, o
, 0);
1367 null
= alloc_filespec(one
->path
);
1368 run_diff_cmd(NULL
, name
, other
, null
, two
, xfrm_msg
, o
, 0);
1372 run_diff_cmd(pgm
, name
, other
, one
, two
, xfrm_msg
, o
,
1379 static void run_diffstat(struct diff_filepair
*p
, struct diff_options
*o
,
1380 struct diffstat_t
*diffstat
)
1384 int complete_rewrite
= 0;
1386 if (DIFF_PAIR_UNMERGED(p
)) {
1388 builtin_diffstat(p
->one
->path
, NULL
, NULL
, NULL
, diffstat
, o
, 0);
1392 name
= p
->one
->path
;
1393 other
= (strcmp(name
, p
->two
->path
) ? p
->two
->path
: NULL
);
1395 diff_fill_sha1_info(p
->one
);
1396 diff_fill_sha1_info(p
->two
);
1398 if (p
->status
== DIFF_STATUS_MODIFIED
&& p
->score
)
1399 complete_rewrite
= 1;
1400 builtin_diffstat(name
, other
, p
->one
, p
->two
, diffstat
, o
, complete_rewrite
);
1403 static void run_checkdiff(struct diff_filepair
*p
, struct diff_options
*o
)
1408 if (DIFF_PAIR_UNMERGED(p
)) {
1413 name
= p
->one
->path
;
1414 other
= (strcmp(name
, p
->two
->path
) ? p
->two
->path
: NULL
);
1416 diff_fill_sha1_info(p
->one
);
1417 diff_fill_sha1_info(p
->two
);
1419 builtin_checkdiff(name
, other
, p
->one
, p
->two
);
1422 void diff_setup(struct diff_options
*options
)
1424 memset(options
, 0, sizeof(*options
));
1425 options
->line_termination
= '\n';
1426 options
->break_opt
= -1;
1427 options
->rename_limit
= -1;
1428 options
->context
= 3;
1429 options
->msg_sep
= "";
1431 options
->change
= diff_change
;
1432 options
->add_remove
= diff_addremove
;
1433 options
->color_diff
= diff_use_color_default
;
1436 int diff_setup_done(struct diff_options
*options
)
1438 if ((options
->find_copies_harder
&&
1439 options
->detect_rename
!= DIFF_DETECT_COPY
) ||
1440 (0 <= options
->rename_limit
&& !options
->detect_rename
))
1443 if (options
->output_format
& (DIFF_FORMAT_NAME
|
1444 DIFF_FORMAT_NAME_STATUS
|
1445 DIFF_FORMAT_CHECKDIFF
|
1446 DIFF_FORMAT_NO_OUTPUT
))
1447 options
->output_format
&= ~(DIFF_FORMAT_RAW
|
1448 DIFF_FORMAT_DIFFSTAT
|
1449 DIFF_FORMAT_SUMMARY
|
1453 * These cases always need recursive; we do not drop caller-supplied
1454 * recursive bits for other formats here.
1456 if (options
->output_format
& (DIFF_FORMAT_PATCH
|
1457 DIFF_FORMAT_DIFFSTAT
|
1458 DIFF_FORMAT_CHECKDIFF
))
1459 options
->recursive
= 1;
1461 * Also pickaxe would not work very well if you do not say recursive
1463 if (options
->pickaxe
)
1464 options
->recursive
= 1;
1466 if (options
->detect_rename
&& options
->rename_limit
< 0)
1467 options
->rename_limit
= diff_rename_limit_default
;
1468 if (options
->setup
& DIFF_SETUP_USE_CACHE
) {
1470 /* read-cache does not die even when it fails
1471 * so it is safe for us to do this here. Also
1472 * it does not smudge active_cache or active_nr
1473 * when it fails, so we do not have to worry about
1474 * cleaning it up ourselves either.
1478 if (options
->setup
& DIFF_SETUP_USE_SIZE_CACHE
)
1480 if (options
->abbrev
<= 0 || 40 < options
->abbrev
)
1481 options
->abbrev
= 40; /* full */
1486 static int opt_arg(const char *arg
, int arg_short
, const char *arg_long
, int *val
)
1496 if (c
== arg_short
) {
1500 if (val
&& isdigit(c
)) {
1502 int n
= strtoul(arg
, &end
, 10);
1513 eq
= strchr(arg
, '=');
1518 if (!len
|| strncmp(arg
, arg_long
, len
))
1523 if (!isdigit(*++eq
))
1525 n
= strtoul(eq
, &end
, 10);
1533 int diff_opt_parse(struct diff_options
*options
, const char **av
, int ac
)
1535 const char *arg
= av
[0];
1536 if (!strcmp(arg
, "-p") || !strcmp(arg
, "-u"))
1537 options
->output_format
|= DIFF_FORMAT_PATCH
;
1538 else if (opt_arg(arg
, 'U', "unified", &options
->context
))
1539 options
->output_format
|= DIFF_FORMAT_PATCH
;
1540 else if (!strcmp(arg
, "--raw"))
1541 options
->output_format
|= DIFF_FORMAT_RAW
;
1542 else if (!strcmp(arg
, "--patch-with-raw")) {
1543 options
->output_format
|= DIFF_FORMAT_PATCH
| DIFF_FORMAT_RAW
;
1545 else if (!strcmp(arg
, "--stat"))
1546 options
->output_format
|= DIFF_FORMAT_DIFFSTAT
;
1547 else if (!strcmp(arg
, "--check"))
1548 options
->output_format
|= DIFF_FORMAT_CHECKDIFF
;
1549 else if (!strcmp(arg
, "--summary"))
1550 options
->output_format
|= DIFF_FORMAT_SUMMARY
;
1551 else if (!strcmp(arg
, "--patch-with-stat")) {
1552 options
->output_format
|= DIFF_FORMAT_PATCH
| DIFF_FORMAT_DIFFSTAT
;
1554 else if (!strcmp(arg
, "-z"))
1555 options
->line_termination
= 0;
1556 else if (!strncmp(arg
, "-l", 2))
1557 options
->rename_limit
= strtoul(arg
+2, NULL
, 10);
1558 else if (!strcmp(arg
, "--full-index"))
1559 options
->full_index
= 1;
1560 else if (!strcmp(arg
, "--binary")) {
1561 options
->output_format
|= DIFF_FORMAT_PATCH
;
1562 options
->full_index
= options
->binary
= 1;
1564 else if (!strcmp(arg
, "-a") || !strcmp(arg
, "--text")) {
1567 else if (!strcmp(arg
, "--name-only"))
1568 options
->output_format
|= DIFF_FORMAT_NAME
;
1569 else if (!strcmp(arg
, "--name-status"))
1570 options
->output_format
|= DIFF_FORMAT_NAME_STATUS
;
1571 else if (!strcmp(arg
, "-R"))
1572 options
->reverse_diff
= 1;
1573 else if (!strncmp(arg
, "-S", 2))
1574 options
->pickaxe
= arg
+ 2;
1575 else if (!strcmp(arg
, "-s")) {
1576 options
->output_format
|= DIFF_FORMAT_NO_OUTPUT
;
1578 else if (!strncmp(arg
, "-O", 2))
1579 options
->orderfile
= arg
+ 2;
1580 else if (!strncmp(arg
, "--diff-filter=", 14))
1581 options
->filter
= arg
+ 14;
1582 else if (!strcmp(arg
, "--pickaxe-all"))
1583 options
->pickaxe_opts
= DIFF_PICKAXE_ALL
;
1584 else if (!strcmp(arg
, "--pickaxe-regex"))
1585 options
->pickaxe_opts
= DIFF_PICKAXE_REGEX
;
1586 else if (!strncmp(arg
, "-B", 2)) {
1587 if ((options
->break_opt
=
1588 diff_scoreopt_parse(arg
)) == -1)
1591 else if (!strncmp(arg
, "-M", 2)) {
1592 if ((options
->rename_score
=
1593 diff_scoreopt_parse(arg
)) == -1)
1595 options
->detect_rename
= DIFF_DETECT_RENAME
;
1597 else if (!strncmp(arg
, "-C", 2)) {
1598 if ((options
->rename_score
=
1599 diff_scoreopt_parse(arg
)) == -1)
1601 options
->detect_rename
= DIFF_DETECT_COPY
;
1603 else if (!strcmp(arg
, "--find-copies-harder"))
1604 options
->find_copies_harder
= 1;
1605 else if (!strcmp(arg
, "--abbrev"))
1606 options
->abbrev
= DEFAULT_ABBREV
;
1607 else if (!strncmp(arg
, "--abbrev=", 9)) {
1608 options
->abbrev
= strtoul(arg
+ 9, NULL
, 10);
1609 if (options
->abbrev
< MINIMUM_ABBREV
)
1610 options
->abbrev
= MINIMUM_ABBREV
;
1611 else if (40 < options
->abbrev
)
1612 options
->abbrev
= 40;
1614 else if (!strcmp(arg
, "--color"))
1615 options
->color_diff
= 1;
1616 else if (!strcmp(arg
, "-w") || !strcmp(arg
, "--ignore-all-space"))
1617 options
->xdl_opts
|= XDF_IGNORE_WHITESPACE
;
1618 else if (!strcmp(arg
, "-b") || !strcmp(arg
, "--ignore-space-change"))
1619 options
->xdl_opts
|= XDF_IGNORE_WHITESPACE_CHANGE
;
1625 static int parse_num(const char **cp_p
)
1627 unsigned long num
, scale
;
1629 const char *cp
= *cp_p
;
1636 if ( !dot
&& ch
== '.' ) {
1639 } else if ( ch
== '%' ) {
1640 scale
= dot
? scale
*100 : 100;
1641 cp
++; /* % is always at the end */
1643 } else if ( ch
>= '0' && ch
<= '9' ) {
1644 if ( scale
< 100000 ) {
1646 num
= (num
*10) + (ch
-'0');
1655 /* user says num divided by scale and we say internally that
1656 * is MAX_SCORE * num / scale.
1658 return (num
>= scale
) ? MAX_SCORE
: (MAX_SCORE
* num
/ scale
);
1661 int diff_scoreopt_parse(const char *opt
)
1663 int opt1
, opt2
, cmd
;
1668 if (cmd
!= 'M' && cmd
!= 'C' && cmd
!= 'B')
1669 return -1; /* that is not a -M, -C nor -B option */
1671 opt1
= parse_num(&opt
);
1677 else if (*opt
!= '/')
1678 return -1; /* we expect -B80/99 or -B80 */
1681 opt2
= parse_num(&opt
);
1686 return opt1
| (opt2
<< 16);
1689 struct diff_queue_struct diff_queued_diff
;
1691 void diff_q(struct diff_queue_struct
*queue
, struct diff_filepair
*dp
)
1693 if (queue
->alloc
<= queue
->nr
) {
1694 queue
->alloc
= alloc_nr(queue
->alloc
);
1695 queue
->queue
= xrealloc(queue
->queue
,
1696 sizeof(dp
) * queue
->alloc
);
1698 queue
->queue
[queue
->nr
++] = dp
;
1701 struct diff_filepair
*diff_queue(struct diff_queue_struct
*queue
,
1702 struct diff_filespec
*one
,
1703 struct diff_filespec
*two
)
1705 struct diff_filepair
*dp
= xmalloc(sizeof(*dp
));
1710 dp
->source_stays
= 0;
1711 dp
->broken_pair
= 0;
1717 void diff_free_filepair(struct diff_filepair
*p
)
1719 diff_free_filespec_data(p
->one
);
1720 diff_free_filespec_data(p
->two
);
1726 /* This is different from find_unique_abbrev() in that
1727 * it stuffs the result with dots for alignment.
1729 const char *diff_unique_abbrev(const unsigned char *sha1
, int len
)
1734 return sha1_to_hex(sha1
);
1736 abbrev
= find_unique_abbrev(sha1
, len
);
1738 return sha1_to_hex(sha1
);
1739 abblen
= strlen(abbrev
);
1741 static char hex
[41];
1742 if (len
< abblen
&& abblen
<= len
+ 2)
1743 sprintf(hex
, "%s%.*s", abbrev
, len
+3-abblen
, "..");
1745 sprintf(hex
, "%s...", abbrev
);
1748 return sha1_to_hex(sha1
);
1751 static void diff_flush_raw(struct diff_filepair
*p
,
1752 struct diff_options
*options
)
1756 int abbrev
= options
->abbrev
;
1757 const char *path_one
, *path_two
;
1758 int inter_name_termination
= '\t';
1759 int line_termination
= options
->line_termination
;
1761 if (!line_termination
)
1762 inter_name_termination
= 0;
1764 path_one
= p
->one
->path
;
1765 path_two
= p
->two
->path
;
1766 if (line_termination
) {
1767 path_one
= quote_one(path_one
);
1768 path_two
= quote_one(path_two
);
1772 sprintf(status
, "%c%03d", p
->status
,
1773 (int)(0.5 + p
->score
* 100.0/MAX_SCORE
));
1775 status
[0] = p
->status
;
1778 switch (p
->status
) {
1779 case DIFF_STATUS_COPIED
:
1780 case DIFF_STATUS_RENAMED
:
1783 case DIFF_STATUS_ADDED
:
1784 case DIFF_STATUS_DELETED
:
1791 if (!(options
->output_format
& DIFF_FORMAT_NAME_STATUS
)) {
1792 printf(":%06o %06o %s ",
1793 p
->one
->mode
, p
->two
->mode
,
1794 diff_unique_abbrev(p
->one
->sha1
, abbrev
));
1796 diff_unique_abbrev(p
->two
->sha1
, abbrev
));
1798 printf("%s%c%s", status
, inter_name_termination
, path_one
);
1800 printf("%c%s", inter_name_termination
, path_two
);
1801 putchar(line_termination
);
1802 if (path_one
!= p
->one
->path
)
1803 free((void*)path_one
);
1804 if (path_two
!= p
->two
->path
)
1805 free((void*)path_two
);
1808 static void diff_flush_name(struct diff_filepair
*p
, int line_termination
)
1810 char *path
= p
->two
->path
;
1812 if (line_termination
)
1813 path
= quote_one(p
->two
->path
);
1814 printf("%s%c", path
, line_termination
);
1815 if (p
->two
->path
!= path
)
1819 int diff_unmodified_pair(struct diff_filepair
*p
)
1821 /* This function is written stricter than necessary to support
1822 * the currently implemented transformers, but the idea is to
1823 * let transformers to produce diff_filepairs any way they want,
1824 * and filter and clean them up here before producing the output.
1826 struct diff_filespec
*one
, *two
;
1828 if (DIFF_PAIR_UNMERGED(p
))
1829 return 0; /* unmerged is interesting */
1834 /* deletion, addition, mode or type change
1835 * and rename are all interesting.
1837 if (DIFF_FILE_VALID(one
) != DIFF_FILE_VALID(two
) ||
1838 DIFF_PAIR_MODE_CHANGED(p
) ||
1839 strcmp(one
->path
, two
->path
))
1842 /* both are valid and point at the same path. that is, we are
1843 * dealing with a change.
1845 if (one
->sha1_valid
&& two
->sha1_valid
&&
1846 !memcmp(one
->sha1
, two
->sha1
, sizeof(one
->sha1
)))
1847 return 1; /* no change */
1848 if (!one
->sha1_valid
&& !two
->sha1_valid
)
1849 return 1; /* both look at the same file on the filesystem. */
1853 static void diff_flush_patch(struct diff_filepair
*p
, struct diff_options
*o
)
1855 if (diff_unmodified_pair(p
))
1858 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
1859 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
1860 return; /* no tree diffs in patch format */
1865 static void diff_flush_stat(struct diff_filepair
*p
, struct diff_options
*o
,
1866 struct diffstat_t
*diffstat
)
1868 if (diff_unmodified_pair(p
))
1871 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
1872 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
1873 return; /* no tree diffs in patch format */
1875 run_diffstat(p
, o
, diffstat
);
1878 static void diff_flush_checkdiff(struct diff_filepair
*p
,
1879 struct diff_options
*o
)
1881 if (diff_unmodified_pair(p
))
1884 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
1885 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
1886 return; /* no tree diffs in patch format */
1888 run_checkdiff(p
, o
);
1891 int diff_queue_is_empty(void)
1893 struct diff_queue_struct
*q
= &diff_queued_diff
;
1895 for (i
= 0; i
< q
->nr
; i
++)
1896 if (!diff_unmodified_pair(q
->queue
[i
]))
1902 void diff_debug_filespec(struct diff_filespec
*s
, int x
, const char *one
)
1904 fprintf(stderr
, "queue[%d] %s (%s) %s %06o %s\n",
1907 DIFF_FILE_VALID(s
) ? "valid" : "invalid",
1909 s
->sha1_valid
? sha1_to_hex(s
->sha1
) : "");
1910 fprintf(stderr
, "queue[%d] %s size %lu flags %d\n",
1912 s
->size
, s
->xfrm_flags
);
1915 void diff_debug_filepair(const struct diff_filepair
*p
, int i
)
1917 diff_debug_filespec(p
->one
, i
, "one");
1918 diff_debug_filespec(p
->two
, i
, "two");
1919 fprintf(stderr
, "score %d, status %c stays %d broken %d\n",
1920 p
->score
, p
->status
? p
->status
: '?',
1921 p
->source_stays
, p
->broken_pair
);
1924 void diff_debug_queue(const char *msg
, struct diff_queue_struct
*q
)
1928 fprintf(stderr
, "%s\n", msg
);
1929 fprintf(stderr
, "q->nr = %d\n", q
->nr
);
1930 for (i
= 0; i
< q
->nr
; i
++) {
1931 struct diff_filepair
*p
= q
->queue
[i
];
1932 diff_debug_filepair(p
, i
);
1937 static void diff_resolve_rename_copy(void)
1940 struct diff_filepair
*p
, *pp
;
1941 struct diff_queue_struct
*q
= &diff_queued_diff
;
1943 diff_debug_queue("resolve-rename-copy", q
);
1945 for (i
= 0; i
< q
->nr
; i
++) {
1947 p
->status
= 0; /* undecided */
1948 if (DIFF_PAIR_UNMERGED(p
))
1949 p
->status
= DIFF_STATUS_UNMERGED
;
1950 else if (!DIFF_FILE_VALID(p
->one
))
1951 p
->status
= DIFF_STATUS_ADDED
;
1952 else if (!DIFF_FILE_VALID(p
->two
))
1953 p
->status
= DIFF_STATUS_DELETED
;
1954 else if (DIFF_PAIR_TYPE_CHANGED(p
))
1955 p
->status
= DIFF_STATUS_TYPE_CHANGED
;
1957 /* from this point on, we are dealing with a pair
1958 * whose both sides are valid and of the same type, i.e.
1959 * either in-place edit or rename/copy edit.
1961 else if (DIFF_PAIR_RENAME(p
)) {
1962 if (p
->source_stays
) {
1963 p
->status
= DIFF_STATUS_COPIED
;
1966 /* See if there is some other filepair that
1967 * copies from the same source as us. If so
1968 * we are a copy. Otherwise we are either a
1969 * copy if the path stays, or a rename if it
1970 * does not, but we already handled "stays" case.
1972 for (j
= i
+ 1; j
< q
->nr
; j
++) {
1974 if (strcmp(pp
->one
->path
, p
->one
->path
))
1975 continue; /* not us */
1976 if (!DIFF_PAIR_RENAME(pp
))
1977 continue; /* not a rename/copy */
1978 /* pp is a rename/copy from the same source */
1979 p
->status
= DIFF_STATUS_COPIED
;
1983 p
->status
= DIFF_STATUS_RENAMED
;
1985 else if (memcmp(p
->one
->sha1
, p
->two
->sha1
, 20) ||
1986 p
->one
->mode
!= p
->two
->mode
)
1987 p
->status
= DIFF_STATUS_MODIFIED
;
1989 /* This is a "no-change" entry and should not
1990 * happen anymore, but prepare for broken callers.
1992 error("feeding unmodified %s to diffcore",
1994 p
->status
= DIFF_STATUS_UNKNOWN
;
1997 diff_debug_queue("resolve-rename-copy done", q
);
2000 static int check_pair_status(struct diff_filepair
*p
)
2002 switch (p
->status
) {
2003 case DIFF_STATUS_UNKNOWN
:
2006 die("internal error in diff-resolve-rename-copy");
2012 static void flush_one_pair(struct diff_filepair
*p
, struct diff_options
*opt
)
2014 int fmt
= opt
->output_format
;
2016 if (fmt
& DIFF_FORMAT_CHECKDIFF
)
2017 diff_flush_checkdiff(p
, opt
);
2018 else if (fmt
& (DIFF_FORMAT_RAW
| DIFF_FORMAT_NAME_STATUS
))
2019 diff_flush_raw(p
, opt
);
2020 else if (fmt
& DIFF_FORMAT_NAME
)
2021 diff_flush_name(p
, opt
->line_termination
);
2024 static void show_file_mode_name(const char *newdelete
, struct diff_filespec
*fs
)
2027 printf(" %s mode %06o %s\n", newdelete
, fs
->mode
, fs
->path
);
2029 printf(" %s %s\n", newdelete
, fs
->path
);
2033 static void show_mode_change(struct diff_filepair
*p
, int show_name
)
2035 if (p
->one
->mode
&& p
->two
->mode
&& p
->one
->mode
!= p
->two
->mode
) {
2037 printf(" mode change %06o => %06o %s\n",
2038 p
->one
->mode
, p
->two
->mode
, p
->two
->path
);
2040 printf(" mode change %06o => %06o\n",
2041 p
->one
->mode
, p
->two
->mode
);
2045 static void show_rename_copy(const char *renamecopy
, struct diff_filepair
*p
)
2047 const char *old
, *new;
2049 /* Find common prefix */
2053 const char *slash_old
, *slash_new
;
2054 slash_old
= strchr(old
, '/');
2055 slash_new
= strchr(new, '/');
2058 slash_old
- old
!= slash_new
- new ||
2059 memcmp(old
, new, slash_new
- new))
2061 old
= slash_old
+ 1;
2062 new = slash_new
+ 1;
2064 /* p->one->path thru old is the common prefix, and old and new
2065 * through the end of names are renames
2067 if (old
!= p
->one
->path
)
2068 printf(" %s %.*s{%s => %s} (%d%%)\n", renamecopy
,
2069 (int)(old
- p
->one
->path
), p
->one
->path
,
2070 old
, new, (int)(0.5 + p
->score
* 100.0/MAX_SCORE
));
2072 printf(" %s %s => %s (%d%%)\n", renamecopy
,
2073 p
->one
->path
, p
->two
->path
,
2074 (int)(0.5 + p
->score
* 100.0/MAX_SCORE
));
2075 show_mode_change(p
, 0);
2078 static void diff_summary(struct diff_filepair
*p
)
2081 case DIFF_STATUS_DELETED
:
2082 show_file_mode_name("delete", p
->one
);
2084 case DIFF_STATUS_ADDED
:
2085 show_file_mode_name("create", p
->two
);
2087 case DIFF_STATUS_COPIED
:
2088 show_rename_copy("copy", p
);
2090 case DIFF_STATUS_RENAMED
:
2091 show_rename_copy("rename", p
);
2095 printf(" rewrite %s (%d%%)\n", p
->two
->path
,
2096 (int)(0.5 + p
->score
* 100.0/MAX_SCORE
));
2097 show_mode_change(p
, 0);
2098 } else show_mode_change(p
, 1);
2104 struct xdiff_emit_state xm
;
2109 static int remove_space(char *line
, int len
)
2115 for (i
= 0; i
< len
; i
++)
2116 if (!isspace((c
= line
[i
])))
2122 static void patch_id_consume(void *priv
, char *line
, unsigned long len
)
2124 struct patch_id_t
*data
= priv
;
2127 /* Ignore line numbers when computing the SHA1 of the patch */
2128 if (!strncmp(line
, "@@ -", 4))
2131 new_len
= remove_space(line
, len
);
2133 SHA1_Update(data
->ctx
, line
, new_len
);
2134 data
->patchlen
+= new_len
;
2137 /* returns 0 upon success, and writes result into sha1 */
2138 static int diff_get_patch_id(struct diff_options
*options
, unsigned char *sha1
)
2140 struct diff_queue_struct
*q
= &diff_queued_diff
;
2143 struct patch_id_t data
;
2144 char buffer
[PATH_MAX
* 4 + 20];
2147 memset(&data
, 0, sizeof(struct patch_id_t
));
2149 data
.xm
.consume
= patch_id_consume
;
2151 for (i
= 0; i
< q
->nr
; i
++) {
2156 struct diff_filepair
*p
= q
->queue
[i
];
2160 return error("internal diff status error");
2161 if (p
->status
== DIFF_STATUS_UNKNOWN
)
2163 if (diff_unmodified_pair(p
))
2165 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
2166 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
2168 if (DIFF_PAIR_UNMERGED(p
))
2171 diff_fill_sha1_info(p
->one
);
2172 diff_fill_sha1_info(p
->two
);
2173 if (fill_mmfile(&mf1
, p
->one
) < 0 ||
2174 fill_mmfile(&mf2
, p
->two
) < 0)
2175 return error("unable to read files to diff");
2177 /* Maybe hash p->two? into the patch id? */
2178 if (mmfile_is_binary(&mf2
))
2181 len1
= remove_space(p
->one
->path
, strlen(p
->one
->path
));
2182 len2
= remove_space(p
->two
->path
, strlen(p
->two
->path
));
2183 if (p
->one
->mode
== 0)
2184 len1
= snprintf(buffer
, sizeof(buffer
),
2185 "diff--gita/%.*sb/%.*s"
2192 len2
, p
->two
->path
);
2193 else if (p
->two
->mode
== 0)
2194 len1
= snprintf(buffer
, sizeof(buffer
),
2195 "diff--gita/%.*sb/%.*s"
2196 "deletedfilemode%06o"
2202 len1
, p
->one
->path
);
2204 len1
= snprintf(buffer
, sizeof(buffer
),
2205 "diff--gita/%.*sb/%.*s"
2211 len2
, p
->two
->path
);
2212 SHA1_Update(&ctx
, buffer
, len1
);
2214 xpp
.flags
= XDF_NEED_MINIMAL
;
2216 xecfg
.flags
= XDL_EMIT_FUNCNAMES
;
2217 ecb
.outf
= xdiff_outf
;
2219 xdl_diff(&mf1
, &mf2
, &xpp
, &xecfg
, &ecb
);
2222 SHA1_Final(sha1
, &ctx
);
2226 int diff_flush_patch_id(struct diff_options
*options
, unsigned char *sha1
)
2228 struct diff_queue_struct
*q
= &diff_queued_diff
;
2230 int result
= diff_get_patch_id(options
, sha1
);
2232 for (i
= 0; i
< q
->nr
; i
++)
2233 diff_free_filepair(q
->queue
[i
]);
2237 q
->nr
= q
->alloc
= 0;
2242 static int is_summary_empty(const struct diff_queue_struct
*q
)
2246 for (i
= 0; i
< q
->nr
; i
++) {
2247 const struct diff_filepair
*p
= q
->queue
[i
];
2249 switch (p
->status
) {
2250 case DIFF_STATUS_DELETED
:
2251 case DIFF_STATUS_ADDED
:
2252 case DIFF_STATUS_COPIED
:
2253 case DIFF_STATUS_RENAMED
:
2258 if (p
->one
->mode
&& p
->two
->mode
&&
2259 p
->one
->mode
!= p
->two
->mode
)
2267 void diff_flush(struct diff_options
*options
)
2269 struct diff_queue_struct
*q
= &diff_queued_diff
;
2270 int i
, output_format
= options
->output_format
;
2274 * Order: raw, stat, summary, patch
2275 * or: name/name-status/checkdiff (other bits clear)
2280 if (output_format
& (DIFF_FORMAT_RAW
|
2282 DIFF_FORMAT_NAME_STATUS
|
2283 DIFF_FORMAT_CHECKDIFF
)) {
2284 for (i
= 0; i
< q
->nr
; i
++) {
2285 struct diff_filepair
*p
= q
->queue
[i
];
2286 if (check_pair_status(p
))
2287 flush_one_pair(p
, options
);
2292 if (output_format
& DIFF_FORMAT_DIFFSTAT
) {
2293 struct diffstat_t diffstat
;
2295 memset(&diffstat
, 0, sizeof(struct diffstat_t
));
2296 diffstat
.xm
.consume
= diffstat_consume
;
2297 for (i
= 0; i
< q
->nr
; i
++) {
2298 struct diff_filepair
*p
= q
->queue
[i
];
2299 if (check_pair_status(p
))
2300 diff_flush_stat(p
, options
, &diffstat
);
2302 show_stats(&diffstat
);
2306 if (output_format
& DIFF_FORMAT_SUMMARY
&& !is_summary_empty(q
)) {
2307 for (i
= 0; i
< q
->nr
; i
++)
2308 diff_summary(q
->queue
[i
]);
2312 if (output_format
& DIFF_FORMAT_PATCH
) {
2314 if (options
->stat_sep
) {
2315 /* attach patch instead of inline */
2316 fputs(options
->stat_sep
, stdout
);
2318 putchar(options
->line_termination
);
2322 for (i
= 0; i
< q
->nr
; i
++) {
2323 struct diff_filepair
*p
= q
->queue
[i
];
2324 if (check_pair_status(p
))
2325 diff_flush_patch(p
, options
);
2329 for (i
= 0; i
< q
->nr
; i
++)
2330 diff_free_filepair(q
->queue
[i
]);
2334 q
->nr
= q
->alloc
= 0;
2337 static void diffcore_apply_filter(const char *filter
)
2340 struct diff_queue_struct
*q
= &diff_queued_diff
;
2341 struct diff_queue_struct outq
;
2343 outq
.nr
= outq
.alloc
= 0;
2348 if (strchr(filter
, DIFF_STATUS_FILTER_AON
)) {
2350 for (i
= found
= 0; !found
&& i
< q
->nr
; i
++) {
2351 struct diff_filepair
*p
= q
->queue
[i
];
2352 if (((p
->status
== DIFF_STATUS_MODIFIED
) &&
2354 strchr(filter
, DIFF_STATUS_FILTER_BROKEN
)) ||
2356 strchr(filter
, DIFF_STATUS_MODIFIED
)))) ||
2357 ((p
->status
!= DIFF_STATUS_MODIFIED
) &&
2358 strchr(filter
, p
->status
)))
2364 /* otherwise we will clear the whole queue
2365 * by copying the empty outq at the end of this
2366 * function, but first clear the current entries
2369 for (i
= 0; i
< q
->nr
; i
++)
2370 diff_free_filepair(q
->queue
[i
]);
2373 /* Only the matching ones */
2374 for (i
= 0; i
< q
->nr
; i
++) {
2375 struct diff_filepair
*p
= q
->queue
[i
];
2377 if (((p
->status
== DIFF_STATUS_MODIFIED
) &&
2379 strchr(filter
, DIFF_STATUS_FILTER_BROKEN
)) ||
2381 strchr(filter
, DIFF_STATUS_MODIFIED
)))) ||
2382 ((p
->status
!= DIFF_STATUS_MODIFIED
) &&
2383 strchr(filter
, p
->status
)))
2386 diff_free_filepair(p
);
2393 void diffcore_std(struct diff_options
*options
)
2395 if (options
->break_opt
!= -1)
2396 diffcore_break(options
->break_opt
);
2397 if (options
->detect_rename
)
2398 diffcore_rename(options
);
2399 if (options
->break_opt
!= -1)
2400 diffcore_merge_broken();
2401 if (options
->pickaxe
)
2402 diffcore_pickaxe(options
->pickaxe
, options
->pickaxe_opts
);
2403 if (options
->orderfile
)
2404 diffcore_order(options
->orderfile
);
2405 diff_resolve_rename_copy();
2406 diffcore_apply_filter(options
->filter
);
2410 void diffcore_std_no_resolve(struct diff_options
*options
)
2412 if (options
->pickaxe
)
2413 diffcore_pickaxe(options
->pickaxe
, options
->pickaxe_opts
);
2414 if (options
->orderfile
)
2415 diffcore_order(options
->orderfile
);
2416 diffcore_apply_filter(options
->filter
);
2419 void diff_addremove(struct diff_options
*options
,
2420 int addremove
, unsigned mode
,
2421 const unsigned char *sha1
,
2422 const char *base
, const char *path
)
2424 char concatpath
[PATH_MAX
];
2425 struct diff_filespec
*one
, *two
;
2427 /* This may look odd, but it is a preparation for
2428 * feeding "there are unchanged files which should
2429 * not produce diffs, but when you are doing copy
2430 * detection you would need them, so here they are"
2431 * entries to the diff-core. They will be prefixed
2432 * with something like '=' or '*' (I haven't decided
2433 * which but should not make any difference).
2434 * Feeding the same new and old to diff_change()
2435 * also has the same effect.
2436 * Before the final output happens, they are pruned after
2437 * merged into rename/copy pairs as appropriate.
2439 if (options
->reverse_diff
)
2440 addremove
= (addremove
== '+' ? '-' :
2441 addremove
== '-' ? '+' : addremove
);
2443 if (!path
) path
= "";
2444 sprintf(concatpath
, "%s%s", base
, path
);
2445 one
= alloc_filespec(concatpath
);
2446 two
= alloc_filespec(concatpath
);
2448 if (addremove
!= '+')
2449 fill_filespec(one
, sha1
, mode
);
2450 if (addremove
!= '-')
2451 fill_filespec(two
, sha1
, mode
);
2453 diff_queue(&diff_queued_diff
, one
, two
);
2456 void diff_change(struct diff_options
*options
,
2457 unsigned old_mode
, unsigned new_mode
,
2458 const unsigned char *old_sha1
,
2459 const unsigned char *new_sha1
,
2460 const char *base
, const char *path
)
2462 char concatpath
[PATH_MAX
];
2463 struct diff_filespec
*one
, *two
;
2465 if (options
->reverse_diff
) {
2467 const unsigned char *tmp_c
;
2468 tmp
= old_mode
; old_mode
= new_mode
; new_mode
= tmp
;
2469 tmp_c
= old_sha1
; old_sha1
= new_sha1
; new_sha1
= tmp_c
;
2471 if (!path
) path
= "";
2472 sprintf(concatpath
, "%s%s", base
, path
);
2473 one
= alloc_filespec(concatpath
);
2474 two
= alloc_filespec(concatpath
);
2475 fill_filespec(one
, old_sha1
, old_mode
);
2476 fill_filespec(two
, new_sha1
, new_mode
);
2478 diff_queue(&diff_queued_diff
, one
, two
);
2481 void diff_unmerge(struct diff_options
*options
,
2484 struct diff_filespec
*one
, *two
;
2485 one
= alloc_filespec(path
);
2486 two
= alloc_filespec(path
);
2487 diff_queue(&diff_queued_diff
, one
, two
);