2 * Copyright (C) 2005 Junio C Hamano
12 static const char *diff_opts
= "-pu";
13 static unsigned char null_sha1
[20] = { 0, };
15 static int reverse_diff
;
16 static int use_size_cache
;
18 static const char *external_diff(void)
20 static const char *external_diff_cmd
= NULL
;
21 static int done_preparing
= 0;
22 const char *env_diff_opts
;
25 return external_diff_cmd
;
28 * Default values above are meant to match the
29 * Linux kernel development style. Examples of
30 * alternative styles you can specify via environment
35 external_diff_cmd
= getenv("GIT_EXTERNAL_DIFF");
37 /* In case external diff fails... */
38 env_diff_opts
= getenv("GIT_DIFF_OPTS");
39 if (env_diff_opts
) diff_opts
= env_diff_opts
;
42 return external_diff_cmd
;
45 #define TEMPFILE_PATH_LEN 50
47 static struct diff_tempfile
{
48 const char *name
; /* filename external diff should read from */
51 char tmp_path
[TEMPFILE_PATH_LEN
];
54 static int count_lines(const char *filename
)
57 int count
, ch
, completely_empty
= 1, nl_just_seen
= 0;
58 in
= fopen(filename
, "r");
60 while ((ch
= fgetc(in
)) != EOF
)
74 count
++; /* no trailing newline */
78 static void print_line_count(int count
)
88 printf("1,%d", count
);
93 static void copy_file(int prefix
, const char *filename
)
96 int ch
, nl_just_seen
= 1;
97 in
= fopen(filename
, "r");
98 while ((ch
= fgetc(in
)) != EOF
) {
109 printf("\n\\ No newline at end of file\n");
112 static void emit_rewrite_diff(const char *name_a
,
114 struct diff_tempfile
*temp
)
116 /* Use temp[i].name as input, name_a and name_b as labels */
118 lc_a
= count_lines(temp
[0].name
);
119 lc_b
= count_lines(temp
[1].name
);
120 printf("--- %s\n+++ %s\n@@ -", name_a
, name_b
);
121 print_line_count(lc_a
);
123 print_line_count(lc_b
);
126 copy_file('-', temp
[0].name
);
128 copy_file('+', temp
[1].name
);
131 static void builtin_diff(const char *name_a
,
133 struct diff_tempfile
*temp
,
134 const char *xfrm_msg
,
135 int complete_rewrite
)
137 int i
, next_at
, cmd_size
;
138 const char *const diff_cmd
= "diff -L%s%s -L%s%s";
139 const char *const diff_arg
= "%s %s||:"; /* "||:" is to return 0 */
140 const char *input_name_sq
[2];
141 const char *path0
[2];
142 const char *path1
[2];
143 const char *name_sq
[2];
146 name_sq
[0] = sq_quote(name_a
);
147 name_sq
[1] = sq_quote(name_b
);
149 /* diff_cmd and diff_arg have 6 %s in total which makes
150 * the sum of these strings 12 bytes larger than required.
151 * we use 2 spaces around diff-opts, and we need to count
152 * terminating NUL, so we subtract 9 here.
154 cmd_size
= (strlen(diff_cmd
) + strlen(diff_opts
) +
155 strlen(diff_arg
) - 9);
156 for (i
= 0; i
< 2; i
++) {
157 input_name_sq
[i
] = sq_quote(temp
[i
].name
);
158 if (!strcmp(temp
[i
].name
, "/dev/null")) {
159 path0
[i
] = "/dev/null";
162 path0
[i
] = i
? "b/" : "a/";
163 path1
[i
] = name_sq
[i
];
165 cmd_size
+= (strlen(path0
[i
]) + strlen(path1
[i
]) +
166 strlen(input_name_sq
[i
]));
169 cmd
= xmalloc(cmd_size
);
172 next_at
+= snprintf(cmd
+next_at
, cmd_size
-next_at
,
174 path0
[0], path1
[0], path0
[1], path1
[1]);
175 next_at
+= snprintf(cmd
+next_at
, cmd_size
-next_at
,
177 next_at
+= snprintf(cmd
+next_at
, cmd_size
-next_at
,
178 diff_arg
, input_name_sq
[0], input_name_sq
[1]);
180 printf("diff --git a/%s b/%s\n", name_a
, name_b
);
182 printf("new file mode %s\n", temp
[1].mode
);
183 if (xfrm_msg
&& xfrm_msg
[0])
186 else if (!path1
[1][0]) {
187 printf("deleted file mode %s\n", temp
[0].mode
);
188 if (xfrm_msg
&& xfrm_msg
[0])
192 if (strcmp(temp
[0].mode
, temp
[1].mode
)) {
193 printf("old mode %s\n", temp
[0].mode
);
194 printf("new mode %s\n", temp
[1].mode
);
196 if (xfrm_msg
&& xfrm_msg
[0])
198 if (strncmp(temp
[0].mode
, temp
[1].mode
, 3))
199 /* we do not run diff between different kind
203 if (complete_rewrite
) {
205 emit_rewrite_diff(name_a
, name_b
, temp
);
210 execlp("/bin/sh","sh", "-c", cmd
, NULL
);
213 struct diff_filespec
*alloc_filespec(const char *path
)
215 int namelen
= strlen(path
);
216 struct diff_filespec
*spec
= xmalloc(sizeof(*spec
) + namelen
+ 1);
218 memset(spec
, 0, sizeof(*spec
));
219 spec
->path
= (char *)(spec
+ 1);
220 memcpy(spec
->path
, path
, namelen
+1);
224 void fill_filespec(struct diff_filespec
*spec
, const unsigned char *sha1
,
228 spec
->mode
= DIFF_FILE_CANON_MODE(mode
);
229 memcpy(spec
->sha1
, sha1
, 20);
230 spec
->sha1_valid
= !!memcmp(sha1
, null_sha1
, 20);
235 * Given a name and sha1 pair, if the dircache tells us the file in
236 * the work tree has that object contents, return true, so that
237 * prepare_temp_file() does not have to inflate and extract.
239 static int work_tree_matches(const char *name
, const unsigned char *sha1
)
241 struct cache_entry
*ce
;
245 /* We do not read the cache ourselves here, because the
246 * benchmark with my previous version that always reads cache
247 * shows that it makes things worse for diff-tree comparing
248 * two linux-2.6 kernel trees in an already checked out work
249 * tree. This is because most diff-tree comparisons deal with
250 * only a small number of files, while reading the cache is
251 * expensive for a large project, and its cost outweighs the
252 * savings we get by not inflating the object to a temporary
253 * file. Practically, this code only helps when we are used
254 * by diff-cache --cached, which does read the cache before
261 pos
= cache_name_pos(name
, len
);
264 ce
= active_cache
[pos
];
265 if ((lstat(name
, &st
) < 0) ||
266 !S_ISREG(st
.st_mode
) || /* careful! */
267 ce_match_stat(ce
, &st
) ||
268 memcmp(sha1
, ce
->sha1
, 20))
270 /* we return 1 only when we can stat, it is a regular file,
271 * stat information matches, and sha1 recorded in the cache
272 * matches. I.e. we know the file in the work tree really is
273 * the same as the <name, sha1> pair.
278 static struct sha1_size_cache
{
279 unsigned char sha1
[20];
282 static int sha1_size_cache_nr
, sha1_size_cache_alloc
;
284 static struct sha1_size_cache
*locate_size_cache(unsigned char *sha1
,
289 struct sha1_size_cache
*e
;
292 last
= sha1_size_cache_nr
;
293 while (last
> first
) {
294 int cmp
, next
= (last
+ first
) >> 1;
295 e
= sha1_size_cache
[next
];
296 cmp
= memcmp(e
->sha1
, sha1
, 20);
308 /* insert to make it at "first" */
309 if (sha1_size_cache_alloc
<= sha1_size_cache_nr
) {
310 sha1_size_cache_alloc
= alloc_nr(sha1_size_cache_alloc
);
311 sha1_size_cache
= xrealloc(sha1_size_cache
,
312 sha1_size_cache_alloc
*
313 sizeof(*sha1_size_cache
));
315 sha1_size_cache_nr
++;
316 if (first
< sha1_size_cache_nr
)
317 memmove(sha1_size_cache
+ first
+ 1, sha1_size_cache
+ first
,
318 (sha1_size_cache_nr
- first
- 1) *
319 sizeof(*sha1_size_cache
));
320 e
= xmalloc(sizeof(struct sha1_size_cache
));
321 sha1_size_cache
[first
] = e
;
322 memcpy(e
->sha1
, sha1
, 20);
328 * While doing rename detection and pickaxe operation, we may need to
329 * grab the data for the blob (or file) for our own in-core comparison.
330 * diff_filespec has data and size fields for this purpose.
332 int diff_populate_filespec(struct diff_filespec
*s
, int size_only
)
335 if (!DIFF_FILE_VALID(s
))
336 die("internal error: asking to populate invalid file.");
337 if (S_ISDIR(s
->mode
))
345 if (!s
->sha1_valid
||
346 work_tree_matches(s
->path
, s
->sha1
)) {
349 if (lstat(s
->path
, &st
) < 0) {
350 if (errno
== ENOENT
) {
359 s
->size
= st
.st_size
;
364 if (S_ISLNK(st
.st_mode
)) {
366 s
->data
= xmalloc(s
->size
);
368 ret
= readlink(s
->path
, s
->data
, s
->size
);
375 fd
= open(s
->path
, O_RDONLY
);
378 s
->data
= mmap(NULL
, s
->size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
380 if (s
->data
== MAP_FAILED
)
382 s
->should_munmap
= 1;
386 struct sha1_size_cache
*e
;
389 e
= locate_size_cache(s
->sha1
, 1, 0);
394 if (!sha1_object_info(s
->sha1
, type
, &s
->size
))
395 locate_size_cache(s
->sha1
, 0, s
->size
);
398 s
->data
= read_sha1_file(s
->sha1
, type
, &s
->size
);
405 void diff_free_filespec_data(struct diff_filespec
*s
)
409 else if (s
->should_munmap
)
410 munmap(s
->data
, s
->size
);
411 s
->should_free
= s
->should_munmap
= 0;
415 static void prep_temp_blob(struct diff_tempfile
*temp
,
423 fd
= git_mkstemp(temp
->tmp_path
, TEMPFILE_PATH_LEN
, ".diff_XXXXXX");
425 die("unable to create temp-file");
426 if (write(fd
, blob
, size
) != size
)
427 die("unable to write temp-file");
429 temp
->name
= temp
->tmp_path
;
430 strcpy(temp
->hex
, sha1_to_hex(sha1
));
432 sprintf(temp
->mode
, "%06o", mode
);
435 static void prepare_temp_file(const char *name
,
436 struct diff_tempfile
*temp
,
437 struct diff_filespec
*one
)
439 if (!DIFF_FILE_VALID(one
)) {
441 /* A '-' entry produces this for file-2, and
442 * a '+' entry produces this for file-1.
444 temp
->name
= "/dev/null";
445 strcpy(temp
->hex
, ".");
446 strcpy(temp
->mode
, ".");
450 if (!one
->sha1_valid
||
451 work_tree_matches(name
, one
->sha1
)) {
453 if (lstat(name
, &st
) < 0) {
455 goto not_a_valid_file
;
456 die("stat(%s): %s", name
, strerror(errno
));
458 if (S_ISLNK(st
.st_mode
)) {
460 char *buf
, buf_
[1024];
461 buf
= ((sizeof(buf_
) < st
.st_size
) ?
462 xmalloc(st
.st_size
) : buf_
);
463 ret
= readlink(name
, buf
, st
.st_size
);
465 die("readlink(%s)", name
);
466 prep_temp_blob(temp
, buf
, st
.st_size
,
468 one
->sha1
: null_sha1
),
470 one
->mode
: S_IFLNK
));
473 /* we can borrow from the file in the work tree */
475 if (!one
->sha1_valid
)
476 strcpy(temp
->hex
, sha1_to_hex(null_sha1
));
478 strcpy(temp
->hex
, sha1_to_hex(one
->sha1
));
479 /* Even though we may sometimes borrow the
480 * contents from the work tree, we always want
481 * one->mode. mode is trustworthy even when
482 * !(one->sha1_valid), as long as
483 * DIFF_FILE_VALID(one).
485 sprintf(temp
->mode
, "%06o", one
->mode
);
490 if (diff_populate_filespec(one
, 0))
491 die("cannot read data blob for %s", one
->path
);
492 prep_temp_blob(temp
, one
->data
, one
->size
,
493 one
->sha1
, one
->mode
);
497 static void remove_tempfile(void)
501 for (i
= 0; i
< 2; i
++)
502 if (diff_temp
[i
].name
== diff_temp
[i
].tmp_path
) {
503 unlink(diff_temp
[i
].name
);
504 diff_temp
[i
].name
= NULL
;
508 static void remove_tempfile_on_signal(int signo
)
513 /* An external diff command takes:
515 * diff-cmd name infile1 infile1-sha1 infile1-mode \
516 * infile2 infile2-sha1 infile2-mode [ rename-to ]
519 static void run_external_diff(const char *pgm
,
522 struct diff_filespec
*one
,
523 struct diff_filespec
*two
,
524 const char *xfrm_msg
,
525 int complete_rewrite
)
527 struct diff_tempfile
*temp
= diff_temp
;
530 static int atexit_asked
= 0;
531 const char *othername
;
533 othername
= (other
? other
: name
);
535 prepare_temp_file(name
, &temp
[0], one
);
536 prepare_temp_file(othername
, &temp
[1], two
);
537 if (! atexit_asked
&&
538 (temp
[0].name
== temp
[0].tmp_path
||
539 temp
[1].name
== temp
[1].tmp_path
)) {
541 atexit(remove_tempfile
);
543 signal(SIGINT
, remove_tempfile_on_signal
);
549 die("unable to fork");
553 const char *exec_arg
[10];
554 const char **arg
= &exec_arg
[0];
557 *arg
++ = temp
[0].name
;
558 *arg
++ = temp
[0].hex
;
559 *arg
++ = temp
[0].mode
;
560 *arg
++ = temp
[1].name
;
561 *arg
++ = temp
[1].hex
;
562 *arg
++ = temp
[1].mode
;
568 execvp(pgm
, (char *const*) exec_arg
);
571 execlp(pgm
, pgm
, name
, NULL
);
574 * otherwise we use the built-in one.
577 builtin_diff(name
, othername
, temp
, xfrm_msg
,
580 printf("* Unmerged path %s\n", name
);
583 if (waitpid(pid
, &status
, 0) < 0 ||
584 !WIFEXITED(status
) || WEXITSTATUS(status
)) {
585 /* Earlier we did not check the exit status because
586 * diff exits non-zero if files are different, and
587 * we are not interested in knowing that. It was a
588 * mistake which made it harder to quit a diff-*
589 * session that uses the git-apply-patch-script as
590 * the GIT_EXTERNAL_DIFF. A custom GIT_EXTERNAL_DIFF
591 * should also exit non-zero only when it wants to
592 * abort the entire diff-* session.
595 fprintf(stderr
, "external diff died, stopping at %s.\n", name
);
601 static void run_diff(struct diff_filepair
*p
)
603 const char *pgm
= external_diff();
604 char msg_
[PATH_MAX
*2+200], *xfrm_msg
;
605 struct diff_filespec
*one
;
606 struct diff_filespec
*two
;
609 int complete_rewrite
= 0;
611 if (DIFF_PAIR_UNMERGED(p
)) {
613 run_external_diff(pgm
, p
->one
->path
, NULL
, NULL
, NULL
, NULL
,
619 other
= (strcmp(name
, p
->two
->path
) ? p
->two
->path
: NULL
);
620 one
= p
->one
; two
= p
->two
;
622 case DIFF_STATUS_COPIED
:
624 "similarity index %d%%\n"
627 (int)(0.5 + p
->score
* 100.0/MAX_SCORE
),
631 case DIFF_STATUS_RENAMED
:
633 "similarity index %d%%\n"
636 (int)(0.5 + p
->score
* 100.0/MAX_SCORE
),
640 case DIFF_STATUS_MODIFIED
:
643 "dissimilarity index %d%%",
644 (int)(0.5 + p
->score
* 100.0/MAX_SCORE
));
646 complete_rewrite
= 1;
655 DIFF_FILE_VALID(one
) && DIFF_FILE_VALID(two
) &&
656 (S_IFMT
& one
->mode
) != (S_IFMT
& two
->mode
)) {
657 /* a filepair that changes between file and symlink
658 * needs to be split into deletion and creation.
660 struct diff_filespec
*null
= alloc_filespec(two
->path
);
661 run_external_diff(NULL
, name
, other
, one
, null
, xfrm_msg
, 0);
663 null
= alloc_filespec(one
->path
);
664 run_external_diff(NULL
, name
, other
, null
, two
, xfrm_msg
, 0);
668 run_external_diff(pgm
, name
, other
, one
, two
, xfrm_msg
,
672 void diff_setup(int flags
)
674 if (flags
& DIFF_SETUP_REVERSE
)
676 if (flags
& DIFF_SETUP_USE_CACHE
) {
678 /* read-cache does not die even when it fails
679 * so it is safe for us to do this here. Also
680 * it does not smudge active_cache or active_nr
681 * when it fails, so we do not have to worry about
682 * cleaning it up oufselves either.
686 if (flags
& DIFF_SETUP_USE_SIZE_CACHE
)
691 static int parse_num(const char **cp_p
)
693 int num
, scale
, ch
, cnt
;
694 const char *cp
= *cp_p
;
698 while ('0' <= (ch
= *cp
) && ch
<= '9') {
700 /* We simply ignore more than 5 digits precision. */
702 num
= num
* 10 + ch
- '0';
708 /* user says num divided by scale and we say internally that
709 * is MAX_SCORE * num / scale.
711 return (MAX_SCORE
* num
/ scale
);
714 int diff_scoreopt_parse(const char *opt
)
721 if (cmd
!= 'M' && cmd
!= 'C' && cmd
!= 'B')
722 return -1; /* that is not a -M, -C nor -B option */
724 opt1
= parse_num(&opt
);
730 else if (*opt
!= '/')
731 return -1; /* we expect -B80/99 or -B80 */
734 opt2
= parse_num(&opt
);
739 return opt1
| (opt2
<< 16);
742 struct diff_queue_struct diff_queued_diff
;
744 void diff_q(struct diff_queue_struct
*queue
, struct diff_filepair
*dp
)
746 if (queue
->alloc
<= queue
->nr
) {
747 queue
->alloc
= alloc_nr(queue
->alloc
);
748 queue
->queue
= xrealloc(queue
->queue
,
749 sizeof(dp
) * queue
->alloc
);
751 queue
->queue
[queue
->nr
++] = dp
;
754 struct diff_filepair
*diff_queue(struct diff_queue_struct
*queue
,
755 struct diff_filespec
*one
,
756 struct diff_filespec
*two
)
758 struct diff_filepair
*dp
= xmalloc(sizeof(*dp
));
763 dp
->source_stays
= 0;
769 void diff_free_filepair(struct diff_filepair
*p
)
771 diff_free_filespec_data(p
->one
);
772 diff_free_filespec_data(p
->two
);
776 static void diff_flush_raw(struct diff_filepair
*p
,
777 int line_termination
,
778 int inter_name_termination
)
783 if (line_termination
) {
784 const char *const err
=
785 "path %s cannot be expressed without -z";
786 if (strchr(p
->one
->path
, line_termination
) ||
787 strchr(p
->one
->path
, inter_name_termination
))
788 die(err
, p
->one
->path
);
789 if (strchr(p
->two
->path
, line_termination
) ||
790 strchr(p
->two
->path
, inter_name_termination
))
791 die(err
, p
->two
->path
);
795 sprintf(status
, "%c%03d", p
->status
,
796 (int)(0.5 + p
->score
* 100.0/MAX_SCORE
));
798 status
[0] = p
->status
;
802 case DIFF_STATUS_COPIED
:
803 case DIFF_STATUS_RENAMED
:
806 case DIFF_STATUS_ADDED
:
807 case DIFF_STATUS_DELETED
:
814 printf(":%06o %06o %s ",
815 p
->one
->mode
, p
->two
->mode
, sha1_to_hex(p
->one
->sha1
));
817 sha1_to_hex(p
->two
->sha1
),
819 inter_name_termination
,
822 printf("%c%s", inter_name_termination
, p
->two
->path
);
823 putchar(line_termination
);
826 static void diff_flush_name(struct diff_filepair
*p
,
827 int line_termination
)
829 printf("%s%c", p
->two
->path
, line_termination
);
832 int diff_unmodified_pair(struct diff_filepair
*p
)
834 /* This function is written stricter than necessary to support
835 * the currently implemented transformers, but the idea is to
836 * let transformers to produce diff_filepairs any way they want,
837 * and filter and clean them up here before producing the output.
839 struct diff_filespec
*one
, *two
;
841 if (DIFF_PAIR_UNMERGED(p
))
842 return 0; /* unmerged is interesting */
847 /* deletion, addition, mode or type change
848 * and rename are all interesting.
850 if (DIFF_FILE_VALID(one
) != DIFF_FILE_VALID(two
) ||
851 DIFF_PAIR_MODE_CHANGED(p
) ||
852 strcmp(one
->path
, two
->path
))
855 /* both are valid and point at the same path. that is, we are
856 * dealing with a change.
858 if (one
->sha1_valid
&& two
->sha1_valid
&&
859 !memcmp(one
->sha1
, two
->sha1
, sizeof(one
->sha1
)))
860 return 1; /* no change */
861 if (!one
->sha1_valid
&& !two
->sha1_valid
)
862 return 1; /* both look at the same file on the filesystem. */
866 static void diff_flush_patch(struct diff_filepair
*p
)
868 if (diff_unmodified_pair(p
))
871 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
872 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
873 return; /* no tree diffs in patch format */
878 int diff_queue_is_empty(void)
880 struct diff_queue_struct
*q
= &diff_queued_diff
;
882 for (i
= 0; i
< q
->nr
; i
++)
883 if (!diff_unmodified_pair(q
->queue
[i
]))
889 void diff_debug_filespec(struct diff_filespec
*s
, int x
, const char *one
)
891 fprintf(stderr
, "queue[%d] %s (%s) %s %06o %s\n",
894 DIFF_FILE_VALID(s
) ? "valid" : "invalid",
896 s
->sha1_valid
? sha1_to_hex(s
->sha1
) : "");
897 fprintf(stderr
, "queue[%d] %s size %lu flags %d\n",
899 s
->size
, s
->xfrm_flags
);
902 void diff_debug_filepair(const struct diff_filepair
*p
, int i
)
904 diff_debug_filespec(p
->one
, i
, "one");
905 diff_debug_filespec(p
->two
, i
, "two");
906 fprintf(stderr
, "score %d, status %c stays %d broken %d\n",
907 p
->score
, p
->status
? p
->status
: '?',
908 p
->source_stays
, p
->broken_pair
);
911 void diff_debug_queue(const char *msg
, struct diff_queue_struct
*q
)
915 fprintf(stderr
, "%s\n", msg
);
916 fprintf(stderr
, "q->nr = %d\n", q
->nr
);
917 for (i
= 0; i
< q
->nr
; i
++) {
918 struct diff_filepair
*p
= q
->queue
[i
];
919 diff_debug_filepair(p
, i
);
924 static void diff_resolve_rename_copy(void)
927 struct diff_filepair
*p
, *pp
;
928 struct diff_queue_struct
*q
= &diff_queued_diff
;
930 diff_debug_queue("resolve-rename-copy", q
);
932 for (i
= 0; i
< q
->nr
; i
++) {
934 p
->status
= 0; /* undecided */
935 if (DIFF_PAIR_UNMERGED(p
))
936 p
->status
= DIFF_STATUS_UNMERGED
;
937 else if (!DIFF_FILE_VALID(p
->one
))
938 p
->status
= DIFF_STATUS_ADDED
;
939 else if (!DIFF_FILE_VALID(p
->two
))
940 p
->status
= DIFF_STATUS_DELETED
;
941 else if (DIFF_PAIR_TYPE_CHANGED(p
))
942 p
->status
= DIFF_STATUS_TYPE_CHANGED
;
944 /* from this point on, we are dealing with a pair
945 * whose both sides are valid and of the same type, i.e.
946 * either in-place edit or rename/copy edit.
948 else if (DIFF_PAIR_RENAME(p
)) {
949 if (p
->source_stays
) {
950 p
->status
= DIFF_STATUS_COPIED
;
953 /* See if there is some other filepair that
954 * copies from the same source as us. If so
955 * we are a copy. Otherwise we are either a
956 * copy if the path stays, or a rename if it
957 * does not, but we already handled "stays" case.
959 for (j
= i
+ 1; j
< q
->nr
; j
++) {
961 if (strcmp(pp
->one
->path
, p
->one
->path
))
962 continue; /* not us */
963 if (!DIFF_PAIR_RENAME(pp
))
964 continue; /* not a rename/copy */
965 /* pp is a rename/copy from the same source */
966 p
->status
= DIFF_STATUS_COPIED
;
970 p
->status
= DIFF_STATUS_RENAMED
;
972 else if (memcmp(p
->one
->sha1
, p
->two
->sha1
, 20) ||
973 p
->one
->mode
!= p
->two
->mode
)
974 p
->status
= DIFF_STATUS_MODIFIED
;
976 /* This is a "no-change" entry and should not
977 * happen anymore, but prepare for broken callers.
979 error("feeding unmodified %s to diffcore",
981 p
->status
= DIFF_STATUS_UNKNOWN
;
984 diff_debug_queue("resolve-rename-copy done", q
);
987 void diff_flush(int diff_output_style
, int line_termination
)
989 struct diff_queue_struct
*q
= &diff_queued_diff
;
991 int inter_name_termination
= '\t';
993 if (!line_termination
)
994 inter_name_termination
= 0;
996 for (i
= 0; i
< q
->nr
; i
++) {
997 struct diff_filepair
*p
= q
->queue
[i
];
998 if ((diff_output_style
== DIFF_FORMAT_NO_OUTPUT
) ||
999 (p
->status
== DIFF_STATUS_UNKNOWN
))
1002 die("internal error in diff-resolve-rename-copy");
1003 switch (diff_output_style
) {
1004 case DIFF_FORMAT_PATCH
:
1005 diff_flush_patch(p
);
1007 case DIFF_FORMAT_RAW
:
1008 diff_flush_raw(p
, line_termination
,
1009 inter_name_termination
);
1011 case DIFF_FORMAT_NAME
:
1012 diff_flush_name(p
, line_termination
);
1015 diff_free_filepair(q
->queue
[i
]);
1019 q
->nr
= q
->alloc
= 0;
1022 static void diffcore_apply_filter(const char *filter
)
1025 struct diff_queue_struct
*q
= &diff_queued_diff
;
1026 struct diff_queue_struct outq
;
1028 outq
.nr
= outq
.alloc
= 0;
1033 if (strchr(filter
, DIFF_STATUS_FILTER_AON
)) {
1035 for (i
= found
= 0; !found
&& i
< q
->nr
; i
++) {
1036 struct diff_filepair
*p
= q
->queue
[i
];
1037 if (((p
->status
== DIFF_STATUS_MODIFIED
) &&
1039 strchr(filter
, DIFF_STATUS_FILTER_BROKEN
)) ||
1041 strchr(filter
, DIFF_STATUS_MODIFIED
)))) ||
1042 ((p
->status
!= DIFF_STATUS_MODIFIED
) &&
1043 strchr(filter
, p
->status
)))
1049 /* otherwise we will clear the whole queue
1050 * by copying the empty outq at the end of this
1051 * function, but first clear the current entries
1054 for (i
= 0; i
< q
->nr
; i
++)
1055 diff_free_filepair(q
->queue
[i
]);
1058 /* Only the matching ones */
1059 for (i
= 0; i
< q
->nr
; i
++) {
1060 struct diff_filepair
*p
= q
->queue
[i
];
1062 if (((p
->status
== DIFF_STATUS_MODIFIED
) &&
1064 strchr(filter
, DIFF_STATUS_FILTER_BROKEN
)) ||
1066 strchr(filter
, DIFF_STATUS_MODIFIED
)))) ||
1067 ((p
->status
!= DIFF_STATUS_MODIFIED
) &&
1068 strchr(filter
, p
->status
)))
1071 diff_free_filepair(p
);
1078 void diffcore_std(const char **paths
,
1079 int detect_rename
, int rename_score
,
1080 const char *pickaxe
, int pickaxe_opts
,
1082 const char *orderfile
,
1085 if (paths
&& paths
[0])
1086 diffcore_pathspec(paths
);
1087 if (break_opt
!= -1)
1088 diffcore_break(break_opt
);
1090 diffcore_rename(detect_rename
, rename_score
);
1091 if (break_opt
!= -1)
1092 diffcore_merge_broken();
1094 diffcore_pickaxe(pickaxe
, pickaxe_opts
);
1096 diffcore_order(orderfile
);
1097 diff_resolve_rename_copy();
1098 diffcore_apply_filter(filter
);
1102 void diffcore_std_no_resolve(const char **paths
,
1103 const char *pickaxe
, int pickaxe_opts
,
1104 const char *orderfile
,
1107 if (paths
&& paths
[0])
1108 diffcore_pathspec(paths
);
1110 diffcore_pickaxe(pickaxe
, pickaxe_opts
);
1112 diffcore_order(orderfile
);
1113 diffcore_apply_filter(filter
);
1116 void diff_addremove(int addremove
, unsigned mode
,
1117 const unsigned char *sha1
,
1118 const char *base
, const char *path
)
1120 char concatpath
[PATH_MAX
];
1121 struct diff_filespec
*one
, *two
;
1123 /* This may look odd, but it is a preparation for
1124 * feeding "there are unchanged files which should
1125 * not produce diffs, but when you are doing copy
1126 * detection you would need them, so here they are"
1127 * entries to the diff-core. They will be prefixed
1128 * with something like '=' or '*' (I haven't decided
1129 * which but should not make any difference).
1130 * Feeding the same new and old to diff_change()
1131 * also has the same effect.
1132 * Before the final output happens, they are pruned after
1133 * merged into rename/copy pairs as appropriate.
1136 addremove
= (addremove
== '+' ? '-' :
1137 addremove
== '-' ? '+' : addremove
);
1139 if (!path
) path
= "";
1140 sprintf(concatpath
, "%s%s", base
, path
);
1141 one
= alloc_filespec(concatpath
);
1142 two
= alloc_filespec(concatpath
);
1144 if (addremove
!= '+')
1145 fill_filespec(one
, sha1
, mode
);
1146 if (addremove
!= '-')
1147 fill_filespec(two
, sha1
, mode
);
1149 diff_queue(&diff_queued_diff
, one
, two
);
1152 void diff_helper_input(unsigned old_mode
,
1154 const unsigned char *old_sha1
,
1155 const unsigned char *new_sha1
,
1156 const char *old_path
,
1159 const char *new_path
)
1161 struct diff_filespec
*one
, *two
;
1162 struct diff_filepair
*dp
;
1164 one
= alloc_filespec(old_path
);
1165 two
= alloc_filespec(new_path
);
1167 fill_filespec(one
, old_sha1
, old_mode
);
1169 fill_filespec(two
, new_sha1
, new_mode
);
1170 dp
= diff_queue(&diff_queued_diff
, one
, two
);
1171 dp
->score
= score
* MAX_SCORE
/ 100;
1172 dp
->status
= status
;
1175 void diff_change(unsigned old_mode
, unsigned new_mode
,
1176 const unsigned char *old_sha1
,
1177 const unsigned char *new_sha1
,
1178 const char *base
, const char *path
)
1180 char concatpath
[PATH_MAX
];
1181 struct diff_filespec
*one
, *two
;
1185 const unsigned char *tmp_c
;
1186 tmp
= old_mode
; old_mode
= new_mode
; new_mode
= tmp
;
1187 tmp_c
= old_sha1
; old_sha1
= new_sha1
; new_sha1
= tmp_c
;
1189 if (!path
) path
= "";
1190 sprintf(concatpath
, "%s%s", base
, path
);
1191 one
= alloc_filespec(concatpath
);
1192 two
= alloc_filespec(concatpath
);
1193 fill_filespec(one
, old_sha1
, old_mode
);
1194 fill_filespec(two
, new_sha1
, new_mode
);
1196 diff_queue(&diff_queued_diff
, one
, two
);
1199 void diff_unmerge(const char *path
)
1201 struct diff_filespec
*one
, *two
;
1202 one
= alloc_filespec(path
);
1203 two
= alloc_filespec(path
);
1204 diff_queue(&diff_queued_diff
, one
, two
);