2 * Copyright (C) 2005 Junio C Hamano
11 #include "xdiff/xdiff.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
)
148 /* Use temp[i].name as input, name_a and name_b as labels */
150 lc_a
= count_lines(one
->data
, one
->size
);
151 lc_b
= count_lines(two
->data
, two
->size
);
152 printf("--- %s\n+++ %s\n@@ -", name_a
, name_b
);
153 print_line_count(lc_a
);
155 print_line_count(lc_b
);
158 copy_file('-', one
->data
, one
->size
);
160 copy_file('+', two
->data
, two
->size
);
163 static int fill_mmfile(mmfile_t
*mf
, struct diff_filespec
*one
)
165 if (!DIFF_FILE_VALID(one
)) {
166 mf
->ptr
= ""; /* does not matter */
170 else if (diff_populate_filespec(one
, 0))
173 mf
->size
= one
->size
;
177 struct emit_callback
{
178 const char **label_path
;
181 static int fn_out(void *priv
, mmbuffer_t
*mb
, int nbuf
)
184 struct emit_callback
*ecbdata
= priv
;
186 if (ecbdata
->label_path
[0]) {
187 printf("--- %s\n", ecbdata
->label_path
[0]);
188 printf("+++ %s\n", ecbdata
->label_path
[1]);
189 ecbdata
->label_path
[0] = ecbdata
->label_path
[1] = NULL
;
191 for (i
= 0; i
< nbuf
; i
++)
192 if (!fwrite(mb
[i
].ptr
, mb
[i
].size
, 1, stdout
))
197 #define FIRST_FEW_BYTES 8000
198 static int mmfile_is_binary(mmfile_t
*mf
)
201 if (FIRST_FEW_BYTES
< sz
)
202 sz
= FIRST_FEW_BYTES
;
203 if (memchr(mf
->ptr
, 0, sz
))
208 static void builtin_diff(const char *name_a
,
210 struct diff_filespec
*one
,
211 struct diff_filespec
*two
,
212 const char *xfrm_msg
,
213 int complete_rewrite
)
219 a_one
= quote_two("a/", name_a
);
220 b_two
= quote_two("b/", name_b
);
221 lbl
[0] = DIFF_FILE_VALID(one
) ? a_one
: "/dev/null";
222 lbl
[1] = DIFF_FILE_VALID(two
) ? b_two
: "/dev/null";
223 printf("diff --git %s %s\n", a_one
, b_two
);
224 if (lbl
[0][0] == '/') {
226 printf("new file mode %06o\n", two
->mode
);
227 if (xfrm_msg
&& xfrm_msg
[0])
230 else if (lbl
[1][0] == '/') {
231 printf("deleted file mode %06o\n", one
->mode
);
232 if (xfrm_msg
&& xfrm_msg
[0])
236 if (one
->mode
!= two
->mode
) {
237 printf("old mode %06o\n", one
->mode
);
238 printf("new mode %06o\n", two
->mode
);
240 if (xfrm_msg
&& xfrm_msg
[0])
243 * we do not run diff between different kind
246 if ((one
->mode
^ two
->mode
) & S_IFMT
)
247 goto free_ab_and_return
;
248 if (complete_rewrite
) {
249 emit_rewrite_diff(name_a
, name_b
, one
, two
);
250 goto free_ab_and_return
;
254 if (fill_mmfile(&mf1
, one
) < 0 || fill_mmfile(&mf2
, two
) < 0)
255 die("unable to read files to diff");
257 if (mmfile_is_binary(&mf1
) || mmfile_is_binary(&mf2
))
258 printf("Binary files %s and %s differ\n", lbl
[0], lbl
[1]);
260 /* Crazy xdl interfaces.. */
261 const char *diffopts
= getenv("GIT_DIFF_OPTS");
265 struct emit_callback ecbdata
;
267 ecbdata
.label_path
= lbl
;
268 xpp
.flags
= XDF_NEED_MINIMAL
;
270 xecfg
.flags
= XDL_EMIT_FUNCNAMES
;
273 else if (!strncmp(diffopts
, "--unified=", 10))
274 xecfg
.ctxlen
= strtoul(diffopts
+ 10, NULL
, 10);
275 else if (!strncmp(diffopts
, "-u", 2))
276 xecfg
.ctxlen
= strtoul(diffopts
+ 2, NULL
, 10);
279 xdl_diff(&mf1
, &mf2
, &xpp
, &xecfg
, &ecb
);
288 struct diff_filespec
*alloc_filespec(const char *path
)
290 int namelen
= strlen(path
);
291 struct diff_filespec
*spec
= xmalloc(sizeof(*spec
) + namelen
+ 1);
293 memset(spec
, 0, sizeof(*spec
));
294 spec
->path
= (char *)(spec
+ 1);
295 memcpy(spec
->path
, path
, namelen
+1);
299 void fill_filespec(struct diff_filespec
*spec
, const unsigned char *sha1
,
303 spec
->mode
= canon_mode(mode
);
304 memcpy(spec
->sha1
, sha1
, 20);
305 spec
->sha1_valid
= !!memcmp(sha1
, null_sha1
, 20);
310 * Given a name and sha1 pair, if the dircache tells us the file in
311 * the work tree has that object contents, return true, so that
312 * prepare_temp_file() does not have to inflate and extract.
314 static int work_tree_matches(const char *name
, const unsigned char *sha1
)
316 struct cache_entry
*ce
;
320 /* We do not read the cache ourselves here, because the
321 * benchmark with my previous version that always reads cache
322 * shows that it makes things worse for diff-tree comparing
323 * two linux-2.6 kernel trees in an already checked out work
324 * tree. This is because most diff-tree comparisons deal with
325 * only a small number of files, while reading the cache is
326 * expensive for a large project, and its cost outweighs the
327 * savings we get by not inflating the object to a temporary
328 * file. Practically, this code only helps when we are used
329 * by diff-cache --cached, which does read the cache before
336 pos
= cache_name_pos(name
, len
);
339 ce
= active_cache
[pos
];
340 if ((lstat(name
, &st
) < 0) ||
341 !S_ISREG(st
.st_mode
) || /* careful! */
342 ce_match_stat(ce
, &st
, 0) ||
343 memcmp(sha1
, ce
->sha1
, 20))
345 /* we return 1 only when we can stat, it is a regular file,
346 * stat information matches, and sha1 recorded in the cache
347 * matches. I.e. we know the file in the work tree really is
348 * the same as the <name, sha1> pair.
353 static struct sha1_size_cache
{
354 unsigned char sha1
[20];
357 static int sha1_size_cache_nr
, sha1_size_cache_alloc
;
359 static struct sha1_size_cache
*locate_size_cache(unsigned char *sha1
,
364 struct sha1_size_cache
*e
;
367 last
= sha1_size_cache_nr
;
368 while (last
> first
) {
369 int cmp
, next
= (last
+ first
) >> 1;
370 e
= sha1_size_cache
[next
];
371 cmp
= memcmp(e
->sha1
, sha1
, 20);
383 /* insert to make it at "first" */
384 if (sha1_size_cache_alloc
<= sha1_size_cache_nr
) {
385 sha1_size_cache_alloc
= alloc_nr(sha1_size_cache_alloc
);
386 sha1_size_cache
= xrealloc(sha1_size_cache
,
387 sha1_size_cache_alloc
*
388 sizeof(*sha1_size_cache
));
390 sha1_size_cache_nr
++;
391 if (first
< sha1_size_cache_nr
)
392 memmove(sha1_size_cache
+ first
+ 1, sha1_size_cache
+ first
,
393 (sha1_size_cache_nr
- first
- 1) *
394 sizeof(*sha1_size_cache
));
395 e
= xmalloc(sizeof(struct sha1_size_cache
));
396 sha1_size_cache
[first
] = e
;
397 memcpy(e
->sha1
, sha1
, 20);
403 * While doing rename detection and pickaxe operation, we may need to
404 * grab the data for the blob (or file) for our own in-core comparison.
405 * diff_filespec has data and size fields for this purpose.
407 int diff_populate_filespec(struct diff_filespec
*s
, int size_only
)
410 if (!DIFF_FILE_VALID(s
))
411 die("internal error: asking to populate invalid file.");
412 if (S_ISDIR(s
->mode
))
420 if (!s
->sha1_valid
||
421 work_tree_matches(s
->path
, s
->sha1
)) {
424 if (lstat(s
->path
, &st
) < 0) {
425 if (errno
== ENOENT
) {
434 s
->size
= st
.st_size
;
439 if (S_ISLNK(st
.st_mode
)) {
441 s
->data
= xmalloc(s
->size
);
443 ret
= readlink(s
->path
, s
->data
, s
->size
);
450 fd
= open(s
->path
, O_RDONLY
);
453 s
->data
= mmap(NULL
, s
->size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
455 if (s
->data
== MAP_FAILED
)
457 s
->should_munmap
= 1;
461 struct sha1_size_cache
*e
;
464 e
= locate_size_cache(s
->sha1
, 1, 0);
469 if (!sha1_object_info(s
->sha1
, type
, &s
->size
))
470 locate_size_cache(s
->sha1
, 0, s
->size
);
473 s
->data
= read_sha1_file(s
->sha1
, type
, &s
->size
);
480 void diff_free_filespec_data(struct diff_filespec
*s
)
484 else if (s
->should_munmap
)
485 munmap(s
->data
, s
->size
);
486 s
->should_free
= s
->should_munmap
= 0;
492 static void prep_temp_blob(struct diff_tempfile
*temp
,
495 const unsigned char *sha1
,
500 fd
= git_mkstemp(temp
->tmp_path
, TEMPFILE_PATH_LEN
, ".diff_XXXXXX");
502 die("unable to create temp-file");
503 if (write(fd
, blob
, size
) != size
)
504 die("unable to write temp-file");
506 temp
->name
= temp
->tmp_path
;
507 strcpy(temp
->hex
, sha1_to_hex(sha1
));
509 sprintf(temp
->mode
, "%06o", mode
);
512 static void prepare_temp_file(const char *name
,
513 struct diff_tempfile
*temp
,
514 struct diff_filespec
*one
)
516 if (!DIFF_FILE_VALID(one
)) {
518 /* A '-' entry produces this for file-2, and
519 * a '+' entry produces this for file-1.
521 temp
->name
= "/dev/null";
522 strcpy(temp
->hex
, ".");
523 strcpy(temp
->mode
, ".");
527 if (!one
->sha1_valid
||
528 work_tree_matches(name
, one
->sha1
)) {
530 if (lstat(name
, &st
) < 0) {
532 goto not_a_valid_file
;
533 die("stat(%s): %s", name
, strerror(errno
));
535 if (S_ISLNK(st
.st_mode
)) {
537 char buf
[PATH_MAX
+ 1]; /* ought to be SYMLINK_MAX */
538 if (sizeof(buf
) <= st
.st_size
)
539 die("symlink too long: %s", name
);
540 ret
= readlink(name
, buf
, st
.st_size
);
542 die("readlink(%s)", name
);
543 prep_temp_blob(temp
, buf
, st
.st_size
,
545 one
->sha1
: null_sha1
),
547 one
->mode
: S_IFLNK
));
550 /* we can borrow from the file in the work tree */
552 if (!one
->sha1_valid
)
553 strcpy(temp
->hex
, sha1_to_hex(null_sha1
));
555 strcpy(temp
->hex
, sha1_to_hex(one
->sha1
));
556 /* Even though we may sometimes borrow the
557 * contents from the work tree, we always want
558 * one->mode. mode is trustworthy even when
559 * !(one->sha1_valid), as long as
560 * DIFF_FILE_VALID(one).
562 sprintf(temp
->mode
, "%06o", one
->mode
);
567 if (diff_populate_filespec(one
, 0))
568 die("cannot read data blob for %s", one
->path
);
569 prep_temp_blob(temp
, one
->data
, one
->size
,
570 one
->sha1
, one
->mode
);
574 static void remove_tempfile(void)
578 for (i
= 0; i
< 2; i
++)
579 if (diff_temp
[i
].name
== diff_temp
[i
].tmp_path
) {
580 unlink(diff_temp
[i
].name
);
581 diff_temp
[i
].name
= NULL
;
585 static void remove_tempfile_on_signal(int signo
)
588 signal(SIGINT
, SIG_DFL
);
592 static int spawn_prog(const char *pgm
, const char **arg
)
600 die("unable to fork");
602 execvp(pgm
, (char *const*) arg
);
606 while (waitpid(pid
, &status
, 0) < 0) {
612 /* Earlier we did not check the exit status because
613 * diff exits non-zero if files are different, and
614 * we are not interested in knowing that. It was a
615 * mistake which made it harder to quit a diff-*
616 * session that uses the git-apply-patch-script as
617 * the GIT_EXTERNAL_DIFF. A custom GIT_EXTERNAL_DIFF
618 * should also exit non-zero only when it wants to
619 * abort the entire diff-* session.
621 if (WIFEXITED(status
) && !WEXITSTATUS(status
))
626 /* An external diff command takes:
628 * diff-cmd name infile1 infile1-sha1 infile1-mode \
629 * infile2 infile2-sha1 infile2-mode [ rename-to ]
632 static void run_external_diff(const char *pgm
,
635 struct diff_filespec
*one
,
636 struct diff_filespec
*two
,
637 const char *xfrm_msg
,
638 int complete_rewrite
)
640 const char *spawn_arg
[10];
641 struct diff_tempfile
*temp
= diff_temp
;
643 static int atexit_asked
= 0;
644 const char *othername
;
645 const char **arg
= &spawn_arg
[0];
647 othername
= (other
? other
: name
);
649 prepare_temp_file(name
, &temp
[0], one
);
650 prepare_temp_file(othername
, &temp
[1], two
);
651 if (! atexit_asked
&&
652 (temp
[0].name
== temp
[0].tmp_path
||
653 temp
[1].name
== temp
[1].tmp_path
)) {
655 atexit(remove_tempfile
);
657 signal(SIGINT
, remove_tempfile_on_signal
);
663 *arg
++ = temp
[0].name
;
664 *arg
++ = temp
[0].hex
;
665 *arg
++ = temp
[0].mode
;
666 *arg
++ = temp
[1].name
;
667 *arg
++ = temp
[1].hex
;
668 *arg
++ = temp
[1].mode
;
678 retval
= spawn_prog(pgm
, spawn_arg
);
681 fprintf(stderr
, "external diff died, stopping at %s.\n", name
);
686 static void run_diff_cmd(const char *pgm
,
689 struct diff_filespec
*one
,
690 struct diff_filespec
*two
,
691 const char *xfrm_msg
,
692 int complete_rewrite
)
695 run_external_diff(pgm
, name
, other
, one
, two
, xfrm_msg
,
700 builtin_diff(name
, other
? other
: name
,
701 one
, two
, xfrm_msg
, complete_rewrite
);
703 printf("* Unmerged path %s\n", name
);
706 static void diff_fill_sha1_info(struct diff_filespec
*one
)
708 if (DIFF_FILE_VALID(one
)) {
709 if (!one
->sha1_valid
) {
711 if (lstat(one
->path
, &st
) < 0)
712 die("stat %s", one
->path
);
713 if (index_path(one
->sha1
, one
->path
, &st
, 0))
714 die("cannot hash %s\n", one
->path
);
718 memset(one
->sha1
, 0, 20);
721 static void run_diff(struct diff_filepair
*p
, struct diff_options
*o
)
723 const char *pgm
= external_diff();
724 char msg
[PATH_MAX
*2+300], *xfrm_msg
;
725 struct diff_filespec
*one
;
726 struct diff_filespec
*two
;
729 char *name_munged
, *other_munged
;
730 int complete_rewrite
= 0;
733 if (DIFF_PAIR_UNMERGED(p
)) {
735 run_diff_cmd(pgm
, p
->one
->path
, NULL
, NULL
, NULL
, NULL
, 0);
740 other
= (strcmp(name
, p
->two
->path
) ? p
->two
->path
: NULL
);
741 name_munged
= quote_one(name
);
742 other_munged
= quote_one(other
);
743 one
= p
->one
; two
= p
->two
;
745 diff_fill_sha1_info(one
);
746 diff_fill_sha1_info(two
);
750 case DIFF_STATUS_COPIED
:
751 len
+= snprintf(msg
+ len
, sizeof(msg
) - len
,
752 "similarity index %d%%\n"
755 (int)(0.5 + p
->score
* 100.0/MAX_SCORE
),
756 name_munged
, other_munged
);
758 case DIFF_STATUS_RENAMED
:
759 len
+= snprintf(msg
+ len
, sizeof(msg
) - len
,
760 "similarity index %d%%\n"
763 (int)(0.5 + p
->score
* 100.0/MAX_SCORE
),
764 name_munged
, other_munged
);
766 case DIFF_STATUS_MODIFIED
:
768 len
+= snprintf(msg
+ len
, sizeof(msg
) - len
,
769 "dissimilarity index %d%%\n",
770 (int)(0.5 + p
->score
*
772 complete_rewrite
= 1;
781 if (memcmp(one
->sha1
, two
->sha1
, 20)) {
783 int abbrev
= o
->full_index
? 40 : DEFAULT_ABBREV
;
784 memcpy(one_sha1
, sha1_to_hex(one
->sha1
), 41);
786 len
+= snprintf(msg
+ len
, sizeof(msg
) - len
,
788 abbrev
, one_sha1
, abbrev
,
789 sha1_to_hex(two
->sha1
));
790 if (one
->mode
== two
->mode
)
791 len
+= snprintf(msg
+ len
, sizeof(msg
) - len
,
793 len
+= snprintf(msg
+ len
, sizeof(msg
) - len
, "\n");
798 xfrm_msg
= len
? msg
: NULL
;
801 DIFF_FILE_VALID(one
) && DIFF_FILE_VALID(two
) &&
802 (S_IFMT
& one
->mode
) != (S_IFMT
& two
->mode
)) {
803 /* a filepair that changes between file and symlink
804 * needs to be split into deletion and creation.
806 struct diff_filespec
*null
= alloc_filespec(two
->path
);
807 run_diff_cmd(NULL
, name
, other
, one
, null
, xfrm_msg
, 0);
809 null
= alloc_filespec(one
->path
);
810 run_diff_cmd(NULL
, name
, other
, null
, two
, xfrm_msg
, 0);
814 run_diff_cmd(pgm
, name
, other
, one
, two
, xfrm_msg
,
821 void diff_setup(struct diff_options
*options
)
823 memset(options
, 0, sizeof(*options
));
824 options
->output_format
= DIFF_FORMAT_RAW
;
825 options
->line_termination
= '\n';
826 options
->break_opt
= -1;
827 options
->rename_limit
= -1;
829 options
->change
= diff_change
;
830 options
->add_remove
= diff_addremove
;
833 int diff_setup_done(struct diff_options
*options
)
835 if ((options
->find_copies_harder
&&
836 options
->detect_rename
!= DIFF_DETECT_COPY
) ||
837 (0 <= options
->rename_limit
&& !options
->detect_rename
))
839 if (options
->detect_rename
&& options
->rename_limit
< 0)
840 options
->rename_limit
= diff_rename_limit_default
;
841 if (options
->setup
& DIFF_SETUP_USE_CACHE
) {
843 /* read-cache does not die even when it fails
844 * so it is safe for us to do this here. Also
845 * it does not smudge active_cache or active_nr
846 * when it fails, so we do not have to worry about
847 * cleaning it up ourselves either.
851 if (options
->setup
& DIFF_SETUP_USE_SIZE_CACHE
)
853 if (options
->abbrev
<= 0 || 40 < options
->abbrev
)
854 options
->abbrev
= 40; /* full */
859 int diff_opt_parse(struct diff_options
*options
, const char **av
, int ac
)
861 const char *arg
= av
[0];
862 if (!strcmp(arg
, "-p") || !strcmp(arg
, "-u"))
863 options
->output_format
= DIFF_FORMAT_PATCH
;
864 else if (!strcmp(arg
, "-z"))
865 options
->line_termination
= 0;
866 else if (!strncmp(arg
, "-l", 2))
867 options
->rename_limit
= strtoul(arg
+2, NULL
, 10);
868 else if (!strcmp(arg
, "--full-index"))
869 options
->full_index
= 1;
870 else if (!strcmp(arg
, "--name-only"))
871 options
->output_format
= DIFF_FORMAT_NAME
;
872 else if (!strcmp(arg
, "--name-status"))
873 options
->output_format
= DIFF_FORMAT_NAME_STATUS
;
874 else if (!strcmp(arg
, "-R"))
875 options
->reverse_diff
= 1;
876 else if (!strncmp(arg
, "-S", 2))
877 options
->pickaxe
= arg
+ 2;
878 else if (!strcmp(arg
, "-s"))
879 options
->output_format
= DIFF_FORMAT_NO_OUTPUT
;
880 else if (!strncmp(arg
, "-O", 2))
881 options
->orderfile
= arg
+ 2;
882 else if (!strncmp(arg
, "--diff-filter=", 14))
883 options
->filter
= arg
+ 14;
884 else if (!strcmp(arg
, "--pickaxe-all"))
885 options
->pickaxe_opts
= DIFF_PICKAXE_ALL
;
886 else if (!strncmp(arg
, "-B", 2)) {
887 if ((options
->break_opt
=
888 diff_scoreopt_parse(arg
)) == -1)
891 else if (!strncmp(arg
, "-M", 2)) {
892 if ((options
->rename_score
=
893 diff_scoreopt_parse(arg
)) == -1)
895 options
->detect_rename
= DIFF_DETECT_RENAME
;
897 else if (!strncmp(arg
, "-C", 2)) {
898 if ((options
->rename_score
=
899 diff_scoreopt_parse(arg
)) == -1)
901 options
->detect_rename
= DIFF_DETECT_COPY
;
903 else if (!strcmp(arg
, "--find-copies-harder"))
904 options
->find_copies_harder
= 1;
905 else if (!strcmp(arg
, "--abbrev"))
906 options
->abbrev
= DEFAULT_ABBREV
;
907 else if (!strncmp(arg
, "--abbrev=", 9)) {
908 options
->abbrev
= strtoul(arg
+ 9, NULL
, 10);
909 if (options
->abbrev
< MINIMUM_ABBREV
)
910 options
->abbrev
= MINIMUM_ABBREV
;
911 else if (40 < options
->abbrev
)
912 options
->abbrev
= 40;
919 static int parse_num(const char **cp_p
)
921 unsigned long num
, scale
;
923 const char *cp
= *cp_p
;
930 if ( !dot
&& ch
== '.' ) {
933 } else if ( ch
== '%' ) {
934 scale
= dot
? scale
*100 : 100;
935 cp
++; /* % is always at the end */
937 } else if ( ch
>= '0' && ch
<= '9' ) {
938 if ( scale
< 100000 ) {
940 num
= (num
*10) + (ch
-'0');
949 /* user says num divided by scale and we say internally that
950 * is MAX_SCORE * num / scale.
952 return (num
>= scale
) ? MAX_SCORE
: (MAX_SCORE
* num
/ scale
);
955 int diff_scoreopt_parse(const char *opt
)
962 if (cmd
!= 'M' && cmd
!= 'C' && cmd
!= 'B')
963 return -1; /* that is not a -M, -C nor -B option */
965 opt1
= parse_num(&opt
);
971 else if (*opt
!= '/')
972 return -1; /* we expect -B80/99 or -B80 */
975 opt2
= parse_num(&opt
);
980 return opt1
| (opt2
<< 16);
983 struct diff_queue_struct diff_queued_diff
;
985 void diff_q(struct diff_queue_struct
*queue
, struct diff_filepair
*dp
)
987 if (queue
->alloc
<= queue
->nr
) {
988 queue
->alloc
= alloc_nr(queue
->alloc
);
989 queue
->queue
= xrealloc(queue
->queue
,
990 sizeof(dp
) * queue
->alloc
);
992 queue
->queue
[queue
->nr
++] = dp
;
995 struct diff_filepair
*diff_queue(struct diff_queue_struct
*queue
,
996 struct diff_filespec
*one
,
997 struct diff_filespec
*two
)
999 struct diff_filepair
*dp
= xmalloc(sizeof(*dp
));
1004 dp
->source_stays
= 0;
1005 dp
->broken_pair
= 0;
1011 void diff_free_filepair(struct diff_filepair
*p
)
1013 diff_free_filespec_data(p
->one
);
1014 diff_free_filespec_data(p
->two
);
1020 /* This is different from find_unique_abbrev() in that
1021 * it stuffs the result with dots for alignment.
1023 const char *diff_unique_abbrev(const unsigned char *sha1
, int len
)
1028 return sha1_to_hex(sha1
);
1030 abbrev
= find_unique_abbrev(sha1
, len
);
1032 return sha1_to_hex(sha1
);
1033 abblen
= strlen(abbrev
);
1035 static char hex
[41];
1036 if (len
< abblen
&& abblen
<= len
+ 2)
1037 sprintf(hex
, "%s%.*s", abbrev
, len
+3-abblen
, "..");
1039 sprintf(hex
, "%s...", abbrev
);
1042 return sha1_to_hex(sha1
);
1045 static void diff_flush_raw(struct diff_filepair
*p
,
1046 int line_termination
,
1047 int inter_name_termination
,
1048 struct diff_options
*options
)
1052 int abbrev
= options
->abbrev
;
1053 const char *path_one
, *path_two
;
1054 int output_format
= options
->output_format
;
1056 path_one
= p
->one
->path
;
1057 path_two
= p
->two
->path
;
1058 if (line_termination
) {
1059 path_one
= quote_one(path_one
);
1060 path_two
= quote_one(path_two
);
1064 sprintf(status
, "%c%03d", p
->status
,
1065 (int)(0.5 + p
->score
* 100.0/MAX_SCORE
));
1067 status
[0] = p
->status
;
1070 switch (p
->status
) {
1071 case DIFF_STATUS_COPIED
:
1072 case DIFF_STATUS_RENAMED
:
1075 case DIFF_STATUS_ADDED
:
1076 case DIFF_STATUS_DELETED
:
1083 if (output_format
!= DIFF_FORMAT_NAME_STATUS
) {
1084 printf(":%06o %06o %s ",
1085 p
->one
->mode
, p
->two
->mode
,
1086 diff_unique_abbrev(p
->one
->sha1
, abbrev
));
1088 diff_unique_abbrev(p
->two
->sha1
, abbrev
));
1090 printf("%s%c%s", status
, inter_name_termination
, path_one
);
1092 printf("%c%s", inter_name_termination
, path_two
);
1093 putchar(line_termination
);
1094 if (path_one
!= p
->one
->path
)
1095 free((void*)path_one
);
1096 if (path_two
!= p
->two
->path
)
1097 free((void*)path_two
);
1100 static void diff_flush_name(struct diff_filepair
*p
,
1101 int inter_name_termination
,
1102 int line_termination
)
1104 char *path
= p
->two
->path
;
1106 if (line_termination
)
1107 path
= quote_one(p
->two
->path
);
1109 path
= p
->two
->path
;
1110 printf("%s%c", path
, line_termination
);
1111 if (p
->two
->path
!= path
)
1115 int diff_unmodified_pair(struct diff_filepair
*p
)
1117 /* This function is written stricter than necessary to support
1118 * the currently implemented transformers, but the idea is to
1119 * let transformers to produce diff_filepairs any way they want,
1120 * and filter and clean them up here before producing the output.
1122 struct diff_filespec
*one
, *two
;
1124 if (DIFF_PAIR_UNMERGED(p
))
1125 return 0; /* unmerged is interesting */
1130 /* deletion, addition, mode or type change
1131 * and rename are all interesting.
1133 if (DIFF_FILE_VALID(one
) != DIFF_FILE_VALID(two
) ||
1134 DIFF_PAIR_MODE_CHANGED(p
) ||
1135 strcmp(one
->path
, two
->path
))
1138 /* both are valid and point at the same path. that is, we are
1139 * dealing with a change.
1141 if (one
->sha1_valid
&& two
->sha1_valid
&&
1142 !memcmp(one
->sha1
, two
->sha1
, sizeof(one
->sha1
)))
1143 return 1; /* no change */
1144 if (!one
->sha1_valid
&& !two
->sha1_valid
)
1145 return 1; /* both look at the same file on the filesystem. */
1149 static void diff_flush_patch(struct diff_filepair
*p
, struct diff_options
*o
)
1151 if (diff_unmodified_pair(p
))
1154 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
1155 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
1156 return; /* no tree diffs in patch format */
1161 int diff_queue_is_empty(void)
1163 struct diff_queue_struct
*q
= &diff_queued_diff
;
1165 for (i
= 0; i
< q
->nr
; i
++)
1166 if (!diff_unmodified_pair(q
->queue
[i
]))
1172 void diff_debug_filespec(struct diff_filespec
*s
, int x
, const char *one
)
1174 fprintf(stderr
, "queue[%d] %s (%s) %s %06o %s\n",
1177 DIFF_FILE_VALID(s
) ? "valid" : "invalid",
1179 s
->sha1_valid
? sha1_to_hex(s
->sha1
) : "");
1180 fprintf(stderr
, "queue[%d] %s size %lu flags %d\n",
1182 s
->size
, s
->xfrm_flags
);
1185 void diff_debug_filepair(const struct diff_filepair
*p
, int i
)
1187 diff_debug_filespec(p
->one
, i
, "one");
1188 diff_debug_filespec(p
->two
, i
, "two");
1189 fprintf(stderr
, "score %d, status %c stays %d broken %d\n",
1190 p
->score
, p
->status
? p
->status
: '?',
1191 p
->source_stays
, p
->broken_pair
);
1194 void diff_debug_queue(const char *msg
, struct diff_queue_struct
*q
)
1198 fprintf(stderr
, "%s\n", msg
);
1199 fprintf(stderr
, "q->nr = %d\n", q
->nr
);
1200 for (i
= 0; i
< q
->nr
; i
++) {
1201 struct diff_filepair
*p
= q
->queue
[i
];
1202 diff_debug_filepair(p
, i
);
1207 static void diff_resolve_rename_copy(void)
1210 struct diff_filepair
*p
, *pp
;
1211 struct diff_queue_struct
*q
= &diff_queued_diff
;
1213 diff_debug_queue("resolve-rename-copy", q
);
1215 for (i
= 0; i
< q
->nr
; i
++) {
1217 p
->status
= 0; /* undecided */
1218 if (DIFF_PAIR_UNMERGED(p
))
1219 p
->status
= DIFF_STATUS_UNMERGED
;
1220 else if (!DIFF_FILE_VALID(p
->one
))
1221 p
->status
= DIFF_STATUS_ADDED
;
1222 else if (!DIFF_FILE_VALID(p
->two
))
1223 p
->status
= DIFF_STATUS_DELETED
;
1224 else if (DIFF_PAIR_TYPE_CHANGED(p
))
1225 p
->status
= DIFF_STATUS_TYPE_CHANGED
;
1227 /* from this point on, we are dealing with a pair
1228 * whose both sides are valid and of the same type, i.e.
1229 * either in-place edit or rename/copy edit.
1231 else if (DIFF_PAIR_RENAME(p
)) {
1232 if (p
->source_stays
) {
1233 p
->status
= DIFF_STATUS_COPIED
;
1236 /* See if there is some other filepair that
1237 * copies from the same source as us. If so
1238 * we are a copy. Otherwise we are either a
1239 * copy if the path stays, or a rename if it
1240 * does not, but we already handled "stays" case.
1242 for (j
= i
+ 1; j
< q
->nr
; j
++) {
1244 if (strcmp(pp
->one
->path
, p
->one
->path
))
1245 continue; /* not us */
1246 if (!DIFF_PAIR_RENAME(pp
))
1247 continue; /* not a rename/copy */
1248 /* pp is a rename/copy from the same source */
1249 p
->status
= DIFF_STATUS_COPIED
;
1253 p
->status
= DIFF_STATUS_RENAMED
;
1255 else if (memcmp(p
->one
->sha1
, p
->two
->sha1
, 20) ||
1256 p
->one
->mode
!= p
->two
->mode
)
1257 p
->status
= DIFF_STATUS_MODIFIED
;
1259 /* This is a "no-change" entry and should not
1260 * happen anymore, but prepare for broken callers.
1262 error("feeding unmodified %s to diffcore",
1264 p
->status
= DIFF_STATUS_UNKNOWN
;
1267 diff_debug_queue("resolve-rename-copy done", q
);
1270 void diff_flush(struct diff_options
*options
)
1272 struct diff_queue_struct
*q
= &diff_queued_diff
;
1274 int inter_name_termination
= '\t';
1275 int diff_output_format
= options
->output_format
;
1276 int line_termination
= options
->line_termination
;
1278 if (!line_termination
)
1279 inter_name_termination
= 0;
1281 for (i
= 0; i
< q
->nr
; i
++) {
1282 struct diff_filepair
*p
= q
->queue
[i
];
1284 switch (p
->status
) {
1285 case DIFF_STATUS_UNKNOWN
:
1288 die("internal error in diff-resolve-rename-copy");
1291 switch (diff_output_format
) {
1292 case DIFF_FORMAT_PATCH
:
1293 diff_flush_patch(p
, options
);
1295 case DIFF_FORMAT_RAW
:
1296 case DIFF_FORMAT_NAME_STATUS
:
1297 diff_flush_raw(p
, line_termination
,
1298 inter_name_termination
,
1301 case DIFF_FORMAT_NAME
:
1303 inter_name_termination
,
1306 case DIFF_FORMAT_NO_OUTPUT
:
1310 diff_free_filepair(p
);
1314 q
->nr
= q
->alloc
= 0;
1317 static void diffcore_apply_filter(const char *filter
)
1320 struct diff_queue_struct
*q
= &diff_queued_diff
;
1321 struct diff_queue_struct outq
;
1323 outq
.nr
= outq
.alloc
= 0;
1328 if (strchr(filter
, DIFF_STATUS_FILTER_AON
)) {
1330 for (i
= found
= 0; !found
&& i
< q
->nr
; i
++) {
1331 struct diff_filepair
*p
= q
->queue
[i
];
1332 if (((p
->status
== DIFF_STATUS_MODIFIED
) &&
1334 strchr(filter
, DIFF_STATUS_FILTER_BROKEN
)) ||
1336 strchr(filter
, DIFF_STATUS_MODIFIED
)))) ||
1337 ((p
->status
!= DIFF_STATUS_MODIFIED
) &&
1338 strchr(filter
, p
->status
)))
1344 /* otherwise we will clear the whole queue
1345 * by copying the empty outq at the end of this
1346 * function, but first clear the current entries
1349 for (i
= 0; i
< q
->nr
; i
++)
1350 diff_free_filepair(q
->queue
[i
]);
1353 /* Only the matching ones */
1354 for (i
= 0; i
< q
->nr
; i
++) {
1355 struct diff_filepair
*p
= q
->queue
[i
];
1357 if (((p
->status
== DIFF_STATUS_MODIFIED
) &&
1359 strchr(filter
, DIFF_STATUS_FILTER_BROKEN
)) ||
1361 strchr(filter
, DIFF_STATUS_MODIFIED
)))) ||
1362 ((p
->status
!= DIFF_STATUS_MODIFIED
) &&
1363 strchr(filter
, p
->status
)))
1366 diff_free_filepair(p
);
1373 void diffcore_std(struct diff_options
*options
)
1375 if (options
->paths
&& options
->paths
[0])
1376 diffcore_pathspec(options
->paths
);
1377 if (options
->break_opt
!= -1)
1378 diffcore_break(options
->break_opt
);
1379 if (options
->detect_rename
)
1380 diffcore_rename(options
);
1381 if (options
->break_opt
!= -1)
1382 diffcore_merge_broken();
1383 if (options
->pickaxe
)
1384 diffcore_pickaxe(options
->pickaxe
, options
->pickaxe_opts
);
1385 if (options
->orderfile
)
1386 diffcore_order(options
->orderfile
);
1387 diff_resolve_rename_copy();
1388 diffcore_apply_filter(options
->filter
);
1392 void diffcore_std_no_resolve(struct diff_options
*options
)
1394 if (options
->pickaxe
)
1395 diffcore_pickaxe(options
->pickaxe
, options
->pickaxe_opts
);
1396 if (options
->orderfile
)
1397 diffcore_order(options
->orderfile
);
1398 diffcore_apply_filter(options
->filter
);
1401 void diff_addremove(struct diff_options
*options
,
1402 int addremove
, unsigned mode
,
1403 const unsigned char *sha1
,
1404 const char *base
, const char *path
)
1406 char concatpath
[PATH_MAX
];
1407 struct diff_filespec
*one
, *two
;
1409 /* This may look odd, but it is a preparation for
1410 * feeding "there are unchanged files which should
1411 * not produce diffs, but when you are doing copy
1412 * detection you would need them, so here they are"
1413 * entries to the diff-core. They will be prefixed
1414 * with something like '=' or '*' (I haven't decided
1415 * which but should not make any difference).
1416 * Feeding the same new and old to diff_change()
1417 * also has the same effect.
1418 * Before the final output happens, they are pruned after
1419 * merged into rename/copy pairs as appropriate.
1421 if (options
->reverse_diff
)
1422 addremove
= (addremove
== '+' ? '-' :
1423 addremove
== '-' ? '+' : addremove
);
1425 if (!path
) path
= "";
1426 sprintf(concatpath
, "%s%s", base
, path
);
1427 one
= alloc_filespec(concatpath
);
1428 two
= alloc_filespec(concatpath
);
1430 if (addremove
!= '+')
1431 fill_filespec(one
, sha1
, mode
);
1432 if (addremove
!= '-')
1433 fill_filespec(two
, sha1
, mode
);
1435 diff_queue(&diff_queued_diff
, one
, two
);
1438 void diff_change(struct diff_options
*options
,
1439 unsigned old_mode
, unsigned new_mode
,
1440 const unsigned char *old_sha1
,
1441 const unsigned char *new_sha1
,
1442 const char *base
, const char *path
)
1444 char concatpath
[PATH_MAX
];
1445 struct diff_filespec
*one
, *two
;
1447 if (options
->reverse_diff
) {
1449 const unsigned char *tmp_c
;
1450 tmp
= old_mode
; old_mode
= new_mode
; new_mode
= tmp
;
1451 tmp_c
= old_sha1
; old_sha1
= new_sha1
; new_sha1
= tmp_c
;
1453 if (!path
) path
= "";
1454 sprintf(concatpath
, "%s%s", base
, path
);
1455 one
= alloc_filespec(concatpath
);
1456 two
= alloc_filespec(concatpath
);
1457 fill_filespec(one
, old_sha1
, old_mode
);
1458 fill_filespec(two
, new_sha1
, new_mode
);
1460 diff_queue(&diff_queued_diff
, one
, two
);
1463 void diff_unmerge(struct diff_options
*options
,
1466 struct diff_filespec
*one
, *two
;
1467 one
= alloc_filespec(path
);
1468 two
= alloc_filespec(path
);
1469 diff_queue(&diff_queued_diff
, one
, two
);