2 * Copyright (C) 2005 Junio C Hamano
12 static const char *diff_opts
= "-pu";
14 static int use_size_cache
;
16 static const char *external_diff(void)
18 static const char *external_diff_cmd
= NULL
;
19 static int done_preparing
= 0;
20 const char *env_diff_opts
;
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 external_diff_cmd
= getenv("GIT_EXTERNAL_DIFF");
35 /* In case external diff fails... */
36 env_diff_opts
= getenv("GIT_DIFF_OPTS");
37 if (env_diff_opts
) diff_opts
= env_diff_opts
;
40 return external_diff_cmd
;
43 #define TEMPFILE_PATH_LEN 50
45 static struct diff_tempfile
{
46 const char *name
; /* filename external diff should read from */
49 char tmp_path
[TEMPFILE_PATH_LEN
];
52 static int count_lines(const char *filename
)
55 int count
, ch
, completely_empty
= 1, nl_just_seen
= 0;
56 in
= fopen(filename
, "r");
58 while ((ch
= fgetc(in
)) != EOF
)
72 count
++; /* no trailing newline */
76 static void print_line_count(int count
)
86 printf("1,%d", count
);
91 static void copy_file(int prefix
, const char *filename
)
94 int ch
, nl_just_seen
= 1;
95 in
= fopen(filename
, "r");
96 while ((ch
= fgetc(in
)) != EOF
) {
107 printf("\n\\ No newline at end of file\n");
110 static void emit_rewrite_diff(const char *name_a
,
112 struct diff_tempfile
*temp
)
114 /* Use temp[i].name as input, name_a and name_b as labels */
116 lc_a
= count_lines(temp
[0].name
);
117 lc_b
= count_lines(temp
[1].name
);
118 printf("--- %s\n+++ %s\n@@ -", name_a
, name_b
);
119 print_line_count(lc_a
);
121 print_line_count(lc_b
);
124 copy_file('-', temp
[0].name
);
126 copy_file('+', temp
[1].name
);
129 static void builtin_diff(const char *name_a
,
131 struct diff_tempfile
*temp
,
132 const char *xfrm_msg
,
133 int complete_rewrite
)
135 int i
, next_at
, cmd_size
;
136 const char *const diff_cmd
= "diff -L%s%s -L%s%s";
137 const char *const diff_arg
= "%s %s||:"; /* "||:" is to return 0 */
138 const char *input_name_sq
[2];
139 const char *path0
[2];
140 const char *path1
[2];
141 const char *name_sq
[2];
144 name_sq
[0] = sq_quote(name_a
);
145 name_sq
[1] = sq_quote(name_b
);
147 /* diff_cmd and diff_arg have 6 %s in total which makes
148 * the sum of these strings 12 bytes larger than required.
149 * we use 2 spaces around diff-opts, and we need to count
150 * terminating NUL, so we subtract 9 here.
152 cmd_size
= (strlen(diff_cmd
) + strlen(diff_opts
) +
153 strlen(diff_arg
) - 9);
154 for (i
= 0; i
< 2; i
++) {
155 input_name_sq
[i
] = sq_quote(temp
[i
].name
);
156 if (!strcmp(temp
[i
].name
, "/dev/null")) {
157 path0
[i
] = "/dev/null";
160 path0
[i
] = i
? "b/" : "a/";
161 path1
[i
] = name_sq
[i
];
163 cmd_size
+= (strlen(path0
[i
]) + strlen(path1
[i
]) +
164 strlen(input_name_sq
[i
]));
167 cmd
= xmalloc(cmd_size
);
170 next_at
+= snprintf(cmd
+next_at
, cmd_size
-next_at
,
172 path0
[0], path1
[0], path0
[1], path1
[1]);
173 next_at
+= snprintf(cmd
+next_at
, cmd_size
-next_at
,
175 next_at
+= snprintf(cmd
+next_at
, cmd_size
-next_at
,
176 diff_arg
, input_name_sq
[0], input_name_sq
[1]);
178 printf("diff --git a/%s b/%s\n", name_a
, name_b
);
180 printf("new file mode %s\n", temp
[1].mode
);
181 if (xfrm_msg
&& xfrm_msg
[0])
184 else if (!path1
[1][0]) {
185 printf("deleted file mode %s\n", temp
[0].mode
);
186 if (xfrm_msg
&& xfrm_msg
[0])
190 if (strcmp(temp
[0].mode
, temp
[1].mode
)) {
191 printf("old mode %s\n", temp
[0].mode
);
192 printf("new mode %s\n", temp
[1].mode
);
194 if (xfrm_msg
&& xfrm_msg
[0])
196 if (strncmp(temp
[0].mode
, temp
[1].mode
, 3))
197 /* we do not run diff between different kind
201 if (complete_rewrite
) {
203 emit_rewrite_diff(name_a
, name_b
, temp
);
208 execlp("/bin/sh","sh", "-c", cmd
, NULL
);
211 struct diff_filespec
*alloc_filespec(const char *path
)
213 int namelen
= strlen(path
);
214 struct diff_filespec
*spec
= xmalloc(sizeof(*spec
) + namelen
+ 1);
216 memset(spec
, 0, sizeof(*spec
));
217 spec
->path
= (char *)(spec
+ 1);
218 memcpy(spec
->path
, path
, namelen
+1);
222 void fill_filespec(struct diff_filespec
*spec
, const unsigned char *sha1
,
226 spec
->mode
= DIFF_FILE_CANON_MODE(mode
);
227 memcpy(spec
->sha1
, sha1
, 20);
228 spec
->sha1_valid
= !!memcmp(sha1
, null_sha1
, 20);
233 * Given a name and sha1 pair, if the dircache tells us the file in
234 * the work tree has that object contents, return true, so that
235 * prepare_temp_file() does not have to inflate and extract.
237 static int work_tree_matches(const char *name
, const unsigned char *sha1
)
239 struct cache_entry
*ce
;
243 /* We do not read the cache ourselves here, because the
244 * benchmark with my previous version that always reads cache
245 * shows that it makes things worse for diff-tree comparing
246 * two linux-2.6 kernel trees in an already checked out work
247 * tree. This is because most diff-tree comparisons deal with
248 * only a small number of files, while reading the cache is
249 * expensive for a large project, and its cost outweighs the
250 * savings we get by not inflating the object to a temporary
251 * file. Practically, this code only helps when we are used
252 * by diff-cache --cached, which does read the cache before
259 pos
= cache_name_pos(name
, len
);
262 ce
= active_cache
[pos
];
263 if ((lstat(name
, &st
) < 0) ||
264 !S_ISREG(st
.st_mode
) || /* careful! */
265 ce_match_stat(ce
, &st
) ||
266 memcmp(sha1
, ce
->sha1
, 20))
268 /* we return 1 only when we can stat, it is a regular file,
269 * stat information matches, and sha1 recorded in the cache
270 * matches. I.e. we know the file in the work tree really is
271 * the same as the <name, sha1> pair.
276 static struct sha1_size_cache
{
277 unsigned char sha1
[20];
280 static int sha1_size_cache_nr
, sha1_size_cache_alloc
;
282 static struct sha1_size_cache
*locate_size_cache(unsigned char *sha1
,
287 struct sha1_size_cache
*e
;
290 last
= sha1_size_cache_nr
;
291 while (last
> first
) {
292 int cmp
, next
= (last
+ first
) >> 1;
293 e
= sha1_size_cache
[next
];
294 cmp
= memcmp(e
->sha1
, sha1
, 20);
306 /* insert to make it at "first" */
307 if (sha1_size_cache_alloc
<= sha1_size_cache_nr
) {
308 sha1_size_cache_alloc
= alloc_nr(sha1_size_cache_alloc
);
309 sha1_size_cache
= xrealloc(sha1_size_cache
,
310 sha1_size_cache_alloc
*
311 sizeof(*sha1_size_cache
));
313 sha1_size_cache_nr
++;
314 if (first
< sha1_size_cache_nr
)
315 memmove(sha1_size_cache
+ first
+ 1, sha1_size_cache
+ first
,
316 (sha1_size_cache_nr
- first
- 1) *
317 sizeof(*sha1_size_cache
));
318 e
= xmalloc(sizeof(struct sha1_size_cache
));
319 sha1_size_cache
[first
] = e
;
320 memcpy(e
->sha1
, sha1
, 20);
326 * While doing rename detection and pickaxe operation, we may need to
327 * grab the data for the blob (or file) for our own in-core comparison.
328 * diff_filespec has data and size fields for this purpose.
330 int diff_populate_filespec(struct diff_filespec
*s
, int size_only
)
333 if (!DIFF_FILE_VALID(s
))
334 die("internal error: asking to populate invalid file.");
335 if (S_ISDIR(s
->mode
))
343 if (!s
->sha1_valid
||
344 work_tree_matches(s
->path
, s
->sha1
)) {
347 if (lstat(s
->path
, &st
) < 0) {
348 if (errno
== ENOENT
) {
357 s
->size
= st
.st_size
;
362 if (S_ISLNK(st
.st_mode
)) {
364 s
->data
= xmalloc(s
->size
);
366 ret
= readlink(s
->path
, s
->data
, s
->size
);
373 fd
= open(s
->path
, O_RDONLY
);
376 s
->data
= mmap(NULL
, s
->size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
378 if (s
->data
== MAP_FAILED
)
380 s
->should_munmap
= 1;
384 struct sha1_size_cache
*e
;
387 e
= locate_size_cache(s
->sha1
, 1, 0);
392 if (!sha1_object_info(s
->sha1
, type
, &s
->size
))
393 locate_size_cache(s
->sha1
, 0, s
->size
);
396 s
->data
= read_sha1_file(s
->sha1
, type
, &s
->size
);
403 void diff_free_filespec_data(struct diff_filespec
*s
)
407 else if (s
->should_munmap
)
408 munmap(s
->data
, s
->size
);
409 s
->should_free
= s
->should_munmap
= 0;
413 static void prep_temp_blob(struct diff_tempfile
*temp
,
416 const unsigned char *sha1
,
421 fd
= git_mkstemp(temp
->tmp_path
, TEMPFILE_PATH_LEN
, ".diff_XXXXXX");
423 die("unable to create temp-file");
424 if (write(fd
, blob
, size
) != size
)
425 die("unable to write temp-file");
427 temp
->name
= temp
->tmp_path
;
428 strcpy(temp
->hex
, sha1_to_hex(sha1
));
430 sprintf(temp
->mode
, "%06o", mode
);
433 static void prepare_temp_file(const char *name
,
434 struct diff_tempfile
*temp
,
435 struct diff_filespec
*one
)
437 if (!DIFF_FILE_VALID(one
)) {
439 /* A '-' entry produces this for file-2, and
440 * a '+' entry produces this for file-1.
442 temp
->name
= "/dev/null";
443 strcpy(temp
->hex
, ".");
444 strcpy(temp
->mode
, ".");
448 if (!one
->sha1_valid
||
449 work_tree_matches(name
, one
->sha1
)) {
451 if (lstat(name
, &st
) < 0) {
453 goto not_a_valid_file
;
454 die("stat(%s): %s", name
, strerror(errno
));
456 if (S_ISLNK(st
.st_mode
)) {
458 char *buf
, buf_
[1024];
459 buf
= ((sizeof(buf_
) < st
.st_size
) ?
460 xmalloc(st
.st_size
) : buf_
);
461 ret
= readlink(name
, buf
, st
.st_size
);
463 die("readlink(%s)", name
);
464 prep_temp_blob(temp
, buf
, st
.st_size
,
466 one
->sha1
: null_sha1
),
468 one
->mode
: S_IFLNK
));
471 /* we can borrow from the file in the work tree */
473 if (!one
->sha1_valid
)
474 strcpy(temp
->hex
, sha1_to_hex(null_sha1
));
476 strcpy(temp
->hex
, sha1_to_hex(one
->sha1
));
477 /* Even though we may sometimes borrow the
478 * contents from the work tree, we always want
479 * one->mode. mode is trustworthy even when
480 * !(one->sha1_valid), as long as
481 * DIFF_FILE_VALID(one).
483 sprintf(temp
->mode
, "%06o", one
->mode
);
488 if (diff_populate_filespec(one
, 0))
489 die("cannot read data blob for %s", one
->path
);
490 prep_temp_blob(temp
, one
->data
, one
->size
,
491 one
->sha1
, one
->mode
);
495 static void remove_tempfile(void)
499 for (i
= 0; i
< 2; i
++)
500 if (diff_temp
[i
].name
== diff_temp
[i
].tmp_path
) {
501 unlink(diff_temp
[i
].name
);
502 diff_temp
[i
].name
= NULL
;
506 static void remove_tempfile_on_signal(int signo
)
511 /* An external diff command takes:
513 * diff-cmd name infile1 infile1-sha1 infile1-mode \
514 * infile2 infile2-sha1 infile2-mode [ rename-to ]
517 static void run_external_diff(const char *pgm
,
520 struct diff_filespec
*one
,
521 struct diff_filespec
*two
,
522 const char *xfrm_msg
,
523 int complete_rewrite
)
525 struct diff_tempfile
*temp
= diff_temp
;
528 static int atexit_asked
= 0;
529 const char *othername
;
531 othername
= (other
? other
: name
);
533 prepare_temp_file(name
, &temp
[0], one
);
534 prepare_temp_file(othername
, &temp
[1], two
);
535 if (! atexit_asked
&&
536 (temp
[0].name
== temp
[0].tmp_path
||
537 temp
[1].name
== temp
[1].tmp_path
)) {
539 atexit(remove_tempfile
);
541 signal(SIGINT
, remove_tempfile_on_signal
);
547 die("unable to fork");
551 const char *exec_arg
[10];
552 const char **arg
= &exec_arg
[0];
555 *arg
++ = temp
[0].name
;
556 *arg
++ = temp
[0].hex
;
557 *arg
++ = temp
[0].mode
;
558 *arg
++ = temp
[1].name
;
559 *arg
++ = temp
[1].hex
;
560 *arg
++ = temp
[1].mode
;
566 execvp(pgm
, (char *const*) exec_arg
);
569 execlp(pgm
, pgm
, name
, NULL
);
572 * otherwise we use the built-in one.
575 builtin_diff(name
, othername
, temp
, xfrm_msg
,
578 printf("* Unmerged path %s\n", name
);
581 if (waitpid(pid
, &status
, 0) < 0 ||
582 !WIFEXITED(status
) || WEXITSTATUS(status
)) {
583 /* Earlier we did not check the exit status because
584 * diff exits non-zero if files are different, and
585 * we are not interested in knowing that. It was a
586 * mistake which made it harder to quit a diff-*
587 * session that uses the git-apply-patch-script as
588 * the GIT_EXTERNAL_DIFF. A custom GIT_EXTERNAL_DIFF
589 * should also exit non-zero only when it wants to
590 * abort the entire diff-* session.
593 fprintf(stderr
, "external diff died, stopping at %s.\n", name
);
599 static void run_diff(struct diff_filepair
*p
)
601 const char *pgm
= external_diff();
602 char msg_
[PATH_MAX
*2+200], *xfrm_msg
;
603 struct diff_filespec
*one
;
604 struct diff_filespec
*two
;
607 int complete_rewrite
= 0;
609 if (DIFF_PAIR_UNMERGED(p
)) {
611 run_external_diff(pgm
, p
->one
->path
, NULL
, NULL
, NULL
, NULL
,
617 other
= (strcmp(name
, p
->two
->path
) ? p
->two
->path
: NULL
);
618 one
= p
->one
; two
= p
->two
;
620 case DIFF_STATUS_COPIED
:
622 "similarity index %d%%\n"
625 (int)(0.5 + p
->score
* 100.0/MAX_SCORE
),
629 case DIFF_STATUS_RENAMED
:
631 "similarity index %d%%\n"
634 (int)(0.5 + p
->score
* 100.0/MAX_SCORE
),
638 case DIFF_STATUS_MODIFIED
:
641 "dissimilarity index %d%%",
642 (int)(0.5 + p
->score
* 100.0/MAX_SCORE
));
644 complete_rewrite
= 1;
653 DIFF_FILE_VALID(one
) && DIFF_FILE_VALID(two
) &&
654 (S_IFMT
& one
->mode
) != (S_IFMT
& two
->mode
)) {
655 /* a filepair that changes between file and symlink
656 * needs to be split into deletion and creation.
658 struct diff_filespec
*null
= alloc_filespec(two
->path
);
659 run_external_diff(NULL
, name
, other
, one
, null
, xfrm_msg
, 0);
661 null
= alloc_filespec(one
->path
);
662 run_external_diff(NULL
, name
, other
, null
, two
, xfrm_msg
, 0);
666 run_external_diff(pgm
, name
, other
, one
, two
, xfrm_msg
,
670 void diff_setup(struct diff_options
*options
)
672 memset(options
, 0, sizeof(*options
));
673 options
->output_format
= DIFF_FORMAT_RAW
;
674 options
->line_termination
= '\n';
675 options
->break_opt
= -1;
676 options
->rename_limit
= -1;
679 int diff_setup_done(struct diff_options
*options
)
681 if ((options
->find_copies_harder
|| 0 <= options
->rename_limit
) &&
682 options
->detect_rename
!= DIFF_DETECT_COPY
)
684 if (options
->setup
& DIFF_SETUP_USE_CACHE
) {
686 /* read-cache does not die even when it fails
687 * so it is safe for us to do this here. Also
688 * it does not smudge active_cache or active_nr
689 * when it fails, so we do not have to worry about
690 * cleaning it up oufselves either.
694 if (options
->setup
& DIFF_SETUP_USE_SIZE_CACHE
)
700 int diff_opt_parse(struct diff_options
*options
, const char **av
, int ac
)
702 const char *arg
= av
[0];
703 if (!strcmp(arg
, "-p") || !strcmp(arg
, "-u"))
704 options
->output_format
= DIFF_FORMAT_PATCH
;
705 else if (!strcmp(arg
, "-z"))
706 options
->line_termination
= 0;
707 else if (!strncmp(arg
, "-l", 2))
708 options
->rename_limit
= strtoul(arg
+2, NULL
, 10);
709 else if (!strcmp(arg
, "--name-only"))
710 options
->output_format
= DIFF_FORMAT_NAME
;
711 else if (!strcmp(arg
, "--name-status"))
712 options
->output_format
= DIFF_FORMAT_NAME_STATUS
;
713 else if (!strcmp(arg
, "-R"))
714 options
->reverse_diff
= 1;
715 else if (!strncmp(arg
, "-S", 2))
716 options
->pickaxe
= arg
+ 2;
717 else if (!strcmp(arg
, "-s"))
718 options
->output_format
= DIFF_FORMAT_NO_OUTPUT
;
719 else if (!strncmp(arg
, "-O", 2))
720 options
->orderfile
= arg
+ 2;
721 else if (!strncmp(arg
, "--diff-filter=", 14))
722 options
->filter
= arg
+ 14;
723 else if (!strcmp(arg
, "--pickaxe-all"))
724 options
->pickaxe_opts
= DIFF_PICKAXE_ALL
;
725 else if (!strncmp(arg
, "-B", 2)) {
726 if ((options
->break_opt
=
727 diff_scoreopt_parse(arg
)) == -1)
730 else if (!strncmp(arg
, "-M", 2)) {
731 if ((options
->rename_score
=
732 diff_scoreopt_parse(arg
)) == -1)
734 options
->detect_rename
= DIFF_DETECT_RENAME
;
736 else if (!strncmp(arg
, "-C", 2)) {
737 if ((options
->rename_score
=
738 diff_scoreopt_parse(arg
)) == -1)
740 options
->detect_rename
= DIFF_DETECT_COPY
;
742 else if (!strcmp(arg
, "--find-copies-harder"))
743 options
->find_copies_harder
= 1;
749 static int parse_num(const char **cp_p
)
751 int num
, scale
, ch
, cnt
;
752 const char *cp
= *cp_p
;
756 while ('0' <= (ch
= *cp
) && ch
<= '9') {
758 /* We simply ignore more than 5 digits precision. */
760 num
= num
* 10 + ch
- '0';
766 /* user says num divided by scale and we say internally that
767 * is MAX_SCORE * num / scale.
769 return (MAX_SCORE
* num
/ scale
);
772 int diff_scoreopt_parse(const char *opt
)
779 if (cmd
!= 'M' && cmd
!= 'C' && cmd
!= 'B')
780 return -1; /* that is not a -M, -C nor -B option */
782 opt1
= parse_num(&opt
);
788 else if (*opt
!= '/')
789 return -1; /* we expect -B80/99 or -B80 */
792 opt2
= parse_num(&opt
);
797 return opt1
| (opt2
<< 16);
800 struct diff_queue_struct diff_queued_diff
;
802 void diff_q(struct diff_queue_struct
*queue
, struct diff_filepair
*dp
)
804 if (queue
->alloc
<= queue
->nr
) {
805 queue
->alloc
= alloc_nr(queue
->alloc
);
806 queue
->queue
= xrealloc(queue
->queue
,
807 sizeof(dp
) * queue
->alloc
);
809 queue
->queue
[queue
->nr
++] = dp
;
812 struct diff_filepair
*diff_queue(struct diff_queue_struct
*queue
,
813 struct diff_filespec
*one
,
814 struct diff_filespec
*two
)
816 struct diff_filepair
*dp
= xmalloc(sizeof(*dp
));
821 dp
->source_stays
= 0;
828 void diff_free_filepair(struct diff_filepair
*p
)
830 diff_free_filespec_data(p
->one
);
831 diff_free_filespec_data(p
->two
);
837 static void diff_flush_raw(struct diff_filepair
*p
,
838 int line_termination
,
839 int inter_name_termination
,
845 if (line_termination
) {
846 const char *const err
=
847 "path %s cannot be expressed without -z";
848 if (strchr(p
->one
->path
, line_termination
) ||
849 strchr(p
->one
->path
, inter_name_termination
))
850 die(err
, p
->one
->path
);
851 if (strchr(p
->two
->path
, line_termination
) ||
852 strchr(p
->two
->path
, inter_name_termination
))
853 die(err
, p
->two
->path
);
857 sprintf(status
, "%c%03d", p
->status
,
858 (int)(0.5 + p
->score
* 100.0/MAX_SCORE
));
860 status
[0] = p
->status
;
864 case DIFF_STATUS_COPIED
:
865 case DIFF_STATUS_RENAMED
:
868 case DIFF_STATUS_ADDED
:
869 case DIFF_STATUS_DELETED
:
876 if (output_format
!= DIFF_FORMAT_NAME_STATUS
) {
877 printf(":%06o %06o %s ",
878 p
->one
->mode
, p
->two
->mode
, sha1_to_hex(p
->one
->sha1
));
879 printf("%s ", sha1_to_hex(p
->two
->sha1
));
881 printf("%s%c%s",status
, inter_name_termination
, p
->one
->path
);
883 printf("%c%s", inter_name_termination
, p
->two
->path
);
884 putchar(line_termination
);
887 static void diff_flush_name(struct diff_filepair
*p
,
888 int line_termination
)
890 printf("%s%c", p
->two
->path
, line_termination
);
893 int diff_unmodified_pair(struct diff_filepair
*p
)
895 /* This function is written stricter than necessary to support
896 * the currently implemented transformers, but the idea is to
897 * let transformers to produce diff_filepairs any way they want,
898 * and filter and clean them up here before producing the output.
900 struct diff_filespec
*one
, *two
;
902 if (DIFF_PAIR_UNMERGED(p
))
903 return 0; /* unmerged is interesting */
908 /* deletion, addition, mode or type change
909 * and rename are all interesting.
911 if (DIFF_FILE_VALID(one
) != DIFF_FILE_VALID(two
) ||
912 DIFF_PAIR_MODE_CHANGED(p
) ||
913 strcmp(one
->path
, two
->path
))
916 /* both are valid and point at the same path. that is, we are
917 * dealing with a change.
919 if (one
->sha1_valid
&& two
->sha1_valid
&&
920 !memcmp(one
->sha1
, two
->sha1
, sizeof(one
->sha1
)))
921 return 1; /* no change */
922 if (!one
->sha1_valid
&& !two
->sha1_valid
)
923 return 1; /* both look at the same file on the filesystem. */
927 static void diff_flush_patch(struct diff_filepair
*p
)
929 if (diff_unmodified_pair(p
))
932 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
933 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
934 return; /* no tree diffs in patch format */
939 int diff_queue_is_empty(void)
941 struct diff_queue_struct
*q
= &diff_queued_diff
;
943 for (i
= 0; i
< q
->nr
; i
++)
944 if (!diff_unmodified_pair(q
->queue
[i
]))
950 void diff_debug_filespec(struct diff_filespec
*s
, int x
, const char *one
)
952 fprintf(stderr
, "queue[%d] %s (%s) %s %06o %s\n",
955 DIFF_FILE_VALID(s
) ? "valid" : "invalid",
957 s
->sha1_valid
? sha1_to_hex(s
->sha1
) : "");
958 fprintf(stderr
, "queue[%d] %s size %lu flags %d\n",
960 s
->size
, s
->xfrm_flags
);
963 void diff_debug_filepair(const struct diff_filepair
*p
, int i
)
965 diff_debug_filespec(p
->one
, i
, "one");
966 diff_debug_filespec(p
->two
, i
, "two");
967 fprintf(stderr
, "score %d, status %c stays %d broken %d\n",
968 p
->score
, p
->status
? p
->status
: '?',
969 p
->source_stays
, p
->broken_pair
);
972 void diff_debug_queue(const char *msg
, struct diff_queue_struct
*q
)
976 fprintf(stderr
, "%s\n", msg
);
977 fprintf(stderr
, "q->nr = %d\n", q
->nr
);
978 for (i
= 0; i
< q
->nr
; i
++) {
979 struct diff_filepair
*p
= q
->queue
[i
];
980 diff_debug_filepair(p
, i
);
985 static void diff_resolve_rename_copy(void)
988 struct diff_filepair
*p
, *pp
;
989 struct diff_queue_struct
*q
= &diff_queued_diff
;
991 diff_debug_queue("resolve-rename-copy", q
);
993 for (i
= 0; i
< q
->nr
; i
++) {
995 p
->status
= 0; /* undecided */
996 if (DIFF_PAIR_UNMERGED(p
))
997 p
->status
= DIFF_STATUS_UNMERGED
;
998 else if (!DIFF_FILE_VALID(p
->one
))
999 p
->status
= DIFF_STATUS_ADDED
;
1000 else if (!DIFF_FILE_VALID(p
->two
))
1001 p
->status
= DIFF_STATUS_DELETED
;
1002 else if (DIFF_PAIR_TYPE_CHANGED(p
))
1003 p
->status
= DIFF_STATUS_TYPE_CHANGED
;
1005 /* from this point on, we are dealing with a pair
1006 * whose both sides are valid and of the same type, i.e.
1007 * either in-place edit or rename/copy edit.
1009 else if (DIFF_PAIR_RENAME(p
)) {
1010 if (p
->source_stays
) {
1011 p
->status
= DIFF_STATUS_COPIED
;
1014 /* See if there is some other filepair that
1015 * copies from the same source as us. If so
1016 * we are a copy. Otherwise we are either a
1017 * copy if the path stays, or a rename if it
1018 * does not, but we already handled "stays" case.
1020 for (j
= i
+ 1; j
< q
->nr
; j
++) {
1022 if (strcmp(pp
->one
->path
, p
->one
->path
))
1023 continue; /* not us */
1024 if (!DIFF_PAIR_RENAME(pp
))
1025 continue; /* not a rename/copy */
1026 /* pp is a rename/copy from the same source */
1027 p
->status
= DIFF_STATUS_COPIED
;
1031 p
->status
= DIFF_STATUS_RENAMED
;
1033 else if (memcmp(p
->one
->sha1
, p
->two
->sha1
, 20) ||
1034 p
->one
->mode
!= p
->two
->mode
)
1035 p
->status
= DIFF_STATUS_MODIFIED
;
1037 /* This is a "no-change" entry and should not
1038 * happen anymore, but prepare for broken callers.
1040 error("feeding unmodified %s to diffcore",
1042 p
->status
= DIFF_STATUS_UNKNOWN
;
1045 diff_debug_queue("resolve-rename-copy done", q
);
1048 void diff_flush(struct diff_options
*options
)
1050 struct diff_queue_struct
*q
= &diff_queued_diff
;
1052 int inter_name_termination
= '\t';
1053 int diff_output_format
= options
->output_format
;
1054 int line_termination
= options
->line_termination
;
1056 if (!line_termination
)
1057 inter_name_termination
= 0;
1059 for (i
= 0; i
< q
->nr
; i
++) {
1060 struct diff_filepair
*p
= q
->queue
[i
];
1061 if ((diff_output_format
== DIFF_FORMAT_NO_OUTPUT
) ||
1062 (p
->status
== DIFF_STATUS_UNKNOWN
))
1065 die("internal error in diff-resolve-rename-copy");
1066 switch (diff_output_format
) {
1067 case DIFF_FORMAT_PATCH
:
1068 diff_flush_patch(p
);
1070 case DIFF_FORMAT_RAW
:
1071 case DIFF_FORMAT_NAME_STATUS
:
1072 diff_flush_raw(p
, line_termination
,
1073 inter_name_termination
,
1074 diff_output_format
);
1076 case DIFF_FORMAT_NAME
:
1077 diff_flush_name(p
, line_termination
);
1080 diff_free_filepair(q
->queue
[i
]);
1084 q
->nr
= q
->alloc
= 0;
1087 static void diffcore_apply_filter(const char *filter
)
1090 struct diff_queue_struct
*q
= &diff_queued_diff
;
1091 struct diff_queue_struct outq
;
1093 outq
.nr
= outq
.alloc
= 0;
1098 if (strchr(filter
, DIFF_STATUS_FILTER_AON
)) {
1100 for (i
= found
= 0; !found
&& i
< q
->nr
; i
++) {
1101 struct diff_filepair
*p
= q
->queue
[i
];
1102 if (((p
->status
== DIFF_STATUS_MODIFIED
) &&
1104 strchr(filter
, DIFF_STATUS_FILTER_BROKEN
)) ||
1106 strchr(filter
, DIFF_STATUS_MODIFIED
)))) ||
1107 ((p
->status
!= DIFF_STATUS_MODIFIED
) &&
1108 strchr(filter
, p
->status
)))
1114 /* otherwise we will clear the whole queue
1115 * by copying the empty outq at the end of this
1116 * function, but first clear the current entries
1119 for (i
= 0; i
< q
->nr
; i
++)
1120 diff_free_filepair(q
->queue
[i
]);
1123 /* Only the matching ones */
1124 for (i
= 0; i
< q
->nr
; i
++) {
1125 struct diff_filepair
*p
= q
->queue
[i
];
1127 if (((p
->status
== DIFF_STATUS_MODIFIED
) &&
1129 strchr(filter
, DIFF_STATUS_FILTER_BROKEN
)) ||
1131 strchr(filter
, DIFF_STATUS_MODIFIED
)))) ||
1132 ((p
->status
!= DIFF_STATUS_MODIFIED
) &&
1133 strchr(filter
, p
->status
)))
1136 diff_free_filepair(p
);
1143 void diffcore_std(struct diff_options
*options
)
1145 if (options
->paths
&& options
->paths
[0])
1146 diffcore_pathspec(options
->paths
);
1147 if (options
->break_opt
!= -1)
1148 diffcore_break(options
->break_opt
);
1149 if (options
->detect_rename
)
1150 diffcore_rename(options
);
1151 if (options
->break_opt
!= -1)
1152 diffcore_merge_broken();
1153 if (options
->pickaxe
)
1154 diffcore_pickaxe(options
->pickaxe
, options
->pickaxe_opts
);
1155 if (options
->orderfile
)
1156 diffcore_order(options
->orderfile
);
1157 diff_resolve_rename_copy();
1158 diffcore_apply_filter(options
->filter
);
1162 void diffcore_std_no_resolve(struct diff_options
*options
)
1164 if (options
->pickaxe
)
1165 diffcore_pickaxe(options
->pickaxe
, options
->pickaxe_opts
);
1166 if (options
->orderfile
)
1167 diffcore_order(options
->orderfile
);
1168 diffcore_apply_filter(options
->filter
);
1171 void diff_addremove(struct diff_options
*options
,
1172 int addremove
, unsigned mode
,
1173 const unsigned char *sha1
,
1174 const char *base
, const char *path
)
1176 char concatpath
[PATH_MAX
];
1177 struct diff_filespec
*one
, *two
;
1179 /* This may look odd, but it is a preparation for
1180 * feeding "there are unchanged files which should
1181 * not produce diffs, but when you are doing copy
1182 * detection you would need them, so here they are"
1183 * entries to the diff-core. They will be prefixed
1184 * with something like '=' or '*' (I haven't decided
1185 * which but should not make any difference).
1186 * Feeding the same new and old to diff_change()
1187 * also has the same effect.
1188 * Before the final output happens, they are pruned after
1189 * merged into rename/copy pairs as appropriate.
1191 if (options
->reverse_diff
)
1192 addremove
= (addremove
== '+' ? '-' :
1193 addremove
== '-' ? '+' : addremove
);
1195 if (!path
) path
= "";
1196 sprintf(concatpath
, "%s%s", base
, path
);
1197 one
= alloc_filespec(concatpath
);
1198 two
= alloc_filespec(concatpath
);
1200 if (addremove
!= '+')
1201 fill_filespec(one
, sha1
, mode
);
1202 if (addremove
!= '-')
1203 fill_filespec(two
, sha1
, mode
);
1205 diff_queue(&diff_queued_diff
, one
, two
);
1208 void diff_change(struct diff_options
*options
,
1209 unsigned old_mode
, unsigned new_mode
,
1210 const unsigned char *old_sha1
,
1211 const unsigned char *new_sha1
,
1212 const char *base
, const char *path
)
1214 char concatpath
[PATH_MAX
];
1215 struct diff_filespec
*one
, *two
;
1217 if (options
->reverse_diff
) {
1219 const unsigned char *tmp_c
;
1220 tmp
= old_mode
; old_mode
= new_mode
; new_mode
= tmp
;
1221 tmp_c
= old_sha1
; old_sha1
= new_sha1
; new_sha1
= tmp_c
;
1223 if (!path
) path
= "";
1224 sprintf(concatpath
, "%s%s", base
, path
);
1225 one
= alloc_filespec(concatpath
);
1226 two
= alloc_filespec(concatpath
);
1227 fill_filespec(one
, old_sha1
, old_mode
);
1228 fill_filespec(two
, new_sha1
, new_mode
);
1230 diff_queue(&diff_queued_diff
, one
, two
);
1233 void diff_unmerge(struct diff_options
*options
,
1236 struct diff_filespec
*one
, *two
;
1237 one
= alloc_filespec(path
);
1238 two
= alloc_filespec(path
);
1239 diff_queue(&diff_queued_diff
, one
, two
);