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 else if (!path1
[1][0])
138 printf("deleted file mode %s\n", temp
[0].mode
);
140 if (strcmp(temp
[0].mode
, temp
[1].mode
)) {
141 printf("old mode %s\n", temp
[0].mode
);
142 printf("new mode %s\n", temp
[1].mode
);
144 if (xfrm_msg
&& xfrm_msg
[0])
147 if (strncmp(temp
[0].mode
, temp
[1].mode
, 3))
148 /* we do not run diff between different kind
154 execlp("/bin/sh","sh", "-c", cmd
, NULL
);
157 struct diff_filespec
*alloc_filespec(const char *path
)
159 int namelen
= strlen(path
);
160 struct diff_filespec
*spec
= xmalloc(sizeof(*spec
) + namelen
+ 1);
161 spec
->path
= (char *)(spec
+ 1);
162 strcpy(spec
->path
, path
);
163 spec
->should_free
= spec
->should_munmap
= 0;
164 spec
->xfrm_flags
= 0;
168 memset(spec
->sha1
, 0, 20);
172 void fill_filespec(struct diff_filespec
*spec
, const unsigned char *sha1
,
176 spec
->mode
= DIFF_FILE_CANON_MODE(mode
);
177 memcpy(spec
->sha1
, sha1
, 20);
178 spec
->sha1_valid
= !!memcmp(sha1
, null_sha1
, 20);
183 * Given a name and sha1 pair, if the dircache tells us the file in
184 * the work tree has that object contents, return true, so that
185 * prepare_temp_file() does not have to inflate and extract.
187 static int work_tree_matches(const char *name
, const unsigned char *sha1
)
189 struct cache_entry
*ce
;
193 /* We do not read the cache ourselves here, because the
194 * benchmark with my previous version that always reads cache
195 * shows that it makes things worse for diff-tree comparing
196 * two linux-2.6 kernel trees in an already checked out work
197 * tree. This is because most diff-tree comparisons deal with
198 * only a small number of files, while reading the cache is
199 * expensive for a large project, and its cost outweighs the
200 * savings we get by not inflating the object to a temporary
201 * file. Practically, this code only helps when we are used
202 * by diff-cache --cached, which does read the cache before
209 pos
= cache_name_pos(name
, len
);
212 ce
= active_cache
[pos
];
213 if ((lstat(name
, &st
) < 0) ||
214 !S_ISREG(st
.st_mode
) || /* careful! */
215 ce_match_stat(ce
, &st
) ||
216 memcmp(sha1
, ce
->sha1
, 20))
218 /* we return 1 only when we can stat, it is a regular file,
219 * stat information matches, and sha1 recorded in the cache
220 * matches. I.e. we know the file in the work tree really is
221 * the same as the <name, sha1> pair.
226 static struct sha1_size_cache
{
227 unsigned char sha1
[20];
230 static int sha1_size_cache_nr
, sha1_size_cache_alloc
;
232 static struct sha1_size_cache
*locate_size_cache(unsigned char *sha1
,
236 struct sha1_size_cache
*e
;
239 last
= sha1_size_cache_nr
;
240 while (last
> first
) {
241 int next
= (last
+ first
) >> 1;
242 e
= sha1_size_cache
[next
];
243 int cmp
= memcmp(e
->sha1
, sha1
, 20);
253 if (size
== UINT_MAX
)
255 /* insert to make it at "first" */
256 if (sha1_size_cache_alloc
<= sha1_size_cache_nr
) {
257 sha1_size_cache_alloc
= alloc_nr(sha1_size_cache_alloc
);
258 sha1_size_cache
= xrealloc(sha1_size_cache
,
259 sha1_size_cache_alloc
*
260 sizeof(*sha1_size_cache
));
262 sha1_size_cache_nr
++;
263 if (first
< sha1_size_cache_nr
)
264 memmove(sha1_size_cache
+ first
+ 1, sha1_size_cache
+ first
,
265 (sha1_size_cache_nr
- first
- 1) *
266 sizeof(*sha1_size_cache
));
267 e
= xmalloc(sizeof(struct sha1_size_cache
));
268 sha1_size_cache
[first
] = e
;
269 memcpy(e
->sha1
, sha1
, 20);
275 * While doing rename detection and pickaxe operation, we may need to
276 * grab the data for the blob (or file) for our own in-core comparison.
277 * diff_filespec has data and size fields for this purpose.
279 int diff_populate_filespec(struct diff_filespec
*s
, int size_only
)
282 if (!DIFF_FILE_VALID(s
))
283 die("internal error: asking to populate invalid file.");
284 if (S_ISDIR(s
->mode
))
292 if (!s
->sha1_valid
||
293 work_tree_matches(s
->path
, s
->sha1
)) {
296 if (lstat(s
->path
, &st
) < 0) {
297 if (errno
== ENOENT
) {
306 s
->size
= st
.st_size
;
311 if (S_ISLNK(st
.st_mode
)) {
313 s
->data
= xmalloc(s
->size
);
315 ret
= readlink(s
->path
, s
->data
, s
->size
);
322 fd
= open(s
->path
, O_RDONLY
);
325 s
->data
= mmap(NULL
, s
->size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
326 s
->should_munmap
= 1;
330 /* We cannot do size only for SHA1 blobs */
332 struct sha1_size_cache
*e
;
335 e
= locate_size_cache(s
->sha1
, UINT_MAX
);
341 s
->data
= read_sha1_file(s
->sha1
, type
, &s
->size
);
343 if (s
->data
&& size_only
)
344 locate_size_cache(s
->sha1
, s
->size
);
349 void diff_free_filespec_data(struct diff_filespec
*s
)
353 else if (s
->should_munmap
)
354 munmap(s
->data
, s
->size
);
355 s
->should_free
= s
->should_munmap
= 0;
359 static void prep_temp_blob(struct diff_tempfile
*temp
,
367 strcpy(temp
->tmp_path
, ".diff_XXXXXX");
368 fd
= mkstemp(temp
->tmp_path
);
370 die("unable to create temp-file");
371 if (write(fd
, blob
, size
) != size
)
372 die("unable to write temp-file");
374 temp
->name
= temp
->tmp_path
;
375 strcpy(temp
->hex
, sha1_to_hex(sha1
));
377 sprintf(temp
->mode
, "%06o", mode
);
380 static void prepare_temp_file(const char *name
,
381 struct diff_tempfile
*temp
,
382 struct diff_filespec
*one
)
384 if (!DIFF_FILE_VALID(one
)) {
386 /* A '-' entry produces this for file-2, and
387 * a '+' entry produces this for file-1.
389 temp
->name
= "/dev/null";
390 strcpy(temp
->hex
, ".");
391 strcpy(temp
->mode
, ".");
395 if (!one
->sha1_valid
||
396 work_tree_matches(name
, one
->sha1
)) {
398 if (lstat(name
, &st
) < 0) {
400 goto not_a_valid_file
;
401 die("stat(%s): %s", name
, strerror(errno
));
403 if (S_ISLNK(st
.st_mode
)) {
405 char *buf
, buf_
[1024];
406 buf
= ((sizeof(buf_
) < st
.st_size
) ?
407 xmalloc(st
.st_size
) : buf_
);
408 ret
= readlink(name
, buf
, st
.st_size
);
410 die("readlink(%s)", name
);
411 prep_temp_blob(temp
, buf
, st
.st_size
,
413 one
->sha1
: null_sha1
),
415 one
->mode
: S_IFLNK
));
418 /* we can borrow from the file in the work tree */
420 if (!one
->sha1_valid
)
421 strcpy(temp
->hex
, sha1_to_hex(null_sha1
));
423 strcpy(temp
->hex
, sha1_to_hex(one
->sha1
));
424 /* Even though we may sometimes borrow the
425 * contents from the work tree, we always want
426 * one->mode. mode is trustworthy even when
427 * !(one->sha1_valid), as long as
428 * DIFF_FILE_VALID(one).
430 sprintf(temp
->mode
, "%06o", one
->mode
);
435 if (diff_populate_filespec(one
, 0))
436 die("cannot read data blob for %s", one
->path
);
437 prep_temp_blob(temp
, one
->data
, one
->size
,
438 one
->sha1
, one
->mode
);
442 static void remove_tempfile(void)
446 for (i
= 0; i
< 2; i
++)
447 if (diff_temp
[i
].name
== diff_temp
[i
].tmp_path
) {
448 unlink(diff_temp
[i
].name
);
449 diff_temp
[i
].name
= NULL
;
453 static void remove_tempfile_on_signal(int signo
)
458 /* An external diff command takes:
460 * diff-cmd name infile1 infile1-sha1 infile1-mode \
461 * infile2 infile2-sha1 infile2-mode [ rename-to ]
464 static void run_external_diff(const char *pgm
,
467 struct diff_filespec
*one
,
468 struct diff_filespec
*two
,
469 const char *xfrm_msg
)
471 struct diff_tempfile
*temp
= diff_temp
;
474 static int atexit_asked
= 0;
477 prepare_temp_file(name
, &temp
[0], one
);
478 prepare_temp_file(other
? : name
, &temp
[1], two
);
479 if (! atexit_asked
&&
480 (temp
[0].name
== temp
[0].tmp_path
||
481 temp
[1].name
== temp
[1].tmp_path
)) {
483 atexit(remove_tempfile
);
485 signal(SIGINT
, remove_tempfile_on_signal
);
491 die("unable to fork");
495 const char *exec_arg
[10];
496 const char **arg
= &exec_arg
[0];
499 *arg
++ = temp
[0].name
;
500 *arg
++ = temp
[0].hex
;
501 *arg
++ = temp
[0].mode
;
502 *arg
++ = temp
[1].name
;
503 *arg
++ = temp
[1].hex
;
504 *arg
++ = temp
[1].mode
;
510 execvp(pgm
, (char *const*) exec_arg
);
513 execlp(pgm
, pgm
, name
, NULL
);
516 * otherwise we use the built-in one.
519 builtin_diff(name
, other
? : name
, temp
, xfrm_msg
);
521 printf("* Unmerged path %s\n", name
);
524 if (waitpid(pid
, &status
, 0) < 0 ||
525 !WIFEXITED(status
) || WEXITSTATUS(status
)) {
526 /* Earlier we did not check the exit status because
527 * diff exits non-zero if files are different, and
528 * we are not interested in knowing that. It was a
529 * mistake which made it harder to quit a diff-*
530 * session that uses the git-apply-patch-script as
531 * the GIT_EXTERNAL_DIFF. A custom GIT_EXTERNAL_DIFF
532 * should also exit non-zero only when it wants to
533 * abort the entire diff-* session.
536 fprintf(stderr
, "external diff died, stopping at %s.\n", name
);
542 static void run_diff(const char *name
,
544 struct diff_filespec
*one
,
545 struct diff_filespec
*two
,
546 const char *xfrm_msg
)
548 const char *pgm
= external_diff();
550 DIFF_FILE_VALID(one
) && DIFF_FILE_VALID(two
) &&
551 (S_IFMT
& one
->mode
) != (S_IFMT
& two
->mode
)) {
552 /* a filepair that changes between file and symlink
553 * needs to be split into deletion and creation.
555 struct diff_filespec
*null
= alloc_filespec(two
->path
);
556 run_external_diff(NULL
, name
, other
, one
, null
, xfrm_msg
);
558 null
= alloc_filespec(one
->path
);
559 run_external_diff(NULL
, name
, other
, null
, two
, xfrm_msg
);
563 run_external_diff(pgm
, name
, other
, one
, two
, xfrm_msg
);
566 void diff_setup(int flags
)
568 if (flags
& DIFF_SETUP_REVERSE
)
570 if (flags
& DIFF_SETUP_USE_CACHE
) {
572 /* read-cache does not die even when it fails
573 * so it is safe for us to do this here. Also
574 * it does not smudge active_cache or active_nr
575 * when it fails, so we do not have to worry about
576 * cleaning it up oufselves either.
580 if (flags
& DIFF_SETUP_USE_SIZE_CACHE
)
585 struct diff_queue_struct diff_queued_diff
;
587 void diff_q(struct diff_queue_struct
*queue
, struct diff_filepair
*dp
)
589 if (queue
->alloc
<= queue
->nr
) {
590 queue
->alloc
= alloc_nr(queue
->alloc
);
591 queue
->queue
= xrealloc(queue
->queue
,
592 sizeof(dp
) * queue
->alloc
);
594 queue
->queue
[queue
->nr
++] = dp
;
597 struct diff_filepair
*diff_queue(struct diff_queue_struct
*queue
,
598 struct diff_filespec
*one
,
599 struct diff_filespec
*two
)
601 struct diff_filepair
*dp
= xmalloc(sizeof(*dp
));
605 dp
->source_stays
= 0;
611 void diff_free_filepair(struct diff_filepair
*p
)
613 diff_free_filespec_data(p
->one
);
614 diff_free_filespec_data(p
->two
);
618 static void diff_flush_raw(struct diff_filepair
*p
,
619 int line_termination
,
620 int inter_name_termination
)
625 if (line_termination
) {
626 const char *err
= "path %s cannot be expressed without -z";
627 if (strchr(p
->one
->path
, line_termination
) ||
628 strchr(p
->one
->path
, inter_name_termination
))
629 die(err
, p
->one
->path
);
630 if (strchr(p
->two
->path
, line_termination
) ||
631 strchr(p
->two
->path
, inter_name_termination
))
632 die(err
, p
->two
->path
);
638 sprintf(status
, "%c%03d", p
->status
,
639 (int)(0.5 + p
->score
* 100.0/MAX_SCORE
));
644 sprintf(status
, "%c%03d", p
->status
,
645 (int)(0.5 + p
->score
* 100.0/MAX_SCORE
));
647 status
[0] = p
->status
;
653 status
[0] = p
->status
;
657 printf(":%06o %06o %s ",
658 p
->one
->mode
, p
->two
->mode
, sha1_to_hex(p
->one
->sha1
));
660 sha1_to_hex(p
->two
->sha1
),
662 inter_name_termination
,
665 printf("%c%s", inter_name_termination
, p
->two
->path
);
666 putchar(line_termination
);
669 int diff_unmodified_pair(struct diff_filepair
*p
)
671 /* This function is written stricter than necessary to support
672 * the currently implemented transformers, but the idea is to
673 * let transformers to produce diff_filepairs any way they want,
674 * and filter and clean them up here before producing the output.
676 struct diff_filespec
*one
, *two
;
678 if (DIFF_PAIR_UNMERGED(p
))
679 return 0; /* unmerged is interesting */
684 /* deletion, addition, mode or type change
685 * and rename are all interesting.
687 if (DIFF_FILE_VALID(one
) != DIFF_FILE_VALID(two
) ||
688 DIFF_PAIR_MODE_CHANGED(p
) ||
689 strcmp(one
->path
, two
->path
))
692 /* both are valid and point at the same path. that is, we are
693 * dealing with a change.
695 if (one
->sha1_valid
&& two
->sha1_valid
&&
696 !memcmp(one
->sha1
, two
->sha1
, sizeof(one
->sha1
)))
697 return 1; /* no change */
698 if (!one
->sha1_valid
&& !two
->sha1_valid
)
699 return 1; /* both look at the same file on the filesystem. */
703 static void diff_flush_patch(struct diff_filepair
*p
)
705 const char *name
, *other
;
706 char msg_
[PATH_MAX
*2+200], *msg
;
708 if (diff_unmodified_pair(p
))
712 other
= (strcmp(name
, p
->two
->path
) ? p
->two
->path
: NULL
);
713 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
714 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
715 return; /* no tree diffs in patch format */
720 "similarity index %d%%\n"
723 (int)(0.5 + p
->score
* 100.0/MAX_SCORE
),
724 p
->one
->path
, p
->two
->path
);
729 "similarity index %d%%\n"
732 (int)(0.5 + p
->score
* 100.0/MAX_SCORE
),
733 p
->one
->path
, p
->two
->path
);
740 if (DIFF_PAIR_UNMERGED(p
))
741 run_diff(name
, NULL
, NULL
, NULL
, NULL
);
743 run_diff(name
, other
, p
->one
, p
->two
, msg
);
746 int diff_queue_is_empty(void)
748 struct diff_queue_struct
*q
= &diff_queued_diff
;
750 for (i
= 0; i
< q
->nr
; i
++)
751 if (!diff_unmodified_pair(q
->queue
[i
]))
757 void diff_debug_filespec(struct diff_filespec
*s
, int x
, const char *one
)
759 fprintf(stderr
, "queue[%d] %s (%s) %s %06o %s\n",
762 DIFF_FILE_VALID(s
) ? "valid" : "invalid",
764 s
->sha1_valid
? sha1_to_hex(s
->sha1
) : "");
765 fprintf(stderr
, "queue[%d] %s size %lu flags %d\n",
767 s
->size
, s
->xfrm_flags
);
770 void diff_debug_filepair(const struct diff_filepair
*p
, int i
)
772 diff_debug_filespec(p
->one
, i
, "one");
773 diff_debug_filespec(p
->two
, i
, "two");
774 fprintf(stderr
, "score %d, status %c stays %d broken %d\n",
775 p
->score
, p
->status
? : '?',
776 p
->source_stays
, p
->broken_pair
);
779 void diff_debug_queue(const char *msg
, struct diff_queue_struct
*q
)
783 fprintf(stderr
, "%s\n", msg
);
784 fprintf(stderr
, "q->nr = %d\n", q
->nr
);
785 for (i
= 0; i
< q
->nr
; i
++) {
786 struct diff_filepair
*p
= q
->queue
[i
];
787 diff_debug_filepair(p
, i
);
792 static void diff_resolve_rename_copy(void)
795 struct diff_filepair
*p
, *pp
;
796 struct diff_queue_struct
*q
= &diff_queued_diff
;
798 diff_debug_queue("resolve-rename-copy", q
);
800 for (i
= 0; i
< q
->nr
; i
++) {
802 p
->status
= 0; /* undecided */
803 if (DIFF_PAIR_UNMERGED(p
))
805 else if (!DIFF_FILE_VALID(p
->one
))
807 else if (!DIFF_FILE_VALID(p
->two
))
809 else if (DIFF_PAIR_TYPE_CHANGED(p
))
812 /* from this point on, we are dealing with a pair
813 * whose both sides are valid and of the same type, i.e.
814 * either in-place edit or rename/copy edit.
816 else if (DIFF_PAIR_RENAME(p
)) {
817 if (p
->source_stays
) {
821 /* See if there is some other filepair that
822 * copies from the same source as us. If so
823 * we are a copy. Otherwise we are a rename.
825 for (j
= i
+ 1; j
< q
->nr
; j
++) {
827 if (strcmp(pp
->one
->path
, p
->one
->path
))
828 continue; /* not us */
829 if (!DIFF_PAIR_RENAME(pp
))
830 continue; /* not a rename/copy */
831 /* pp is a rename/copy from the same source */
838 else if (memcmp(p
->one
->sha1
, p
->two
->sha1
, 20) ||
839 p
->one
->mode
!= p
->two
->mode
)
842 /* this is a "no-change" entry.
843 * should not happen anymore.
846 die("internal error in diffcore: unmodified entry remains");
848 diff_debug_queue("resolve-rename-copy done", q
);
851 void diff_flush(int diff_output_style
, int resolve_rename_copy
)
853 struct diff_queue_struct
*q
= &diff_queued_diff
;
855 int line_termination
= '\n';
856 int inter_name_termination
= '\t';
858 if (diff_output_style
== DIFF_FORMAT_MACHINE
)
859 line_termination
= inter_name_termination
= 0;
860 if (resolve_rename_copy
)
861 diff_resolve_rename_copy();
863 for (i
= 0; i
< q
->nr
; i
++) {
864 struct diff_filepair
*p
= q
->queue
[i
];
865 if ((diff_output_style
== DIFF_FORMAT_NO_OUTPUT
) ||
869 die("internal error in diff-resolve-rename-copy");
870 switch (diff_output_style
) {
871 case DIFF_FORMAT_PATCH
:
874 case DIFF_FORMAT_HUMAN
:
875 case DIFF_FORMAT_MACHINE
:
876 diff_flush_raw(p
, line_termination
,
877 inter_name_termination
);
881 for (i
= 0; i
< q
->nr
; i
++)
882 diff_free_filepair(q
->queue
[i
]);
885 q
->nr
= q
->alloc
= 0;
888 void diffcore_std(const char **paths
,
889 int detect_rename
, int rename_score
,
890 const char *pickaxe
, int pickaxe_opts
,
893 if (paths
&& paths
[0])
894 diffcore_pathspec(paths
);
896 diffcore_break(break_opt
);
898 diffcore_rename(detect_rename
, rename_score
);
900 diffcore_pickaxe(pickaxe
, pickaxe_opts
);
903 void diff_addremove(int addremove
, unsigned mode
,
904 const unsigned char *sha1
,
905 const char *base
, const char *path
)
907 char concatpath
[PATH_MAX
];
908 struct diff_filespec
*one
, *two
;
910 /* This may look odd, but it is a preparation for
911 * feeding "there are unchanged files which should
912 * not produce diffs, but when you are doing copy
913 * detection you would need them, so here they are"
914 * entries to the diff-core. They will be prefixed
915 * with something like '=' or '*' (I haven't decided
916 * which but should not make any difference).
917 * Feeding the same new and old to diff_change()
918 * also has the same effect.
919 * Before the final output happens, they are pruned after
920 * merged into rename/copy pairs as appropriate.
923 addremove
= (addremove
== '+' ? '-' :
924 addremove
== '-' ? '+' : addremove
);
926 if (!path
) path
= "";
927 sprintf(concatpath
, "%s%s", base
, path
);
928 one
= alloc_filespec(concatpath
);
929 two
= alloc_filespec(concatpath
);
931 if (addremove
!= '+')
932 fill_filespec(one
, sha1
, mode
);
933 if (addremove
!= '-')
934 fill_filespec(two
, sha1
, mode
);
936 diff_queue(&diff_queued_diff
, one
, two
);
939 void diff_helper_input(unsigned old_mode
,
941 const unsigned char *old_sha1
,
942 const unsigned char *new_sha1
,
943 const char *old_path
,
946 const char *new_path
)
948 struct diff_filespec
*one
, *two
;
949 struct diff_filepair
*dp
;
951 one
= alloc_filespec(old_path
);
952 two
= alloc_filespec(new_path
);
954 fill_filespec(one
, old_sha1
, old_mode
);
956 fill_filespec(two
, new_sha1
, new_mode
);
957 dp
= diff_queue(&diff_queued_diff
, one
, two
);
958 dp
->score
= score
* MAX_SCORE
/ 100;
962 void diff_change(unsigned old_mode
, unsigned new_mode
,
963 const unsigned char *old_sha1
,
964 const unsigned char *new_sha1
,
965 const char *base
, const char *path
)
967 char concatpath
[PATH_MAX
];
968 struct diff_filespec
*one
, *two
;
972 const unsigned char *tmp_c
;
973 tmp
= old_mode
; old_mode
= new_mode
; new_mode
= tmp
;
974 tmp_c
= old_sha1
; old_sha1
= new_sha1
; new_sha1
= tmp_c
;
976 if (!path
) path
= "";
977 sprintf(concatpath
, "%s%s", base
, path
);
978 one
= alloc_filespec(concatpath
);
979 two
= alloc_filespec(concatpath
);
980 fill_filespec(one
, old_sha1
, old_mode
);
981 fill_filespec(two
, new_sha1
, new_mode
);
983 diff_queue(&diff_queued_diff
, one
, two
);
986 void diff_unmerge(const char *path
)
988 struct diff_filespec
*one
, *two
;
989 one
= alloc_filespec(path
);
990 two
= alloc_filespec(path
);
991 diff_queue(&diff_queued_diff
, one
, two
);