2 * Copyright (C) 2005 Junio C Hamano
11 static const char *diff_opts
= "-pu";
12 static unsigned char null_sha1
[20] = { 0, };
14 static int reverse_diff
;
15 static int use_size_cache
;
17 static const char *external_diff(void)
19 static const char *external_diff_cmd
= NULL
;
20 static int done_preparing
= 0;
23 return external_diff_cmd
;
26 * Default values above are meant to match the
27 * Linux kernel development style. Examples of
28 * alternative styles you can specify via environment
33 if (gitenv("GIT_EXTERNAL_DIFF"))
34 external_diff_cmd
= gitenv("GIT_EXTERNAL_DIFF");
36 /* In case external diff fails... */
37 diff_opts
= gitenv("GIT_DIFF_OPTS") ? : diff_opts
;
40 return external_diff_cmd
;
43 /* Help to copy the thing properly quoted for the shell safety.
44 * any single quote is replaced with '\'', and the caller is
45 * expected to enclose the result within a single quote pair.
48 * original sq_expand result
49 * name ==> name ==> 'name'
50 * a b ==> a b ==> 'a b'
51 * a'b ==> a'\''b ==> 'a'\''b'
53 static char *sq_expand(const char *src
)
55 static char *buf
= NULL
;
60 /* count bytes needed to store the quoted string. */
61 for (cnt
= 1, cp
= src
; *cp
; cnt
++, cp
++)
67 while ((c
= *src
++)) {
71 bp
= strcpy(bp
, "'\\''");
79 static struct diff_tempfile
{
80 const char *name
; /* filename external diff should read from */
86 static int count_lines(const char *filename
)
89 int count
, ch
, completely_empty
= 1, nl_just_seen
= 0;
90 in
= fopen(filename
, "r");
92 while ((ch
= fgetc(in
)) != EOF
)
100 completely_empty
= 0;
103 if (completely_empty
)
106 count
++; /* no trailing newline */
110 static void print_line_count(int count
)
120 printf("1,%d", count
);
125 static void copy_file(int prefix
, const char *filename
)
128 int ch
, nl_just_seen
= 1;
129 in
= fopen(filename
, "r");
130 while ((ch
= fgetc(in
)) != EOF
) {
141 printf("\n\\ No newline at end of file\n");
144 static void emit_rewrite_diff(const char *name_a
,
146 struct diff_tempfile
*temp
)
148 /* Use temp[i].name as input, name_a and name_b as labels */
150 lc_a
= count_lines(temp
[0].name
);
151 lc_b
= count_lines(temp
[1].name
);
152 printf("--- %s\n+++ %s\n@@ -", name_a
, name_b
);
153 print_line_count(lc_a
);
155 print_line_count(lc_b
);
158 copy_file('-', temp
[0].name
);
160 copy_file('+', temp
[1].name
);
163 static void builtin_diff(const char *name_a
,
165 struct diff_tempfile
*temp
,
166 const char *xfrm_msg
,
167 int complete_rewrite
)
169 int i
, next_at
, cmd_size
;
170 const char *diff_cmd
= "diff -L'%s%s' -L'%s%s'";
171 const char *diff_arg
= "'%s' '%s'||:"; /* "||:" is to return 0 */
172 const char *input_name_sq
[2];
173 const char *path0
[2];
174 const char *path1
[2];
175 const char *name_sq
[2];
178 name_sq
[0] = sq_expand(name_a
);
179 name_sq
[1] = sq_expand(name_b
);
181 /* diff_cmd and diff_arg have 6 %s in total which makes
182 * the sum of these strings 12 bytes larger than required.
183 * we use 2 spaces around diff-opts, and we need to count
184 * terminating NUL, so we subtract 9 here.
186 cmd_size
= (strlen(diff_cmd
) + strlen(diff_opts
) +
187 strlen(diff_arg
) - 9);
188 for (i
= 0; i
< 2; i
++) {
189 input_name_sq
[i
] = sq_expand(temp
[i
].name
);
190 if (!strcmp(temp
[i
].name
, "/dev/null")) {
191 path0
[i
] = "/dev/null";
194 path0
[i
] = i
? "b/" : "a/";
195 path1
[i
] = name_sq
[i
];
197 cmd_size
+= (strlen(path0
[i
]) + strlen(path1
[i
]) +
198 strlen(input_name_sq
[i
]));
201 cmd
= xmalloc(cmd_size
);
204 next_at
+= snprintf(cmd
+next_at
, cmd_size
-next_at
,
206 path0
[0], path1
[0], path0
[1], path1
[1]);
207 next_at
+= snprintf(cmd
+next_at
, cmd_size
-next_at
,
209 next_at
+= snprintf(cmd
+next_at
, cmd_size
-next_at
,
210 diff_arg
, input_name_sq
[0], input_name_sq
[1]);
212 printf("diff --git a/%s b/%s\n", name_a
, name_b
);
214 printf("new file mode %s\n", temp
[1].mode
);
215 if (xfrm_msg
&& xfrm_msg
[0])
218 else if (!path1
[1][0]) {
219 printf("deleted file mode %s\n", temp
[0].mode
);
220 if (xfrm_msg
&& xfrm_msg
[0])
224 if (strcmp(temp
[0].mode
, temp
[1].mode
)) {
225 printf("old mode %s\n", temp
[0].mode
);
226 printf("new mode %s\n", temp
[1].mode
);
228 if (xfrm_msg
&& xfrm_msg
[0])
230 if (strncmp(temp
[0].mode
, temp
[1].mode
, 3))
231 /* we do not run diff between different kind
235 if (complete_rewrite
) {
237 emit_rewrite_diff(name_a
, name_b
, temp
);
242 execlp("/bin/sh","sh", "-c", cmd
, NULL
);
245 struct diff_filespec
*alloc_filespec(const char *path
)
247 int namelen
= strlen(path
);
248 struct diff_filespec
*spec
= xmalloc(sizeof(*spec
) + namelen
+ 1);
249 spec
->path
= (char *)(spec
+ 1);
250 strcpy(spec
->path
, path
);
251 spec
->should_free
= spec
->should_munmap
= 0;
252 spec
->xfrm_flags
= 0;
256 memset(spec
->sha1
, 0, 20);
260 void fill_filespec(struct diff_filespec
*spec
, const unsigned char *sha1
,
264 spec
->mode
= DIFF_FILE_CANON_MODE(mode
);
265 memcpy(spec
->sha1
, sha1
, 20);
266 spec
->sha1_valid
= !!memcmp(sha1
, null_sha1
, 20);
271 * Given a name and sha1 pair, if the dircache tells us the file in
272 * the work tree has that object contents, return true, so that
273 * prepare_temp_file() does not have to inflate and extract.
275 static int work_tree_matches(const char *name
, const unsigned char *sha1
)
277 struct cache_entry
*ce
;
281 /* We do not read the cache ourselves here, because the
282 * benchmark with my previous version that always reads cache
283 * shows that it makes things worse for diff-tree comparing
284 * two linux-2.6 kernel trees in an already checked out work
285 * tree. This is because most diff-tree comparisons deal with
286 * only a small number of files, while reading the cache is
287 * expensive for a large project, and its cost outweighs the
288 * savings we get by not inflating the object to a temporary
289 * file. Practically, this code only helps when we are used
290 * by diff-cache --cached, which does read the cache before
297 pos
= cache_name_pos(name
, len
);
300 ce
= active_cache
[pos
];
301 if ((lstat(name
, &st
) < 0) ||
302 !S_ISREG(st
.st_mode
) || /* careful! */
303 ce_match_stat(ce
, &st
) ||
304 memcmp(sha1
, ce
->sha1
, 20))
306 /* we return 1 only when we can stat, it is a regular file,
307 * stat information matches, and sha1 recorded in the cache
308 * matches. I.e. we know the file in the work tree really is
309 * the same as the <name, sha1> pair.
314 static struct sha1_size_cache
{
315 unsigned char sha1
[20];
318 static int sha1_size_cache_nr
, sha1_size_cache_alloc
;
320 static struct sha1_size_cache
*locate_size_cache(unsigned char *sha1
,
325 struct sha1_size_cache
*e
;
328 last
= sha1_size_cache_nr
;
329 while (last
> first
) {
330 int cmp
, next
= (last
+ first
) >> 1;
331 e
= sha1_size_cache
[next
];
332 cmp
= memcmp(e
->sha1
, sha1
, 20);
344 /* insert to make it at "first" */
345 if (sha1_size_cache_alloc
<= sha1_size_cache_nr
) {
346 sha1_size_cache_alloc
= alloc_nr(sha1_size_cache_alloc
);
347 sha1_size_cache
= xrealloc(sha1_size_cache
,
348 sha1_size_cache_alloc
*
349 sizeof(*sha1_size_cache
));
351 sha1_size_cache_nr
++;
352 if (first
< sha1_size_cache_nr
)
353 memmove(sha1_size_cache
+ first
+ 1, sha1_size_cache
+ first
,
354 (sha1_size_cache_nr
- first
- 1) *
355 sizeof(*sha1_size_cache
));
356 e
= xmalloc(sizeof(struct sha1_size_cache
));
357 sha1_size_cache
[first
] = e
;
358 memcpy(e
->sha1
, sha1
, 20);
364 * While doing rename detection and pickaxe operation, we may need to
365 * grab the data for the blob (or file) for our own in-core comparison.
366 * diff_filespec has data and size fields for this purpose.
368 int diff_populate_filespec(struct diff_filespec
*s
, int size_only
)
371 if (!DIFF_FILE_VALID(s
))
372 die("internal error: asking to populate invalid file.");
373 if (S_ISDIR(s
->mode
))
381 if (!s
->sha1_valid
||
382 work_tree_matches(s
->path
, s
->sha1
)) {
385 if (lstat(s
->path
, &st
) < 0) {
386 if (errno
== ENOENT
) {
395 s
->size
= st
.st_size
;
400 if (S_ISLNK(st
.st_mode
)) {
402 s
->data
= xmalloc(s
->size
);
404 ret
= readlink(s
->path
, s
->data
, s
->size
);
411 fd
= open(s
->path
, O_RDONLY
);
414 s
->data
= mmap(NULL
, s
->size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
415 s
->should_munmap
= 1;
420 struct sha1_size_cache
*e
;
423 e
= locate_size_cache(s
->sha1
, 1, 0);
428 if (!sha1_file_size(s
->sha1
, &s
->size
))
429 locate_size_cache(s
->sha1
, 0, s
->size
);
432 s
->data
= read_sha1_file(s
->sha1
, type
, &s
->size
);
439 void diff_free_filespec_data(struct diff_filespec
*s
)
443 else if (s
->should_munmap
)
444 munmap(s
->data
, s
->size
);
445 s
->should_free
= s
->should_munmap
= 0;
449 static void prep_temp_blob(struct diff_tempfile
*temp
,
457 strcpy(temp
->tmp_path
, ".diff_XXXXXX");
458 fd
= mkstemp(temp
->tmp_path
);
460 die("unable to create temp-file");
461 if (write(fd
, blob
, size
) != size
)
462 die("unable to write temp-file");
464 temp
->name
= temp
->tmp_path
;
465 strcpy(temp
->hex
, sha1_to_hex(sha1
));
467 sprintf(temp
->mode
, "%06o", mode
);
470 static void prepare_temp_file(const char *name
,
471 struct diff_tempfile
*temp
,
472 struct diff_filespec
*one
)
474 if (!DIFF_FILE_VALID(one
)) {
476 /* A '-' entry produces this for file-2, and
477 * a '+' entry produces this for file-1.
479 temp
->name
= "/dev/null";
480 strcpy(temp
->hex
, ".");
481 strcpy(temp
->mode
, ".");
485 if (!one
->sha1_valid
||
486 work_tree_matches(name
, one
->sha1
)) {
488 if (lstat(name
, &st
) < 0) {
490 goto not_a_valid_file
;
491 die("stat(%s): %s", name
, strerror(errno
));
493 if (S_ISLNK(st
.st_mode
)) {
495 char *buf
, buf_
[1024];
496 buf
= ((sizeof(buf_
) < st
.st_size
) ?
497 xmalloc(st
.st_size
) : buf_
);
498 ret
= readlink(name
, buf
, st
.st_size
);
500 die("readlink(%s)", name
);
501 prep_temp_blob(temp
, buf
, st
.st_size
,
503 one
->sha1
: null_sha1
),
505 one
->mode
: S_IFLNK
));
508 /* we can borrow from the file in the work tree */
510 if (!one
->sha1_valid
)
511 strcpy(temp
->hex
, sha1_to_hex(null_sha1
));
513 strcpy(temp
->hex
, sha1_to_hex(one
->sha1
));
514 /* Even though we may sometimes borrow the
515 * contents from the work tree, we always want
516 * one->mode. mode is trustworthy even when
517 * !(one->sha1_valid), as long as
518 * DIFF_FILE_VALID(one).
520 sprintf(temp
->mode
, "%06o", one
->mode
);
525 if (diff_populate_filespec(one
, 0))
526 die("cannot read data blob for %s", one
->path
);
527 prep_temp_blob(temp
, one
->data
, one
->size
,
528 one
->sha1
, one
->mode
);
532 static void remove_tempfile(void)
536 for (i
= 0; i
< 2; i
++)
537 if (diff_temp
[i
].name
== diff_temp
[i
].tmp_path
) {
538 unlink(diff_temp
[i
].name
);
539 diff_temp
[i
].name
= NULL
;
543 static void remove_tempfile_on_signal(int signo
)
548 /* An external diff command takes:
550 * diff-cmd name infile1 infile1-sha1 infile1-mode \
551 * infile2 infile2-sha1 infile2-mode [ rename-to ]
554 static void run_external_diff(const char *pgm
,
557 struct diff_filespec
*one
,
558 struct diff_filespec
*two
,
559 const char *xfrm_msg
,
560 int complete_rewrite
)
562 struct diff_tempfile
*temp
= diff_temp
;
565 static int atexit_asked
= 0;
568 prepare_temp_file(name
, &temp
[0], one
);
569 prepare_temp_file(other
? : name
, &temp
[1], two
);
570 if (! atexit_asked
&&
571 (temp
[0].name
== temp
[0].tmp_path
||
572 temp
[1].name
== temp
[1].tmp_path
)) {
574 atexit(remove_tempfile
);
576 signal(SIGINT
, remove_tempfile_on_signal
);
582 die("unable to fork");
586 const char *exec_arg
[10];
587 const char **arg
= &exec_arg
[0];
590 *arg
++ = temp
[0].name
;
591 *arg
++ = temp
[0].hex
;
592 *arg
++ = temp
[0].mode
;
593 *arg
++ = temp
[1].name
;
594 *arg
++ = temp
[1].hex
;
595 *arg
++ = temp
[1].mode
;
601 execvp(pgm
, (char *const*) exec_arg
);
604 execlp(pgm
, pgm
, name
, NULL
);
607 * otherwise we use the built-in one.
610 builtin_diff(name
, other
? : name
, temp
, xfrm_msg
,
613 printf("* Unmerged path %s\n", name
);
616 if (waitpid(pid
, &status
, 0) < 0 ||
617 !WIFEXITED(status
) || WEXITSTATUS(status
)) {
618 /* Earlier we did not check the exit status because
619 * diff exits non-zero if files are different, and
620 * we are not interested in knowing that. It was a
621 * mistake which made it harder to quit a diff-*
622 * session that uses the git-apply-patch-script as
623 * the GIT_EXTERNAL_DIFF. A custom GIT_EXTERNAL_DIFF
624 * should also exit non-zero only when it wants to
625 * abort the entire diff-* session.
628 fprintf(stderr
, "external diff died, stopping at %s.\n", name
);
634 static void run_diff(struct diff_filepair
*p
)
636 const char *pgm
= external_diff();
637 char msg_
[PATH_MAX
*2+200], *xfrm_msg
;
638 struct diff_filespec
*one
;
639 struct diff_filespec
*two
;
642 int complete_rewrite
= 0;
644 if (DIFF_PAIR_UNMERGED(p
)) {
646 run_external_diff(pgm
, p
->one
->path
, NULL
, NULL
, NULL
, NULL
,
652 other
= (strcmp(name
, p
->two
->path
) ? p
->two
->path
: NULL
);
653 one
= p
->one
; two
= p
->two
;
657 "similarity index %d%%\n"
660 (int)(0.5 + p
->score
* 100.0/MAX_SCORE
),
666 "similarity index %d%%\n"
669 (int)(0.5 + p
->score
* 100.0/MAX_SCORE
),
676 "dissimilarity index %d%%",
677 (int)(0.5 + p
->score
* 100.0/MAX_SCORE
));
679 complete_rewrite
= 1;
688 DIFF_FILE_VALID(one
) && DIFF_FILE_VALID(two
) &&
689 (S_IFMT
& one
->mode
) != (S_IFMT
& two
->mode
)) {
690 /* a filepair that changes between file and symlink
691 * needs to be split into deletion and creation.
693 struct diff_filespec
*null
= alloc_filespec(two
->path
);
694 run_external_diff(NULL
, name
, other
, one
, null
, xfrm_msg
, 0);
696 null
= alloc_filespec(one
->path
);
697 run_external_diff(NULL
, name
, other
, null
, two
, xfrm_msg
, 0);
701 run_external_diff(pgm
, name
, other
, one
, two
, xfrm_msg
,
705 void diff_setup(int flags
)
707 if (flags
& DIFF_SETUP_REVERSE
)
709 if (flags
& DIFF_SETUP_USE_CACHE
) {
711 /* read-cache does not die even when it fails
712 * so it is safe for us to do this here. Also
713 * it does not smudge active_cache or active_nr
714 * when it fails, so we do not have to worry about
715 * cleaning it up oufselves either.
719 if (flags
& DIFF_SETUP_USE_SIZE_CACHE
)
724 static int parse_num(const char **cp_p
)
726 int num
, scale
, ch
, cnt
;
727 const char *cp
= *cp_p
;
731 while ('0' <= (ch
= *cp
) && ch
<= '9') {
733 /* We simply ignore more than 5 digits precision. */
735 num
= num
* 10 + ch
- '0';
741 /* user says num divided by scale and we say internally that
742 * is MAX_SCORE * num / scale.
744 return (MAX_SCORE
* num
/ scale
);
747 int diff_scoreopt_parse(const char *opt
)
754 if (cmd
!= 'M' && cmd
!= 'C' && cmd
!= 'B')
755 return -1; /* that is not a -M, -C nor -B option */
757 opt1
= parse_num(&opt
);
763 else if (*opt
!= '/')
764 return -1; /* we expect -B80/99 or -B80 */
767 opt2
= parse_num(&opt
);
772 return opt1
| (opt2
<< 16);
775 struct diff_queue_struct diff_queued_diff
;
777 void diff_q(struct diff_queue_struct
*queue
, struct diff_filepair
*dp
)
779 if (queue
->alloc
<= queue
->nr
) {
780 queue
->alloc
= alloc_nr(queue
->alloc
);
781 queue
->queue
= xrealloc(queue
->queue
,
782 sizeof(dp
) * queue
->alloc
);
784 queue
->queue
[queue
->nr
++] = dp
;
787 struct diff_filepair
*diff_queue(struct diff_queue_struct
*queue
,
788 struct diff_filespec
*one
,
789 struct diff_filespec
*two
)
791 struct diff_filepair
*dp
= xmalloc(sizeof(*dp
));
796 dp
->source_stays
= 0;
802 void diff_free_filepair(struct diff_filepair
*p
)
804 diff_free_filespec_data(p
->one
);
805 diff_free_filespec_data(p
->two
);
809 static void diff_flush_raw(struct diff_filepair
*p
,
810 int line_termination
,
811 int inter_name_termination
)
816 if (line_termination
) {
817 const char *err
= "path %s cannot be expressed without -z";
818 if (strchr(p
->one
->path
, line_termination
) ||
819 strchr(p
->one
->path
, inter_name_termination
))
820 die(err
, p
->one
->path
);
821 if (strchr(p
->two
->path
, line_termination
) ||
822 strchr(p
->two
->path
, inter_name_termination
))
823 die(err
, p
->two
->path
);
827 sprintf(status
, "%c%03d", p
->status
,
828 (int)(0.5 + p
->score
* 100.0/MAX_SCORE
));
830 status
[0] = p
->status
;
844 printf(":%06o %06o %s ",
845 p
->one
->mode
, p
->two
->mode
, sha1_to_hex(p
->one
->sha1
));
847 sha1_to_hex(p
->two
->sha1
),
849 inter_name_termination
,
852 printf("%c%s", inter_name_termination
, p
->two
->path
);
853 putchar(line_termination
);
856 int diff_unmodified_pair(struct diff_filepair
*p
)
858 /* This function is written stricter than necessary to support
859 * the currently implemented transformers, but the idea is to
860 * let transformers to produce diff_filepairs any way they want,
861 * and filter and clean them up here before producing the output.
863 struct diff_filespec
*one
, *two
;
865 if (DIFF_PAIR_UNMERGED(p
))
866 return 0; /* unmerged is interesting */
871 /* deletion, addition, mode or type change
872 * and rename are all interesting.
874 if (DIFF_FILE_VALID(one
) != DIFF_FILE_VALID(two
) ||
875 DIFF_PAIR_MODE_CHANGED(p
) ||
876 strcmp(one
->path
, two
->path
))
879 /* both are valid and point at the same path. that is, we are
880 * dealing with a change.
882 if (one
->sha1_valid
&& two
->sha1_valid
&&
883 !memcmp(one
->sha1
, two
->sha1
, sizeof(one
->sha1
)))
884 return 1; /* no change */
885 if (!one
->sha1_valid
&& !two
->sha1_valid
)
886 return 1; /* both look at the same file on the filesystem. */
890 static void diff_flush_patch(struct diff_filepair
*p
)
892 if (diff_unmodified_pair(p
))
895 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
896 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
897 return; /* no tree diffs in patch format */
902 int diff_queue_is_empty(void)
904 struct diff_queue_struct
*q
= &diff_queued_diff
;
906 for (i
= 0; i
< q
->nr
; i
++)
907 if (!diff_unmodified_pair(q
->queue
[i
]))
913 void diff_debug_filespec(struct diff_filespec
*s
, int x
, const char *one
)
915 fprintf(stderr
, "queue[%d] %s (%s) %s %06o %s\n",
918 DIFF_FILE_VALID(s
) ? "valid" : "invalid",
920 s
->sha1_valid
? sha1_to_hex(s
->sha1
) : "");
921 fprintf(stderr
, "queue[%d] %s size %lu flags %d\n",
923 s
->size
, s
->xfrm_flags
);
926 void diff_debug_filepair(const struct diff_filepair
*p
, int i
)
928 diff_debug_filespec(p
->one
, i
, "one");
929 diff_debug_filespec(p
->two
, i
, "two");
930 fprintf(stderr
, "score %d, status %c stays %d broken %d\n",
931 p
->score
, p
->status
? : '?',
932 p
->source_stays
, p
->broken_pair
);
935 void diff_debug_queue(const char *msg
, struct diff_queue_struct
*q
)
939 fprintf(stderr
, "%s\n", msg
);
940 fprintf(stderr
, "q->nr = %d\n", q
->nr
);
941 for (i
= 0; i
< q
->nr
; i
++) {
942 struct diff_filepair
*p
= q
->queue
[i
];
943 diff_debug_filepair(p
, i
);
948 static void diff_resolve_rename_copy(void)
951 struct diff_filepair
*p
, *pp
;
952 struct diff_queue_struct
*q
= &diff_queued_diff
;
954 diff_debug_queue("resolve-rename-copy", q
);
956 for (i
= 0; i
< q
->nr
; i
++) {
958 p
->status
= 0; /* undecided */
959 if (DIFF_PAIR_UNMERGED(p
))
961 else if (!DIFF_FILE_VALID(p
->one
))
963 else if (!DIFF_FILE_VALID(p
->two
))
965 else if (DIFF_PAIR_TYPE_CHANGED(p
))
968 /* from this point on, we are dealing with a pair
969 * whose both sides are valid and of the same type, i.e.
970 * either in-place edit or rename/copy edit.
972 else if (DIFF_PAIR_RENAME(p
)) {
973 if (p
->source_stays
) {
977 /* See if there is some other filepair that
978 * copies from the same source as us. If so
979 * we are a copy. Otherwise we are a rename.
981 for (j
= i
+ 1; j
< q
->nr
; j
++) {
983 if (strcmp(pp
->one
->path
, p
->one
->path
))
984 continue; /* not us */
985 if (!DIFF_PAIR_RENAME(pp
))
986 continue; /* not a rename/copy */
987 /* pp is a rename/copy from the same source */
994 else if (memcmp(p
->one
->sha1
, p
->two
->sha1
, 20) ||
995 p
->one
->mode
!= p
->two
->mode
)
998 /* This is a "no-change" entry and should not
999 * happen anymore, but prepare for broken callers.
1001 error("feeding unmodified %s to diffcore",
1006 diff_debug_queue("resolve-rename-copy done", q
);
1009 void diff_flush(int diff_output_style
)
1011 struct diff_queue_struct
*q
= &diff_queued_diff
;
1013 int line_termination
= '\n';
1014 int inter_name_termination
= '\t';
1016 if (diff_output_style
== DIFF_FORMAT_MACHINE
)
1017 line_termination
= inter_name_termination
= 0;
1019 for (i
= 0; i
< q
->nr
; i
++) {
1020 struct diff_filepair
*p
= q
->queue
[i
];
1021 if ((diff_output_style
== DIFF_FORMAT_NO_OUTPUT
) ||
1025 die("internal error in diff-resolve-rename-copy");
1026 switch (diff_output_style
) {
1027 case DIFF_FORMAT_PATCH
:
1028 diff_flush_patch(p
);
1030 case DIFF_FORMAT_HUMAN
:
1031 case DIFF_FORMAT_MACHINE
:
1032 diff_flush_raw(p
, line_termination
,
1033 inter_name_termination
);
1037 for (i
= 0; i
< q
->nr
; i
++)
1038 diff_free_filepair(q
->queue
[i
]);
1041 q
->nr
= q
->alloc
= 0;
1044 static void diffcore_apply_filter(const char *filter
)
1047 struct diff_queue_struct
*q
= &diff_queued_diff
;
1048 struct diff_queue_struct outq
;
1050 outq
.nr
= outq
.alloc
= 0;
1055 if (strchr(filter
, 'A')) {
1058 for (i
= found
= 0; !found
&& i
< q
->nr
; i
++) {
1059 struct diff_filepair
*p
= q
->queue
[i
];
1060 if (((p
->status
== 'M') &&
1061 ((p
->score
&& strchr(filter
, 'B')) ||
1062 (!p
->score
&& strchr(filter
, 'M')))) ||
1063 ((p
->status
!= 'M') && strchr(filter
, p
->status
)))
1069 /* otherwise we will clear the whole queue
1070 * by copying the empty outq at the end of this
1071 * function, but first clear the current entries
1074 for (i
= 0; i
< q
->nr
; i
++)
1075 diff_free_filepair(q
->queue
[i
]);
1078 /* Only the matching ones */
1079 for (i
= 0; i
< q
->nr
; i
++) {
1080 struct diff_filepair
*p
= q
->queue
[i
];
1081 if (((p
->status
== 'M') &&
1082 ((p
->score
&& strchr(filter
, 'B')) ||
1083 (!p
->score
&& strchr(filter
, 'M')))) ||
1084 ((p
->status
!= 'M') && strchr(filter
, p
->status
)))
1087 diff_free_filepair(p
);
1094 void diffcore_std(const char **paths
,
1095 int detect_rename
, int rename_score
,
1096 const char *pickaxe
, int pickaxe_opts
,
1098 const char *orderfile
,
1101 if (paths
&& paths
[0])
1102 diffcore_pathspec(paths
);
1103 if (break_opt
!= -1)
1104 diffcore_break(break_opt
);
1106 diffcore_rename(detect_rename
, rename_score
);
1107 if (break_opt
!= -1)
1108 diffcore_merge_broken();
1110 diffcore_pickaxe(pickaxe
, pickaxe_opts
);
1112 diffcore_order(orderfile
);
1113 diff_resolve_rename_copy();
1114 diffcore_apply_filter(filter
);
1118 void diffcore_std_no_resolve(const char **paths
,
1119 const char *pickaxe
, int pickaxe_opts
,
1120 const char *orderfile
,
1123 if (paths
&& paths
[0])
1124 diffcore_pathspec(paths
);
1126 diffcore_pickaxe(pickaxe
, pickaxe_opts
);
1128 diffcore_order(orderfile
);
1129 diffcore_apply_filter(filter
);
1132 void diff_addremove(int addremove
, unsigned mode
,
1133 const unsigned char *sha1
,
1134 const char *base
, const char *path
)
1136 char concatpath
[PATH_MAX
];
1137 struct diff_filespec
*one
, *two
;
1139 /* This may look odd, but it is a preparation for
1140 * feeding "there are unchanged files which should
1141 * not produce diffs, but when you are doing copy
1142 * detection you would need them, so here they are"
1143 * entries to the diff-core. They will be prefixed
1144 * with something like '=' or '*' (I haven't decided
1145 * which but should not make any difference).
1146 * Feeding the same new and old to diff_change()
1147 * also has the same effect.
1148 * Before the final output happens, they are pruned after
1149 * merged into rename/copy pairs as appropriate.
1152 addremove
= (addremove
== '+' ? '-' :
1153 addremove
== '-' ? '+' : addremove
);
1155 if (!path
) path
= "";
1156 sprintf(concatpath
, "%s%s", base
, path
);
1157 one
= alloc_filespec(concatpath
);
1158 two
= alloc_filespec(concatpath
);
1160 if (addremove
!= '+')
1161 fill_filespec(one
, sha1
, mode
);
1162 if (addremove
!= '-')
1163 fill_filespec(two
, sha1
, mode
);
1165 diff_queue(&diff_queued_diff
, one
, two
);
1168 void diff_helper_input(unsigned old_mode
,
1170 const unsigned char *old_sha1
,
1171 const unsigned char *new_sha1
,
1172 const char *old_path
,
1175 const char *new_path
)
1177 struct diff_filespec
*one
, *two
;
1178 struct diff_filepair
*dp
;
1180 one
= alloc_filespec(old_path
);
1181 two
= alloc_filespec(new_path
);
1183 fill_filespec(one
, old_sha1
, old_mode
);
1185 fill_filespec(two
, new_sha1
, new_mode
);
1186 dp
= diff_queue(&diff_queued_diff
, one
, two
);
1187 dp
->score
= score
* MAX_SCORE
/ 100;
1188 dp
->status
= status
;
1191 void diff_change(unsigned old_mode
, unsigned new_mode
,
1192 const unsigned char *old_sha1
,
1193 const unsigned char *new_sha1
,
1194 const char *base
, const char *path
)
1196 char concatpath
[PATH_MAX
];
1197 struct diff_filespec
*one
, *two
;
1201 const unsigned char *tmp_c
;
1202 tmp
= old_mode
; old_mode
= new_mode
; new_mode
= tmp
;
1203 tmp_c
= old_sha1
; old_sha1
= new_sha1
; new_sha1
= tmp_c
;
1205 if (!path
) path
= "";
1206 sprintf(concatpath
, "%s%s", base
, path
);
1207 one
= alloc_filespec(concatpath
);
1208 two
= alloc_filespec(concatpath
);
1209 fill_filespec(one
, old_sha1
, old_mode
);
1210 fill_filespec(two
, new_sha1
, new_mode
);
1212 diff_queue(&diff_queued_diff
, one
, two
);
1215 void diff_unmerge(const char *path
)
1217 struct diff_filespec
*one
, *two
;
1218 one
= alloc_filespec(path
);
1219 two
= alloc_filespec(path
);
1220 diff_queue(&diff_queued_diff
, one
, two
);