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
)
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
))
198 #define FIRST_FEW_BYTES 8000
199 static int mmfile_is_binary(mmfile_t
*mf
)
202 if (FIRST_FEW_BYTES
< sz
)
203 sz
= FIRST_FEW_BYTES
;
204 if (memchr(mf
->ptr
, 0, sz
))
209 static void builtin_diff(const char *name_a
,
211 struct diff_filespec
*one
,
212 struct diff_filespec
*two
,
213 const char *xfrm_msg
,
214 int complete_rewrite
)
220 a_one
= quote_two("a/", name_a
);
221 b_two
= quote_two("b/", name_b
);
222 lbl
[0] = DIFF_FILE_VALID(one
) ? a_one
: "/dev/null";
223 lbl
[1] = DIFF_FILE_VALID(two
) ? b_two
: "/dev/null";
224 printf("diff --git %s %s\n", a_one
, b_two
);
225 if (lbl
[0][0] == '/') {
227 printf("new file mode %06o\n", two
->mode
);
228 if (xfrm_msg
&& xfrm_msg
[0])
231 else if (lbl
[1][0] == '/') {
232 printf("deleted file mode %06o\n", one
->mode
);
233 if (xfrm_msg
&& xfrm_msg
[0])
237 if (one
->mode
!= two
->mode
) {
238 printf("old mode %06o\n", one
->mode
);
239 printf("new mode %06o\n", two
->mode
);
241 if (xfrm_msg
&& xfrm_msg
[0])
244 * we do not run diff between different kind
247 if ((one
->mode
^ two
->mode
) & S_IFMT
)
248 goto free_ab_and_return
;
249 if (complete_rewrite
) {
250 emit_rewrite_diff(name_a
, name_b
, one
, two
);
251 goto free_ab_and_return
;
255 if (fill_mmfile(&mf1
, one
) < 0 || fill_mmfile(&mf2
, two
) < 0)
256 die("unable to read files to diff");
258 if (mmfile_is_binary(&mf1
) || mmfile_is_binary(&mf2
))
259 printf("Binary files %s and %s differ\n", lbl
[0], lbl
[1]);
261 /* Crazy xdl interfaces.. */
262 const char *diffopts
= getenv("GIT_DIFF_OPTS");
266 struct emit_callback ecbdata
;
268 ecbdata
.label_path
= lbl
;
269 xpp
.flags
= XDF_NEED_MINIMAL
;
271 xecfg
.flags
= XDL_EMIT_FUNCNAMES
;
274 else if (!strncmp(diffopts
, "--unified=", 10))
275 xecfg
.ctxlen
= strtoul(diffopts
+ 10, NULL
, 10);
276 else if (!strncmp(diffopts
, "-u", 2))
277 xecfg
.ctxlen
= strtoul(diffopts
+ 2, NULL
, 10);
280 xdl_diff(&mf1
, &mf2
, &xpp
, &xecfg
, &ecb
);
289 struct diff_filespec
*alloc_filespec(const char *path
)
291 int namelen
= strlen(path
);
292 struct diff_filespec
*spec
= xmalloc(sizeof(*spec
) + namelen
+ 1);
294 memset(spec
, 0, sizeof(*spec
));
295 spec
->path
= (char *)(spec
+ 1);
296 memcpy(spec
->path
, path
, namelen
+1);
300 void fill_filespec(struct diff_filespec
*spec
, const unsigned char *sha1
,
304 spec
->mode
= canon_mode(mode
);
305 memcpy(spec
->sha1
, sha1
, 20);
306 spec
->sha1_valid
= !!memcmp(sha1
, null_sha1
, 20);
311 * Given a name and sha1 pair, if the dircache tells us the file in
312 * the work tree has that object contents, return true, so that
313 * prepare_temp_file() does not have to inflate and extract.
315 static int work_tree_matches(const char *name
, const unsigned char *sha1
)
317 struct cache_entry
*ce
;
321 /* We do not read the cache ourselves here, because the
322 * benchmark with my previous version that always reads cache
323 * shows that it makes things worse for diff-tree comparing
324 * two linux-2.6 kernel trees in an already checked out work
325 * tree. This is because most diff-tree comparisons deal with
326 * only a small number of files, while reading the cache is
327 * expensive for a large project, and its cost outweighs the
328 * savings we get by not inflating the object to a temporary
329 * file. Practically, this code only helps when we are used
330 * by diff-cache --cached, which does read the cache before
337 pos
= cache_name_pos(name
, len
);
340 ce
= active_cache
[pos
];
341 if ((lstat(name
, &st
) < 0) ||
342 !S_ISREG(st
.st_mode
) || /* careful! */
343 ce_match_stat(ce
, &st
, 0) ||
344 memcmp(sha1
, ce
->sha1
, 20))
346 /* we return 1 only when we can stat, it is a regular file,
347 * stat information matches, and sha1 recorded in the cache
348 * matches. I.e. we know the file in the work tree really is
349 * the same as the <name, sha1> pair.
354 static struct sha1_size_cache
{
355 unsigned char sha1
[20];
358 static int sha1_size_cache_nr
, sha1_size_cache_alloc
;
360 static struct sha1_size_cache
*locate_size_cache(unsigned char *sha1
,
365 struct sha1_size_cache
*e
;
368 last
= sha1_size_cache_nr
;
369 while (last
> first
) {
370 int cmp
, next
= (last
+ first
) >> 1;
371 e
= sha1_size_cache
[next
];
372 cmp
= memcmp(e
->sha1
, sha1
, 20);
384 /* insert to make it at "first" */
385 if (sha1_size_cache_alloc
<= sha1_size_cache_nr
) {
386 sha1_size_cache_alloc
= alloc_nr(sha1_size_cache_alloc
);
387 sha1_size_cache
= xrealloc(sha1_size_cache
,
388 sha1_size_cache_alloc
*
389 sizeof(*sha1_size_cache
));
391 sha1_size_cache_nr
++;
392 if (first
< sha1_size_cache_nr
)
393 memmove(sha1_size_cache
+ first
+ 1, sha1_size_cache
+ first
,
394 (sha1_size_cache_nr
- first
- 1) *
395 sizeof(*sha1_size_cache
));
396 e
= xmalloc(sizeof(struct sha1_size_cache
));
397 sha1_size_cache
[first
] = e
;
398 memcpy(e
->sha1
, sha1
, 20);
404 * While doing rename detection and pickaxe operation, we may need to
405 * grab the data for the blob (or file) for our own in-core comparison.
406 * diff_filespec has data and size fields for this purpose.
408 int diff_populate_filespec(struct diff_filespec
*s
, int size_only
)
411 if (!DIFF_FILE_VALID(s
))
412 die("internal error: asking to populate invalid file.");
413 if (S_ISDIR(s
->mode
))
421 if (!s
->sha1_valid
||
422 work_tree_matches(s
->path
, s
->sha1
)) {
425 if (lstat(s
->path
, &st
) < 0) {
426 if (errno
== ENOENT
) {
435 s
->size
= st
.st_size
;
440 if (S_ISLNK(st
.st_mode
)) {
442 s
->data
= xmalloc(s
->size
);
444 ret
= readlink(s
->path
, s
->data
, s
->size
);
451 fd
= open(s
->path
, O_RDONLY
);
454 s
->data
= mmap(NULL
, s
->size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
456 if (s
->data
== MAP_FAILED
)
458 s
->should_munmap
= 1;
462 struct sha1_size_cache
*e
;
465 e
= locate_size_cache(s
->sha1
, 1, 0);
470 if (!sha1_object_info(s
->sha1
, type
, &s
->size
))
471 locate_size_cache(s
->sha1
, 0, s
->size
);
474 s
->data
= read_sha1_file(s
->sha1
, type
, &s
->size
);
481 void diff_free_filespec_data(struct diff_filespec
*s
)
485 else if (s
->should_munmap
)
486 munmap(s
->data
, s
->size
);
487 s
->should_free
= s
->should_munmap
= 0;
493 static void prep_temp_blob(struct diff_tempfile
*temp
,
496 const unsigned char *sha1
,
501 fd
= git_mkstemp(temp
->tmp_path
, TEMPFILE_PATH_LEN
, ".diff_XXXXXX");
503 die("unable to create temp-file");
504 if (write(fd
, blob
, size
) != size
)
505 die("unable to write temp-file");
507 temp
->name
= temp
->tmp_path
;
508 strcpy(temp
->hex
, sha1_to_hex(sha1
));
510 sprintf(temp
->mode
, "%06o", mode
);
513 static void prepare_temp_file(const char *name
,
514 struct diff_tempfile
*temp
,
515 struct diff_filespec
*one
)
517 if (!DIFF_FILE_VALID(one
)) {
519 /* A '-' entry produces this for file-2, and
520 * a '+' entry produces this for file-1.
522 temp
->name
= "/dev/null";
523 strcpy(temp
->hex
, ".");
524 strcpy(temp
->mode
, ".");
528 if (!one
->sha1_valid
||
529 work_tree_matches(name
, one
->sha1
)) {
531 if (lstat(name
, &st
) < 0) {
533 goto not_a_valid_file
;
534 die("stat(%s): %s", name
, strerror(errno
));
536 if (S_ISLNK(st
.st_mode
)) {
538 char buf
[PATH_MAX
+ 1]; /* ought to be SYMLINK_MAX */
539 if (sizeof(buf
) <= st
.st_size
)
540 die("symlink too long: %s", name
);
541 ret
= readlink(name
, buf
, st
.st_size
);
543 die("readlink(%s)", name
);
544 prep_temp_blob(temp
, buf
, st
.st_size
,
546 one
->sha1
: null_sha1
),
548 one
->mode
: S_IFLNK
));
551 /* we can borrow from the file in the work tree */
553 if (!one
->sha1_valid
)
554 strcpy(temp
->hex
, sha1_to_hex(null_sha1
));
556 strcpy(temp
->hex
, sha1_to_hex(one
->sha1
));
557 /* Even though we may sometimes borrow the
558 * contents from the work tree, we always want
559 * one->mode. mode is trustworthy even when
560 * !(one->sha1_valid), as long as
561 * DIFF_FILE_VALID(one).
563 sprintf(temp
->mode
, "%06o", one
->mode
);
568 if (diff_populate_filespec(one
, 0))
569 die("cannot read data blob for %s", one
->path
);
570 prep_temp_blob(temp
, one
->data
, one
->size
,
571 one
->sha1
, one
->mode
);
575 static void remove_tempfile(void)
579 for (i
= 0; i
< 2; i
++)
580 if (diff_temp
[i
].name
== diff_temp
[i
].tmp_path
) {
581 unlink(diff_temp
[i
].name
);
582 diff_temp
[i
].name
= NULL
;
586 static void remove_tempfile_on_signal(int signo
)
589 signal(SIGINT
, SIG_DFL
);
593 static int spawn_prog(const char *pgm
, const char **arg
)
601 die("unable to fork");
603 execvp(pgm
, (char *const*) arg
);
607 while (waitpid(pid
, &status
, 0) < 0) {
613 /* Earlier we did not check the exit status because
614 * diff exits non-zero if files are different, and
615 * we are not interested in knowing that. It was a
616 * mistake which made it harder to quit a diff-*
617 * session that uses the git-apply-patch-script as
618 * the GIT_EXTERNAL_DIFF. A custom GIT_EXTERNAL_DIFF
619 * should also exit non-zero only when it wants to
620 * abort the entire diff-* session.
622 if (WIFEXITED(status
) && !WEXITSTATUS(status
))
627 /* An external diff command takes:
629 * diff-cmd name infile1 infile1-sha1 infile1-mode \
630 * infile2 infile2-sha1 infile2-mode [ rename-to ]
633 static void run_external_diff(const char *pgm
,
636 struct diff_filespec
*one
,
637 struct diff_filespec
*two
,
638 const char *xfrm_msg
,
639 int complete_rewrite
)
641 const char *spawn_arg
[10];
642 struct diff_tempfile
*temp
= diff_temp
;
644 static int atexit_asked
= 0;
645 const char *othername
;
646 const char **arg
= &spawn_arg
[0];
648 othername
= (other
? other
: name
);
650 prepare_temp_file(name
, &temp
[0], one
);
651 prepare_temp_file(othername
, &temp
[1], two
);
652 if (! atexit_asked
&&
653 (temp
[0].name
== temp
[0].tmp_path
||
654 temp
[1].name
== temp
[1].tmp_path
)) {
656 atexit(remove_tempfile
);
658 signal(SIGINT
, remove_tempfile_on_signal
);
664 *arg
++ = temp
[0].name
;
665 *arg
++ = temp
[0].hex
;
666 *arg
++ = temp
[0].mode
;
667 *arg
++ = temp
[1].name
;
668 *arg
++ = temp
[1].hex
;
669 *arg
++ = temp
[1].mode
;
679 retval
= spawn_prog(pgm
, spawn_arg
);
682 fprintf(stderr
, "external diff died, stopping at %s.\n", name
);
687 static void run_diff_cmd(const char *pgm
,
690 struct diff_filespec
*one
,
691 struct diff_filespec
*two
,
692 const char *xfrm_msg
,
693 int complete_rewrite
)
696 run_external_diff(pgm
, name
, other
, one
, two
, xfrm_msg
,
701 builtin_diff(name
, other
? other
: name
,
702 one
, two
, xfrm_msg
, complete_rewrite
);
704 printf("* Unmerged path %s\n", name
);
707 static void diff_fill_sha1_info(struct diff_filespec
*one
)
709 if (DIFF_FILE_VALID(one
)) {
710 if (!one
->sha1_valid
) {
712 if (lstat(one
->path
, &st
) < 0)
713 die("stat %s", one
->path
);
714 if (index_path(one
->sha1
, one
->path
, &st
, 0))
715 die("cannot hash %s\n", one
->path
);
719 memset(one
->sha1
, 0, 20);
722 static void run_diff(struct diff_filepair
*p
, struct diff_options
*o
)
724 const char *pgm
= external_diff();
725 char msg
[PATH_MAX
*2+300], *xfrm_msg
;
726 struct diff_filespec
*one
;
727 struct diff_filespec
*two
;
730 char *name_munged
, *other_munged
;
731 int complete_rewrite
= 0;
734 if (DIFF_PAIR_UNMERGED(p
)) {
736 run_diff_cmd(pgm
, p
->one
->path
, NULL
, NULL
, NULL
, NULL
, 0);
741 other
= (strcmp(name
, p
->two
->path
) ? p
->two
->path
: NULL
);
742 name_munged
= quote_one(name
);
743 other_munged
= quote_one(other
);
744 one
= p
->one
; two
= p
->two
;
746 diff_fill_sha1_info(one
);
747 diff_fill_sha1_info(two
);
751 case DIFF_STATUS_COPIED
:
752 len
+= snprintf(msg
+ len
, sizeof(msg
) - len
,
753 "similarity index %d%%\n"
756 (int)(0.5 + p
->score
* 100.0/MAX_SCORE
),
757 name_munged
, other_munged
);
759 case DIFF_STATUS_RENAMED
:
760 len
+= snprintf(msg
+ len
, sizeof(msg
) - len
,
761 "similarity index %d%%\n"
764 (int)(0.5 + p
->score
* 100.0/MAX_SCORE
),
765 name_munged
, other_munged
);
767 case DIFF_STATUS_MODIFIED
:
769 len
+= snprintf(msg
+ len
, sizeof(msg
) - len
,
770 "dissimilarity index %d%%\n",
771 (int)(0.5 + p
->score
*
773 complete_rewrite
= 1;
782 if (memcmp(one
->sha1
, two
->sha1
, 20)) {
784 int abbrev
= o
->full_index
? 40 : DEFAULT_ABBREV
;
785 memcpy(one_sha1
, sha1_to_hex(one
->sha1
), 41);
787 len
+= snprintf(msg
+ len
, sizeof(msg
) - len
,
789 abbrev
, one_sha1
, abbrev
,
790 sha1_to_hex(two
->sha1
));
791 if (one
->mode
== two
->mode
)
792 len
+= snprintf(msg
+ len
, sizeof(msg
) - len
,
794 len
+= snprintf(msg
+ len
, sizeof(msg
) - len
, "\n");
799 xfrm_msg
= len
? msg
: NULL
;
802 DIFF_FILE_VALID(one
) && DIFF_FILE_VALID(two
) &&
803 (S_IFMT
& one
->mode
) != (S_IFMT
& two
->mode
)) {
804 /* a filepair that changes between file and symlink
805 * needs to be split into deletion and creation.
807 struct diff_filespec
*null
= alloc_filespec(two
->path
);
808 run_diff_cmd(NULL
, name
, other
, one
, null
, xfrm_msg
, 0);
810 null
= alloc_filespec(one
->path
);
811 run_diff_cmd(NULL
, name
, other
, null
, two
, xfrm_msg
, 0);
815 run_diff_cmd(pgm
, name
, other
, one
, two
, xfrm_msg
,
822 void diff_setup(struct diff_options
*options
)
824 memset(options
, 0, sizeof(*options
));
825 options
->output_format
= DIFF_FORMAT_RAW
;
826 options
->line_termination
= '\n';
827 options
->break_opt
= -1;
828 options
->rename_limit
= -1;
830 options
->change
= diff_change
;
831 options
->add_remove
= diff_addremove
;
834 int diff_setup_done(struct diff_options
*options
)
836 if ((options
->find_copies_harder
&&
837 options
->detect_rename
!= DIFF_DETECT_COPY
) ||
838 (0 <= options
->rename_limit
&& !options
->detect_rename
))
840 if (options
->detect_rename
&& options
->rename_limit
< 0)
841 options
->rename_limit
= diff_rename_limit_default
;
842 if (options
->setup
& DIFF_SETUP_USE_CACHE
) {
844 /* read-cache does not die even when it fails
845 * so it is safe for us to do this here. Also
846 * it does not smudge active_cache or active_nr
847 * when it fails, so we do not have to worry about
848 * cleaning it up ourselves either.
852 if (options
->setup
& DIFF_SETUP_USE_SIZE_CACHE
)
854 if (options
->abbrev
<= 0 || 40 < options
->abbrev
)
855 options
->abbrev
= 40; /* full */
860 int diff_opt_parse(struct diff_options
*options
, const char **av
, int ac
)
862 const char *arg
= av
[0];
863 if (!strcmp(arg
, "-p") || !strcmp(arg
, "-u"))
864 options
->output_format
= DIFF_FORMAT_PATCH
;
865 else if (!strcmp(arg
, "--patch-with-raw")) {
866 options
->output_format
= DIFF_FORMAT_PATCH
;
867 options
->with_raw
= 1;
869 else if (!strcmp(arg
, "-z"))
870 options
->line_termination
= 0;
871 else if (!strncmp(arg
, "-l", 2))
872 options
->rename_limit
= strtoul(arg
+2, NULL
, 10);
873 else if (!strcmp(arg
, "--full-index"))
874 options
->full_index
= 1;
875 else if (!strcmp(arg
, "--name-only"))
876 options
->output_format
= DIFF_FORMAT_NAME
;
877 else if (!strcmp(arg
, "--name-status"))
878 options
->output_format
= DIFF_FORMAT_NAME_STATUS
;
879 else if (!strcmp(arg
, "-R"))
880 options
->reverse_diff
= 1;
881 else if (!strncmp(arg
, "-S", 2))
882 options
->pickaxe
= arg
+ 2;
883 else if (!strcmp(arg
, "-s"))
884 options
->output_format
= DIFF_FORMAT_NO_OUTPUT
;
885 else if (!strncmp(arg
, "-O", 2))
886 options
->orderfile
= arg
+ 2;
887 else if (!strncmp(arg
, "--diff-filter=", 14))
888 options
->filter
= arg
+ 14;
889 else if (!strcmp(arg
, "--pickaxe-all"))
890 options
->pickaxe_opts
= DIFF_PICKAXE_ALL
;
891 else if (!strcmp(arg
, "--pickaxe-regex"))
892 options
->pickaxe_opts
= DIFF_PICKAXE_REGEX
;
893 else if (!strncmp(arg
, "-B", 2)) {
894 if ((options
->break_opt
=
895 diff_scoreopt_parse(arg
)) == -1)
898 else if (!strncmp(arg
, "-M", 2)) {
899 if ((options
->rename_score
=
900 diff_scoreopt_parse(arg
)) == -1)
902 options
->detect_rename
= DIFF_DETECT_RENAME
;
904 else if (!strncmp(arg
, "-C", 2)) {
905 if ((options
->rename_score
=
906 diff_scoreopt_parse(arg
)) == -1)
908 options
->detect_rename
= DIFF_DETECT_COPY
;
910 else if (!strcmp(arg
, "--find-copies-harder"))
911 options
->find_copies_harder
= 1;
912 else if (!strcmp(arg
, "--abbrev"))
913 options
->abbrev
= DEFAULT_ABBREV
;
914 else if (!strncmp(arg
, "--abbrev=", 9)) {
915 options
->abbrev
= strtoul(arg
+ 9, NULL
, 10);
916 if (options
->abbrev
< MINIMUM_ABBREV
)
917 options
->abbrev
= MINIMUM_ABBREV
;
918 else if (40 < options
->abbrev
)
919 options
->abbrev
= 40;
926 static int parse_num(const char **cp_p
)
928 unsigned long num
, scale
;
930 const char *cp
= *cp_p
;
937 if ( !dot
&& ch
== '.' ) {
940 } else if ( ch
== '%' ) {
941 scale
= dot
? scale
*100 : 100;
942 cp
++; /* % is always at the end */
944 } else if ( ch
>= '0' && ch
<= '9' ) {
945 if ( scale
< 100000 ) {
947 num
= (num
*10) + (ch
-'0');
956 /* user says num divided by scale and we say internally that
957 * is MAX_SCORE * num / scale.
959 return (num
>= scale
) ? MAX_SCORE
: (MAX_SCORE
* num
/ scale
);
962 int diff_scoreopt_parse(const char *opt
)
969 if (cmd
!= 'M' && cmd
!= 'C' && cmd
!= 'B')
970 return -1; /* that is not a -M, -C nor -B option */
972 opt1
= parse_num(&opt
);
978 else if (*opt
!= '/')
979 return -1; /* we expect -B80/99 or -B80 */
982 opt2
= parse_num(&opt
);
987 return opt1
| (opt2
<< 16);
990 struct diff_queue_struct diff_queued_diff
;
992 void diff_q(struct diff_queue_struct
*queue
, struct diff_filepair
*dp
)
994 if (queue
->alloc
<= queue
->nr
) {
995 queue
->alloc
= alloc_nr(queue
->alloc
);
996 queue
->queue
= xrealloc(queue
->queue
,
997 sizeof(dp
) * queue
->alloc
);
999 queue
->queue
[queue
->nr
++] = dp
;
1002 struct diff_filepair
*diff_queue(struct diff_queue_struct
*queue
,
1003 struct diff_filespec
*one
,
1004 struct diff_filespec
*two
)
1006 struct diff_filepair
*dp
= xmalloc(sizeof(*dp
));
1011 dp
->source_stays
= 0;
1012 dp
->broken_pair
= 0;
1018 void diff_free_filepair(struct diff_filepair
*p
)
1020 diff_free_filespec_data(p
->one
);
1021 diff_free_filespec_data(p
->two
);
1027 /* This is different from find_unique_abbrev() in that
1028 * it stuffs the result with dots for alignment.
1030 const char *diff_unique_abbrev(const unsigned char *sha1
, int len
)
1035 return sha1_to_hex(sha1
);
1037 abbrev
= find_unique_abbrev(sha1
, len
);
1039 return sha1_to_hex(sha1
);
1040 abblen
= strlen(abbrev
);
1042 static char hex
[41];
1043 if (len
< abblen
&& abblen
<= len
+ 2)
1044 sprintf(hex
, "%s%.*s", abbrev
, len
+3-abblen
, "..");
1046 sprintf(hex
, "%s...", abbrev
);
1049 return sha1_to_hex(sha1
);
1052 static void diff_flush_raw(struct diff_filepair
*p
,
1053 int line_termination
,
1054 int inter_name_termination
,
1055 struct diff_options
*options
,
1060 int abbrev
= options
->abbrev
;
1061 const char *path_one
, *path_two
;
1063 path_one
= p
->one
->path
;
1064 path_two
= p
->two
->path
;
1065 if (line_termination
) {
1066 path_one
= quote_one(path_one
);
1067 path_two
= quote_one(path_two
);
1071 sprintf(status
, "%c%03d", p
->status
,
1072 (int)(0.5 + p
->score
* 100.0/MAX_SCORE
));
1074 status
[0] = p
->status
;
1077 switch (p
->status
) {
1078 case DIFF_STATUS_COPIED
:
1079 case DIFF_STATUS_RENAMED
:
1082 case DIFF_STATUS_ADDED
:
1083 case DIFF_STATUS_DELETED
:
1090 if (output_format
!= DIFF_FORMAT_NAME_STATUS
) {
1091 printf(":%06o %06o %s ",
1092 p
->one
->mode
, p
->two
->mode
,
1093 diff_unique_abbrev(p
->one
->sha1
, abbrev
));
1095 diff_unique_abbrev(p
->two
->sha1
, abbrev
));
1097 printf("%s%c%s", status
, inter_name_termination
, path_one
);
1099 printf("%c%s", inter_name_termination
, path_two
);
1100 putchar(line_termination
);
1101 if (path_one
!= p
->one
->path
)
1102 free((void*)path_one
);
1103 if (path_two
!= p
->two
->path
)
1104 free((void*)path_two
);
1107 static void diff_flush_name(struct diff_filepair
*p
,
1108 int inter_name_termination
,
1109 int line_termination
)
1111 char *path
= p
->two
->path
;
1113 if (line_termination
)
1114 path
= quote_one(p
->two
->path
);
1116 path
= p
->two
->path
;
1117 printf("%s%c", path
, line_termination
);
1118 if (p
->two
->path
!= path
)
1122 int diff_unmodified_pair(struct diff_filepair
*p
)
1124 /* This function is written stricter than necessary to support
1125 * the currently implemented transformers, but the idea is to
1126 * let transformers to produce diff_filepairs any way they want,
1127 * and filter and clean them up here before producing the output.
1129 struct diff_filespec
*one
, *two
;
1131 if (DIFF_PAIR_UNMERGED(p
))
1132 return 0; /* unmerged is interesting */
1137 /* deletion, addition, mode or type change
1138 * and rename are all interesting.
1140 if (DIFF_FILE_VALID(one
) != DIFF_FILE_VALID(two
) ||
1141 DIFF_PAIR_MODE_CHANGED(p
) ||
1142 strcmp(one
->path
, two
->path
))
1145 /* both are valid and point at the same path. that is, we are
1146 * dealing with a change.
1148 if (one
->sha1_valid
&& two
->sha1_valid
&&
1149 !memcmp(one
->sha1
, two
->sha1
, sizeof(one
->sha1
)))
1150 return 1; /* no change */
1151 if (!one
->sha1_valid
&& !two
->sha1_valid
)
1152 return 1; /* both look at the same file on the filesystem. */
1156 static void diff_flush_patch(struct diff_filepair
*p
, struct diff_options
*o
)
1158 if (diff_unmodified_pair(p
))
1161 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
1162 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
1163 return; /* no tree diffs in patch format */
1168 int diff_queue_is_empty(void)
1170 struct diff_queue_struct
*q
= &diff_queued_diff
;
1172 for (i
= 0; i
< q
->nr
; i
++)
1173 if (!diff_unmodified_pair(q
->queue
[i
]))
1179 void diff_debug_filespec(struct diff_filespec
*s
, int x
, const char *one
)
1181 fprintf(stderr
, "queue[%d] %s (%s) %s %06o %s\n",
1184 DIFF_FILE_VALID(s
) ? "valid" : "invalid",
1186 s
->sha1_valid
? sha1_to_hex(s
->sha1
) : "");
1187 fprintf(stderr
, "queue[%d] %s size %lu flags %d\n",
1189 s
->size
, s
->xfrm_flags
);
1192 void diff_debug_filepair(const struct diff_filepair
*p
, int i
)
1194 diff_debug_filespec(p
->one
, i
, "one");
1195 diff_debug_filespec(p
->two
, i
, "two");
1196 fprintf(stderr
, "score %d, status %c stays %d broken %d\n",
1197 p
->score
, p
->status
? p
->status
: '?',
1198 p
->source_stays
, p
->broken_pair
);
1201 void diff_debug_queue(const char *msg
, struct diff_queue_struct
*q
)
1205 fprintf(stderr
, "%s\n", msg
);
1206 fprintf(stderr
, "q->nr = %d\n", q
->nr
);
1207 for (i
= 0; i
< q
->nr
; i
++) {
1208 struct diff_filepair
*p
= q
->queue
[i
];
1209 diff_debug_filepair(p
, i
);
1214 static void diff_resolve_rename_copy(void)
1217 struct diff_filepair
*p
, *pp
;
1218 struct diff_queue_struct
*q
= &diff_queued_diff
;
1220 diff_debug_queue("resolve-rename-copy", q
);
1222 for (i
= 0; i
< q
->nr
; i
++) {
1224 p
->status
= 0; /* undecided */
1225 if (DIFF_PAIR_UNMERGED(p
))
1226 p
->status
= DIFF_STATUS_UNMERGED
;
1227 else if (!DIFF_FILE_VALID(p
->one
))
1228 p
->status
= DIFF_STATUS_ADDED
;
1229 else if (!DIFF_FILE_VALID(p
->two
))
1230 p
->status
= DIFF_STATUS_DELETED
;
1231 else if (DIFF_PAIR_TYPE_CHANGED(p
))
1232 p
->status
= DIFF_STATUS_TYPE_CHANGED
;
1234 /* from this point on, we are dealing with a pair
1235 * whose both sides are valid and of the same type, i.e.
1236 * either in-place edit or rename/copy edit.
1238 else if (DIFF_PAIR_RENAME(p
)) {
1239 if (p
->source_stays
) {
1240 p
->status
= DIFF_STATUS_COPIED
;
1243 /* See if there is some other filepair that
1244 * copies from the same source as us. If so
1245 * we are a copy. Otherwise we are either a
1246 * copy if the path stays, or a rename if it
1247 * does not, but we already handled "stays" case.
1249 for (j
= i
+ 1; j
< q
->nr
; j
++) {
1251 if (strcmp(pp
->one
->path
, p
->one
->path
))
1252 continue; /* not us */
1253 if (!DIFF_PAIR_RENAME(pp
))
1254 continue; /* not a rename/copy */
1255 /* pp is a rename/copy from the same source */
1256 p
->status
= DIFF_STATUS_COPIED
;
1260 p
->status
= DIFF_STATUS_RENAMED
;
1262 else if (memcmp(p
->one
->sha1
, p
->two
->sha1
, 20) ||
1263 p
->one
->mode
!= p
->two
->mode
)
1264 p
->status
= DIFF_STATUS_MODIFIED
;
1266 /* This is a "no-change" entry and should not
1267 * happen anymore, but prepare for broken callers.
1269 error("feeding unmodified %s to diffcore",
1271 p
->status
= DIFF_STATUS_UNKNOWN
;
1274 diff_debug_queue("resolve-rename-copy done", q
);
1277 static void flush_one_pair(struct diff_filepair
*p
,
1278 int diff_output_format
,
1279 struct diff_options
*options
)
1281 int inter_name_termination
= '\t';
1282 int line_termination
= options
->line_termination
;
1283 if (!line_termination
)
1284 inter_name_termination
= 0;
1286 switch (p
->status
) {
1287 case DIFF_STATUS_UNKNOWN
:
1290 die("internal error in diff-resolve-rename-copy");
1293 switch (diff_output_format
) {
1294 case DIFF_FORMAT_PATCH
:
1295 diff_flush_patch(p
, options
);
1297 case DIFF_FORMAT_RAW
:
1298 case DIFF_FORMAT_NAME_STATUS
:
1299 diff_flush_raw(p
, line_termination
,
1300 inter_name_termination
,
1301 options
, diff_output_format
);
1303 case DIFF_FORMAT_NAME
:
1305 inter_name_termination
,
1308 case DIFF_FORMAT_NO_OUTPUT
:
1314 void diff_flush(struct diff_options
*options
)
1316 struct diff_queue_struct
*q
= &diff_queued_diff
;
1318 int diff_output_format
= options
->output_format
;
1320 if (options
->with_raw
) {
1321 for (i
= 0; i
< q
->nr
; i
++) {
1322 struct diff_filepair
*p
= q
->queue
[i
];
1323 flush_one_pair(p
, DIFF_FORMAT_RAW
, options
);
1326 for (i
= 0; i
< q
->nr
; i
++) {
1327 struct diff_filepair
*p
= q
->queue
[i
];
1328 flush_one_pair(p
, diff_output_format
, options
);
1329 diff_free_filepair(p
);
1333 q
->nr
= q
->alloc
= 0;
1336 static void diffcore_apply_filter(const char *filter
)
1339 struct diff_queue_struct
*q
= &diff_queued_diff
;
1340 struct diff_queue_struct outq
;
1342 outq
.nr
= outq
.alloc
= 0;
1347 if (strchr(filter
, DIFF_STATUS_FILTER_AON
)) {
1349 for (i
= found
= 0; !found
&& i
< q
->nr
; i
++) {
1350 struct diff_filepair
*p
= q
->queue
[i
];
1351 if (((p
->status
== DIFF_STATUS_MODIFIED
) &&
1353 strchr(filter
, DIFF_STATUS_FILTER_BROKEN
)) ||
1355 strchr(filter
, DIFF_STATUS_MODIFIED
)))) ||
1356 ((p
->status
!= DIFF_STATUS_MODIFIED
) &&
1357 strchr(filter
, p
->status
)))
1363 /* otherwise we will clear the whole queue
1364 * by copying the empty outq at the end of this
1365 * function, but first clear the current entries
1368 for (i
= 0; i
< q
->nr
; i
++)
1369 diff_free_filepair(q
->queue
[i
]);
1372 /* Only the matching ones */
1373 for (i
= 0; i
< q
->nr
; i
++) {
1374 struct diff_filepair
*p
= q
->queue
[i
];
1376 if (((p
->status
== DIFF_STATUS_MODIFIED
) &&
1378 strchr(filter
, DIFF_STATUS_FILTER_BROKEN
)) ||
1380 strchr(filter
, DIFF_STATUS_MODIFIED
)))) ||
1381 ((p
->status
!= DIFF_STATUS_MODIFIED
) &&
1382 strchr(filter
, p
->status
)))
1385 diff_free_filepair(p
);
1392 void diffcore_std(struct diff_options
*options
)
1394 if (options
->break_opt
!= -1)
1395 diffcore_break(options
->break_opt
);
1396 if (options
->detect_rename
)
1397 diffcore_rename(options
);
1398 if (options
->break_opt
!= -1)
1399 diffcore_merge_broken();
1400 if (options
->pickaxe
)
1401 diffcore_pickaxe(options
->pickaxe
, options
->pickaxe_opts
);
1402 if (options
->orderfile
)
1403 diffcore_order(options
->orderfile
);
1404 diff_resolve_rename_copy();
1405 diffcore_apply_filter(options
->filter
);
1409 void diffcore_std_no_resolve(struct diff_options
*options
)
1411 if (options
->pickaxe
)
1412 diffcore_pickaxe(options
->pickaxe
, options
->pickaxe_opts
);
1413 if (options
->orderfile
)
1414 diffcore_order(options
->orderfile
);
1415 diffcore_apply_filter(options
->filter
);
1418 void diff_addremove(struct diff_options
*options
,
1419 int addremove
, unsigned mode
,
1420 const unsigned char *sha1
,
1421 const char *base
, const char *path
)
1423 char concatpath
[PATH_MAX
];
1424 struct diff_filespec
*one
, *two
;
1426 /* This may look odd, but it is a preparation for
1427 * feeding "there are unchanged files which should
1428 * not produce diffs, but when you are doing copy
1429 * detection you would need them, so here they are"
1430 * entries to the diff-core. They will be prefixed
1431 * with something like '=' or '*' (I haven't decided
1432 * which but should not make any difference).
1433 * Feeding the same new and old to diff_change()
1434 * also has the same effect.
1435 * Before the final output happens, they are pruned after
1436 * merged into rename/copy pairs as appropriate.
1438 if (options
->reverse_diff
)
1439 addremove
= (addremove
== '+' ? '-' :
1440 addremove
== '-' ? '+' : addremove
);
1442 if (!path
) path
= "";
1443 sprintf(concatpath
, "%s%s", base
, path
);
1444 one
= alloc_filespec(concatpath
);
1445 two
= alloc_filespec(concatpath
);
1447 if (addremove
!= '+')
1448 fill_filespec(one
, sha1
, mode
);
1449 if (addremove
!= '-')
1450 fill_filespec(two
, sha1
, mode
);
1452 diff_queue(&diff_queued_diff
, one
, two
);
1455 void diff_change(struct diff_options
*options
,
1456 unsigned old_mode
, unsigned new_mode
,
1457 const unsigned char *old_sha1
,
1458 const unsigned char *new_sha1
,
1459 const char *base
, const char *path
)
1461 char concatpath
[PATH_MAX
];
1462 struct diff_filespec
*one
, *two
;
1464 if (options
->reverse_diff
) {
1466 const unsigned char *tmp_c
;
1467 tmp
= old_mode
; old_mode
= new_mode
; new_mode
= tmp
;
1468 tmp_c
= old_sha1
; old_sha1
= new_sha1
; new_sha1
= tmp_c
;
1470 if (!path
) path
= "";
1471 sprintf(concatpath
, "%s%s", base
, path
);
1472 one
= alloc_filespec(concatpath
);
1473 two
= alloc_filespec(concatpath
);
1474 fill_filespec(one
, old_sha1
, old_mode
);
1475 fill_filespec(two
, new_sha1
, new_mode
);
1477 diff_queue(&diff_queued_diff
, one
, two
);
1480 void diff_unmerge(struct diff_options
*options
,
1483 struct diff_filespec
*one
, *two
;
1484 one
= alloc_filespec(path
);
1485 two
= alloc_filespec(path
);
1486 diff_queue(&diff_queued_diff
, one
, two
);