2 * Copyright (C) 2005 Junio C Hamano
11 #include "xdiff/xdiff.h"
13 static const char *diff_opts
= "-pu";
15 static int use_size_cache
;
17 int diff_rename_limit_default
= -1;
19 int git_diff_config(const char *var
, const char *value
)
21 if (!strcmp(var
, "diff.renamelimit")) {
22 diff_rename_limit_default
= git_config_int(var
, value
);
26 return git_default_config(var
, value
);
29 static char *quote_one(const char *str
)
36 needlen
= quote_c_style(str
, NULL
, NULL
, 0);
39 xp
= xmalloc(needlen
+ 1);
40 quote_c_style(str
, xp
, NULL
, 0);
44 static char *quote_two(const char *one
, const char *two
)
46 int need_one
= quote_c_style(one
, NULL
, NULL
, 1);
47 int need_two
= quote_c_style(two
, NULL
, NULL
, 1);
50 if (need_one
+ need_two
) {
51 if (!need_one
) need_one
= strlen(one
);
52 if (!need_two
) need_one
= strlen(two
);
54 xp
= xmalloc(need_one
+ need_two
+ 3);
56 quote_c_style(one
, xp
+ 1, NULL
, 1);
57 quote_c_style(two
, xp
+ need_one
+ 1, NULL
, 1);
58 strcpy(xp
+ need_one
+ need_two
+ 1, "\"");
61 need_one
= strlen(one
);
62 need_two
= strlen(two
);
63 xp
= xmalloc(need_one
+ need_two
+ 1);
65 strcpy(xp
+ need_one
, two
);
69 static const char *external_diff(void)
71 static const char *external_diff_cmd
= NULL
;
72 static int done_preparing
= 0;
73 const char *env_diff_opts
;
76 return external_diff_cmd
;
79 * Default values above are meant to match the
80 * Linux kernel development style. Examples of
81 * alternative styles you can specify via environment
86 external_diff_cmd
= getenv("GIT_EXTERNAL_DIFF");
88 /* In case external diff fails... */
89 env_diff_opts
= getenv("GIT_DIFF_OPTS");
90 if (env_diff_opts
) diff_opts
= env_diff_opts
;
93 return external_diff_cmd
;
96 #define TEMPFILE_PATH_LEN 50
98 static struct diff_tempfile
{
99 const char *name
; /* filename external diff should read from */
102 char tmp_path
[TEMPFILE_PATH_LEN
];
105 static int count_lines(const char *filename
)
108 int count
, ch
, completely_empty
= 1, nl_just_seen
= 0;
109 in
= fopen(filename
, "r");
111 while ((ch
= fgetc(in
)) != EOF
)
115 completely_empty
= 0;
119 completely_empty
= 0;
122 if (completely_empty
)
125 count
++; /* no trailing newline */
129 static void print_line_count(int count
)
139 printf("1,%d", count
);
144 static void copy_file(int prefix
, const char *filename
)
147 int ch
, nl_just_seen
= 1;
148 in
= fopen(filename
, "r");
149 while ((ch
= fgetc(in
)) != EOF
) {
160 printf("\n\\ No newline at end of file\n");
163 static void emit_rewrite_diff(const char *name_a
,
165 struct diff_tempfile
*temp
)
167 /* Use temp[i].name as input, name_a and name_b as labels */
169 lc_a
= count_lines(temp
[0].name
);
170 lc_b
= count_lines(temp
[1].name
);
171 printf("--- %s\n+++ %s\n@@ -", name_a
, name_b
);
172 print_line_count(lc_a
);
174 print_line_count(lc_b
);
177 copy_file('-', temp
[0].name
);
179 copy_file('+', temp
[1].name
);
182 static int fill_mmfile(mmfile_t
*mf
, const char *file
)
184 int fd
= open(file
, O_RDONLY
);
199 int retval
= read(fd
, buf
, size
);
201 if (errno
== EINTR
|| errno
== EAGAIN
)
215 struct emit_callback
{
216 const char **label_path
;
219 static int fn_out(void *priv
, mmbuffer_t
*mb
, int nbuf
)
222 struct emit_callback
*ecbdata
= priv
;
224 if (ecbdata
->label_path
[0]) {
225 printf("--- %s\n", ecbdata
->label_path
[0]);
226 printf("+++ %s\n", ecbdata
->label_path
[1]);
227 ecbdata
->label_path
[0] = ecbdata
->label_path
[1] = NULL
;
229 for (i
= 0; i
< nbuf
; i
++)
230 if (!fwrite(mb
[i
].ptr
, mb
[i
].size
, 1, stdout
))
235 #define FIRST_FEW_BYTES 8000
236 static int mmfile_is_binary(mmfile_t
*mf
)
239 if (FIRST_FEW_BYTES
< sz
)
240 sz
= FIRST_FEW_BYTES
;
241 if (memchr(mf
->ptr
, 0, sz
))
246 static const char *builtin_diff(const char *name_a
,
248 struct diff_tempfile
*temp
,
249 const char *xfrm_msg
,
250 int complete_rewrite
,
253 int i
, next_at
, cmd_size
;
255 const char *const diff_cmd
= "diff -L%s -L%s";
256 const char *const diff_arg
= "-- %s %s||:"; /* "||:" is to return 0 */
257 const char *input_name_sq
[2];
258 const char *label_path
[2];
261 /* diff_cmd and diff_arg have 4 %s in total which makes
262 * the sum of these strings 8 bytes larger than required.
263 * we use 2 spaces around diff-opts, and we need to count
264 * terminating NUL; we used to subtract 5 here, but we do not
265 * care about small leaks in this subprocess that is about
266 * to exec "diff" anymore.
268 cmd_size
= (strlen(diff_cmd
) + strlen(diff_opts
) + strlen(diff_arg
)
271 for (i
= 0; i
< 2; i
++) {
272 input_name_sq
[i
] = sq_quote(temp
[i
].name
);
273 if (!strcmp(temp
[i
].name
, "/dev/null"))
274 label_path
[i
] = "/dev/null";
276 label_path
[i
] = sq_quote(quote_two("a/", name_a
));
278 label_path
[i
] = sq_quote(quote_two("b/", name_b
));
279 cmd_size
+= (strlen(label_path
[i
]) + strlen(input_name_sq
[i
]));
282 cmd
= xmalloc(cmd_size
);
285 next_at
+= snprintf(cmd
+next_at
, cmd_size
-next_at
,
286 diff_cmd
, label_path
[0], label_path
[1]);
287 next_at
+= snprintf(cmd
+next_at
, cmd_size
-next_at
,
289 next_at
+= snprintf(cmd
+next_at
, cmd_size
-next_at
,
290 diff_arg
, input_name_sq
[0], input_name_sq
[1]);
292 printf("diff --git %s %s\n",
293 quote_two("a/", name_a
), quote_two("b/", name_b
));
294 if (label_path
[0][0] == '/') {
296 printf("new file mode %s\n", temp
[1].mode
);
297 if (xfrm_msg
&& xfrm_msg
[0])
300 else if (label_path
[1][0] == '/') {
301 printf("deleted file mode %s\n", temp
[0].mode
);
302 if (xfrm_msg
&& xfrm_msg
[0])
306 if (strcmp(temp
[0].mode
, temp
[1].mode
)) {
307 printf("old mode %s\n", temp
[0].mode
);
308 printf("new mode %s\n", temp
[1].mode
);
310 if (xfrm_msg
&& xfrm_msg
[0])
313 * we do not run diff between different kind
316 if (strncmp(temp
[0].mode
, temp
[1].mode
, 3))
318 if (complete_rewrite
) {
319 emit_rewrite_diff(name_a
, name_b
, temp
);
324 /* Un-quote the paths */
325 if (label_path
[0][0] != '/')
326 label_path
[0] = quote_two("a/", name_a
);
327 if (label_path
[1][0] != '/')
328 label_path
[1] = quote_two("b/", name_b
);
330 if (fill_mmfile(&mf1
, temp
[0].name
) < 0 ||
331 fill_mmfile(&mf2
, temp
[1].name
) < 0)
332 die("unable to read files to diff");
334 if (mmfile_is_binary(&mf1
) || mmfile_is_binary(&mf2
))
335 printf("Binary files %s and %s differ\n",
336 label_path
[0], label_path
[1]);
338 /* Crazy xdl interfaces.. */
339 const char *diffopts
= getenv("GIT_DIFF_OPTS");
343 struct emit_callback ecbdata
;
345 ecbdata
.label_path
= label_path
;
346 xpp
.flags
= XDF_NEED_MINIMAL
;
350 else if (!strncmp(diffopts
, "--unified=", 10))
351 xecfg
.ctxlen
= strtoul(diffopts
+ 10, NULL
, 10);
352 else if (!strncmp(diffopts
, "-u", 2))
353 xecfg
.ctxlen
= strtoul(diffopts
+ 2, NULL
, 10);
356 xdl_diff(&mf1
, &mf2
, &xpp
, &xecfg
, &ecb
);
364 struct diff_filespec
*alloc_filespec(const char *path
)
366 int namelen
= strlen(path
);
367 struct diff_filespec
*spec
= xmalloc(sizeof(*spec
) + namelen
+ 1);
369 memset(spec
, 0, sizeof(*spec
));
370 spec
->path
= (char *)(spec
+ 1);
371 memcpy(spec
->path
, path
, namelen
+1);
375 void fill_filespec(struct diff_filespec
*spec
, const unsigned char *sha1
,
379 spec
->mode
= DIFF_FILE_CANON_MODE(mode
);
380 memcpy(spec
->sha1
, sha1
, 20);
381 spec
->sha1_valid
= !!memcmp(sha1
, null_sha1
, 20);
386 * Given a name and sha1 pair, if the dircache tells us the file in
387 * the work tree has that object contents, return true, so that
388 * prepare_temp_file() does not have to inflate and extract.
390 static int work_tree_matches(const char *name
, const unsigned char *sha1
)
392 struct cache_entry
*ce
;
396 /* We do not read the cache ourselves here, because the
397 * benchmark with my previous version that always reads cache
398 * shows that it makes things worse for diff-tree comparing
399 * two linux-2.6 kernel trees in an already checked out work
400 * tree. This is because most diff-tree comparisons deal with
401 * only a small number of files, while reading the cache is
402 * expensive for a large project, and its cost outweighs the
403 * savings we get by not inflating the object to a temporary
404 * file. Practically, this code only helps when we are used
405 * by diff-cache --cached, which does read the cache before
412 pos
= cache_name_pos(name
, len
);
415 ce
= active_cache
[pos
];
416 if ((lstat(name
, &st
) < 0) ||
417 !S_ISREG(st
.st_mode
) || /* careful! */
418 ce_match_stat(ce
, &st
, 0) ||
419 memcmp(sha1
, ce
->sha1
, 20))
421 /* we return 1 only when we can stat, it is a regular file,
422 * stat information matches, and sha1 recorded in the cache
423 * matches. I.e. we know the file in the work tree really is
424 * the same as the <name, sha1> pair.
429 static struct sha1_size_cache
{
430 unsigned char sha1
[20];
433 static int sha1_size_cache_nr
, sha1_size_cache_alloc
;
435 static struct sha1_size_cache
*locate_size_cache(unsigned char *sha1
,
440 struct sha1_size_cache
*e
;
443 last
= sha1_size_cache_nr
;
444 while (last
> first
) {
445 int cmp
, next
= (last
+ first
) >> 1;
446 e
= sha1_size_cache
[next
];
447 cmp
= memcmp(e
->sha1
, sha1
, 20);
459 /* insert to make it at "first" */
460 if (sha1_size_cache_alloc
<= sha1_size_cache_nr
) {
461 sha1_size_cache_alloc
= alloc_nr(sha1_size_cache_alloc
);
462 sha1_size_cache
= xrealloc(sha1_size_cache
,
463 sha1_size_cache_alloc
*
464 sizeof(*sha1_size_cache
));
466 sha1_size_cache_nr
++;
467 if (first
< sha1_size_cache_nr
)
468 memmove(sha1_size_cache
+ first
+ 1, sha1_size_cache
+ first
,
469 (sha1_size_cache_nr
- first
- 1) *
470 sizeof(*sha1_size_cache
));
471 e
= xmalloc(sizeof(struct sha1_size_cache
));
472 sha1_size_cache
[first
] = e
;
473 memcpy(e
->sha1
, sha1
, 20);
479 * While doing rename detection and pickaxe operation, we may need to
480 * grab the data for the blob (or file) for our own in-core comparison.
481 * diff_filespec has data and size fields for this purpose.
483 int diff_populate_filespec(struct diff_filespec
*s
, int size_only
)
486 if (!DIFF_FILE_VALID(s
))
487 die("internal error: asking to populate invalid file.");
488 if (S_ISDIR(s
->mode
))
496 if (!s
->sha1_valid
||
497 work_tree_matches(s
->path
, s
->sha1
)) {
500 if (lstat(s
->path
, &st
) < 0) {
501 if (errno
== ENOENT
) {
510 s
->size
= st
.st_size
;
515 if (S_ISLNK(st
.st_mode
)) {
517 s
->data
= xmalloc(s
->size
);
519 ret
= readlink(s
->path
, s
->data
, s
->size
);
526 fd
= open(s
->path
, O_RDONLY
);
529 s
->data
= mmap(NULL
, s
->size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
531 if (s
->data
== MAP_FAILED
)
533 s
->should_munmap
= 1;
537 struct sha1_size_cache
*e
;
540 e
= locate_size_cache(s
->sha1
, 1, 0);
545 if (!sha1_object_info(s
->sha1
, type
, &s
->size
))
546 locate_size_cache(s
->sha1
, 0, s
->size
);
549 s
->data
= read_sha1_file(s
->sha1
, type
, &s
->size
);
556 void diff_free_filespec_data(struct diff_filespec
*s
)
560 else if (s
->should_munmap
)
561 munmap(s
->data
, s
->size
);
562 s
->should_free
= s
->should_munmap
= 0;
568 static void prep_temp_blob(struct diff_tempfile
*temp
,
571 const unsigned char *sha1
,
576 fd
= git_mkstemp(temp
->tmp_path
, TEMPFILE_PATH_LEN
, ".diff_XXXXXX");
578 die("unable to create temp-file");
579 if (write(fd
, blob
, size
) != size
)
580 die("unable to write temp-file");
582 temp
->name
= temp
->tmp_path
;
583 strcpy(temp
->hex
, sha1_to_hex(sha1
));
585 sprintf(temp
->mode
, "%06o", mode
);
588 static void prepare_temp_file(const char *name
,
589 struct diff_tempfile
*temp
,
590 struct diff_filespec
*one
)
592 if (!DIFF_FILE_VALID(one
)) {
594 /* A '-' entry produces this for file-2, and
595 * a '+' entry produces this for file-1.
597 temp
->name
= "/dev/null";
598 strcpy(temp
->hex
, ".");
599 strcpy(temp
->mode
, ".");
603 if (!one
->sha1_valid
||
604 work_tree_matches(name
, one
->sha1
)) {
606 if (lstat(name
, &st
) < 0) {
608 goto not_a_valid_file
;
609 die("stat(%s): %s", name
, strerror(errno
));
611 if (S_ISLNK(st
.st_mode
)) {
613 char buf
[PATH_MAX
+ 1]; /* ought to be SYMLINK_MAX */
614 if (sizeof(buf
) <= st
.st_size
)
615 die("symlink too long: %s", name
);
616 ret
= readlink(name
, buf
, st
.st_size
);
618 die("readlink(%s)", name
);
619 prep_temp_blob(temp
, buf
, st
.st_size
,
621 one
->sha1
: null_sha1
),
623 one
->mode
: S_IFLNK
));
626 /* we can borrow from the file in the work tree */
628 if (!one
->sha1_valid
)
629 strcpy(temp
->hex
, sha1_to_hex(null_sha1
));
631 strcpy(temp
->hex
, sha1_to_hex(one
->sha1
));
632 /* Even though we may sometimes borrow the
633 * contents from the work tree, we always want
634 * one->mode. mode is trustworthy even when
635 * !(one->sha1_valid), as long as
636 * DIFF_FILE_VALID(one).
638 sprintf(temp
->mode
, "%06o", one
->mode
);
643 if (diff_populate_filespec(one
, 0))
644 die("cannot read data blob for %s", one
->path
);
645 prep_temp_blob(temp
, one
->data
, one
->size
,
646 one
->sha1
, one
->mode
);
650 static void remove_tempfile(void)
654 for (i
= 0; i
< 2; i
++)
655 if (diff_temp
[i
].name
== diff_temp
[i
].tmp_path
) {
656 unlink(diff_temp
[i
].name
);
657 diff_temp
[i
].name
= NULL
;
661 static void remove_tempfile_on_signal(int signo
)
664 signal(SIGINT
, SIG_DFL
);
668 static int spawn_prog(const char *pgm
, const char **arg
)
676 die("unable to fork");
678 execvp(pgm
, (char *const*) arg
);
682 while (waitpid(pid
, &status
, 0) < 0) {
688 /* Earlier we did not check the exit status because
689 * diff exits non-zero if files are different, and
690 * we are not interested in knowing that. It was a
691 * mistake which made it harder to quit a diff-*
692 * session that uses the git-apply-patch-script as
693 * the GIT_EXTERNAL_DIFF. A custom GIT_EXTERNAL_DIFF
694 * should also exit non-zero only when it wants to
695 * abort the entire diff-* session.
697 if (WIFEXITED(status
) && !WEXITSTATUS(status
))
702 /* An external diff command takes:
704 * diff-cmd name infile1 infile1-sha1 infile1-mode \
705 * infile2 infile2-sha1 infile2-mode [ rename-to ]
708 static void run_external_diff(const char *pgm
,
711 struct diff_filespec
*one
,
712 struct diff_filespec
*two
,
713 const char *xfrm_msg
,
714 int complete_rewrite
)
716 const char *spawn_arg
[10];
717 struct diff_tempfile
*temp
= diff_temp
;
719 static int atexit_asked
= 0;
720 const char *othername
;
722 othername
= (other
? other
: name
);
724 prepare_temp_file(name
, &temp
[0], one
);
725 prepare_temp_file(othername
, &temp
[1], two
);
726 if (! atexit_asked
&&
727 (temp
[0].name
== temp
[0].tmp_path
||
728 temp
[1].name
== temp
[1].tmp_path
)) {
730 atexit(remove_tempfile
);
732 signal(SIGINT
, remove_tempfile_on_signal
);
736 const char **arg
= &spawn_arg
[0];
740 *arg
++ = temp
[0].name
;
741 *arg
++ = temp
[0].hex
;
742 *arg
++ = temp
[0].mode
;
743 *arg
++ = temp
[1].name
;
744 *arg
++ = temp
[1].hex
;
745 *arg
++ = temp
[1].mode
;
757 pgm
= builtin_diff(name
, othername
, temp
, xfrm_msg
, complete_rewrite
, spawn_arg
);
759 printf("* Unmerged path %s\n", name
);
764 retval
= spawn_prog(pgm
, spawn_arg
);
767 fprintf(stderr
, "external diff died, stopping at %s.\n", name
);
772 static void diff_fill_sha1_info(struct diff_filespec
*one
)
774 if (DIFF_FILE_VALID(one
)) {
775 if (!one
->sha1_valid
) {
777 if (lstat(one
->path
, &st
) < 0)
778 die("stat %s", one
->path
);
779 if (index_path(one
->sha1
, one
->path
, &st
, 0))
780 die("cannot hash %s\n", one
->path
);
784 memset(one
->sha1
, 0, 20);
787 static void run_diff(struct diff_filepair
*p
, struct diff_options
*o
)
789 const char *pgm
= external_diff();
790 char msg
[PATH_MAX
*2+300], *xfrm_msg
;
791 struct diff_filespec
*one
;
792 struct diff_filespec
*two
;
795 char *name_munged
, *other_munged
;
796 int complete_rewrite
= 0;
799 if (DIFF_PAIR_UNMERGED(p
)) {
801 run_external_diff(pgm
, p
->one
->path
, NULL
, NULL
, NULL
, NULL
,
807 other
= (strcmp(name
, p
->two
->path
) ? p
->two
->path
: NULL
);
808 name_munged
= quote_one(name
);
809 other_munged
= quote_one(other
);
810 one
= p
->one
; two
= p
->two
;
812 diff_fill_sha1_info(one
);
813 diff_fill_sha1_info(two
);
817 case DIFF_STATUS_COPIED
:
818 len
+= snprintf(msg
+ len
, sizeof(msg
) - len
,
819 "similarity index %d%%\n"
822 (int)(0.5 + p
->score
* 100.0/MAX_SCORE
),
823 name_munged
, other_munged
);
825 case DIFF_STATUS_RENAMED
:
826 len
+= snprintf(msg
+ len
, sizeof(msg
) - len
,
827 "similarity index %d%%\n"
830 (int)(0.5 + p
->score
* 100.0/MAX_SCORE
),
831 name_munged
, other_munged
);
833 case DIFF_STATUS_MODIFIED
:
835 len
+= snprintf(msg
+ len
, sizeof(msg
) - len
,
836 "dissimilarity index %d%%\n",
837 (int)(0.5 + p
->score
*
839 complete_rewrite
= 1;
848 if (memcmp(one
->sha1
, two
->sha1
, 20)) {
850 int abbrev
= o
->full_index
? 40 : DEFAULT_ABBREV
;
851 memcpy(one_sha1
, sha1_to_hex(one
->sha1
), 41);
853 len
+= snprintf(msg
+ len
, sizeof(msg
) - len
,
855 abbrev
, one_sha1
, abbrev
,
856 sha1_to_hex(two
->sha1
));
857 if (one
->mode
== two
->mode
)
858 len
+= snprintf(msg
+ len
, sizeof(msg
) - len
,
860 len
+= snprintf(msg
+ len
, sizeof(msg
) - len
, "\n");
865 xfrm_msg
= len
? msg
: NULL
;
868 DIFF_FILE_VALID(one
) && DIFF_FILE_VALID(two
) &&
869 (S_IFMT
& one
->mode
) != (S_IFMT
& two
->mode
)) {
870 /* a filepair that changes between file and symlink
871 * needs to be split into deletion and creation.
873 struct diff_filespec
*null
= alloc_filespec(two
->path
);
874 run_external_diff(NULL
, name
, other
, one
, null
, xfrm_msg
, 0);
876 null
= alloc_filespec(one
->path
);
877 run_external_diff(NULL
, name
, other
, null
, two
, xfrm_msg
, 0);
881 run_external_diff(pgm
, name
, other
, one
, two
, xfrm_msg
,
888 void diff_setup(struct diff_options
*options
)
890 memset(options
, 0, sizeof(*options
));
891 options
->output_format
= DIFF_FORMAT_RAW
;
892 options
->line_termination
= '\n';
893 options
->break_opt
= -1;
894 options
->rename_limit
= -1;
896 options
->change
= diff_change
;
897 options
->add_remove
= diff_addremove
;
900 int diff_setup_done(struct diff_options
*options
)
902 if ((options
->find_copies_harder
&&
903 options
->detect_rename
!= DIFF_DETECT_COPY
) ||
904 (0 <= options
->rename_limit
&& !options
->detect_rename
))
906 if (options
->detect_rename
&& options
->rename_limit
< 0)
907 options
->rename_limit
= diff_rename_limit_default
;
908 if (options
->setup
& DIFF_SETUP_USE_CACHE
) {
910 /* read-cache does not die even when it fails
911 * so it is safe for us to do this here. Also
912 * it does not smudge active_cache or active_nr
913 * when it fails, so we do not have to worry about
914 * cleaning it up ourselves either.
918 if (options
->setup
& DIFF_SETUP_USE_SIZE_CACHE
)
920 if (options
->abbrev
<= 0 || 40 < options
->abbrev
)
921 options
->abbrev
= 40; /* full */
926 int diff_opt_parse(struct diff_options
*options
, const char **av
, int ac
)
928 const char *arg
= av
[0];
929 if (!strcmp(arg
, "-p") || !strcmp(arg
, "-u"))
930 options
->output_format
= DIFF_FORMAT_PATCH
;
931 else if (!strcmp(arg
, "-z"))
932 options
->line_termination
= 0;
933 else if (!strncmp(arg
, "-l", 2))
934 options
->rename_limit
= strtoul(arg
+2, NULL
, 10);
935 else if (!strcmp(arg
, "--full-index"))
936 options
->full_index
= 1;
937 else if (!strcmp(arg
, "--name-only"))
938 options
->output_format
= DIFF_FORMAT_NAME
;
939 else if (!strcmp(arg
, "--name-status"))
940 options
->output_format
= DIFF_FORMAT_NAME_STATUS
;
941 else if (!strcmp(arg
, "-R"))
942 options
->reverse_diff
= 1;
943 else if (!strncmp(arg
, "-S", 2))
944 options
->pickaxe
= arg
+ 2;
945 else if (!strcmp(arg
, "-s"))
946 options
->output_format
= DIFF_FORMAT_NO_OUTPUT
;
947 else if (!strncmp(arg
, "-O", 2))
948 options
->orderfile
= arg
+ 2;
949 else if (!strncmp(arg
, "--diff-filter=", 14))
950 options
->filter
= arg
+ 14;
951 else if (!strcmp(arg
, "--pickaxe-all"))
952 options
->pickaxe_opts
= DIFF_PICKAXE_ALL
;
953 else if (!strncmp(arg
, "-B", 2)) {
954 if ((options
->break_opt
=
955 diff_scoreopt_parse(arg
)) == -1)
958 else if (!strncmp(arg
, "-M", 2)) {
959 if ((options
->rename_score
=
960 diff_scoreopt_parse(arg
)) == -1)
962 options
->detect_rename
= DIFF_DETECT_RENAME
;
964 else if (!strncmp(arg
, "-C", 2)) {
965 if ((options
->rename_score
=
966 diff_scoreopt_parse(arg
)) == -1)
968 options
->detect_rename
= DIFF_DETECT_COPY
;
970 else if (!strcmp(arg
, "--find-copies-harder"))
971 options
->find_copies_harder
= 1;
972 else if (!strcmp(arg
, "--abbrev"))
973 options
->abbrev
= DEFAULT_ABBREV
;
974 else if (!strncmp(arg
, "--abbrev=", 9)) {
975 options
->abbrev
= strtoul(arg
+ 9, NULL
, 10);
976 if (options
->abbrev
< MINIMUM_ABBREV
)
977 options
->abbrev
= MINIMUM_ABBREV
;
978 else if (40 < options
->abbrev
)
979 options
->abbrev
= 40;
986 static int parse_num(const char **cp_p
)
988 unsigned long num
, scale
;
990 const char *cp
= *cp_p
;
997 if ( !dot
&& ch
== '.' ) {
1000 } else if ( ch
== '%' ) {
1001 scale
= dot
? scale
*100 : 100;
1002 cp
++; /* % is always at the end */
1004 } else if ( ch
>= '0' && ch
<= '9' ) {
1005 if ( scale
< 100000 ) {
1007 num
= (num
*10) + (ch
-'0');
1016 /* user says num divided by scale and we say internally that
1017 * is MAX_SCORE * num / scale.
1019 return (num
>= scale
) ? MAX_SCORE
: (MAX_SCORE
* num
/ scale
);
1022 int diff_scoreopt_parse(const char *opt
)
1024 int opt1
, opt2
, cmd
;
1029 if (cmd
!= 'M' && cmd
!= 'C' && cmd
!= 'B')
1030 return -1; /* that is not a -M, -C nor -B option */
1032 opt1
= parse_num(&opt
);
1038 else if (*opt
!= '/')
1039 return -1; /* we expect -B80/99 or -B80 */
1042 opt2
= parse_num(&opt
);
1047 return opt1
| (opt2
<< 16);
1050 struct diff_queue_struct diff_queued_diff
;
1052 void diff_q(struct diff_queue_struct
*queue
, struct diff_filepair
*dp
)
1054 if (queue
->alloc
<= queue
->nr
) {
1055 queue
->alloc
= alloc_nr(queue
->alloc
);
1056 queue
->queue
= xrealloc(queue
->queue
,
1057 sizeof(dp
) * queue
->alloc
);
1059 queue
->queue
[queue
->nr
++] = dp
;
1062 struct diff_filepair
*diff_queue(struct diff_queue_struct
*queue
,
1063 struct diff_filespec
*one
,
1064 struct diff_filespec
*two
)
1066 struct diff_filepair
*dp
= xmalloc(sizeof(*dp
));
1071 dp
->source_stays
= 0;
1072 dp
->broken_pair
= 0;
1078 void diff_free_filepair(struct diff_filepair
*p
)
1080 diff_free_filespec_data(p
->one
);
1081 diff_free_filespec_data(p
->two
);
1087 /* This is different from find_unique_abbrev() in that
1088 * it stuffs the result with dots for alignment.
1090 const char *diff_unique_abbrev(const unsigned char *sha1
, int len
)
1095 return sha1_to_hex(sha1
);
1097 abbrev
= find_unique_abbrev(sha1
, len
);
1099 return sha1_to_hex(sha1
);
1100 abblen
= strlen(abbrev
);
1102 static char hex
[41];
1103 if (len
< abblen
&& abblen
<= len
+ 2)
1104 sprintf(hex
, "%s%.*s", abbrev
, len
+3-abblen
, "..");
1106 sprintf(hex
, "%s...", abbrev
);
1109 return sha1_to_hex(sha1
);
1112 static void diff_flush_raw(struct diff_filepair
*p
,
1113 int line_termination
,
1114 int inter_name_termination
,
1115 struct diff_options
*options
)
1119 int abbrev
= options
->abbrev
;
1120 const char *path_one
, *path_two
;
1121 int output_format
= options
->output_format
;
1123 path_one
= p
->one
->path
;
1124 path_two
= p
->two
->path
;
1125 if (line_termination
) {
1126 path_one
= quote_one(path_one
);
1127 path_two
= quote_one(path_two
);
1131 sprintf(status
, "%c%03d", p
->status
,
1132 (int)(0.5 + p
->score
* 100.0/MAX_SCORE
));
1134 status
[0] = p
->status
;
1137 switch (p
->status
) {
1138 case DIFF_STATUS_COPIED
:
1139 case DIFF_STATUS_RENAMED
:
1142 case DIFF_STATUS_ADDED
:
1143 case DIFF_STATUS_DELETED
:
1150 if (output_format
!= DIFF_FORMAT_NAME_STATUS
) {
1151 printf(":%06o %06o %s ",
1152 p
->one
->mode
, p
->two
->mode
,
1153 diff_unique_abbrev(p
->one
->sha1
, abbrev
));
1155 diff_unique_abbrev(p
->two
->sha1
, abbrev
));
1157 printf("%s%c%s", status
, inter_name_termination
, path_one
);
1159 printf("%c%s", inter_name_termination
, path_two
);
1160 putchar(line_termination
);
1161 if (path_one
!= p
->one
->path
)
1162 free((void*)path_one
);
1163 if (path_two
!= p
->two
->path
)
1164 free((void*)path_two
);
1167 static void diff_flush_name(struct diff_filepair
*p
,
1168 int inter_name_termination
,
1169 int line_termination
)
1171 char *path
= p
->two
->path
;
1173 if (line_termination
)
1174 path
= quote_one(p
->two
->path
);
1176 path
= p
->two
->path
;
1177 printf("%s%c", path
, line_termination
);
1178 if (p
->two
->path
!= path
)
1182 int diff_unmodified_pair(struct diff_filepair
*p
)
1184 /* This function is written stricter than necessary to support
1185 * the currently implemented transformers, but the idea is to
1186 * let transformers to produce diff_filepairs any way they want,
1187 * and filter and clean them up here before producing the output.
1189 struct diff_filespec
*one
, *two
;
1191 if (DIFF_PAIR_UNMERGED(p
))
1192 return 0; /* unmerged is interesting */
1197 /* deletion, addition, mode or type change
1198 * and rename are all interesting.
1200 if (DIFF_FILE_VALID(one
) != DIFF_FILE_VALID(two
) ||
1201 DIFF_PAIR_MODE_CHANGED(p
) ||
1202 strcmp(one
->path
, two
->path
))
1205 /* both are valid and point at the same path. that is, we are
1206 * dealing with a change.
1208 if (one
->sha1_valid
&& two
->sha1_valid
&&
1209 !memcmp(one
->sha1
, two
->sha1
, sizeof(one
->sha1
)))
1210 return 1; /* no change */
1211 if (!one
->sha1_valid
&& !two
->sha1_valid
)
1212 return 1; /* both look at the same file on the filesystem. */
1216 static void diff_flush_patch(struct diff_filepair
*p
, struct diff_options
*o
)
1218 if (diff_unmodified_pair(p
))
1221 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
1222 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
1223 return; /* no tree diffs in patch format */
1228 int diff_queue_is_empty(void)
1230 struct diff_queue_struct
*q
= &diff_queued_diff
;
1232 for (i
= 0; i
< q
->nr
; i
++)
1233 if (!diff_unmodified_pair(q
->queue
[i
]))
1239 void diff_debug_filespec(struct diff_filespec
*s
, int x
, const char *one
)
1241 fprintf(stderr
, "queue[%d] %s (%s) %s %06o %s\n",
1244 DIFF_FILE_VALID(s
) ? "valid" : "invalid",
1246 s
->sha1_valid
? sha1_to_hex(s
->sha1
) : "");
1247 fprintf(stderr
, "queue[%d] %s size %lu flags %d\n",
1249 s
->size
, s
->xfrm_flags
);
1252 void diff_debug_filepair(const struct diff_filepair
*p
, int i
)
1254 diff_debug_filespec(p
->one
, i
, "one");
1255 diff_debug_filespec(p
->two
, i
, "two");
1256 fprintf(stderr
, "score %d, status %c stays %d broken %d\n",
1257 p
->score
, p
->status
? p
->status
: '?',
1258 p
->source_stays
, p
->broken_pair
);
1261 void diff_debug_queue(const char *msg
, struct diff_queue_struct
*q
)
1265 fprintf(stderr
, "%s\n", msg
);
1266 fprintf(stderr
, "q->nr = %d\n", q
->nr
);
1267 for (i
= 0; i
< q
->nr
; i
++) {
1268 struct diff_filepair
*p
= q
->queue
[i
];
1269 diff_debug_filepair(p
, i
);
1274 static void diff_resolve_rename_copy(void)
1277 struct diff_filepair
*p
, *pp
;
1278 struct diff_queue_struct
*q
= &diff_queued_diff
;
1280 diff_debug_queue("resolve-rename-copy", q
);
1282 for (i
= 0; i
< q
->nr
; i
++) {
1284 p
->status
= 0; /* undecided */
1285 if (DIFF_PAIR_UNMERGED(p
))
1286 p
->status
= DIFF_STATUS_UNMERGED
;
1287 else if (!DIFF_FILE_VALID(p
->one
))
1288 p
->status
= DIFF_STATUS_ADDED
;
1289 else if (!DIFF_FILE_VALID(p
->two
))
1290 p
->status
= DIFF_STATUS_DELETED
;
1291 else if (DIFF_PAIR_TYPE_CHANGED(p
))
1292 p
->status
= DIFF_STATUS_TYPE_CHANGED
;
1294 /* from this point on, we are dealing with a pair
1295 * whose both sides are valid and of the same type, i.e.
1296 * either in-place edit or rename/copy edit.
1298 else if (DIFF_PAIR_RENAME(p
)) {
1299 if (p
->source_stays
) {
1300 p
->status
= DIFF_STATUS_COPIED
;
1303 /* See if there is some other filepair that
1304 * copies from the same source as us. If so
1305 * we are a copy. Otherwise we are either a
1306 * copy if the path stays, or a rename if it
1307 * does not, but we already handled "stays" case.
1309 for (j
= i
+ 1; j
< q
->nr
; j
++) {
1311 if (strcmp(pp
->one
->path
, p
->one
->path
))
1312 continue; /* not us */
1313 if (!DIFF_PAIR_RENAME(pp
))
1314 continue; /* not a rename/copy */
1315 /* pp is a rename/copy from the same source */
1316 p
->status
= DIFF_STATUS_COPIED
;
1320 p
->status
= DIFF_STATUS_RENAMED
;
1322 else if (memcmp(p
->one
->sha1
, p
->two
->sha1
, 20) ||
1323 p
->one
->mode
!= p
->two
->mode
)
1324 p
->status
= DIFF_STATUS_MODIFIED
;
1326 /* This is a "no-change" entry and should not
1327 * happen anymore, but prepare for broken callers.
1329 error("feeding unmodified %s to diffcore",
1331 p
->status
= DIFF_STATUS_UNKNOWN
;
1334 diff_debug_queue("resolve-rename-copy done", q
);
1337 void diff_flush(struct diff_options
*options
)
1339 struct diff_queue_struct
*q
= &diff_queued_diff
;
1341 int inter_name_termination
= '\t';
1342 int diff_output_format
= options
->output_format
;
1343 int line_termination
= options
->line_termination
;
1345 if (!line_termination
)
1346 inter_name_termination
= 0;
1348 for (i
= 0; i
< q
->nr
; i
++) {
1349 struct diff_filepair
*p
= q
->queue
[i
];
1350 if ((diff_output_format
== DIFF_FORMAT_NO_OUTPUT
) ||
1351 (p
->status
== DIFF_STATUS_UNKNOWN
))
1354 die("internal error in diff-resolve-rename-copy");
1355 switch (diff_output_format
) {
1356 case DIFF_FORMAT_PATCH
:
1357 diff_flush_patch(p
, options
);
1359 case DIFF_FORMAT_RAW
:
1360 case DIFF_FORMAT_NAME_STATUS
:
1361 diff_flush_raw(p
, line_termination
,
1362 inter_name_termination
,
1365 case DIFF_FORMAT_NAME
:
1367 inter_name_termination
,
1371 diff_free_filepair(q
->queue
[i
]);
1375 q
->nr
= q
->alloc
= 0;
1378 static void diffcore_apply_filter(const char *filter
)
1381 struct diff_queue_struct
*q
= &diff_queued_diff
;
1382 struct diff_queue_struct outq
;
1384 outq
.nr
= outq
.alloc
= 0;
1389 if (strchr(filter
, DIFF_STATUS_FILTER_AON
)) {
1391 for (i
= found
= 0; !found
&& i
< q
->nr
; i
++) {
1392 struct diff_filepair
*p
= q
->queue
[i
];
1393 if (((p
->status
== DIFF_STATUS_MODIFIED
) &&
1395 strchr(filter
, DIFF_STATUS_FILTER_BROKEN
)) ||
1397 strchr(filter
, DIFF_STATUS_MODIFIED
)))) ||
1398 ((p
->status
!= DIFF_STATUS_MODIFIED
) &&
1399 strchr(filter
, p
->status
)))
1405 /* otherwise we will clear the whole queue
1406 * by copying the empty outq at the end of this
1407 * function, but first clear the current entries
1410 for (i
= 0; i
< q
->nr
; i
++)
1411 diff_free_filepair(q
->queue
[i
]);
1414 /* Only the matching ones */
1415 for (i
= 0; i
< q
->nr
; i
++) {
1416 struct diff_filepair
*p
= q
->queue
[i
];
1418 if (((p
->status
== DIFF_STATUS_MODIFIED
) &&
1420 strchr(filter
, DIFF_STATUS_FILTER_BROKEN
)) ||
1422 strchr(filter
, DIFF_STATUS_MODIFIED
)))) ||
1423 ((p
->status
!= DIFF_STATUS_MODIFIED
) &&
1424 strchr(filter
, p
->status
)))
1427 diff_free_filepair(p
);
1434 void diffcore_std(struct diff_options
*options
)
1436 if (options
->paths
&& options
->paths
[0])
1437 diffcore_pathspec(options
->paths
);
1438 if (options
->break_opt
!= -1)
1439 diffcore_break(options
->break_opt
);
1440 if (options
->detect_rename
)
1441 diffcore_rename(options
);
1442 if (options
->break_opt
!= -1)
1443 diffcore_merge_broken();
1444 if (options
->pickaxe
)
1445 diffcore_pickaxe(options
->pickaxe
, options
->pickaxe_opts
);
1446 if (options
->orderfile
)
1447 diffcore_order(options
->orderfile
);
1448 diff_resolve_rename_copy();
1449 diffcore_apply_filter(options
->filter
);
1453 void diffcore_std_no_resolve(struct diff_options
*options
)
1455 if (options
->pickaxe
)
1456 diffcore_pickaxe(options
->pickaxe
, options
->pickaxe_opts
);
1457 if (options
->orderfile
)
1458 diffcore_order(options
->orderfile
);
1459 diffcore_apply_filter(options
->filter
);
1462 void diff_addremove(struct diff_options
*options
,
1463 int addremove
, unsigned mode
,
1464 const unsigned char *sha1
,
1465 const char *base
, const char *path
)
1467 char concatpath
[PATH_MAX
];
1468 struct diff_filespec
*one
, *two
;
1470 /* This may look odd, but it is a preparation for
1471 * feeding "there are unchanged files which should
1472 * not produce diffs, but when you are doing copy
1473 * detection you would need them, so here they are"
1474 * entries to the diff-core. They will be prefixed
1475 * with something like '=' or '*' (I haven't decided
1476 * which but should not make any difference).
1477 * Feeding the same new and old to diff_change()
1478 * also has the same effect.
1479 * Before the final output happens, they are pruned after
1480 * merged into rename/copy pairs as appropriate.
1482 if (options
->reverse_diff
)
1483 addremove
= (addremove
== '+' ? '-' :
1484 addremove
== '-' ? '+' : addremove
);
1486 if (!path
) path
= "";
1487 sprintf(concatpath
, "%s%s", base
, path
);
1488 one
= alloc_filespec(concatpath
);
1489 two
= alloc_filespec(concatpath
);
1491 if (addremove
!= '+')
1492 fill_filespec(one
, sha1
, mode
);
1493 if (addremove
!= '-')
1494 fill_filespec(two
, sha1
, mode
);
1496 diff_queue(&diff_queued_diff
, one
, two
);
1499 void diff_change(struct diff_options
*options
,
1500 unsigned old_mode
, unsigned new_mode
,
1501 const unsigned char *old_sha1
,
1502 const unsigned char *new_sha1
,
1503 const char *base
, const char *path
)
1505 char concatpath
[PATH_MAX
];
1506 struct diff_filespec
*one
, *two
;
1508 if (options
->reverse_diff
) {
1510 const unsigned char *tmp_c
;
1511 tmp
= old_mode
; old_mode
= new_mode
; new_mode
= tmp
;
1512 tmp_c
= old_sha1
; old_sha1
= new_sha1
; new_sha1
= tmp_c
;
1514 if (!path
) path
= "";
1515 sprintf(concatpath
, "%s%s", base
, path
);
1516 one
= alloc_filespec(concatpath
);
1517 two
= alloc_filespec(concatpath
);
1518 fill_filespec(one
, old_sha1
, old_mode
);
1519 fill_filespec(two
, new_sha1
, new_mode
);
1521 diff_queue(&diff_queued_diff
, one
, two
);
1524 void diff_unmerge(struct diff_options
*options
,
1527 struct diff_filespec
*one
, *two
;
1528 one
= alloc_filespec(path
);
1529 two
= alloc_filespec(path
);
1530 diff_queue(&diff_queued_diff
, one
, two
);