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 void builtin_diff(const char *name_a
,
88 struct diff_tempfile
*temp
,
91 int i
, next_at
, cmd_size
;
92 const char *diff_cmd
= "diff -L'%s%s' -L'%s%s'";
93 const char *diff_arg
= "'%s' '%s'||:"; /* "||:" is to return 0 */
94 const char *input_name_sq
[2];
97 const char *name_sq
[2];
100 name_sq
[0] = sq_expand(name_a
);
101 name_sq
[1] = sq_expand(name_b
);
103 /* diff_cmd and diff_arg have 6 %s in total which makes
104 * the sum of these strings 12 bytes larger than required.
105 * we use 2 spaces around diff-opts, and we need to count
106 * terminating NUL, so we subtract 9 here.
108 cmd_size
= (strlen(diff_cmd
) + strlen(diff_opts
) +
109 strlen(diff_arg
) - 9);
110 for (i
= 0; i
< 2; i
++) {
111 input_name_sq
[i
] = sq_expand(temp
[i
].name
);
112 if (!strcmp(temp
[i
].name
, "/dev/null")) {
113 path0
[i
] = "/dev/null";
116 path0
[i
] = i
? "b/" : "a/";
117 path1
[i
] = name_sq
[i
];
119 cmd_size
+= (strlen(path0
[i
]) + strlen(path1
[i
]) +
120 strlen(input_name_sq
[i
]));
123 cmd
= xmalloc(cmd_size
);
126 next_at
+= snprintf(cmd
+next_at
, cmd_size
-next_at
,
128 path0
[0], path1
[0], path0
[1], path1
[1]);
129 next_at
+= snprintf(cmd
+next_at
, cmd_size
-next_at
,
131 next_at
+= snprintf(cmd
+next_at
, cmd_size
-next_at
,
132 diff_arg
, input_name_sq
[0], input_name_sq
[1]);
134 printf("diff --git a/%s b/%s\n", name_a
, name_b
);
136 printf("new file mode %s\n", temp
[1].mode
);
137 if (xfrm_msg
&& xfrm_msg
[0])
140 else if (!path1
[1][0]) {
141 printf("deleted file mode %s\n", temp
[0].mode
);
142 if (xfrm_msg
&& xfrm_msg
[0])
146 if (strcmp(temp
[0].mode
, temp
[1].mode
)) {
147 printf("old mode %s\n", temp
[0].mode
);
148 printf("new mode %s\n", temp
[1].mode
);
150 if (xfrm_msg
&& xfrm_msg
[0])
153 if (strncmp(temp
[0].mode
, temp
[1].mode
, 3))
154 /* we do not run diff between different kind
160 execlp("/bin/sh","sh", "-c", cmd
, NULL
);
163 struct diff_filespec
*alloc_filespec(const char *path
)
165 int namelen
= strlen(path
);
166 struct diff_filespec
*spec
= xmalloc(sizeof(*spec
) + namelen
+ 1);
167 spec
->path
= (char *)(spec
+ 1);
168 strcpy(spec
->path
, path
);
169 spec
->should_free
= spec
->should_munmap
= 0;
170 spec
->xfrm_flags
= 0;
174 memset(spec
->sha1
, 0, 20);
178 void fill_filespec(struct diff_filespec
*spec
, const unsigned char *sha1
,
182 spec
->mode
= DIFF_FILE_CANON_MODE(mode
);
183 memcpy(spec
->sha1
, sha1
, 20);
184 spec
->sha1_valid
= !!memcmp(sha1
, null_sha1
, 20);
189 * Given a name and sha1 pair, if the dircache tells us the file in
190 * the work tree has that object contents, return true, so that
191 * prepare_temp_file() does not have to inflate and extract.
193 static int work_tree_matches(const char *name
, const unsigned char *sha1
)
195 struct cache_entry
*ce
;
199 /* We do not read the cache ourselves here, because the
200 * benchmark with my previous version that always reads cache
201 * shows that it makes things worse for diff-tree comparing
202 * two linux-2.6 kernel trees in an already checked out work
203 * tree. This is because most diff-tree comparisons deal with
204 * only a small number of files, while reading the cache is
205 * expensive for a large project, and its cost outweighs the
206 * savings we get by not inflating the object to a temporary
207 * file. Practically, this code only helps when we are used
208 * by diff-cache --cached, which does read the cache before
215 pos
= cache_name_pos(name
, len
);
218 ce
= active_cache
[pos
];
219 if ((lstat(name
, &st
) < 0) ||
220 !S_ISREG(st
.st_mode
) || /* careful! */
221 ce_match_stat(ce
, &st
) ||
222 memcmp(sha1
, ce
->sha1
, 20))
224 /* we return 1 only when we can stat, it is a regular file,
225 * stat information matches, and sha1 recorded in the cache
226 * matches. I.e. we know the file in the work tree really is
227 * the same as the <name, sha1> pair.
232 static struct sha1_size_cache
{
233 unsigned char sha1
[20];
236 static int sha1_size_cache_nr
, sha1_size_cache_alloc
;
238 static struct sha1_size_cache
*locate_size_cache(unsigned char *sha1
,
243 struct sha1_size_cache
*e
;
246 last
= sha1_size_cache_nr
;
247 while (last
> first
) {
248 int cmp
, next
= (last
+ first
) >> 1;
249 e
= sha1_size_cache
[next
];
250 cmp
= memcmp(e
->sha1
, sha1
, 20);
262 /* insert to make it at "first" */
263 if (sha1_size_cache_alloc
<= sha1_size_cache_nr
) {
264 sha1_size_cache_alloc
= alloc_nr(sha1_size_cache_alloc
);
265 sha1_size_cache
= xrealloc(sha1_size_cache
,
266 sha1_size_cache_alloc
*
267 sizeof(*sha1_size_cache
));
269 sha1_size_cache_nr
++;
270 if (first
< sha1_size_cache_nr
)
271 memmove(sha1_size_cache
+ first
+ 1, sha1_size_cache
+ first
,
272 (sha1_size_cache_nr
- first
- 1) *
273 sizeof(*sha1_size_cache
));
274 e
= xmalloc(sizeof(struct sha1_size_cache
));
275 sha1_size_cache
[first
] = e
;
276 memcpy(e
->sha1
, sha1
, 20);
282 * While doing rename detection and pickaxe operation, we may need to
283 * grab the data for the blob (or file) for our own in-core comparison.
284 * diff_filespec has data and size fields for this purpose.
286 int diff_populate_filespec(struct diff_filespec
*s
, int size_only
)
289 if (!DIFF_FILE_VALID(s
))
290 die("internal error: asking to populate invalid file.");
291 if (S_ISDIR(s
->mode
))
299 if (!s
->sha1_valid
||
300 work_tree_matches(s
->path
, s
->sha1
)) {
303 if (lstat(s
->path
, &st
) < 0) {
304 if (errno
== ENOENT
) {
313 s
->size
= st
.st_size
;
318 if (S_ISLNK(st
.st_mode
)) {
320 s
->data
= xmalloc(s
->size
);
322 ret
= readlink(s
->path
, s
->data
, s
->size
);
329 fd
= open(s
->path
, O_RDONLY
);
332 s
->data
= mmap(NULL
, s
->size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
333 s
->should_munmap
= 1;
338 struct sha1_size_cache
*e
;
341 e
= locate_size_cache(s
->sha1
, 1, 0);
346 if (!sha1_file_size(s
->sha1
, &s
->size
))
347 locate_size_cache(s
->sha1
, 0, s
->size
);
350 s
->data
= read_sha1_file(s
->sha1
, type
, &s
->size
);
357 void diff_free_filespec_data(struct diff_filespec
*s
)
361 else if (s
->should_munmap
)
362 munmap(s
->data
, s
->size
);
363 s
->should_free
= s
->should_munmap
= 0;
367 static void prep_temp_blob(struct diff_tempfile
*temp
,
375 strcpy(temp
->tmp_path
, ".diff_XXXXXX");
376 fd
= mkstemp(temp
->tmp_path
);
378 die("unable to create temp-file");
379 if (write(fd
, blob
, size
) != size
)
380 die("unable to write temp-file");
382 temp
->name
= temp
->tmp_path
;
383 strcpy(temp
->hex
, sha1_to_hex(sha1
));
385 sprintf(temp
->mode
, "%06o", mode
);
388 static void prepare_temp_file(const char *name
,
389 struct diff_tempfile
*temp
,
390 struct diff_filespec
*one
)
392 if (!DIFF_FILE_VALID(one
)) {
394 /* A '-' entry produces this for file-2, and
395 * a '+' entry produces this for file-1.
397 temp
->name
= "/dev/null";
398 strcpy(temp
->hex
, ".");
399 strcpy(temp
->mode
, ".");
403 if (!one
->sha1_valid
||
404 work_tree_matches(name
, one
->sha1
)) {
406 if (lstat(name
, &st
) < 0) {
408 goto not_a_valid_file
;
409 die("stat(%s): %s", name
, strerror(errno
));
411 if (S_ISLNK(st
.st_mode
)) {
413 char *buf
, buf_
[1024];
414 buf
= ((sizeof(buf_
) < st
.st_size
) ?
415 xmalloc(st
.st_size
) : buf_
);
416 ret
= readlink(name
, buf
, st
.st_size
);
418 die("readlink(%s)", name
);
419 prep_temp_blob(temp
, buf
, st
.st_size
,
421 one
->sha1
: null_sha1
),
423 one
->mode
: S_IFLNK
));
426 /* we can borrow from the file in the work tree */
428 if (!one
->sha1_valid
)
429 strcpy(temp
->hex
, sha1_to_hex(null_sha1
));
431 strcpy(temp
->hex
, sha1_to_hex(one
->sha1
));
432 /* Even though we may sometimes borrow the
433 * contents from the work tree, we always want
434 * one->mode. mode is trustworthy even when
435 * !(one->sha1_valid), as long as
436 * DIFF_FILE_VALID(one).
438 sprintf(temp
->mode
, "%06o", one
->mode
);
443 if (diff_populate_filespec(one
, 0))
444 die("cannot read data blob for %s", one
->path
);
445 prep_temp_blob(temp
, one
->data
, one
->size
,
446 one
->sha1
, one
->mode
);
450 static void remove_tempfile(void)
454 for (i
= 0; i
< 2; i
++)
455 if (diff_temp
[i
].name
== diff_temp
[i
].tmp_path
) {
456 unlink(diff_temp
[i
].name
);
457 diff_temp
[i
].name
= NULL
;
461 static void remove_tempfile_on_signal(int signo
)
466 /* An external diff command takes:
468 * diff-cmd name infile1 infile1-sha1 infile1-mode \
469 * infile2 infile2-sha1 infile2-mode [ rename-to ]
472 static void run_external_diff(const char *pgm
,
475 struct diff_filespec
*one
,
476 struct diff_filespec
*two
,
477 const char *xfrm_msg
)
479 struct diff_tempfile
*temp
= diff_temp
;
482 static int atexit_asked
= 0;
485 prepare_temp_file(name
, &temp
[0], one
);
486 prepare_temp_file(other
? : name
, &temp
[1], two
);
487 if (! atexit_asked
&&
488 (temp
[0].name
== temp
[0].tmp_path
||
489 temp
[1].name
== temp
[1].tmp_path
)) {
491 atexit(remove_tempfile
);
493 signal(SIGINT
, remove_tempfile_on_signal
);
499 die("unable to fork");
503 const char *exec_arg
[10];
504 const char **arg
= &exec_arg
[0];
507 *arg
++ = temp
[0].name
;
508 *arg
++ = temp
[0].hex
;
509 *arg
++ = temp
[0].mode
;
510 *arg
++ = temp
[1].name
;
511 *arg
++ = temp
[1].hex
;
512 *arg
++ = temp
[1].mode
;
518 execvp(pgm
, (char *const*) exec_arg
);
521 execlp(pgm
, pgm
, name
, NULL
);
524 * otherwise we use the built-in one.
527 builtin_diff(name
, other
? : name
, temp
, xfrm_msg
);
529 printf("* Unmerged path %s\n", name
);
532 if (waitpid(pid
, &status
, 0) < 0 ||
533 !WIFEXITED(status
) || WEXITSTATUS(status
)) {
534 /* Earlier we did not check the exit status because
535 * diff exits non-zero if files are different, and
536 * we are not interested in knowing that. It was a
537 * mistake which made it harder to quit a diff-*
538 * session that uses the git-apply-patch-script as
539 * the GIT_EXTERNAL_DIFF. A custom GIT_EXTERNAL_DIFF
540 * should also exit non-zero only when it wants to
541 * abort the entire diff-* session.
544 fprintf(stderr
, "external diff died, stopping at %s.\n", name
);
550 static void run_diff(const char *name
,
552 struct diff_filespec
*one
,
553 struct diff_filespec
*two
,
554 const char *xfrm_msg
)
556 const char *pgm
= external_diff();
559 DIFF_FILE_VALID(one
) && DIFF_FILE_VALID(two
) &&
560 (S_IFMT
& one
->mode
) != (S_IFMT
& two
->mode
)) {
561 /* a filepair that changes between file and symlink
562 * needs to be split into deletion and creation.
564 struct diff_filespec
*null
= alloc_filespec(two
->path
);
565 run_external_diff(NULL
, name
, other
, one
, null
, xfrm_msg
);
567 null
= alloc_filespec(one
->path
);
568 run_external_diff(NULL
, name
, other
, null
, two
, xfrm_msg
);
572 run_external_diff(pgm
, name
, other
, one
, two
, xfrm_msg
);
575 void diff_setup(int flags
)
577 if (flags
& DIFF_SETUP_REVERSE
)
579 if (flags
& DIFF_SETUP_USE_CACHE
) {
581 /* read-cache does not die even when it fails
582 * so it is safe for us to do this here. Also
583 * it does not smudge active_cache or active_nr
584 * when it fails, so we do not have to worry about
585 * cleaning it up oufselves either.
589 if (flags
& DIFF_SETUP_USE_SIZE_CACHE
)
594 static int parse_num(const char **cp_p
)
596 int num
, scale
, ch
, cnt
;
597 const char *cp
= *cp_p
;
601 while ('0' <= (ch
= *cp
) && ch
<= '9') {
603 /* We simply ignore more than 5 digits precision. */
605 num
= num
* 10 + ch
- '0';
611 /* user says num divided by scale and we say internally that
612 * is MAX_SCORE * num / scale.
614 return (MAX_SCORE
* num
/ scale
);
617 int diff_scoreopt_parse(const char *opt
)
624 if (cmd
!= 'M' && cmd
!= 'C' && cmd
!= 'B')
625 return -1; /* that is not a -M, -C nor -B option */
627 opt1
= parse_num(&opt
);
633 else if (*opt
!= '/')
634 return -1; /* we expect -B80/99 or -B80 */
637 opt2
= parse_num(&opt
);
642 return opt1
| (opt2
<< 16);
645 struct diff_queue_struct diff_queued_diff
;
647 void diff_q(struct diff_queue_struct
*queue
, struct diff_filepair
*dp
)
649 if (queue
->alloc
<= queue
->nr
) {
650 queue
->alloc
= alloc_nr(queue
->alloc
);
651 queue
->queue
= xrealloc(queue
->queue
,
652 sizeof(dp
) * queue
->alloc
);
654 queue
->queue
[queue
->nr
++] = dp
;
657 struct diff_filepair
*diff_queue(struct diff_queue_struct
*queue
,
658 struct diff_filespec
*one
,
659 struct diff_filespec
*two
)
661 struct diff_filepair
*dp
= xmalloc(sizeof(*dp
));
666 dp
->source_stays
= 0;
672 void diff_free_filepair(struct diff_filepair
*p
)
674 diff_free_filespec_data(p
->one
);
675 diff_free_filespec_data(p
->two
);
679 static void diff_flush_raw(struct diff_filepair
*p
,
680 int line_termination
,
681 int inter_name_termination
)
686 if (line_termination
) {
687 const char *err
= "path %s cannot be expressed without -z";
688 if (strchr(p
->one
->path
, line_termination
) ||
689 strchr(p
->one
->path
, inter_name_termination
))
690 die(err
, p
->one
->path
);
691 if (strchr(p
->two
->path
, line_termination
) ||
692 strchr(p
->two
->path
, inter_name_termination
))
693 die(err
, p
->two
->path
);
699 sprintf(status
, "%c%03d", p
->status
,
700 (int)(0.5 + p
->score
* 100.0/MAX_SCORE
));
705 sprintf(status
, "%c%03d", p
->status
,
706 (int)(0.5 + p
->score
* 100.0/MAX_SCORE
));
708 status
[0] = p
->status
;
714 status
[0] = p
->status
;
718 printf(":%06o %06o %s ",
719 p
->one
->mode
, p
->two
->mode
, sha1_to_hex(p
->one
->sha1
));
721 sha1_to_hex(p
->two
->sha1
),
723 inter_name_termination
,
726 printf("%c%s", inter_name_termination
, p
->two
->path
);
727 putchar(line_termination
);
730 int diff_unmodified_pair(struct diff_filepair
*p
)
732 /* This function is written stricter than necessary to support
733 * the currently implemented transformers, but the idea is to
734 * let transformers to produce diff_filepairs any way they want,
735 * and filter and clean them up here before producing the output.
737 struct diff_filespec
*one
, *two
;
739 if (DIFF_PAIR_UNMERGED(p
))
740 return 0; /* unmerged is interesting */
745 /* deletion, addition, mode or type change
746 * and rename are all interesting.
748 if (DIFF_FILE_VALID(one
) != DIFF_FILE_VALID(two
) ||
749 DIFF_PAIR_MODE_CHANGED(p
) ||
750 strcmp(one
->path
, two
->path
))
753 /* both are valid and point at the same path. that is, we are
754 * dealing with a change.
756 if (one
->sha1_valid
&& two
->sha1_valid
&&
757 !memcmp(one
->sha1
, two
->sha1
, sizeof(one
->sha1
)))
758 return 1; /* no change */
759 if (!one
->sha1_valid
&& !two
->sha1_valid
)
760 return 1; /* both look at the same file on the filesystem. */
764 static void diff_flush_patch(struct diff_filepair
*p
)
766 const char *name
, *other
;
767 char msg_
[PATH_MAX
*2+200], *msg
;
769 if (diff_unmodified_pair(p
))
773 other
= (strcmp(name
, p
->two
->path
) ? p
->two
->path
: NULL
);
774 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
775 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
776 return; /* no tree diffs in patch format */
781 "similarity index %d%%\n"
784 (int)(0.5 + p
->score
* 100.0/MAX_SCORE
),
785 p
->one
->path
, p
->two
->path
);
790 "similarity index %d%%\n"
793 (int)(0.5 + p
->score
* 100.0/MAX_SCORE
),
794 p
->one
->path
, p
->two
->path
);
798 if (DIFF_PAIR_BROKEN(p
)) {
800 "dissimilarity index %d%%",
801 (int)(0.5 + p
->score
* 100.0/MAX_SCORE
));
811 if (DIFF_PAIR_UNMERGED(p
))
812 run_diff(name
, NULL
, NULL
, NULL
, NULL
);
814 run_diff(name
, other
, p
->one
, p
->two
, msg
);
817 int diff_queue_is_empty(void)
819 struct diff_queue_struct
*q
= &diff_queued_diff
;
821 for (i
= 0; i
< q
->nr
; i
++)
822 if (!diff_unmodified_pair(q
->queue
[i
]))
828 void diff_debug_filespec(struct diff_filespec
*s
, int x
, const char *one
)
830 fprintf(stderr
, "queue[%d] %s (%s) %s %06o %s\n",
833 DIFF_FILE_VALID(s
) ? "valid" : "invalid",
835 s
->sha1_valid
? sha1_to_hex(s
->sha1
) : "");
836 fprintf(stderr
, "queue[%d] %s size %lu flags %d\n",
838 s
->size
, s
->xfrm_flags
);
841 void diff_debug_filepair(const struct diff_filepair
*p
, int i
)
843 diff_debug_filespec(p
->one
, i
, "one");
844 diff_debug_filespec(p
->two
, i
, "two");
845 fprintf(stderr
, "score %d, status %c stays %d broken %d\n",
846 p
->score
, p
->status
? : '?',
847 p
->source_stays
, p
->broken_pair
);
850 void diff_debug_queue(const char *msg
, struct diff_queue_struct
*q
)
854 fprintf(stderr
, "%s\n", msg
);
855 fprintf(stderr
, "q->nr = %d\n", q
->nr
);
856 for (i
= 0; i
< q
->nr
; i
++) {
857 struct diff_filepair
*p
= q
->queue
[i
];
858 diff_debug_filepair(p
, i
);
863 static void diff_resolve_rename_copy(void)
866 struct diff_filepair
*p
, *pp
;
867 struct diff_queue_struct
*q
= &diff_queued_diff
;
869 diff_debug_queue("resolve-rename-copy", q
);
871 for (i
= 0; i
< q
->nr
; i
++) {
873 p
->status
= 0; /* undecided */
874 if (DIFF_PAIR_UNMERGED(p
))
876 else if (!DIFF_FILE_VALID(p
->one
))
878 else if (!DIFF_FILE_VALID(p
->two
))
880 else if (DIFF_PAIR_TYPE_CHANGED(p
))
883 /* from this point on, we are dealing with a pair
884 * whose both sides are valid and of the same type, i.e.
885 * either in-place edit or rename/copy edit.
887 else if (DIFF_PAIR_RENAME(p
)) {
888 if (p
->source_stays
) {
892 /* See if there is some other filepair that
893 * copies from the same source as us. If so
894 * we are a copy. Otherwise we are a rename.
896 for (j
= i
+ 1; j
< q
->nr
; j
++) {
898 if (strcmp(pp
->one
->path
, p
->one
->path
))
899 continue; /* not us */
900 if (!DIFF_PAIR_RENAME(pp
))
901 continue; /* not a rename/copy */
902 /* pp is a rename/copy from the same source */
909 else if (memcmp(p
->one
->sha1
, p
->two
->sha1
, 20) ||
910 p
->one
->mode
!= p
->two
->mode
)
913 /* This is a "no-change" entry and should not
914 * happen anymore, but prepare for broken callers.
916 error("feeding unmodified %s to diffcore",
921 diff_debug_queue("resolve-rename-copy done", q
);
924 void diff_flush(int diff_output_style
)
926 struct diff_queue_struct
*q
= &diff_queued_diff
;
928 int line_termination
= '\n';
929 int inter_name_termination
= '\t';
931 if (diff_output_style
== DIFF_FORMAT_MACHINE
)
932 line_termination
= inter_name_termination
= 0;
934 for (i
= 0; i
< q
->nr
; i
++) {
935 struct diff_filepair
*p
= q
->queue
[i
];
936 if ((diff_output_style
== DIFF_FORMAT_NO_OUTPUT
) ||
940 die("internal error in diff-resolve-rename-copy");
941 switch (diff_output_style
) {
942 case DIFF_FORMAT_PATCH
:
945 case DIFF_FORMAT_HUMAN
:
946 case DIFF_FORMAT_MACHINE
:
947 diff_flush_raw(p
, line_termination
,
948 inter_name_termination
);
952 for (i
= 0; i
< q
->nr
; i
++)
953 diff_free_filepair(q
->queue
[i
]);
956 q
->nr
= q
->alloc
= 0;
959 static void diffcore_apply_filter(const char *filter
)
962 struct diff_queue_struct
*q
= &diff_queued_diff
;
963 struct diff_queue_struct outq
;
965 outq
.nr
= outq
.alloc
= 0;
970 if (strchr(filter
, 'A')) {
973 for (i
= found
= 0; !found
&& i
< q
->nr
; i
++) {
974 struct diff_filepair
*p
= q
->queue
[i
];
975 if ((p
->broken_pair
&& strchr(filter
, 'B')) ||
976 (!p
->broken_pair
&& strchr(filter
, p
->status
)))
982 /* otherwise we will clear the whole queue
983 * by copying the empty outq at the end of this
984 * function, but first clear the current entries
987 for (i
= 0; i
< q
->nr
; i
++)
988 diff_free_filepair(q
->queue
[i
]);
991 /* Only the matching ones */
992 for (i
= 0; i
< q
->nr
; i
++) {
993 struct diff_filepair
*p
= q
->queue
[i
];
994 if ((p
->broken_pair
&& strchr(filter
, 'B')) ||
995 (!p
->broken_pair
&& strchr(filter
, p
->status
)))
998 diff_free_filepair(p
);
1005 void diffcore_std(const char **paths
,
1006 int detect_rename
, int rename_score
,
1007 const char *pickaxe
, int pickaxe_opts
,
1009 const char *orderfile
,
1012 if (paths
&& paths
[0])
1013 diffcore_pathspec(paths
);
1014 if (break_opt
!= -1)
1015 diffcore_break(break_opt
);
1017 diffcore_rename(detect_rename
, rename_score
);
1018 if (break_opt
!= -1)
1019 diffcore_merge_broken();
1021 diffcore_pickaxe(pickaxe
, pickaxe_opts
);
1023 diffcore_order(orderfile
);
1024 diff_resolve_rename_copy();
1025 diffcore_apply_filter(filter
);
1029 void diffcore_std_no_resolve(const char **paths
,
1030 const char *pickaxe
, int pickaxe_opts
,
1031 const char *orderfile
,
1034 if (paths
&& paths
[0])
1035 diffcore_pathspec(paths
);
1037 diffcore_pickaxe(pickaxe
, pickaxe_opts
);
1039 diffcore_order(orderfile
);
1040 diffcore_apply_filter(filter
);
1043 void diff_addremove(int addremove
, unsigned mode
,
1044 const unsigned char *sha1
,
1045 const char *base
, const char *path
)
1047 char concatpath
[PATH_MAX
];
1048 struct diff_filespec
*one
, *two
;
1050 /* This may look odd, but it is a preparation for
1051 * feeding "there are unchanged files which should
1052 * not produce diffs, but when you are doing copy
1053 * detection you would need them, so here they are"
1054 * entries to the diff-core. They will be prefixed
1055 * with something like '=' or '*' (I haven't decided
1056 * which but should not make any difference).
1057 * Feeding the same new and old to diff_change()
1058 * also has the same effect.
1059 * Before the final output happens, they are pruned after
1060 * merged into rename/copy pairs as appropriate.
1063 addremove
= (addremove
== '+' ? '-' :
1064 addremove
== '-' ? '+' : addremove
);
1066 if (!path
) path
= "";
1067 sprintf(concatpath
, "%s%s", base
, path
);
1068 one
= alloc_filespec(concatpath
);
1069 two
= alloc_filespec(concatpath
);
1071 if (addremove
!= '+')
1072 fill_filespec(one
, sha1
, mode
);
1073 if (addremove
!= '-')
1074 fill_filespec(two
, sha1
, mode
);
1076 diff_queue(&diff_queued_diff
, one
, two
);
1079 void diff_helper_input(unsigned old_mode
,
1081 const unsigned char *old_sha1
,
1082 const unsigned char *new_sha1
,
1083 const char *old_path
,
1086 const char *new_path
)
1088 struct diff_filespec
*one
, *two
;
1089 struct diff_filepair
*dp
;
1091 one
= alloc_filespec(old_path
);
1092 two
= alloc_filespec(new_path
);
1094 fill_filespec(one
, old_sha1
, old_mode
);
1096 fill_filespec(two
, new_sha1
, new_mode
);
1097 dp
= diff_queue(&diff_queued_diff
, one
, two
);
1098 dp
->score
= score
* MAX_SCORE
/ 100;
1099 dp
->status
= status
;
1102 void diff_change(unsigned old_mode
, unsigned new_mode
,
1103 const unsigned char *old_sha1
,
1104 const unsigned char *new_sha1
,
1105 const char *base
, const char *path
)
1107 char concatpath
[PATH_MAX
];
1108 struct diff_filespec
*one
, *two
;
1112 const unsigned char *tmp_c
;
1113 tmp
= old_mode
; old_mode
= new_mode
; new_mode
= tmp
;
1114 tmp_c
= old_sha1
; old_sha1
= new_sha1
; new_sha1
= tmp_c
;
1116 if (!path
) path
= "";
1117 sprintf(concatpath
, "%s%s", base
, path
);
1118 one
= alloc_filespec(concatpath
);
1119 two
= alloc_filespec(concatpath
);
1120 fill_filespec(one
, old_sha1
, old_mode
);
1121 fill_filespec(two
, new_sha1
, new_mode
);
1123 diff_queue(&diff_queued_diff
, one
, two
);
1126 void diff_unmerge(const char *path
)
1128 struct diff_filespec
*one
, *two
;
1129 one
= alloc_filespec(path
);
1130 two
= alloc_filespec(path
);
1131 diff_queue(&diff_queued_diff
, one
, two
);