2 * Copyright (C) 2005 Junio C Hamano
12 static const char *diff_opts
= "-pu";
14 static int use_size_cache
;
16 int diff_rename_limit_default
= -1;
18 int git_diff_config(const char *var
, const char *value
)
20 if (!strcmp(var
, "diff.renamelimit")) {
21 diff_rename_limit_default
= git_config_int(var
, value
);
25 return git_default_config(var
, value
);
28 static char *quote_one(const char *str
)
35 needlen
= quote_c_style(str
, NULL
, NULL
, 0);
38 xp
= xmalloc(needlen
+ 1);
39 quote_c_style(str
, xp
, NULL
, 0);
43 static char *quote_two(const char *one
, const char *two
)
45 int need_one
= quote_c_style(one
, NULL
, NULL
, 1);
46 int need_two
= quote_c_style(two
, NULL
, NULL
, 1);
49 if (need_one
+ need_two
) {
50 if (!need_one
) need_one
= strlen(one
);
51 if (!need_two
) need_one
= strlen(two
);
53 xp
= xmalloc(need_one
+ need_two
+ 3);
55 quote_c_style(one
, xp
+ 1, NULL
, 1);
56 quote_c_style(two
, xp
+ need_one
+ 1, NULL
, 1);
57 strcpy(xp
+ need_one
+ need_two
+ 1, "\"");
60 need_one
= strlen(one
);
61 need_two
= strlen(two
);
62 xp
= xmalloc(need_one
+ need_two
+ 1);
64 strcpy(xp
+ need_one
, two
);
68 static const char *external_diff(void)
70 static const char *external_diff_cmd
= NULL
;
71 static int done_preparing
= 0;
72 const char *env_diff_opts
;
75 return external_diff_cmd
;
78 * Default values above are meant to match the
79 * Linux kernel development style. Examples of
80 * alternative styles you can specify via environment
85 external_diff_cmd
= getenv("GIT_EXTERNAL_DIFF");
87 /* In case external diff fails... */
88 env_diff_opts
= getenv("GIT_DIFF_OPTS");
89 if (env_diff_opts
) diff_opts
= env_diff_opts
;
92 return external_diff_cmd
;
95 #define TEMPFILE_PATH_LEN 50
97 static struct diff_tempfile
{
98 const char *name
; /* filename external diff should read from */
101 char tmp_path
[TEMPFILE_PATH_LEN
];
104 static int count_lines(const char *filename
)
107 int count
, ch
, completely_empty
= 1, nl_just_seen
= 0;
108 in
= fopen(filename
, "r");
110 while ((ch
= fgetc(in
)) != EOF
)
114 completely_empty
= 0;
118 completely_empty
= 0;
121 if (completely_empty
)
124 count
++; /* no trailing newline */
128 static void print_line_count(int count
)
138 printf("1,%d", count
);
143 static void copy_file(int prefix
, const char *filename
)
146 int ch
, nl_just_seen
= 1;
147 in
= fopen(filename
, "r");
148 while ((ch
= fgetc(in
)) != EOF
) {
159 printf("\n\\ No newline at end of file\n");
162 static void emit_rewrite_diff(const char *name_a
,
164 struct diff_tempfile
*temp
)
166 /* Use temp[i].name as input, name_a and name_b as labels */
168 lc_a
= count_lines(temp
[0].name
);
169 lc_b
= count_lines(temp
[1].name
);
170 printf("--- %s\n+++ %s\n@@ -", name_a
, name_b
);
171 print_line_count(lc_a
);
173 print_line_count(lc_b
);
176 copy_file('-', temp
[0].name
);
178 copy_file('+', temp
[1].name
);
181 static const char *builtin_diff(const char *name_a
,
183 struct diff_tempfile
*temp
,
184 const char *xfrm_msg
,
185 int complete_rewrite
,
188 int i
, next_at
, cmd_size
;
189 const char *const diff_cmd
= "diff -L%s -L%s";
190 const char *const diff_arg
= "-- %s %s||:"; /* "||:" is to return 0 */
191 const char *input_name_sq
[2];
192 const char *label_path
[2];
195 /* diff_cmd and diff_arg have 4 %s in total which makes
196 * the sum of these strings 8 bytes larger than required.
197 * we use 2 spaces around diff-opts, and we need to count
198 * terminating NUL; we used to subtract 5 here, but we do not
199 * care about small leaks in this subprocess that is about
200 * to exec "diff" anymore.
202 cmd_size
= (strlen(diff_cmd
) + strlen(diff_opts
) + strlen(diff_arg
)
205 for (i
= 0; i
< 2; i
++) {
206 input_name_sq
[i
] = sq_quote(temp
[i
].name
);
207 if (!strcmp(temp
[i
].name
, "/dev/null"))
208 label_path
[i
] = "/dev/null";
210 label_path
[i
] = sq_quote(quote_two("a/", name_a
));
212 label_path
[i
] = sq_quote(quote_two("b/", name_b
));
213 cmd_size
+= (strlen(label_path
[i
]) + strlen(input_name_sq
[i
]));
216 cmd
= xmalloc(cmd_size
);
219 next_at
+= snprintf(cmd
+next_at
, cmd_size
-next_at
,
220 diff_cmd
, label_path
[0], label_path
[1]);
221 next_at
+= snprintf(cmd
+next_at
, cmd_size
-next_at
,
223 next_at
+= snprintf(cmd
+next_at
, cmd_size
-next_at
,
224 diff_arg
, input_name_sq
[0], input_name_sq
[1]);
226 printf("diff --git %s %s\n",
227 quote_two("a/", name_a
), quote_two("b/", name_b
));
228 if (label_path
[0][0] == '/') {
230 printf("new file mode %s\n", temp
[1].mode
);
231 if (xfrm_msg
&& xfrm_msg
[0])
234 else if (label_path
[1][0] == '/') {
235 printf("deleted file mode %s\n", temp
[0].mode
);
236 if (xfrm_msg
&& xfrm_msg
[0])
240 if (strcmp(temp
[0].mode
, temp
[1].mode
)) {
241 printf("old mode %s\n", temp
[0].mode
);
242 printf("new mode %s\n", temp
[1].mode
);
244 if (xfrm_msg
&& xfrm_msg
[0])
247 * we do not run diff between different kind
250 if (strncmp(temp
[0].mode
, temp
[1].mode
, 3))
252 if (complete_rewrite
) {
253 emit_rewrite_diff(name_a
, name_b
, temp
);
258 /* This is disgusting */
266 struct diff_filespec
*alloc_filespec(const char *path
)
268 int namelen
= strlen(path
);
269 struct diff_filespec
*spec
= xmalloc(sizeof(*spec
) + namelen
+ 1);
271 memset(spec
, 0, sizeof(*spec
));
272 spec
->path
= (char *)(spec
+ 1);
273 memcpy(spec
->path
, path
, namelen
+1);
277 void fill_filespec(struct diff_filespec
*spec
, const unsigned char *sha1
,
281 spec
->mode
= DIFF_FILE_CANON_MODE(mode
);
282 memcpy(spec
->sha1
, sha1
, 20);
283 spec
->sha1_valid
= !!memcmp(sha1
, null_sha1
, 20);
288 * Given a name and sha1 pair, if the dircache tells us the file in
289 * the work tree has that object contents, return true, so that
290 * prepare_temp_file() does not have to inflate and extract.
292 static int work_tree_matches(const char *name
, const unsigned char *sha1
)
294 struct cache_entry
*ce
;
298 /* We do not read the cache ourselves here, because the
299 * benchmark with my previous version that always reads cache
300 * shows that it makes things worse for diff-tree comparing
301 * two linux-2.6 kernel trees in an already checked out work
302 * tree. This is because most diff-tree comparisons deal with
303 * only a small number of files, while reading the cache is
304 * expensive for a large project, and its cost outweighs the
305 * savings we get by not inflating the object to a temporary
306 * file. Practically, this code only helps when we are used
307 * by diff-cache --cached, which does read the cache before
314 pos
= cache_name_pos(name
, len
);
317 ce
= active_cache
[pos
];
318 if ((lstat(name
, &st
) < 0) ||
319 !S_ISREG(st
.st_mode
) || /* careful! */
320 ce_match_stat(ce
, &st
, 0) ||
321 memcmp(sha1
, ce
->sha1
, 20))
323 /* we return 1 only when we can stat, it is a regular file,
324 * stat information matches, and sha1 recorded in the cache
325 * matches. I.e. we know the file in the work tree really is
326 * the same as the <name, sha1> pair.
331 static struct sha1_size_cache
{
332 unsigned char sha1
[20];
335 static int sha1_size_cache_nr
, sha1_size_cache_alloc
;
337 static struct sha1_size_cache
*locate_size_cache(unsigned char *sha1
,
342 struct sha1_size_cache
*e
;
345 last
= sha1_size_cache_nr
;
346 while (last
> first
) {
347 int cmp
, next
= (last
+ first
) >> 1;
348 e
= sha1_size_cache
[next
];
349 cmp
= memcmp(e
->sha1
, sha1
, 20);
361 /* insert to make it at "first" */
362 if (sha1_size_cache_alloc
<= sha1_size_cache_nr
) {
363 sha1_size_cache_alloc
= alloc_nr(sha1_size_cache_alloc
);
364 sha1_size_cache
= xrealloc(sha1_size_cache
,
365 sha1_size_cache_alloc
*
366 sizeof(*sha1_size_cache
));
368 sha1_size_cache_nr
++;
369 if (first
< sha1_size_cache_nr
)
370 memmove(sha1_size_cache
+ first
+ 1, sha1_size_cache
+ first
,
371 (sha1_size_cache_nr
- first
- 1) *
372 sizeof(*sha1_size_cache
));
373 e
= xmalloc(sizeof(struct sha1_size_cache
));
374 sha1_size_cache
[first
] = e
;
375 memcpy(e
->sha1
, sha1
, 20);
381 * While doing rename detection and pickaxe operation, we may need to
382 * grab the data for the blob (or file) for our own in-core comparison.
383 * diff_filespec has data and size fields for this purpose.
385 int diff_populate_filespec(struct diff_filespec
*s
, int size_only
)
388 if (!DIFF_FILE_VALID(s
))
389 die("internal error: asking to populate invalid file.");
390 if (S_ISDIR(s
->mode
))
398 if (!s
->sha1_valid
||
399 work_tree_matches(s
->path
, s
->sha1
)) {
402 if (lstat(s
->path
, &st
) < 0) {
403 if (errno
== ENOENT
) {
412 s
->size
= st
.st_size
;
417 if (S_ISLNK(st
.st_mode
)) {
419 s
->data
= xmalloc(s
->size
);
421 ret
= readlink(s
->path
, s
->data
, s
->size
);
428 fd
= open(s
->path
, O_RDONLY
);
431 s
->data
= mmap(NULL
, s
->size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
433 if (s
->data
== MAP_FAILED
)
435 s
->should_munmap
= 1;
439 struct sha1_size_cache
*e
;
442 e
= locate_size_cache(s
->sha1
, 1, 0);
447 if (!sha1_object_info(s
->sha1
, type
, &s
->size
))
448 locate_size_cache(s
->sha1
, 0, s
->size
);
451 s
->data
= read_sha1_file(s
->sha1
, type
, &s
->size
);
458 void diff_free_filespec_data(struct diff_filespec
*s
)
462 else if (s
->should_munmap
)
463 munmap(s
->data
, s
->size
);
464 s
->should_free
= s
->should_munmap
= 0;
468 static void prep_temp_blob(struct diff_tempfile
*temp
,
471 const unsigned char *sha1
,
476 fd
= git_mkstemp(temp
->tmp_path
, TEMPFILE_PATH_LEN
, ".diff_XXXXXX");
478 die("unable to create temp-file");
479 if (write(fd
, blob
, size
) != size
)
480 die("unable to write temp-file");
482 temp
->name
= temp
->tmp_path
;
483 strcpy(temp
->hex
, sha1_to_hex(sha1
));
485 sprintf(temp
->mode
, "%06o", mode
);
488 static void prepare_temp_file(const char *name
,
489 struct diff_tempfile
*temp
,
490 struct diff_filespec
*one
)
492 if (!DIFF_FILE_VALID(one
)) {
494 /* A '-' entry produces this for file-2, and
495 * a '+' entry produces this for file-1.
497 temp
->name
= "/dev/null";
498 strcpy(temp
->hex
, ".");
499 strcpy(temp
->mode
, ".");
503 if (!one
->sha1_valid
||
504 work_tree_matches(name
, one
->sha1
)) {
506 if (lstat(name
, &st
) < 0) {
508 goto not_a_valid_file
;
509 die("stat(%s): %s", name
, strerror(errno
));
511 if (S_ISLNK(st
.st_mode
)) {
513 char buf
[PATH_MAX
+ 1]; /* ought to be SYMLINK_MAX */
514 if (sizeof(buf
) <= st
.st_size
)
515 die("symlink too long: %s", name
);
516 ret
= readlink(name
, buf
, st
.st_size
);
518 die("readlink(%s)", name
);
519 prep_temp_blob(temp
, buf
, st
.st_size
,
521 one
->sha1
: null_sha1
),
523 one
->mode
: S_IFLNK
));
526 /* we can borrow from the file in the work tree */
528 if (!one
->sha1_valid
)
529 strcpy(temp
->hex
, sha1_to_hex(null_sha1
));
531 strcpy(temp
->hex
, sha1_to_hex(one
->sha1
));
532 /* Even though we may sometimes borrow the
533 * contents from the work tree, we always want
534 * one->mode. mode is trustworthy even when
535 * !(one->sha1_valid), as long as
536 * DIFF_FILE_VALID(one).
538 sprintf(temp
->mode
, "%06o", one
->mode
);
543 if (diff_populate_filespec(one
, 0))
544 die("cannot read data blob for %s", one
->path
);
545 prep_temp_blob(temp
, one
->data
, one
->size
,
546 one
->sha1
, one
->mode
);
550 static void remove_tempfile(void)
554 for (i
= 0; i
< 2; i
++)
555 if (diff_temp
[i
].name
== diff_temp
[i
].tmp_path
) {
556 unlink(diff_temp
[i
].name
);
557 diff_temp
[i
].name
= NULL
;
561 static void remove_tempfile_on_signal(int signo
)
564 signal(SIGINT
, SIG_DFL
);
568 static int spawn_prog(const char *pgm
, const char **arg
)
576 die("unable to fork");
578 execvp(pgm
, (char *const*) arg
);
582 while (waitpid(pid
, &status
, 0) < 0) {
588 /* Earlier we did not check the exit status because
589 * diff exits non-zero if files are different, and
590 * we are not interested in knowing that. It was a
591 * mistake which made it harder to quit a diff-*
592 * session that uses the git-apply-patch-script as
593 * the GIT_EXTERNAL_DIFF. A custom GIT_EXTERNAL_DIFF
594 * should also exit non-zero only when it wants to
595 * abort the entire diff-* session.
597 if (WIFEXITED(status
) && !WEXITSTATUS(status
))
602 /* An external diff command takes:
604 * diff-cmd name infile1 infile1-sha1 infile1-mode \
605 * infile2 infile2-sha1 infile2-mode [ rename-to ]
608 static void run_external_diff(const char *pgm
,
611 struct diff_filespec
*one
,
612 struct diff_filespec
*two
,
613 const char *xfrm_msg
,
614 int complete_rewrite
)
616 const char *spawn_arg
[10];
617 struct diff_tempfile
*temp
= diff_temp
;
619 static int atexit_asked
= 0;
620 const char *othername
;
622 othername
= (other
? other
: name
);
624 prepare_temp_file(name
, &temp
[0], one
);
625 prepare_temp_file(othername
, &temp
[1], two
);
626 if (! atexit_asked
&&
627 (temp
[0].name
== temp
[0].tmp_path
||
628 temp
[1].name
== temp
[1].tmp_path
)) {
630 atexit(remove_tempfile
);
632 signal(SIGINT
, remove_tempfile_on_signal
);
636 const char **arg
= &spawn_arg
[0];
640 *arg
++ = temp
[0].name
;
641 *arg
++ = temp
[0].hex
;
642 *arg
++ = temp
[0].mode
;
643 *arg
++ = temp
[1].name
;
644 *arg
++ = temp
[1].hex
;
645 *arg
++ = temp
[1].mode
;
657 pgm
= builtin_diff(name
, othername
, temp
, xfrm_msg
, complete_rewrite
, spawn_arg
);
659 printf("* Unmerged path %s\n", name
);
664 retval
= spawn_prog(pgm
, spawn_arg
);
667 fprintf(stderr
, "external diff died, stopping at %s.\n", name
);
672 static void diff_fill_sha1_info(struct diff_filespec
*one
)
674 if (DIFF_FILE_VALID(one
)) {
675 if (!one
->sha1_valid
) {
677 if (lstat(one
->path
, &st
) < 0)
678 die("stat %s", one
->path
);
679 if (index_path(one
->sha1
, one
->path
, &st
, 0))
680 die("cannot hash %s\n", one
->path
);
684 memset(one
->sha1
, 0, 20);
687 static void run_diff(struct diff_filepair
*p
, struct diff_options
*o
)
689 const char *pgm
= external_diff();
690 char msg
[PATH_MAX
*2+300], *xfrm_msg
;
691 struct diff_filespec
*one
;
692 struct diff_filespec
*two
;
695 char *name_munged
, *other_munged
;
696 int complete_rewrite
= 0;
699 if (DIFF_PAIR_UNMERGED(p
)) {
701 run_external_diff(pgm
, p
->one
->path
, NULL
, NULL
, NULL
, NULL
,
707 other
= (strcmp(name
, p
->two
->path
) ? p
->two
->path
: NULL
);
708 name_munged
= quote_one(name
);
709 other_munged
= quote_one(other
);
710 one
= p
->one
; two
= p
->two
;
712 diff_fill_sha1_info(one
);
713 diff_fill_sha1_info(two
);
717 case DIFF_STATUS_COPIED
:
718 len
+= snprintf(msg
+ len
, sizeof(msg
) - len
,
719 "similarity index %d%%\n"
722 (int)(0.5 + p
->score
* 100.0/MAX_SCORE
),
723 name_munged
, other_munged
);
725 case DIFF_STATUS_RENAMED
:
726 len
+= snprintf(msg
+ len
, sizeof(msg
) - len
,
727 "similarity index %d%%\n"
730 (int)(0.5 + p
->score
* 100.0/MAX_SCORE
),
731 name_munged
, other_munged
);
733 case DIFF_STATUS_MODIFIED
:
735 len
+= snprintf(msg
+ len
, sizeof(msg
) - len
,
736 "dissimilarity index %d%%\n",
737 (int)(0.5 + p
->score
*
739 complete_rewrite
= 1;
748 if (memcmp(one
->sha1
, two
->sha1
, 20)) {
750 int abbrev
= o
->full_index
? 40 : DEFAULT_ABBREV
;
751 memcpy(one_sha1
, sha1_to_hex(one
->sha1
), 41);
753 len
+= snprintf(msg
+ len
, sizeof(msg
) - len
,
755 abbrev
, one_sha1
, abbrev
,
756 sha1_to_hex(two
->sha1
));
757 if (one
->mode
== two
->mode
)
758 len
+= snprintf(msg
+ len
, sizeof(msg
) - len
,
760 len
+= snprintf(msg
+ len
, sizeof(msg
) - len
, "\n");
765 xfrm_msg
= len
? msg
: NULL
;
768 DIFF_FILE_VALID(one
) && DIFF_FILE_VALID(two
) &&
769 (S_IFMT
& one
->mode
) != (S_IFMT
& two
->mode
)) {
770 /* a filepair that changes between file and symlink
771 * needs to be split into deletion and creation.
773 struct diff_filespec
*null
= alloc_filespec(two
->path
);
774 run_external_diff(NULL
, name
, other
, one
, null
, xfrm_msg
, 0);
776 null
= alloc_filespec(one
->path
);
777 run_external_diff(NULL
, name
, other
, null
, two
, xfrm_msg
, 0);
781 run_external_diff(pgm
, name
, other
, one
, two
, xfrm_msg
,
788 void diff_setup(struct diff_options
*options
)
790 memset(options
, 0, sizeof(*options
));
791 options
->output_format
= DIFF_FORMAT_RAW
;
792 options
->line_termination
= '\n';
793 options
->break_opt
= -1;
794 options
->rename_limit
= -1;
796 options
->change
= diff_change
;
797 options
->add_remove
= diff_addremove
;
800 int diff_setup_done(struct diff_options
*options
)
802 if ((options
->find_copies_harder
&&
803 options
->detect_rename
!= DIFF_DETECT_COPY
) ||
804 (0 <= options
->rename_limit
&& !options
->detect_rename
))
806 if (options
->detect_rename
&& options
->rename_limit
< 0)
807 options
->rename_limit
= diff_rename_limit_default
;
808 if (options
->setup
& DIFF_SETUP_USE_CACHE
) {
810 /* read-cache does not die even when it fails
811 * so it is safe for us to do this here. Also
812 * it does not smudge active_cache or active_nr
813 * when it fails, so we do not have to worry about
814 * cleaning it up ourselves either.
818 if (options
->setup
& DIFF_SETUP_USE_SIZE_CACHE
)
820 if (options
->abbrev
<= 0 || 40 < options
->abbrev
)
821 options
->abbrev
= 40; /* full */
826 int diff_opt_parse(struct diff_options
*options
, const char **av
, int ac
)
828 const char *arg
= av
[0];
829 if (!strcmp(arg
, "-p") || !strcmp(arg
, "-u"))
830 options
->output_format
= DIFF_FORMAT_PATCH
;
831 else if (!strcmp(arg
, "-z"))
832 options
->line_termination
= 0;
833 else if (!strncmp(arg
, "-l", 2))
834 options
->rename_limit
= strtoul(arg
+2, NULL
, 10);
835 else if (!strcmp(arg
, "--full-index"))
836 options
->full_index
= 1;
837 else if (!strcmp(arg
, "--name-only"))
838 options
->output_format
= DIFF_FORMAT_NAME
;
839 else if (!strcmp(arg
, "--name-status"))
840 options
->output_format
= DIFF_FORMAT_NAME_STATUS
;
841 else if (!strcmp(arg
, "-R"))
842 options
->reverse_diff
= 1;
843 else if (!strncmp(arg
, "-S", 2))
844 options
->pickaxe
= arg
+ 2;
845 else if (!strcmp(arg
, "-s"))
846 options
->output_format
= DIFF_FORMAT_NO_OUTPUT
;
847 else if (!strncmp(arg
, "-O", 2))
848 options
->orderfile
= arg
+ 2;
849 else if (!strncmp(arg
, "--diff-filter=", 14))
850 options
->filter
= arg
+ 14;
851 else if (!strcmp(arg
, "--pickaxe-all"))
852 options
->pickaxe_opts
= DIFF_PICKAXE_ALL
;
853 else if (!strncmp(arg
, "-B", 2)) {
854 if ((options
->break_opt
=
855 diff_scoreopt_parse(arg
)) == -1)
858 else if (!strncmp(arg
, "-M", 2)) {
859 if ((options
->rename_score
=
860 diff_scoreopt_parse(arg
)) == -1)
862 options
->detect_rename
= DIFF_DETECT_RENAME
;
864 else if (!strncmp(arg
, "-C", 2)) {
865 if ((options
->rename_score
=
866 diff_scoreopt_parse(arg
)) == -1)
868 options
->detect_rename
= DIFF_DETECT_COPY
;
870 else if (!strcmp(arg
, "--find-copies-harder"))
871 options
->find_copies_harder
= 1;
872 else if (!strcmp(arg
, "--abbrev"))
873 options
->abbrev
= DEFAULT_ABBREV
;
874 else if (!strncmp(arg
, "--abbrev=", 9)) {
875 options
->abbrev
= strtoul(arg
+ 9, NULL
, 10);
876 if (options
->abbrev
< MINIMUM_ABBREV
)
877 options
->abbrev
= MINIMUM_ABBREV
;
878 else if (40 < options
->abbrev
)
879 options
->abbrev
= 40;
886 static int parse_num(const char **cp_p
)
888 unsigned long num
, scale
;
890 const char *cp
= *cp_p
;
897 if ( !dot
&& ch
== '.' ) {
900 } else if ( ch
== '%' ) {
901 scale
= dot
? scale
*100 : 100;
902 cp
++; /* % is always at the end */
904 } else if ( ch
>= '0' && ch
<= '9' ) {
905 if ( scale
< 100000 ) {
907 num
= (num
*10) + (ch
-'0');
916 /* user says num divided by scale and we say internally that
917 * is MAX_SCORE * num / scale.
919 return (num
>= scale
) ? MAX_SCORE
: (MAX_SCORE
* num
/ scale
);
922 int diff_scoreopt_parse(const char *opt
)
929 if (cmd
!= 'M' && cmd
!= 'C' && cmd
!= 'B')
930 return -1; /* that is not a -M, -C nor -B option */
932 opt1
= parse_num(&opt
);
938 else if (*opt
!= '/')
939 return -1; /* we expect -B80/99 or -B80 */
942 opt2
= parse_num(&opt
);
947 return opt1
| (opt2
<< 16);
950 struct diff_queue_struct diff_queued_diff
;
952 void diff_q(struct diff_queue_struct
*queue
, struct diff_filepair
*dp
)
954 if (queue
->alloc
<= queue
->nr
) {
955 queue
->alloc
= alloc_nr(queue
->alloc
);
956 queue
->queue
= xrealloc(queue
->queue
,
957 sizeof(dp
) * queue
->alloc
);
959 queue
->queue
[queue
->nr
++] = dp
;
962 struct diff_filepair
*diff_queue(struct diff_queue_struct
*queue
,
963 struct diff_filespec
*one
,
964 struct diff_filespec
*two
)
966 struct diff_filepair
*dp
= xmalloc(sizeof(*dp
));
971 dp
->source_stays
= 0;
978 void diff_free_filepair(struct diff_filepair
*p
)
980 diff_free_filespec_data(p
->one
);
981 diff_free_filespec_data(p
->two
);
987 /* This is different from find_unique_abbrev() in that
988 * it stuffs the result with dots for alignment.
990 const char *diff_unique_abbrev(const unsigned char *sha1
, int len
)
995 return sha1_to_hex(sha1
);
997 abbrev
= find_unique_abbrev(sha1
, len
);
999 return sha1_to_hex(sha1
);
1000 abblen
= strlen(abbrev
);
1002 static char hex
[41];
1003 if (len
< abblen
&& abblen
<= len
+ 2)
1004 sprintf(hex
, "%s%.*s", abbrev
, len
+3-abblen
, "..");
1006 sprintf(hex
, "%s...", abbrev
);
1009 return sha1_to_hex(sha1
);
1012 static void diff_flush_raw(struct diff_filepair
*p
,
1013 int line_termination
,
1014 int inter_name_termination
,
1015 struct diff_options
*options
)
1019 int abbrev
= options
->abbrev
;
1020 const char *path_one
, *path_two
;
1021 int output_format
= options
->output_format
;
1023 path_one
= p
->one
->path
;
1024 path_two
= p
->two
->path
;
1025 if (line_termination
) {
1026 path_one
= quote_one(path_one
);
1027 path_two
= quote_one(path_two
);
1031 sprintf(status
, "%c%03d", p
->status
,
1032 (int)(0.5 + p
->score
* 100.0/MAX_SCORE
));
1034 status
[0] = p
->status
;
1037 switch (p
->status
) {
1038 case DIFF_STATUS_COPIED
:
1039 case DIFF_STATUS_RENAMED
:
1042 case DIFF_STATUS_ADDED
:
1043 case DIFF_STATUS_DELETED
:
1050 if (output_format
!= DIFF_FORMAT_NAME_STATUS
) {
1051 printf(":%06o %06o %s ",
1052 p
->one
->mode
, p
->two
->mode
,
1053 diff_unique_abbrev(p
->one
->sha1
, abbrev
));
1055 diff_unique_abbrev(p
->two
->sha1
, abbrev
));
1057 printf("%s%c%s", status
, inter_name_termination
, path_one
);
1059 printf("%c%s", inter_name_termination
, path_two
);
1060 putchar(line_termination
);
1061 if (path_one
!= p
->one
->path
)
1062 free((void*)path_one
);
1063 if (path_two
!= p
->two
->path
)
1064 free((void*)path_two
);
1067 static void diff_flush_name(struct diff_filepair
*p
,
1068 int inter_name_termination
,
1069 int line_termination
)
1071 char *path
= p
->two
->path
;
1073 if (line_termination
)
1074 path
= quote_one(p
->two
->path
);
1076 path
= p
->two
->path
;
1077 printf("%s%c", path
, line_termination
);
1078 if (p
->two
->path
!= path
)
1082 int diff_unmodified_pair(struct diff_filepair
*p
)
1084 /* This function is written stricter than necessary to support
1085 * the currently implemented transformers, but the idea is to
1086 * let transformers to produce diff_filepairs any way they want,
1087 * and filter and clean them up here before producing the output.
1089 struct diff_filespec
*one
, *two
;
1091 if (DIFF_PAIR_UNMERGED(p
))
1092 return 0; /* unmerged is interesting */
1097 /* deletion, addition, mode or type change
1098 * and rename are all interesting.
1100 if (DIFF_FILE_VALID(one
) != DIFF_FILE_VALID(two
) ||
1101 DIFF_PAIR_MODE_CHANGED(p
) ||
1102 strcmp(one
->path
, two
->path
))
1105 /* both are valid and point at the same path. that is, we are
1106 * dealing with a change.
1108 if (one
->sha1_valid
&& two
->sha1_valid
&&
1109 !memcmp(one
->sha1
, two
->sha1
, sizeof(one
->sha1
)))
1110 return 1; /* no change */
1111 if (!one
->sha1_valid
&& !two
->sha1_valid
)
1112 return 1; /* both look at the same file on the filesystem. */
1116 static void diff_flush_patch(struct diff_filepair
*p
, struct diff_options
*o
)
1118 if (diff_unmodified_pair(p
))
1121 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
1122 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
1123 return; /* no tree diffs in patch format */
1128 int diff_queue_is_empty(void)
1130 struct diff_queue_struct
*q
= &diff_queued_diff
;
1132 for (i
= 0; i
< q
->nr
; i
++)
1133 if (!diff_unmodified_pair(q
->queue
[i
]))
1139 void diff_debug_filespec(struct diff_filespec
*s
, int x
, const char *one
)
1141 fprintf(stderr
, "queue[%d] %s (%s) %s %06o %s\n",
1144 DIFF_FILE_VALID(s
) ? "valid" : "invalid",
1146 s
->sha1_valid
? sha1_to_hex(s
->sha1
) : "");
1147 fprintf(stderr
, "queue[%d] %s size %lu flags %d\n",
1149 s
->size
, s
->xfrm_flags
);
1152 void diff_debug_filepair(const struct diff_filepair
*p
, int i
)
1154 diff_debug_filespec(p
->one
, i
, "one");
1155 diff_debug_filespec(p
->two
, i
, "two");
1156 fprintf(stderr
, "score %d, status %c stays %d broken %d\n",
1157 p
->score
, p
->status
? p
->status
: '?',
1158 p
->source_stays
, p
->broken_pair
);
1161 void diff_debug_queue(const char *msg
, struct diff_queue_struct
*q
)
1165 fprintf(stderr
, "%s\n", msg
);
1166 fprintf(stderr
, "q->nr = %d\n", q
->nr
);
1167 for (i
= 0; i
< q
->nr
; i
++) {
1168 struct diff_filepair
*p
= q
->queue
[i
];
1169 diff_debug_filepair(p
, i
);
1174 static void diff_resolve_rename_copy(void)
1177 struct diff_filepair
*p
, *pp
;
1178 struct diff_queue_struct
*q
= &diff_queued_diff
;
1180 diff_debug_queue("resolve-rename-copy", q
);
1182 for (i
= 0; i
< q
->nr
; i
++) {
1184 p
->status
= 0; /* undecided */
1185 if (DIFF_PAIR_UNMERGED(p
))
1186 p
->status
= DIFF_STATUS_UNMERGED
;
1187 else if (!DIFF_FILE_VALID(p
->one
))
1188 p
->status
= DIFF_STATUS_ADDED
;
1189 else if (!DIFF_FILE_VALID(p
->two
))
1190 p
->status
= DIFF_STATUS_DELETED
;
1191 else if (DIFF_PAIR_TYPE_CHANGED(p
))
1192 p
->status
= DIFF_STATUS_TYPE_CHANGED
;
1194 /* from this point on, we are dealing with a pair
1195 * whose both sides are valid and of the same type, i.e.
1196 * either in-place edit or rename/copy edit.
1198 else if (DIFF_PAIR_RENAME(p
)) {
1199 if (p
->source_stays
) {
1200 p
->status
= DIFF_STATUS_COPIED
;
1203 /* See if there is some other filepair that
1204 * copies from the same source as us. If so
1205 * we are a copy. Otherwise we are either a
1206 * copy if the path stays, or a rename if it
1207 * does not, but we already handled "stays" case.
1209 for (j
= i
+ 1; j
< q
->nr
; j
++) {
1211 if (strcmp(pp
->one
->path
, p
->one
->path
))
1212 continue; /* not us */
1213 if (!DIFF_PAIR_RENAME(pp
))
1214 continue; /* not a rename/copy */
1215 /* pp is a rename/copy from the same source */
1216 p
->status
= DIFF_STATUS_COPIED
;
1220 p
->status
= DIFF_STATUS_RENAMED
;
1222 else if (memcmp(p
->one
->sha1
, p
->two
->sha1
, 20) ||
1223 p
->one
->mode
!= p
->two
->mode
)
1224 p
->status
= DIFF_STATUS_MODIFIED
;
1226 /* This is a "no-change" entry and should not
1227 * happen anymore, but prepare for broken callers.
1229 error("feeding unmodified %s to diffcore",
1231 p
->status
= DIFF_STATUS_UNKNOWN
;
1234 diff_debug_queue("resolve-rename-copy done", q
);
1237 void diff_flush(struct diff_options
*options
)
1239 struct diff_queue_struct
*q
= &diff_queued_diff
;
1241 int inter_name_termination
= '\t';
1242 int diff_output_format
= options
->output_format
;
1243 int line_termination
= options
->line_termination
;
1245 if (!line_termination
)
1246 inter_name_termination
= 0;
1248 for (i
= 0; i
< q
->nr
; i
++) {
1249 struct diff_filepair
*p
= q
->queue
[i
];
1250 if ((diff_output_format
== DIFF_FORMAT_NO_OUTPUT
) ||
1251 (p
->status
== DIFF_STATUS_UNKNOWN
))
1254 die("internal error in diff-resolve-rename-copy");
1255 switch (diff_output_format
) {
1256 case DIFF_FORMAT_PATCH
:
1257 diff_flush_patch(p
, options
);
1259 case DIFF_FORMAT_RAW
:
1260 case DIFF_FORMAT_NAME_STATUS
:
1261 diff_flush_raw(p
, line_termination
,
1262 inter_name_termination
,
1265 case DIFF_FORMAT_NAME
:
1267 inter_name_termination
,
1271 diff_free_filepair(q
->queue
[i
]);
1275 q
->nr
= q
->alloc
= 0;
1278 static void diffcore_apply_filter(const char *filter
)
1281 struct diff_queue_struct
*q
= &diff_queued_diff
;
1282 struct diff_queue_struct outq
;
1284 outq
.nr
= outq
.alloc
= 0;
1289 if (strchr(filter
, DIFF_STATUS_FILTER_AON
)) {
1291 for (i
= found
= 0; !found
&& i
< q
->nr
; i
++) {
1292 struct diff_filepair
*p
= q
->queue
[i
];
1293 if (((p
->status
== DIFF_STATUS_MODIFIED
) &&
1295 strchr(filter
, DIFF_STATUS_FILTER_BROKEN
)) ||
1297 strchr(filter
, DIFF_STATUS_MODIFIED
)))) ||
1298 ((p
->status
!= DIFF_STATUS_MODIFIED
) &&
1299 strchr(filter
, p
->status
)))
1305 /* otherwise we will clear the whole queue
1306 * by copying the empty outq at the end of this
1307 * function, but first clear the current entries
1310 for (i
= 0; i
< q
->nr
; i
++)
1311 diff_free_filepair(q
->queue
[i
]);
1314 /* Only the matching ones */
1315 for (i
= 0; i
< q
->nr
; i
++) {
1316 struct diff_filepair
*p
= q
->queue
[i
];
1318 if (((p
->status
== DIFF_STATUS_MODIFIED
) &&
1320 strchr(filter
, DIFF_STATUS_FILTER_BROKEN
)) ||
1322 strchr(filter
, DIFF_STATUS_MODIFIED
)))) ||
1323 ((p
->status
!= DIFF_STATUS_MODIFIED
) &&
1324 strchr(filter
, p
->status
)))
1327 diff_free_filepair(p
);
1334 void diffcore_std(struct diff_options
*options
)
1336 if (options
->paths
&& options
->paths
[0])
1337 diffcore_pathspec(options
->paths
);
1338 if (options
->break_opt
!= -1)
1339 diffcore_break(options
->break_opt
);
1340 if (options
->detect_rename
)
1341 diffcore_rename(options
);
1342 if (options
->break_opt
!= -1)
1343 diffcore_merge_broken();
1344 if (options
->pickaxe
)
1345 diffcore_pickaxe(options
->pickaxe
, options
->pickaxe_opts
);
1346 if (options
->orderfile
)
1347 diffcore_order(options
->orderfile
);
1348 diff_resolve_rename_copy();
1349 diffcore_apply_filter(options
->filter
);
1353 void diffcore_std_no_resolve(struct diff_options
*options
)
1355 if (options
->pickaxe
)
1356 diffcore_pickaxe(options
->pickaxe
, options
->pickaxe_opts
);
1357 if (options
->orderfile
)
1358 diffcore_order(options
->orderfile
);
1359 diffcore_apply_filter(options
->filter
);
1362 void diff_addremove(struct diff_options
*options
,
1363 int addremove
, unsigned mode
,
1364 const unsigned char *sha1
,
1365 const char *base
, const char *path
)
1367 char concatpath
[PATH_MAX
];
1368 struct diff_filespec
*one
, *two
;
1370 /* This may look odd, but it is a preparation for
1371 * feeding "there are unchanged files which should
1372 * not produce diffs, but when you are doing copy
1373 * detection you would need them, so here they are"
1374 * entries to the diff-core. They will be prefixed
1375 * with something like '=' or '*' (I haven't decided
1376 * which but should not make any difference).
1377 * Feeding the same new and old to diff_change()
1378 * also has the same effect.
1379 * Before the final output happens, they are pruned after
1380 * merged into rename/copy pairs as appropriate.
1382 if (options
->reverse_diff
)
1383 addremove
= (addremove
== '+' ? '-' :
1384 addremove
== '-' ? '+' : addremove
);
1386 if (!path
) path
= "";
1387 sprintf(concatpath
, "%s%s", base
, path
);
1388 one
= alloc_filespec(concatpath
);
1389 two
= alloc_filespec(concatpath
);
1391 if (addremove
!= '+')
1392 fill_filespec(one
, sha1
, mode
);
1393 if (addremove
!= '-')
1394 fill_filespec(two
, sha1
, mode
);
1396 diff_queue(&diff_queued_diff
, one
, two
);
1399 void diff_change(struct diff_options
*options
,
1400 unsigned old_mode
, unsigned new_mode
,
1401 const unsigned char *old_sha1
,
1402 const unsigned char *new_sha1
,
1403 const char *base
, const char *path
)
1405 char concatpath
[PATH_MAX
];
1406 struct diff_filespec
*one
, *two
;
1408 if (options
->reverse_diff
) {
1410 const unsigned char *tmp_c
;
1411 tmp
= old_mode
; old_mode
= new_mode
; new_mode
= tmp
;
1412 tmp_c
= old_sha1
; old_sha1
= new_sha1
; new_sha1
= tmp_c
;
1414 if (!path
) path
= "";
1415 sprintf(concatpath
, "%s%s", base
, path
);
1416 one
= alloc_filespec(concatpath
);
1417 two
= alloc_filespec(concatpath
);
1418 fill_filespec(one
, old_sha1
, old_mode
);
1419 fill_filespec(two
, new_sha1
, new_mode
);
1421 diff_queue(&diff_queued_diff
, one
, two
);
1424 void diff_unmerge(struct diff_options
*options
,
1427 struct diff_filespec
*one
, *two
;
1428 one
= alloc_filespec(path
);
1429 two
= alloc_filespec(path
);
1430 diff_queue(&diff_queued_diff
, one
, two
);