2 * Copyright (C) 2005 Junio C Hamano
11 #include "xdiff-interface.h"
13 static int use_size_cache
;
15 int diff_rename_limit_default
= -1;
17 int git_diff_config(const char *var
, const char *value
)
19 if (!strcmp(var
, "diff.renamelimit")) {
20 diff_rename_limit_default
= git_config_int(var
, value
);
24 return git_default_config(var
, value
);
27 static char *quote_one(const char *str
)
34 needlen
= quote_c_style(str
, NULL
, NULL
, 0);
37 xp
= xmalloc(needlen
+ 1);
38 quote_c_style(str
, xp
, NULL
, 0);
42 static char *quote_two(const char *one
, const char *two
)
44 int need_one
= quote_c_style(one
, NULL
, NULL
, 1);
45 int need_two
= quote_c_style(two
, NULL
, NULL
, 1);
48 if (need_one
+ need_two
) {
49 if (!need_one
) need_one
= strlen(one
);
50 if (!need_two
) need_one
= strlen(two
);
52 xp
= xmalloc(need_one
+ need_two
+ 3);
54 quote_c_style(one
, xp
+ 1, NULL
, 1);
55 quote_c_style(two
, xp
+ need_one
+ 1, NULL
, 1);
56 strcpy(xp
+ need_one
+ need_two
+ 1, "\"");
59 need_one
= strlen(one
);
60 need_two
= strlen(two
);
61 xp
= xmalloc(need_one
+ need_two
+ 1);
63 strcpy(xp
+ need_one
, two
);
67 static const char *external_diff(void)
69 static const char *external_diff_cmd
= NULL
;
70 static int done_preparing
= 0;
73 return external_diff_cmd
;
74 external_diff_cmd
= getenv("GIT_EXTERNAL_DIFF");
76 return external_diff_cmd
;
79 #define TEMPFILE_PATH_LEN 50
81 static struct diff_tempfile
{
82 const char *name
; /* filename external diff should read from */
85 char tmp_path
[TEMPFILE_PATH_LEN
];
88 static int count_lines(const char *data
, int size
)
90 int count
, ch
, completely_empty
= 1, nl_just_seen
= 0;
101 completely_empty
= 0;
104 if (completely_empty
)
107 count
++; /* no trailing newline */
111 static void print_line_count(int count
)
121 printf("1,%d", count
);
126 static void copy_file(int prefix
, const char *data
, int size
)
128 int ch
, nl_just_seen
= 1;
140 printf("\n\\ No newline at end of file\n");
143 static void emit_rewrite_diff(const char *name_a
,
145 struct diff_filespec
*one
,
146 struct diff_filespec
*two
)
149 diff_populate_filespec(one
, 0);
150 diff_populate_filespec(two
, 0);
151 lc_a
= count_lines(one
->data
, one
->size
);
152 lc_b
= count_lines(two
->data
, two
->size
);
153 printf("--- %s\n+++ %s\n@@ -", name_a
, name_b
);
154 print_line_count(lc_a
);
156 print_line_count(lc_b
);
159 copy_file('-', one
->data
, one
->size
);
161 copy_file('+', two
->data
, two
->size
);
164 static int fill_mmfile(mmfile_t
*mf
, struct diff_filespec
*one
)
166 if (!DIFF_FILE_VALID(one
)) {
167 mf
->ptr
= ""; /* does not matter */
171 else if (diff_populate_filespec(one
, 0))
174 mf
->size
= one
->size
;
178 struct emit_callback
{
179 const char **label_path
;
182 static int fn_out(void *priv
, mmbuffer_t
*mb
, int nbuf
)
185 struct emit_callback
*ecbdata
= priv
;
187 if (ecbdata
->label_path
[0]) {
188 printf("--- %s\n", ecbdata
->label_path
[0]);
189 printf("+++ %s\n", ecbdata
->label_path
[1]);
190 ecbdata
->label_path
[0] = ecbdata
->label_path
[1] = NULL
;
192 for (i
= 0; i
< nbuf
; i
++)
193 if (!fwrite(mb
[i
].ptr
, mb
[i
].size
, 1, stdout
))
199 struct xdiff_emit_state xm
;
203 struct diffstat_file
{
205 unsigned int added
, deleted
;
209 static struct diffstat_file
*diffstat_add(struct diffstat_t
*diffstat
,
212 struct diffstat_file
*x
;
213 x
= xcalloc(sizeof (*x
), 1);
214 if (diffstat
->nr
== diffstat
->alloc
) {
215 diffstat
->alloc
= alloc_nr(diffstat
->alloc
);
216 diffstat
->files
= xrealloc(diffstat
->files
,
217 diffstat
->alloc
* sizeof(x
));
219 diffstat
->files
[diffstat
->nr
++] = x
;
220 x
->name
= strdup(name
);
224 static void diffstat_consume(void *priv
, char *line
, unsigned long len
)
226 struct diffstat_t
*diffstat
= priv
;
227 struct diffstat_file
*x
= diffstat
->files
[diffstat
->nr
- 1];
231 else if (line
[0] == '-')
235 static const char pluses
[] = "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++";
236 static const char minuses
[]= "----------------------------------------------------------------------";
238 static void show_stats(struct diffstat_t
* data
)
241 int i
, len
, add
, del
, total
, adds
= 0, dels
= 0;
242 int max
, max_change
= 0, max_len
= 0;
243 int total_files
= data
->nr
;
250 for (i
= 0; i
< data
->nr
; i
++) {
251 struct diffstat_file
*file
= data
->files
[i
];
253 if (max_change
< file
->added
+ file
->deleted
)
254 max_change
= file
->added
+ file
->deleted
;
255 len
= strlen(file
->name
);
260 for (i
= 0; i
< data
->nr
; i
++) {
261 char *name
= data
->files
[i
]->name
;
262 int added
= data
->files
[i
]->added
;
263 int deleted
= data
->files
[i
]->deleted
;
265 if (0 < (len
= quote_c_style(name
, NULL
, NULL
, 0))) {
266 char *qname
= xmalloc(len
+ 1);
267 quote_c_style(name
, qname
, NULL
, 0);
269 data
->files
[i
]->name
= name
= qname
;
273 * "scale" the filename
284 slash
= strchr(name
, '/');
291 * scale the add/delete
299 printf(" %s%-*s | Bin\n", prefix
, len
, name
);
300 goto free_diffstat_file
;
301 } else if (added
+ deleted
== 0) {
303 goto free_diffstat_file
;
312 if (max_change
> 0) {
313 total
= (total
* max
+ max_change
/ 2) / max_change
;
314 add
= (add
* max
+ max_change
/ 2) / max_change
;
317 printf(" %s%-*s |%5d %.*s%.*s\n", prefix
,
318 len
, name
, added
+ deleted
,
319 add
, pluses
, del
, minuses
);
321 free(data
->files
[i
]->name
);
322 free(data
->files
[i
]);
325 printf(" %d files changed, %d insertions(+), %d deletions(-)\n",
326 total_files
, adds
, dels
);
329 #define FIRST_FEW_BYTES 8000
330 static int mmfile_is_binary(mmfile_t
*mf
)
333 if (FIRST_FEW_BYTES
< sz
)
334 sz
= FIRST_FEW_BYTES
;
335 if (memchr(mf
->ptr
, 0, sz
))
340 static void builtin_diff(const char *name_a
,
342 struct diff_filespec
*one
,
343 struct diff_filespec
*two
,
344 const char *xfrm_msg
,
345 int complete_rewrite
)
351 a_one
= quote_two("a/", name_a
);
352 b_two
= quote_two("b/", name_b
);
353 lbl
[0] = DIFF_FILE_VALID(one
) ? a_one
: "/dev/null";
354 lbl
[1] = DIFF_FILE_VALID(two
) ? b_two
: "/dev/null";
355 printf("diff --git %s %s\n", a_one
, b_two
);
356 if (lbl
[0][0] == '/') {
358 printf("new file mode %06o\n", two
->mode
);
359 if (xfrm_msg
&& xfrm_msg
[0])
362 else if (lbl
[1][0] == '/') {
363 printf("deleted file mode %06o\n", one
->mode
);
364 if (xfrm_msg
&& xfrm_msg
[0])
368 if (one
->mode
!= two
->mode
) {
369 printf("old mode %06o\n", one
->mode
);
370 printf("new mode %06o\n", two
->mode
);
372 if (xfrm_msg
&& xfrm_msg
[0])
375 * we do not run diff between different kind
378 if ((one
->mode
^ two
->mode
) & S_IFMT
)
379 goto free_ab_and_return
;
380 if (complete_rewrite
) {
381 emit_rewrite_diff(name_a
, name_b
, one
, two
);
382 goto free_ab_and_return
;
386 if (fill_mmfile(&mf1
, one
) < 0 || fill_mmfile(&mf2
, two
) < 0)
387 die("unable to read files to diff");
389 if (mmfile_is_binary(&mf1
) || mmfile_is_binary(&mf2
))
390 printf("Binary files %s and %s differ\n", lbl
[0], lbl
[1]);
392 /* Crazy xdl interfaces.. */
393 const char *diffopts
= getenv("GIT_DIFF_OPTS");
397 struct emit_callback ecbdata
;
399 ecbdata
.label_path
= lbl
;
400 xpp
.flags
= XDF_NEED_MINIMAL
;
402 xecfg
.flags
= XDL_EMIT_FUNCNAMES
;
405 else if (!strncmp(diffopts
, "--unified=", 10))
406 xecfg
.ctxlen
= strtoul(diffopts
+ 10, NULL
, 10);
407 else if (!strncmp(diffopts
, "-u", 2))
408 xecfg
.ctxlen
= strtoul(diffopts
+ 2, NULL
, 10);
411 xdl_diff(&mf1
, &mf2
, &xpp
, &xecfg
, &ecb
);
420 static void builtin_diffstat(const char *name_a
, const char *name_b
,
421 struct diff_filespec
*one
, struct diff_filespec
*two
,
422 struct diffstat_t
*diffstat
)
425 struct diffstat_file
*data
;
427 data
= diffstat_add(diffstat
, name_a
? name_a
: name_b
);
429 if (fill_mmfile(&mf1
, one
) < 0 || fill_mmfile(&mf2
, two
) < 0)
430 die("unable to read files to diff");
432 if (mmfile_is_binary(&mf1
) || mmfile_is_binary(&mf2
))
435 /* Crazy xdl interfaces.. */
440 xpp
.flags
= XDF_NEED_MINIMAL
;
443 ecb
.outf
= xdiff_outf
;
445 xdl_diff(&mf1
, &mf2
, &xpp
, &xecfg
, &ecb
);
449 struct diff_filespec
*alloc_filespec(const char *path
)
451 int namelen
= strlen(path
);
452 struct diff_filespec
*spec
= xmalloc(sizeof(*spec
) + namelen
+ 1);
454 memset(spec
, 0, sizeof(*spec
));
455 spec
->path
= (char *)(spec
+ 1);
456 memcpy(spec
->path
, path
, namelen
+1);
460 void fill_filespec(struct diff_filespec
*spec
, const unsigned char *sha1
,
464 spec
->mode
= canon_mode(mode
);
465 memcpy(spec
->sha1
, sha1
, 20);
466 spec
->sha1_valid
= !!memcmp(sha1
, null_sha1
, 20);
471 * Given a name and sha1 pair, if the dircache tells us the file in
472 * the work tree has that object contents, return true, so that
473 * prepare_temp_file() does not have to inflate and extract.
475 static int work_tree_matches(const char *name
, const unsigned char *sha1
)
477 struct cache_entry
*ce
;
481 /* We do not read the cache ourselves here, because the
482 * benchmark with my previous version that always reads cache
483 * shows that it makes things worse for diff-tree comparing
484 * two linux-2.6 kernel trees in an already checked out work
485 * tree. This is because most diff-tree comparisons deal with
486 * only a small number of files, while reading the cache is
487 * expensive for a large project, and its cost outweighs the
488 * savings we get by not inflating the object to a temporary
489 * file. Practically, this code only helps when we are used
490 * by diff-cache --cached, which does read the cache before
497 pos
= cache_name_pos(name
, len
);
500 ce
= active_cache
[pos
];
501 if ((lstat(name
, &st
) < 0) ||
502 !S_ISREG(st
.st_mode
) || /* careful! */
503 ce_match_stat(ce
, &st
, 0) ||
504 memcmp(sha1
, ce
->sha1
, 20))
506 /* we return 1 only when we can stat, it is a regular file,
507 * stat information matches, and sha1 recorded in the cache
508 * matches. I.e. we know the file in the work tree really is
509 * the same as the <name, sha1> pair.
514 static struct sha1_size_cache
{
515 unsigned char sha1
[20];
518 static int sha1_size_cache_nr
, sha1_size_cache_alloc
;
520 static struct sha1_size_cache
*locate_size_cache(unsigned char *sha1
,
525 struct sha1_size_cache
*e
;
528 last
= sha1_size_cache_nr
;
529 while (last
> first
) {
530 int cmp
, next
= (last
+ first
) >> 1;
531 e
= sha1_size_cache
[next
];
532 cmp
= memcmp(e
->sha1
, sha1
, 20);
544 /* insert to make it at "first" */
545 if (sha1_size_cache_alloc
<= sha1_size_cache_nr
) {
546 sha1_size_cache_alloc
= alloc_nr(sha1_size_cache_alloc
);
547 sha1_size_cache
= xrealloc(sha1_size_cache
,
548 sha1_size_cache_alloc
*
549 sizeof(*sha1_size_cache
));
551 sha1_size_cache_nr
++;
552 if (first
< sha1_size_cache_nr
)
553 memmove(sha1_size_cache
+ first
+ 1, sha1_size_cache
+ first
,
554 (sha1_size_cache_nr
- first
- 1) *
555 sizeof(*sha1_size_cache
));
556 e
= xmalloc(sizeof(struct sha1_size_cache
));
557 sha1_size_cache
[first
] = e
;
558 memcpy(e
->sha1
, sha1
, 20);
564 * While doing rename detection and pickaxe operation, we may need to
565 * grab the data for the blob (or file) for our own in-core comparison.
566 * diff_filespec has data and size fields for this purpose.
568 int diff_populate_filespec(struct diff_filespec
*s
, int size_only
)
571 if (!DIFF_FILE_VALID(s
))
572 die("internal error: asking to populate invalid file.");
573 if (S_ISDIR(s
->mode
))
581 if (!s
->sha1_valid
||
582 work_tree_matches(s
->path
, s
->sha1
)) {
585 if (lstat(s
->path
, &st
) < 0) {
586 if (errno
== ENOENT
) {
595 s
->size
= st
.st_size
;
600 if (S_ISLNK(st
.st_mode
)) {
602 s
->data
= xmalloc(s
->size
);
604 ret
= readlink(s
->path
, s
->data
, s
->size
);
611 fd
= open(s
->path
, O_RDONLY
);
614 s
->data
= mmap(NULL
, s
->size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
616 if (s
->data
== MAP_FAILED
)
618 s
->should_munmap
= 1;
622 struct sha1_size_cache
*e
;
625 e
= locate_size_cache(s
->sha1
, 1, 0);
630 if (!sha1_object_info(s
->sha1
, type
, &s
->size
))
631 locate_size_cache(s
->sha1
, 0, s
->size
);
634 s
->data
= read_sha1_file(s
->sha1
, type
, &s
->size
);
641 void diff_free_filespec_data(struct diff_filespec
*s
)
645 else if (s
->should_munmap
)
646 munmap(s
->data
, s
->size
);
647 s
->should_free
= s
->should_munmap
= 0;
653 static void prep_temp_blob(struct diff_tempfile
*temp
,
656 const unsigned char *sha1
,
661 fd
= git_mkstemp(temp
->tmp_path
, TEMPFILE_PATH_LEN
, ".diff_XXXXXX");
663 die("unable to create temp-file");
664 if (write(fd
, blob
, size
) != size
)
665 die("unable to write temp-file");
667 temp
->name
= temp
->tmp_path
;
668 strcpy(temp
->hex
, sha1_to_hex(sha1
));
670 sprintf(temp
->mode
, "%06o", mode
);
673 static void prepare_temp_file(const char *name
,
674 struct diff_tempfile
*temp
,
675 struct diff_filespec
*one
)
677 if (!DIFF_FILE_VALID(one
)) {
679 /* A '-' entry produces this for file-2, and
680 * a '+' entry produces this for file-1.
682 temp
->name
= "/dev/null";
683 strcpy(temp
->hex
, ".");
684 strcpy(temp
->mode
, ".");
688 if (!one
->sha1_valid
||
689 work_tree_matches(name
, one
->sha1
)) {
691 if (lstat(name
, &st
) < 0) {
693 goto not_a_valid_file
;
694 die("stat(%s): %s", name
, strerror(errno
));
696 if (S_ISLNK(st
.st_mode
)) {
698 char buf
[PATH_MAX
+ 1]; /* ought to be SYMLINK_MAX */
699 if (sizeof(buf
) <= st
.st_size
)
700 die("symlink too long: %s", name
);
701 ret
= readlink(name
, buf
, st
.st_size
);
703 die("readlink(%s)", name
);
704 prep_temp_blob(temp
, buf
, st
.st_size
,
706 one
->sha1
: null_sha1
),
708 one
->mode
: S_IFLNK
));
711 /* we can borrow from the file in the work tree */
713 if (!one
->sha1_valid
)
714 strcpy(temp
->hex
, sha1_to_hex(null_sha1
));
716 strcpy(temp
->hex
, sha1_to_hex(one
->sha1
));
717 /* Even though we may sometimes borrow the
718 * contents from the work tree, we always want
719 * one->mode. mode is trustworthy even when
720 * !(one->sha1_valid), as long as
721 * DIFF_FILE_VALID(one).
723 sprintf(temp
->mode
, "%06o", one
->mode
);
728 if (diff_populate_filespec(one
, 0))
729 die("cannot read data blob for %s", one
->path
);
730 prep_temp_blob(temp
, one
->data
, one
->size
,
731 one
->sha1
, one
->mode
);
735 static void remove_tempfile(void)
739 for (i
= 0; i
< 2; i
++)
740 if (diff_temp
[i
].name
== diff_temp
[i
].tmp_path
) {
741 unlink(diff_temp
[i
].name
);
742 diff_temp
[i
].name
= NULL
;
746 static void remove_tempfile_on_signal(int signo
)
749 signal(SIGINT
, SIG_DFL
);
753 static int spawn_prog(const char *pgm
, const char **arg
)
761 die("unable to fork");
763 execvp(pgm
, (char *const*) arg
);
767 while (waitpid(pid
, &status
, 0) < 0) {
773 /* Earlier we did not check the exit status because
774 * diff exits non-zero if files are different, and
775 * we are not interested in knowing that. It was a
776 * mistake which made it harder to quit a diff-*
777 * session that uses the git-apply-patch-script as
778 * the GIT_EXTERNAL_DIFF. A custom GIT_EXTERNAL_DIFF
779 * should also exit non-zero only when it wants to
780 * abort the entire diff-* session.
782 if (WIFEXITED(status
) && !WEXITSTATUS(status
))
787 /* An external diff command takes:
789 * diff-cmd name infile1 infile1-sha1 infile1-mode \
790 * infile2 infile2-sha1 infile2-mode [ rename-to ]
793 static void run_external_diff(const char *pgm
,
796 struct diff_filespec
*one
,
797 struct diff_filespec
*two
,
798 const char *xfrm_msg
,
799 int complete_rewrite
)
801 const char *spawn_arg
[10];
802 struct diff_tempfile
*temp
= diff_temp
;
804 static int atexit_asked
= 0;
805 const char *othername
;
806 const char **arg
= &spawn_arg
[0];
808 othername
= (other
? other
: name
);
810 prepare_temp_file(name
, &temp
[0], one
);
811 prepare_temp_file(othername
, &temp
[1], two
);
812 if (! atexit_asked
&&
813 (temp
[0].name
== temp
[0].tmp_path
||
814 temp
[1].name
== temp
[1].tmp_path
)) {
816 atexit(remove_tempfile
);
818 signal(SIGINT
, remove_tempfile_on_signal
);
824 *arg
++ = temp
[0].name
;
825 *arg
++ = temp
[0].hex
;
826 *arg
++ = temp
[0].mode
;
827 *arg
++ = temp
[1].name
;
828 *arg
++ = temp
[1].hex
;
829 *arg
++ = temp
[1].mode
;
839 retval
= spawn_prog(pgm
, spawn_arg
);
842 fprintf(stderr
, "external diff died, stopping at %s.\n", name
);
847 static void run_diff_cmd(const char *pgm
,
850 struct diff_filespec
*one
,
851 struct diff_filespec
*two
,
852 const char *xfrm_msg
,
853 int complete_rewrite
)
856 run_external_diff(pgm
, name
, other
, one
, two
, xfrm_msg
,
861 builtin_diff(name
, other
? other
: name
,
862 one
, two
, xfrm_msg
, complete_rewrite
);
864 printf("* Unmerged path %s\n", name
);
867 static void diff_fill_sha1_info(struct diff_filespec
*one
)
869 if (DIFF_FILE_VALID(one
)) {
870 if (!one
->sha1_valid
) {
872 if (lstat(one
->path
, &st
) < 0)
873 die("stat %s", one
->path
);
874 if (index_path(one
->sha1
, one
->path
, &st
, 0))
875 die("cannot hash %s\n", one
->path
);
879 memset(one
->sha1
, 0, 20);
882 static void run_diff(struct diff_filepair
*p
, struct diff_options
*o
)
884 const char *pgm
= external_diff();
885 char msg
[PATH_MAX
*2+300], *xfrm_msg
;
886 struct diff_filespec
*one
;
887 struct diff_filespec
*two
;
890 char *name_munged
, *other_munged
;
891 int complete_rewrite
= 0;
894 if (DIFF_PAIR_UNMERGED(p
)) {
896 run_diff_cmd(pgm
, p
->one
->path
, NULL
, NULL
, NULL
, NULL
, 0);
901 other
= (strcmp(name
, p
->two
->path
) ? p
->two
->path
: NULL
);
902 name_munged
= quote_one(name
);
903 other_munged
= quote_one(other
);
904 one
= p
->one
; two
= p
->two
;
906 diff_fill_sha1_info(one
);
907 diff_fill_sha1_info(two
);
911 case DIFF_STATUS_COPIED
:
912 len
+= snprintf(msg
+ len
, sizeof(msg
) - len
,
913 "similarity index %d%%\n"
916 (int)(0.5 + p
->score
* 100.0/MAX_SCORE
),
917 name_munged
, other_munged
);
919 case DIFF_STATUS_RENAMED
:
920 len
+= snprintf(msg
+ len
, sizeof(msg
) - len
,
921 "similarity index %d%%\n"
924 (int)(0.5 + p
->score
* 100.0/MAX_SCORE
),
925 name_munged
, other_munged
);
927 case DIFF_STATUS_MODIFIED
:
929 len
+= snprintf(msg
+ len
, sizeof(msg
) - len
,
930 "dissimilarity index %d%%\n",
931 (int)(0.5 + p
->score
*
933 complete_rewrite
= 1;
942 if (memcmp(one
->sha1
, two
->sha1
, 20)) {
944 int abbrev
= o
->full_index
? 40 : DEFAULT_ABBREV
;
945 memcpy(one_sha1
, sha1_to_hex(one
->sha1
), 41);
947 len
+= snprintf(msg
+ len
, sizeof(msg
) - len
,
949 abbrev
, one_sha1
, abbrev
,
950 sha1_to_hex(two
->sha1
));
951 if (one
->mode
== two
->mode
)
952 len
+= snprintf(msg
+ len
, sizeof(msg
) - len
,
954 len
+= snprintf(msg
+ len
, sizeof(msg
) - len
, "\n");
959 xfrm_msg
= len
? msg
: NULL
;
962 DIFF_FILE_VALID(one
) && DIFF_FILE_VALID(two
) &&
963 (S_IFMT
& one
->mode
) != (S_IFMT
& two
->mode
)) {
964 /* a filepair that changes between file and symlink
965 * needs to be split into deletion and creation.
967 struct diff_filespec
*null
= alloc_filespec(two
->path
);
968 run_diff_cmd(NULL
, name
, other
, one
, null
, xfrm_msg
, 0);
970 null
= alloc_filespec(one
->path
);
971 run_diff_cmd(NULL
, name
, other
, null
, two
, xfrm_msg
, 0);
975 run_diff_cmd(pgm
, name
, other
, one
, two
, xfrm_msg
,
982 static void run_diffstat(struct diff_filepair
*p
, struct diff_options
*o
,
983 struct diffstat_t
*diffstat
)
988 if (DIFF_PAIR_UNMERGED(p
)) {
990 builtin_diffstat(p
->one
->path
, NULL
, NULL
, NULL
, diffstat
);
995 other
= (strcmp(name
, p
->two
->path
) ? p
->two
->path
: NULL
);
997 diff_fill_sha1_info(p
->one
);
998 diff_fill_sha1_info(p
->two
);
1000 builtin_diffstat(name
, other
, p
->one
, p
->two
, diffstat
);
1003 void diff_setup(struct diff_options
*options
)
1005 memset(options
, 0, sizeof(*options
));
1006 options
->output_format
= DIFF_FORMAT_RAW
;
1007 options
->line_termination
= '\n';
1008 options
->break_opt
= -1;
1009 options
->rename_limit
= -1;
1011 options
->change
= diff_change
;
1012 options
->add_remove
= diff_addremove
;
1015 int diff_setup_done(struct diff_options
*options
)
1017 if ((options
->find_copies_harder
&&
1018 options
->detect_rename
!= DIFF_DETECT_COPY
) ||
1019 (0 <= options
->rename_limit
&& !options
->detect_rename
))
1021 if (options
->detect_rename
&& options
->rename_limit
< 0)
1022 options
->rename_limit
= diff_rename_limit_default
;
1023 if (options
->setup
& DIFF_SETUP_USE_CACHE
) {
1025 /* read-cache does not die even when it fails
1026 * so it is safe for us to do this here. Also
1027 * it does not smudge active_cache or active_nr
1028 * when it fails, so we do not have to worry about
1029 * cleaning it up ourselves either.
1033 if (options
->setup
& DIFF_SETUP_USE_SIZE_CACHE
)
1035 if (options
->abbrev
<= 0 || 40 < options
->abbrev
)
1036 options
->abbrev
= 40; /* full */
1041 int diff_opt_parse(struct diff_options
*options
, const char **av
, int ac
)
1043 const char *arg
= av
[0];
1044 if (!strcmp(arg
, "-p") || !strcmp(arg
, "-u"))
1045 options
->output_format
= DIFF_FORMAT_PATCH
;
1046 else if (!strcmp(arg
, "--patch-with-raw")) {
1047 options
->output_format
= DIFF_FORMAT_PATCH
;
1048 options
->with_raw
= 1;
1050 else if (!strcmp(arg
, "--stat"))
1051 options
->output_format
= DIFF_FORMAT_DIFFSTAT
;
1052 else if (!strcmp(arg
, "-z"))
1053 options
->line_termination
= 0;
1054 else if (!strncmp(arg
, "-l", 2))
1055 options
->rename_limit
= strtoul(arg
+2, NULL
, 10);
1056 else if (!strcmp(arg
, "--full-index"))
1057 options
->full_index
= 1;
1058 else if (!strcmp(arg
, "--name-only"))
1059 options
->output_format
= DIFF_FORMAT_NAME
;
1060 else if (!strcmp(arg
, "--name-status"))
1061 options
->output_format
= DIFF_FORMAT_NAME_STATUS
;
1062 else if (!strcmp(arg
, "-R"))
1063 options
->reverse_diff
= 1;
1064 else if (!strncmp(arg
, "-S", 2))
1065 options
->pickaxe
= arg
+ 2;
1066 else if (!strcmp(arg
, "-s"))
1067 options
->output_format
= DIFF_FORMAT_NO_OUTPUT
;
1068 else if (!strncmp(arg
, "-O", 2))
1069 options
->orderfile
= arg
+ 2;
1070 else if (!strncmp(arg
, "--diff-filter=", 14))
1071 options
->filter
= arg
+ 14;
1072 else if (!strcmp(arg
, "--pickaxe-all"))
1073 options
->pickaxe_opts
= DIFF_PICKAXE_ALL
;
1074 else if (!strcmp(arg
, "--pickaxe-regex"))
1075 options
->pickaxe_opts
= DIFF_PICKAXE_REGEX
;
1076 else if (!strncmp(arg
, "-B", 2)) {
1077 if ((options
->break_opt
=
1078 diff_scoreopt_parse(arg
)) == -1)
1081 else if (!strncmp(arg
, "-M", 2)) {
1082 if ((options
->rename_score
=
1083 diff_scoreopt_parse(arg
)) == -1)
1085 options
->detect_rename
= DIFF_DETECT_RENAME
;
1087 else if (!strncmp(arg
, "-C", 2)) {
1088 if ((options
->rename_score
=
1089 diff_scoreopt_parse(arg
)) == -1)
1091 options
->detect_rename
= DIFF_DETECT_COPY
;
1093 else if (!strcmp(arg
, "--find-copies-harder"))
1094 options
->find_copies_harder
= 1;
1095 else if (!strcmp(arg
, "--abbrev"))
1096 options
->abbrev
= DEFAULT_ABBREV
;
1097 else if (!strncmp(arg
, "--abbrev=", 9)) {
1098 options
->abbrev
= strtoul(arg
+ 9, NULL
, 10);
1099 if (options
->abbrev
< MINIMUM_ABBREV
)
1100 options
->abbrev
= MINIMUM_ABBREV
;
1101 else if (40 < options
->abbrev
)
1102 options
->abbrev
= 40;
1109 static int parse_num(const char **cp_p
)
1111 unsigned long num
, scale
;
1113 const char *cp
= *cp_p
;
1120 if ( !dot
&& ch
== '.' ) {
1123 } else if ( ch
== '%' ) {
1124 scale
= dot
? scale
*100 : 100;
1125 cp
++; /* % is always at the end */
1127 } else if ( ch
>= '0' && ch
<= '9' ) {
1128 if ( scale
< 100000 ) {
1130 num
= (num
*10) + (ch
-'0');
1139 /* user says num divided by scale and we say internally that
1140 * is MAX_SCORE * num / scale.
1142 return (num
>= scale
) ? MAX_SCORE
: (MAX_SCORE
* num
/ scale
);
1145 int diff_scoreopt_parse(const char *opt
)
1147 int opt1
, opt2
, cmd
;
1152 if (cmd
!= 'M' && cmd
!= 'C' && cmd
!= 'B')
1153 return -1; /* that is not a -M, -C nor -B option */
1155 opt1
= parse_num(&opt
);
1161 else if (*opt
!= '/')
1162 return -1; /* we expect -B80/99 or -B80 */
1165 opt2
= parse_num(&opt
);
1170 return opt1
| (opt2
<< 16);
1173 struct diff_queue_struct diff_queued_diff
;
1175 void diff_q(struct diff_queue_struct
*queue
, struct diff_filepair
*dp
)
1177 if (queue
->alloc
<= queue
->nr
) {
1178 queue
->alloc
= alloc_nr(queue
->alloc
);
1179 queue
->queue
= xrealloc(queue
->queue
,
1180 sizeof(dp
) * queue
->alloc
);
1182 queue
->queue
[queue
->nr
++] = dp
;
1185 struct diff_filepair
*diff_queue(struct diff_queue_struct
*queue
,
1186 struct diff_filespec
*one
,
1187 struct diff_filespec
*two
)
1189 struct diff_filepair
*dp
= xmalloc(sizeof(*dp
));
1194 dp
->source_stays
= 0;
1195 dp
->broken_pair
= 0;
1201 void diff_free_filepair(struct diff_filepair
*p
)
1203 diff_free_filespec_data(p
->one
);
1204 diff_free_filespec_data(p
->two
);
1210 /* This is different from find_unique_abbrev() in that
1211 * it stuffs the result with dots for alignment.
1213 const char *diff_unique_abbrev(const unsigned char *sha1
, int len
)
1218 return sha1_to_hex(sha1
);
1220 abbrev
= find_unique_abbrev(sha1
, len
);
1222 return sha1_to_hex(sha1
);
1223 abblen
= strlen(abbrev
);
1225 static char hex
[41];
1226 if (len
< abblen
&& abblen
<= len
+ 2)
1227 sprintf(hex
, "%s%.*s", abbrev
, len
+3-abblen
, "..");
1229 sprintf(hex
, "%s...", abbrev
);
1232 return sha1_to_hex(sha1
);
1235 static void diff_flush_raw(struct diff_filepair
*p
,
1236 int line_termination
,
1237 int inter_name_termination
,
1238 struct diff_options
*options
,
1243 int abbrev
= options
->abbrev
;
1244 const char *path_one
, *path_two
;
1246 path_one
= p
->one
->path
;
1247 path_two
= p
->two
->path
;
1248 if (line_termination
) {
1249 path_one
= quote_one(path_one
);
1250 path_two
= quote_one(path_two
);
1254 sprintf(status
, "%c%03d", p
->status
,
1255 (int)(0.5 + p
->score
* 100.0/MAX_SCORE
));
1257 status
[0] = p
->status
;
1260 switch (p
->status
) {
1261 case DIFF_STATUS_COPIED
:
1262 case DIFF_STATUS_RENAMED
:
1265 case DIFF_STATUS_ADDED
:
1266 case DIFF_STATUS_DELETED
:
1273 if (output_format
!= DIFF_FORMAT_NAME_STATUS
) {
1274 printf(":%06o %06o %s ",
1275 p
->one
->mode
, p
->two
->mode
,
1276 diff_unique_abbrev(p
->one
->sha1
, abbrev
));
1278 diff_unique_abbrev(p
->two
->sha1
, abbrev
));
1280 printf("%s%c%s", status
, inter_name_termination
, path_one
);
1282 printf("%c%s", inter_name_termination
, path_two
);
1283 putchar(line_termination
);
1284 if (path_one
!= p
->one
->path
)
1285 free((void*)path_one
);
1286 if (path_two
!= p
->two
->path
)
1287 free((void*)path_two
);
1290 static void diff_flush_name(struct diff_filepair
*p
,
1291 int inter_name_termination
,
1292 int line_termination
)
1294 char *path
= p
->two
->path
;
1296 if (line_termination
)
1297 path
= quote_one(p
->two
->path
);
1299 path
= p
->two
->path
;
1300 printf("%s%c", path
, line_termination
);
1301 if (p
->two
->path
!= path
)
1305 int diff_unmodified_pair(struct diff_filepair
*p
)
1307 /* This function is written stricter than necessary to support
1308 * the currently implemented transformers, but the idea is to
1309 * let transformers to produce diff_filepairs any way they want,
1310 * and filter and clean them up here before producing the output.
1312 struct diff_filespec
*one
, *two
;
1314 if (DIFF_PAIR_UNMERGED(p
))
1315 return 0; /* unmerged is interesting */
1320 /* deletion, addition, mode or type change
1321 * and rename are all interesting.
1323 if (DIFF_FILE_VALID(one
) != DIFF_FILE_VALID(two
) ||
1324 DIFF_PAIR_MODE_CHANGED(p
) ||
1325 strcmp(one
->path
, two
->path
))
1328 /* both are valid and point at the same path. that is, we are
1329 * dealing with a change.
1331 if (one
->sha1_valid
&& two
->sha1_valid
&&
1332 !memcmp(one
->sha1
, two
->sha1
, sizeof(one
->sha1
)))
1333 return 1; /* no change */
1334 if (!one
->sha1_valid
&& !two
->sha1_valid
)
1335 return 1; /* both look at the same file on the filesystem. */
1339 static void diff_flush_patch(struct diff_filepair
*p
, struct diff_options
*o
)
1341 if (diff_unmodified_pair(p
))
1344 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
1345 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
1346 return; /* no tree diffs in patch format */
1351 static void diff_flush_stat(struct diff_filepair
*p
, struct diff_options
*o
,
1352 struct diffstat_t
*diffstat
)
1354 if (diff_unmodified_pair(p
))
1357 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
1358 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
1359 return; /* no tree diffs in patch format */
1361 run_diffstat(p
, o
, diffstat
);
1364 int diff_queue_is_empty(void)
1366 struct diff_queue_struct
*q
= &diff_queued_diff
;
1368 for (i
= 0; i
< q
->nr
; i
++)
1369 if (!diff_unmodified_pair(q
->queue
[i
]))
1375 void diff_debug_filespec(struct diff_filespec
*s
, int x
, const char *one
)
1377 fprintf(stderr
, "queue[%d] %s (%s) %s %06o %s\n",
1380 DIFF_FILE_VALID(s
) ? "valid" : "invalid",
1382 s
->sha1_valid
? sha1_to_hex(s
->sha1
) : "");
1383 fprintf(stderr
, "queue[%d] %s size %lu flags %d\n",
1385 s
->size
, s
->xfrm_flags
);
1388 void diff_debug_filepair(const struct diff_filepair
*p
, int i
)
1390 diff_debug_filespec(p
->one
, i
, "one");
1391 diff_debug_filespec(p
->two
, i
, "two");
1392 fprintf(stderr
, "score %d, status %c stays %d broken %d\n",
1393 p
->score
, p
->status
? p
->status
: '?',
1394 p
->source_stays
, p
->broken_pair
);
1397 void diff_debug_queue(const char *msg
, struct diff_queue_struct
*q
)
1401 fprintf(stderr
, "%s\n", msg
);
1402 fprintf(stderr
, "q->nr = %d\n", q
->nr
);
1403 for (i
= 0; i
< q
->nr
; i
++) {
1404 struct diff_filepair
*p
= q
->queue
[i
];
1405 diff_debug_filepair(p
, i
);
1410 static void diff_resolve_rename_copy(void)
1413 struct diff_filepair
*p
, *pp
;
1414 struct diff_queue_struct
*q
= &diff_queued_diff
;
1416 diff_debug_queue("resolve-rename-copy", q
);
1418 for (i
= 0; i
< q
->nr
; i
++) {
1420 p
->status
= 0; /* undecided */
1421 if (DIFF_PAIR_UNMERGED(p
))
1422 p
->status
= DIFF_STATUS_UNMERGED
;
1423 else if (!DIFF_FILE_VALID(p
->one
))
1424 p
->status
= DIFF_STATUS_ADDED
;
1425 else if (!DIFF_FILE_VALID(p
->two
))
1426 p
->status
= DIFF_STATUS_DELETED
;
1427 else if (DIFF_PAIR_TYPE_CHANGED(p
))
1428 p
->status
= DIFF_STATUS_TYPE_CHANGED
;
1430 /* from this point on, we are dealing with a pair
1431 * whose both sides are valid and of the same type, i.e.
1432 * either in-place edit or rename/copy edit.
1434 else if (DIFF_PAIR_RENAME(p
)) {
1435 if (p
->source_stays
) {
1436 p
->status
= DIFF_STATUS_COPIED
;
1439 /* See if there is some other filepair that
1440 * copies from the same source as us. If so
1441 * we are a copy. Otherwise we are either a
1442 * copy if the path stays, or a rename if it
1443 * does not, but we already handled "stays" case.
1445 for (j
= i
+ 1; j
< q
->nr
; j
++) {
1447 if (strcmp(pp
->one
->path
, p
->one
->path
))
1448 continue; /* not us */
1449 if (!DIFF_PAIR_RENAME(pp
))
1450 continue; /* not a rename/copy */
1451 /* pp is a rename/copy from the same source */
1452 p
->status
= DIFF_STATUS_COPIED
;
1456 p
->status
= DIFF_STATUS_RENAMED
;
1458 else if (memcmp(p
->one
->sha1
, p
->two
->sha1
, 20) ||
1459 p
->one
->mode
!= p
->two
->mode
)
1460 p
->status
= DIFF_STATUS_MODIFIED
;
1462 /* This is a "no-change" entry and should not
1463 * happen anymore, but prepare for broken callers.
1465 error("feeding unmodified %s to diffcore",
1467 p
->status
= DIFF_STATUS_UNKNOWN
;
1470 diff_debug_queue("resolve-rename-copy done", q
);
1473 static void flush_one_pair(struct diff_filepair
*p
,
1474 int diff_output_format
,
1475 struct diff_options
*options
,
1476 struct diffstat_t
*diffstat
)
1478 int inter_name_termination
= '\t';
1479 int line_termination
= options
->line_termination
;
1480 if (!line_termination
)
1481 inter_name_termination
= 0;
1483 switch (p
->status
) {
1484 case DIFF_STATUS_UNKNOWN
:
1487 die("internal error in diff-resolve-rename-copy");
1490 switch (diff_output_format
) {
1491 case DIFF_FORMAT_DIFFSTAT
:
1492 diff_flush_stat(p
, options
, diffstat
);
1494 case DIFF_FORMAT_PATCH
:
1495 diff_flush_patch(p
, options
);
1497 case DIFF_FORMAT_RAW
:
1498 case DIFF_FORMAT_NAME_STATUS
:
1499 diff_flush_raw(p
, line_termination
,
1500 inter_name_termination
,
1501 options
, diff_output_format
);
1503 case DIFF_FORMAT_NAME
:
1505 inter_name_termination
,
1508 case DIFF_FORMAT_NO_OUTPUT
:
1514 void diff_flush(struct diff_options
*options
)
1516 struct diff_queue_struct
*q
= &diff_queued_diff
;
1518 int diff_output_format
= options
->output_format
;
1519 struct diffstat_t
*diffstat
= NULL
;
1521 if (diff_output_format
== DIFF_FORMAT_DIFFSTAT
) {
1522 diffstat
= xcalloc(sizeof (struct diffstat_t
), 1);
1523 diffstat
->xm
.consume
= diffstat_consume
;
1526 if (options
->with_raw
) {
1527 for (i
= 0; i
< q
->nr
; i
++) {
1528 struct diff_filepair
*p
= q
->queue
[i
];
1529 flush_one_pair(p
, DIFF_FORMAT_RAW
, options
, NULL
);
1531 putchar(options
->line_termination
);
1533 for (i
= 0; i
< q
->nr
; i
++) {
1534 struct diff_filepair
*p
= q
->queue
[i
];
1535 flush_one_pair(p
, diff_output_format
, options
, diffstat
);
1536 diff_free_filepair(p
);
1540 show_stats(diffstat
);
1546 q
->nr
= q
->alloc
= 0;
1549 static void diffcore_apply_filter(const char *filter
)
1552 struct diff_queue_struct
*q
= &diff_queued_diff
;
1553 struct diff_queue_struct outq
;
1555 outq
.nr
= outq
.alloc
= 0;
1560 if (strchr(filter
, DIFF_STATUS_FILTER_AON
)) {
1562 for (i
= found
= 0; !found
&& i
< q
->nr
; i
++) {
1563 struct diff_filepair
*p
= q
->queue
[i
];
1564 if (((p
->status
== DIFF_STATUS_MODIFIED
) &&
1566 strchr(filter
, DIFF_STATUS_FILTER_BROKEN
)) ||
1568 strchr(filter
, DIFF_STATUS_MODIFIED
)))) ||
1569 ((p
->status
!= DIFF_STATUS_MODIFIED
) &&
1570 strchr(filter
, p
->status
)))
1576 /* otherwise we will clear the whole queue
1577 * by copying the empty outq at the end of this
1578 * function, but first clear the current entries
1581 for (i
= 0; i
< q
->nr
; i
++)
1582 diff_free_filepair(q
->queue
[i
]);
1585 /* Only the matching ones */
1586 for (i
= 0; i
< q
->nr
; i
++) {
1587 struct diff_filepair
*p
= q
->queue
[i
];
1589 if (((p
->status
== DIFF_STATUS_MODIFIED
) &&
1591 strchr(filter
, DIFF_STATUS_FILTER_BROKEN
)) ||
1593 strchr(filter
, DIFF_STATUS_MODIFIED
)))) ||
1594 ((p
->status
!= DIFF_STATUS_MODIFIED
) &&
1595 strchr(filter
, p
->status
)))
1598 diff_free_filepair(p
);
1605 void diffcore_std(struct diff_options
*options
)
1607 if (options
->break_opt
!= -1)
1608 diffcore_break(options
->break_opt
);
1609 if (options
->detect_rename
)
1610 diffcore_rename(options
);
1611 if (options
->break_opt
!= -1)
1612 diffcore_merge_broken();
1613 if (options
->pickaxe
)
1614 diffcore_pickaxe(options
->pickaxe
, options
->pickaxe_opts
);
1615 if (options
->orderfile
)
1616 diffcore_order(options
->orderfile
);
1617 diff_resolve_rename_copy();
1618 diffcore_apply_filter(options
->filter
);
1622 void diffcore_std_no_resolve(struct diff_options
*options
)
1624 if (options
->pickaxe
)
1625 diffcore_pickaxe(options
->pickaxe
, options
->pickaxe_opts
);
1626 if (options
->orderfile
)
1627 diffcore_order(options
->orderfile
);
1628 diffcore_apply_filter(options
->filter
);
1631 void diff_addremove(struct diff_options
*options
,
1632 int addremove
, unsigned mode
,
1633 const unsigned char *sha1
,
1634 const char *base
, const char *path
)
1636 char concatpath
[PATH_MAX
];
1637 struct diff_filespec
*one
, *two
;
1639 /* This may look odd, but it is a preparation for
1640 * feeding "there are unchanged files which should
1641 * not produce diffs, but when you are doing copy
1642 * detection you would need them, so here they are"
1643 * entries to the diff-core. They will be prefixed
1644 * with something like '=' or '*' (I haven't decided
1645 * which but should not make any difference).
1646 * Feeding the same new and old to diff_change()
1647 * also has the same effect.
1648 * Before the final output happens, they are pruned after
1649 * merged into rename/copy pairs as appropriate.
1651 if (options
->reverse_diff
)
1652 addremove
= (addremove
== '+' ? '-' :
1653 addremove
== '-' ? '+' : addremove
);
1655 if (!path
) path
= "";
1656 sprintf(concatpath
, "%s%s", base
, path
);
1657 one
= alloc_filespec(concatpath
);
1658 two
= alloc_filespec(concatpath
);
1660 if (addremove
!= '+')
1661 fill_filespec(one
, sha1
, mode
);
1662 if (addremove
!= '-')
1663 fill_filespec(two
, sha1
, mode
);
1665 diff_queue(&diff_queued_diff
, one
, two
);
1668 void diff_change(struct diff_options
*options
,
1669 unsigned old_mode
, unsigned new_mode
,
1670 const unsigned char *old_sha1
,
1671 const unsigned char *new_sha1
,
1672 const char *base
, const char *path
)
1674 char concatpath
[PATH_MAX
];
1675 struct diff_filespec
*one
, *two
;
1677 if (options
->reverse_diff
) {
1679 const unsigned char *tmp_c
;
1680 tmp
= old_mode
; old_mode
= new_mode
; new_mode
= tmp
;
1681 tmp_c
= old_sha1
; old_sha1
= new_sha1
; new_sha1
= tmp_c
;
1683 if (!path
) path
= "";
1684 sprintf(concatpath
, "%s%s", base
, path
);
1685 one
= alloc_filespec(concatpath
);
1686 two
= alloc_filespec(concatpath
);
1687 fill_filespec(one
, old_sha1
, old_mode
);
1688 fill_filespec(two
, new_sha1
, new_mode
);
1690 diff_queue(&diff_queued_diff
, one
, two
);
1693 void diff_unmerge(struct diff_options
*options
,
1696 struct diff_filespec
*one
, *two
;
1697 one
= alloc_filespec(path
);
1698 two
= alloc_filespec(path
);
1699 diff_queue(&diff_queued_diff
, one
, two
);