2 * Copyright (C) 2005 Junio C Hamano
12 static const char *diff_opts
= "-pu";
13 static unsigned char null_sha1
[20] = { 0, };
15 static int use_size_cache
;
17 static const char *external_diff(void)
19 static const char *external_diff_cmd
= NULL
;
20 static int done_preparing
= 0;
21 const char *env_diff_opts
;
24 return external_diff_cmd
;
27 * Default values above are meant to match the
28 * Linux kernel development style. Examples of
29 * alternative styles you can specify via environment
34 external_diff_cmd
= getenv("GIT_EXTERNAL_DIFF");
36 /* In case external diff fails... */
37 env_diff_opts
= getenv("GIT_DIFF_OPTS");
38 if (env_diff_opts
) diff_opts
= env_diff_opts
;
41 return external_diff_cmd
;
44 #define TEMPFILE_PATH_LEN 50
46 static struct diff_tempfile
{
47 const char *name
; /* filename external diff should read from */
50 char tmp_path
[TEMPFILE_PATH_LEN
];
53 static int count_lines(const char *filename
)
56 int count
, ch
, completely_empty
= 1, nl_just_seen
= 0;
57 in
= fopen(filename
, "r");
59 while ((ch
= fgetc(in
)) != EOF
)
73 count
++; /* no trailing newline */
77 static void print_line_count(int count
)
87 printf("1,%d", count
);
92 static void copy_file(int prefix
, const char *filename
)
95 int ch
, nl_just_seen
= 1;
96 in
= fopen(filename
, "r");
97 while ((ch
= fgetc(in
)) != EOF
) {
108 printf("\n\\ No newline at end of file\n");
111 static void emit_rewrite_diff(const char *name_a
,
113 struct diff_tempfile
*temp
)
115 /* Use temp[i].name as input, name_a and name_b as labels */
117 lc_a
= count_lines(temp
[0].name
);
118 lc_b
= count_lines(temp
[1].name
);
119 printf("--- %s\n+++ %s\n@@ -", name_a
, name_b
);
120 print_line_count(lc_a
);
122 print_line_count(lc_b
);
125 copy_file('-', temp
[0].name
);
127 copy_file('+', temp
[1].name
);
130 static void builtin_diff(const char *name_a
,
132 struct diff_tempfile
*temp
,
133 const char *xfrm_msg
,
134 int complete_rewrite
)
136 int i
, next_at
, cmd_size
;
137 const char *const diff_cmd
= "diff -L%s%s -L%s%s";
138 const char *const diff_arg
= "%s %s||:"; /* "||:" is to return 0 */
139 const char *input_name_sq
[2];
140 const char *path0
[2];
141 const char *path1
[2];
142 const char *name_sq
[2];
145 name_sq
[0] = sq_quote(name_a
);
146 name_sq
[1] = sq_quote(name_b
);
148 /* diff_cmd and diff_arg have 6 %s in total which makes
149 * the sum of these strings 12 bytes larger than required.
150 * we use 2 spaces around diff-opts, and we need to count
151 * terminating NUL, so we subtract 9 here.
153 cmd_size
= (strlen(diff_cmd
) + strlen(diff_opts
) +
154 strlen(diff_arg
) - 9);
155 for (i
= 0; i
< 2; i
++) {
156 input_name_sq
[i
] = sq_quote(temp
[i
].name
);
157 if (!strcmp(temp
[i
].name
, "/dev/null")) {
158 path0
[i
] = "/dev/null";
161 path0
[i
] = i
? "b/" : "a/";
162 path1
[i
] = name_sq
[i
];
164 cmd_size
+= (strlen(path0
[i
]) + strlen(path1
[i
]) +
165 strlen(input_name_sq
[i
]));
168 cmd
= xmalloc(cmd_size
);
171 next_at
+= snprintf(cmd
+next_at
, cmd_size
-next_at
,
173 path0
[0], path1
[0], path0
[1], path1
[1]);
174 next_at
+= snprintf(cmd
+next_at
, cmd_size
-next_at
,
176 next_at
+= snprintf(cmd
+next_at
, cmd_size
-next_at
,
177 diff_arg
, input_name_sq
[0], input_name_sq
[1]);
179 printf("diff --git a/%s b/%s\n", name_a
, name_b
);
181 printf("new file mode %s\n", temp
[1].mode
);
182 if (xfrm_msg
&& xfrm_msg
[0])
185 else if (!path1
[1][0]) {
186 printf("deleted file mode %s\n", temp
[0].mode
);
187 if (xfrm_msg
&& xfrm_msg
[0])
191 if (strcmp(temp
[0].mode
, temp
[1].mode
)) {
192 printf("old mode %s\n", temp
[0].mode
);
193 printf("new mode %s\n", temp
[1].mode
);
195 if (xfrm_msg
&& xfrm_msg
[0])
197 if (strncmp(temp
[0].mode
, temp
[1].mode
, 3))
198 /* we do not run diff between different kind
202 if (complete_rewrite
) {
204 emit_rewrite_diff(name_a
, name_b
, temp
);
209 execlp("/bin/sh","sh", "-c", cmd
, NULL
);
212 struct diff_filespec
*alloc_filespec(const char *path
)
214 int namelen
= strlen(path
);
215 struct diff_filespec
*spec
= xmalloc(sizeof(*spec
) + namelen
+ 1);
217 memset(spec
, 0, sizeof(*spec
));
218 spec
->path
= (char *)(spec
+ 1);
219 memcpy(spec
->path
, path
, namelen
+1);
223 void fill_filespec(struct diff_filespec
*spec
, const unsigned char *sha1
,
227 spec
->mode
= DIFF_FILE_CANON_MODE(mode
);
228 memcpy(spec
->sha1
, sha1
, 20);
229 spec
->sha1_valid
= !!memcmp(sha1
, null_sha1
, 20);
234 * Given a name and sha1 pair, if the dircache tells us the file in
235 * the work tree has that object contents, return true, so that
236 * prepare_temp_file() does not have to inflate and extract.
238 static int work_tree_matches(const char *name
, const unsigned char *sha1
)
240 struct cache_entry
*ce
;
244 /* We do not read the cache ourselves here, because the
245 * benchmark with my previous version that always reads cache
246 * shows that it makes things worse for diff-tree comparing
247 * two linux-2.6 kernel trees in an already checked out work
248 * tree. This is because most diff-tree comparisons deal with
249 * only a small number of files, while reading the cache is
250 * expensive for a large project, and its cost outweighs the
251 * savings we get by not inflating the object to a temporary
252 * file. Practically, this code only helps when we are used
253 * by diff-cache --cached, which does read the cache before
260 pos
= cache_name_pos(name
, len
);
263 ce
= active_cache
[pos
];
264 if ((lstat(name
, &st
) < 0) ||
265 !S_ISREG(st
.st_mode
) || /* careful! */
266 ce_match_stat(ce
, &st
) ||
267 memcmp(sha1
, ce
->sha1
, 20))
269 /* we return 1 only when we can stat, it is a regular file,
270 * stat information matches, and sha1 recorded in the cache
271 * matches. I.e. we know the file in the work tree really is
272 * the same as the <name, sha1> pair.
277 static struct sha1_size_cache
{
278 unsigned char sha1
[20];
281 static int sha1_size_cache_nr
, sha1_size_cache_alloc
;
283 static struct sha1_size_cache
*locate_size_cache(unsigned char *sha1
,
288 struct sha1_size_cache
*e
;
291 last
= sha1_size_cache_nr
;
292 while (last
> first
) {
293 int cmp
, next
= (last
+ first
) >> 1;
294 e
= sha1_size_cache
[next
];
295 cmp
= memcmp(e
->sha1
, sha1
, 20);
307 /* insert to make it at "first" */
308 if (sha1_size_cache_alloc
<= sha1_size_cache_nr
) {
309 sha1_size_cache_alloc
= alloc_nr(sha1_size_cache_alloc
);
310 sha1_size_cache
= xrealloc(sha1_size_cache
,
311 sha1_size_cache_alloc
*
312 sizeof(*sha1_size_cache
));
314 sha1_size_cache_nr
++;
315 if (first
< sha1_size_cache_nr
)
316 memmove(sha1_size_cache
+ first
+ 1, sha1_size_cache
+ first
,
317 (sha1_size_cache_nr
- first
- 1) *
318 sizeof(*sha1_size_cache
));
319 e
= xmalloc(sizeof(struct sha1_size_cache
));
320 sha1_size_cache
[first
] = e
;
321 memcpy(e
->sha1
, sha1
, 20);
327 * While doing rename detection and pickaxe operation, we may need to
328 * grab the data for the blob (or file) for our own in-core comparison.
329 * diff_filespec has data and size fields for this purpose.
331 int diff_populate_filespec(struct diff_filespec
*s
, int size_only
)
334 if (!DIFF_FILE_VALID(s
))
335 die("internal error: asking to populate invalid file.");
336 if (S_ISDIR(s
->mode
))
344 if (!s
->sha1_valid
||
345 work_tree_matches(s
->path
, s
->sha1
)) {
348 if (lstat(s
->path
, &st
) < 0) {
349 if (errno
== ENOENT
) {
358 s
->size
= st
.st_size
;
363 if (S_ISLNK(st
.st_mode
)) {
365 s
->data
= xmalloc(s
->size
);
367 ret
= readlink(s
->path
, s
->data
, s
->size
);
374 fd
= open(s
->path
, O_RDONLY
);
377 s
->data
= mmap(NULL
, s
->size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
379 if (s
->data
== MAP_FAILED
)
381 s
->should_munmap
= 1;
385 struct sha1_size_cache
*e
;
388 e
= locate_size_cache(s
->sha1
, 1, 0);
393 if (!sha1_object_info(s
->sha1
, type
, &s
->size
))
394 locate_size_cache(s
->sha1
, 0, s
->size
);
397 s
->data
= read_sha1_file(s
->sha1
, type
, &s
->size
);
404 void diff_free_filespec_data(struct diff_filespec
*s
)
408 else if (s
->should_munmap
)
409 munmap(s
->data
, s
->size
);
410 s
->should_free
= s
->should_munmap
= 0;
414 static void prep_temp_blob(struct diff_tempfile
*temp
,
422 fd
= git_mkstemp(temp
->tmp_path
, TEMPFILE_PATH_LEN
, ".diff_XXXXXX");
424 die("unable to create temp-file");
425 if (write(fd
, blob
, size
) != size
)
426 die("unable to write temp-file");
428 temp
->name
= temp
->tmp_path
;
429 strcpy(temp
->hex
, sha1_to_hex(sha1
));
431 sprintf(temp
->mode
, "%06o", mode
);
434 static void prepare_temp_file(const char *name
,
435 struct diff_tempfile
*temp
,
436 struct diff_filespec
*one
)
438 if (!DIFF_FILE_VALID(one
)) {
440 /* A '-' entry produces this for file-2, and
441 * a '+' entry produces this for file-1.
443 temp
->name
= "/dev/null";
444 strcpy(temp
->hex
, ".");
445 strcpy(temp
->mode
, ".");
449 if (!one
->sha1_valid
||
450 work_tree_matches(name
, one
->sha1
)) {
452 if (lstat(name
, &st
) < 0) {
454 goto not_a_valid_file
;
455 die("stat(%s): %s", name
, strerror(errno
));
457 if (S_ISLNK(st
.st_mode
)) {
459 char *buf
, buf_
[1024];
460 buf
= ((sizeof(buf_
) < st
.st_size
) ?
461 xmalloc(st
.st_size
) : buf_
);
462 ret
= readlink(name
, buf
, st
.st_size
);
464 die("readlink(%s)", name
);
465 prep_temp_blob(temp
, buf
, st
.st_size
,
467 one
->sha1
: null_sha1
),
469 one
->mode
: S_IFLNK
));
472 /* we can borrow from the file in the work tree */
474 if (!one
->sha1_valid
)
475 strcpy(temp
->hex
, sha1_to_hex(null_sha1
));
477 strcpy(temp
->hex
, sha1_to_hex(one
->sha1
));
478 /* Even though we may sometimes borrow the
479 * contents from the work tree, we always want
480 * one->mode. mode is trustworthy even when
481 * !(one->sha1_valid), as long as
482 * DIFF_FILE_VALID(one).
484 sprintf(temp
->mode
, "%06o", one
->mode
);
489 if (diff_populate_filespec(one
, 0))
490 die("cannot read data blob for %s", one
->path
);
491 prep_temp_blob(temp
, one
->data
, one
->size
,
492 one
->sha1
, one
->mode
);
496 static void remove_tempfile(void)
500 for (i
= 0; i
< 2; i
++)
501 if (diff_temp
[i
].name
== diff_temp
[i
].tmp_path
) {
502 unlink(diff_temp
[i
].name
);
503 diff_temp
[i
].name
= NULL
;
507 static void remove_tempfile_on_signal(int signo
)
512 /* An external diff command takes:
514 * diff-cmd name infile1 infile1-sha1 infile1-mode \
515 * infile2 infile2-sha1 infile2-mode [ rename-to ]
518 static void run_external_diff(const char *pgm
,
521 struct diff_filespec
*one
,
522 struct diff_filespec
*two
,
523 const char *xfrm_msg
,
524 int complete_rewrite
)
526 struct diff_tempfile
*temp
= diff_temp
;
529 static int atexit_asked
= 0;
530 const char *othername
;
532 othername
= (other
? other
: name
);
534 prepare_temp_file(name
, &temp
[0], one
);
535 prepare_temp_file(othername
, &temp
[1], two
);
536 if (! atexit_asked
&&
537 (temp
[0].name
== temp
[0].tmp_path
||
538 temp
[1].name
== temp
[1].tmp_path
)) {
540 atexit(remove_tempfile
);
542 signal(SIGINT
, remove_tempfile_on_signal
);
548 die("unable to fork");
552 const char *exec_arg
[10];
553 const char **arg
= &exec_arg
[0];
556 *arg
++ = temp
[0].name
;
557 *arg
++ = temp
[0].hex
;
558 *arg
++ = temp
[0].mode
;
559 *arg
++ = temp
[1].name
;
560 *arg
++ = temp
[1].hex
;
561 *arg
++ = temp
[1].mode
;
567 execvp(pgm
, (char *const*) exec_arg
);
570 execlp(pgm
, pgm
, name
, NULL
);
573 * otherwise we use the built-in one.
576 builtin_diff(name
, othername
, temp
, xfrm_msg
,
579 printf("* Unmerged path %s\n", name
);
582 if (waitpid(pid
, &status
, 0) < 0 ||
583 !WIFEXITED(status
) || WEXITSTATUS(status
)) {
584 /* Earlier we did not check the exit status because
585 * diff exits non-zero if files are different, and
586 * we are not interested in knowing that. It was a
587 * mistake which made it harder to quit a diff-*
588 * session that uses the git-apply-patch-script as
589 * the GIT_EXTERNAL_DIFF. A custom GIT_EXTERNAL_DIFF
590 * should also exit non-zero only when it wants to
591 * abort the entire diff-* session.
594 fprintf(stderr
, "external diff died, stopping at %s.\n", name
);
600 static void run_diff(struct diff_filepair
*p
)
602 const char *pgm
= external_diff();
603 char msg_
[PATH_MAX
*2+200], *xfrm_msg
;
604 struct diff_filespec
*one
;
605 struct diff_filespec
*two
;
608 int complete_rewrite
= 0;
610 if (DIFF_PAIR_UNMERGED(p
)) {
612 run_external_diff(pgm
, p
->one
->path
, NULL
, NULL
, NULL
, NULL
,
618 other
= (strcmp(name
, p
->two
->path
) ? p
->two
->path
: NULL
);
619 one
= p
->one
; two
= p
->two
;
621 case DIFF_STATUS_COPIED
:
623 "similarity index %d%%\n"
626 (int)(0.5 + p
->score
* 100.0/MAX_SCORE
),
630 case DIFF_STATUS_RENAMED
:
632 "similarity index %d%%\n"
635 (int)(0.5 + p
->score
* 100.0/MAX_SCORE
),
639 case DIFF_STATUS_MODIFIED
:
642 "dissimilarity index %d%%",
643 (int)(0.5 + p
->score
* 100.0/MAX_SCORE
));
645 complete_rewrite
= 1;
654 DIFF_FILE_VALID(one
) && DIFF_FILE_VALID(two
) &&
655 (S_IFMT
& one
->mode
) != (S_IFMT
& two
->mode
)) {
656 /* a filepair that changes between file and symlink
657 * needs to be split into deletion and creation.
659 struct diff_filespec
*null
= alloc_filespec(two
->path
);
660 run_external_diff(NULL
, name
, other
, one
, null
, xfrm_msg
, 0);
662 null
= alloc_filespec(one
->path
);
663 run_external_diff(NULL
, name
, other
, null
, two
, xfrm_msg
, 0);
667 run_external_diff(pgm
, name
, other
, one
, two
, xfrm_msg
,
671 void diff_setup(struct diff_options
*options
)
673 memset(options
, 0, sizeof(*options
));
674 options
->output_format
= DIFF_FORMAT_RAW
;
675 options
->line_termination
= '\n';
676 options
->break_opt
= -1;
677 options
->rename_limit
= -1;
680 int diff_setup_done(struct diff_options
*options
)
682 if ((options
->find_copies_harder
|| 0 <= options
->rename_limit
) &&
683 options
->detect_rename
!= DIFF_DETECT_COPY
)
685 if (options
->setup
& DIFF_SETUP_USE_CACHE
) {
687 /* read-cache does not die even when it fails
688 * so it is safe for us to do this here. Also
689 * it does not smudge active_cache or active_nr
690 * when it fails, so we do not have to worry about
691 * cleaning it up oufselves either.
695 if (options
->setup
& DIFF_SETUP_USE_SIZE_CACHE
)
701 int diff_opt_parse(struct diff_options
*options
, const char **av
, int ac
)
703 const char *arg
= av
[0];
704 if (!strcmp(arg
, "-p") || !strcmp(arg
, "-u"))
705 options
->output_format
= DIFF_FORMAT_PATCH
;
706 else if (!strcmp(arg
, "-z"))
707 options
->line_termination
= 0;
708 else if (!strncmp(arg
, "-l", 2))
709 options
->rename_limit
= strtoul(arg
+2, NULL
, 10);
710 else if (!strcmp(arg
, "--name-only"))
711 options
->output_format
= DIFF_FORMAT_NAME
;
712 else if (!strcmp(arg
, "-R"))
713 options
->reverse_diff
= 1;
714 else if (!strncmp(arg
, "-S", 2))
715 options
->pickaxe
= arg
+ 2;
716 else if (!strcmp(arg
, "-s"))
717 options
->output_format
= DIFF_FORMAT_NO_OUTPUT
;
718 else if (!strncmp(arg
, "-O", 2))
719 options
->orderfile
= arg
+ 2;
720 else if (!strncmp(arg
, "--diff-filter=", 14))
721 options
->filter
= arg
+ 14;
722 else if (!strcmp(arg
, "--pickaxe-all"))
723 options
->pickaxe_opts
= DIFF_PICKAXE_ALL
;
724 else if (!strncmp(arg
, "-B", 2)) {
725 if ((options
->break_opt
=
726 diff_scoreopt_parse(arg
)) == -1)
729 else if (!strncmp(arg
, "-M", 2)) {
730 if ((options
->rename_score
=
731 diff_scoreopt_parse(arg
)) == -1)
733 options
->detect_rename
= DIFF_DETECT_RENAME
;
735 else if (!strncmp(arg
, "-C", 2)) {
736 if ((options
->rename_score
=
737 diff_scoreopt_parse(arg
)) == -1)
739 options
->detect_rename
= DIFF_DETECT_COPY
;
741 else if (!strcmp(arg
, "--find-copies-harder"))
742 options
->find_copies_harder
= 1;
748 static int parse_num(const char **cp_p
)
750 int num
, scale
, ch
, cnt
;
751 const char *cp
= *cp_p
;
755 while ('0' <= (ch
= *cp
) && ch
<= '9') {
757 /* We simply ignore more than 5 digits precision. */
759 num
= num
* 10 + ch
- '0';
765 /* user says num divided by scale and we say internally that
766 * is MAX_SCORE * num / scale.
768 return (MAX_SCORE
* num
/ scale
);
771 int diff_scoreopt_parse(const char *opt
)
778 if (cmd
!= 'M' && cmd
!= 'C' && cmd
!= 'B')
779 return -1; /* that is not a -M, -C nor -B option */
781 opt1
= parse_num(&opt
);
787 else if (*opt
!= '/')
788 return -1; /* we expect -B80/99 or -B80 */
791 opt2
= parse_num(&opt
);
796 return opt1
| (opt2
<< 16);
799 struct diff_queue_struct diff_queued_diff
;
801 void diff_q(struct diff_queue_struct
*queue
, struct diff_filepair
*dp
)
803 if (queue
->alloc
<= queue
->nr
) {
804 queue
->alloc
= alloc_nr(queue
->alloc
);
805 queue
->queue
= xrealloc(queue
->queue
,
806 sizeof(dp
) * queue
->alloc
);
808 queue
->queue
[queue
->nr
++] = dp
;
811 struct diff_filepair
*diff_queue(struct diff_queue_struct
*queue
,
812 struct diff_filespec
*one
,
813 struct diff_filespec
*two
)
815 struct diff_filepair
*dp
= xmalloc(sizeof(*dp
));
820 dp
->source_stays
= 0;
827 void diff_free_filepair(struct diff_filepair
*p
)
829 diff_free_filespec_data(p
->one
);
830 diff_free_filespec_data(p
->two
);
836 static void diff_flush_raw(struct diff_filepair
*p
,
837 int line_termination
,
838 int inter_name_termination
)
843 if (line_termination
) {
844 const char *const err
=
845 "path %s cannot be expressed without -z";
846 if (strchr(p
->one
->path
, line_termination
) ||
847 strchr(p
->one
->path
, inter_name_termination
))
848 die(err
, p
->one
->path
);
849 if (strchr(p
->two
->path
, line_termination
) ||
850 strchr(p
->two
->path
, inter_name_termination
))
851 die(err
, p
->two
->path
);
855 sprintf(status
, "%c%03d", p
->status
,
856 (int)(0.5 + p
->score
* 100.0/MAX_SCORE
));
858 status
[0] = p
->status
;
862 case DIFF_STATUS_COPIED
:
863 case DIFF_STATUS_RENAMED
:
866 case DIFF_STATUS_ADDED
:
867 case DIFF_STATUS_DELETED
:
874 printf(":%06o %06o %s ",
875 p
->one
->mode
, p
->two
->mode
, sha1_to_hex(p
->one
->sha1
));
877 sha1_to_hex(p
->two
->sha1
),
879 inter_name_termination
,
882 printf("%c%s", inter_name_termination
, p
->two
->path
);
883 putchar(line_termination
);
886 static void diff_flush_name(struct diff_filepair
*p
,
887 int line_termination
)
889 printf("%s%c", p
->two
->path
, line_termination
);
892 int diff_unmodified_pair(struct diff_filepair
*p
)
894 /* This function is written stricter than necessary to support
895 * the currently implemented transformers, but the idea is to
896 * let transformers to produce diff_filepairs any way they want,
897 * and filter and clean them up here before producing the output.
899 struct diff_filespec
*one
, *two
;
901 if (DIFF_PAIR_UNMERGED(p
))
902 return 0; /* unmerged is interesting */
907 /* deletion, addition, mode or type change
908 * and rename are all interesting.
910 if (DIFF_FILE_VALID(one
) != DIFF_FILE_VALID(two
) ||
911 DIFF_PAIR_MODE_CHANGED(p
) ||
912 strcmp(one
->path
, two
->path
))
915 /* both are valid and point at the same path. that is, we are
916 * dealing with a change.
918 if (one
->sha1_valid
&& two
->sha1_valid
&&
919 !memcmp(one
->sha1
, two
->sha1
, sizeof(one
->sha1
)))
920 return 1; /* no change */
921 if (!one
->sha1_valid
&& !two
->sha1_valid
)
922 return 1; /* both look at the same file on the filesystem. */
926 static void diff_flush_patch(struct diff_filepair
*p
)
928 if (diff_unmodified_pair(p
))
931 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
932 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
933 return; /* no tree diffs in patch format */
938 int diff_queue_is_empty(void)
940 struct diff_queue_struct
*q
= &diff_queued_diff
;
942 for (i
= 0; i
< q
->nr
; i
++)
943 if (!diff_unmodified_pair(q
->queue
[i
]))
949 void diff_debug_filespec(struct diff_filespec
*s
, int x
, const char *one
)
951 fprintf(stderr
, "queue[%d] %s (%s) %s %06o %s\n",
954 DIFF_FILE_VALID(s
) ? "valid" : "invalid",
956 s
->sha1_valid
? sha1_to_hex(s
->sha1
) : "");
957 fprintf(stderr
, "queue[%d] %s size %lu flags %d\n",
959 s
->size
, s
->xfrm_flags
);
962 void diff_debug_filepair(const struct diff_filepair
*p
, int i
)
964 diff_debug_filespec(p
->one
, i
, "one");
965 diff_debug_filespec(p
->two
, i
, "two");
966 fprintf(stderr
, "score %d, status %c stays %d broken %d\n",
967 p
->score
, p
->status
? p
->status
: '?',
968 p
->source_stays
, p
->broken_pair
);
971 void diff_debug_queue(const char *msg
, struct diff_queue_struct
*q
)
975 fprintf(stderr
, "%s\n", msg
);
976 fprintf(stderr
, "q->nr = %d\n", q
->nr
);
977 for (i
= 0; i
< q
->nr
; i
++) {
978 struct diff_filepair
*p
= q
->queue
[i
];
979 diff_debug_filepair(p
, i
);
984 static void diff_resolve_rename_copy(void)
987 struct diff_filepair
*p
, *pp
;
988 struct diff_queue_struct
*q
= &diff_queued_diff
;
990 diff_debug_queue("resolve-rename-copy", q
);
992 for (i
= 0; i
< q
->nr
; i
++) {
994 p
->status
= 0; /* undecided */
995 if (DIFF_PAIR_UNMERGED(p
))
996 p
->status
= DIFF_STATUS_UNMERGED
;
997 else if (!DIFF_FILE_VALID(p
->one
))
998 p
->status
= DIFF_STATUS_ADDED
;
999 else if (!DIFF_FILE_VALID(p
->two
))
1000 p
->status
= DIFF_STATUS_DELETED
;
1001 else if (DIFF_PAIR_TYPE_CHANGED(p
))
1002 p
->status
= DIFF_STATUS_TYPE_CHANGED
;
1004 /* from this point on, we are dealing with a pair
1005 * whose both sides are valid and of the same type, i.e.
1006 * either in-place edit or rename/copy edit.
1008 else if (DIFF_PAIR_RENAME(p
)) {
1009 if (p
->source_stays
) {
1010 p
->status
= DIFF_STATUS_COPIED
;
1013 /* See if there is some other filepair that
1014 * copies from the same source as us. If so
1015 * we are a copy. Otherwise we are either a
1016 * copy if the path stays, or a rename if it
1017 * does not, but we already handled "stays" case.
1019 for (j
= i
+ 1; j
< q
->nr
; j
++) {
1021 if (strcmp(pp
->one
->path
, p
->one
->path
))
1022 continue; /* not us */
1023 if (!DIFF_PAIR_RENAME(pp
))
1024 continue; /* not a rename/copy */
1025 /* pp is a rename/copy from the same source */
1026 p
->status
= DIFF_STATUS_COPIED
;
1030 p
->status
= DIFF_STATUS_RENAMED
;
1032 else if (memcmp(p
->one
->sha1
, p
->two
->sha1
, 20) ||
1033 p
->one
->mode
!= p
->two
->mode
)
1034 p
->status
= DIFF_STATUS_MODIFIED
;
1036 /* This is a "no-change" entry and should not
1037 * happen anymore, but prepare for broken callers.
1039 error("feeding unmodified %s to diffcore",
1041 p
->status
= DIFF_STATUS_UNKNOWN
;
1044 diff_debug_queue("resolve-rename-copy done", q
);
1047 void diff_flush(struct diff_options
*options
)
1049 struct diff_queue_struct
*q
= &diff_queued_diff
;
1051 int inter_name_termination
= '\t';
1052 int diff_output_format
= options
->output_format
;
1053 int line_termination
= options
->line_termination
;
1055 if (!line_termination
)
1056 inter_name_termination
= 0;
1058 for (i
= 0; i
< q
->nr
; i
++) {
1059 struct diff_filepair
*p
= q
->queue
[i
];
1060 if ((diff_output_format
== DIFF_FORMAT_NO_OUTPUT
) ||
1061 (p
->status
== DIFF_STATUS_UNKNOWN
))
1064 die("internal error in diff-resolve-rename-copy");
1065 switch (diff_output_format
) {
1066 case DIFF_FORMAT_PATCH
:
1067 diff_flush_patch(p
);
1069 case DIFF_FORMAT_RAW
:
1070 diff_flush_raw(p
, line_termination
,
1071 inter_name_termination
);
1073 case DIFF_FORMAT_NAME
:
1074 diff_flush_name(p
, line_termination
);
1077 diff_free_filepair(q
->queue
[i
]);
1081 q
->nr
= q
->alloc
= 0;
1084 static void diffcore_apply_filter(const char *filter
)
1087 struct diff_queue_struct
*q
= &diff_queued_diff
;
1088 struct diff_queue_struct outq
;
1090 outq
.nr
= outq
.alloc
= 0;
1095 if (strchr(filter
, DIFF_STATUS_FILTER_AON
)) {
1097 for (i
= found
= 0; !found
&& i
< q
->nr
; i
++) {
1098 struct diff_filepair
*p
= q
->queue
[i
];
1099 if (((p
->status
== DIFF_STATUS_MODIFIED
) &&
1101 strchr(filter
, DIFF_STATUS_FILTER_BROKEN
)) ||
1103 strchr(filter
, DIFF_STATUS_MODIFIED
)))) ||
1104 ((p
->status
!= DIFF_STATUS_MODIFIED
) &&
1105 strchr(filter
, p
->status
)))
1111 /* otherwise we will clear the whole queue
1112 * by copying the empty outq at the end of this
1113 * function, but first clear the current entries
1116 for (i
= 0; i
< q
->nr
; i
++)
1117 diff_free_filepair(q
->queue
[i
]);
1120 /* Only the matching ones */
1121 for (i
= 0; i
< q
->nr
; i
++) {
1122 struct diff_filepair
*p
= q
->queue
[i
];
1124 if (((p
->status
== DIFF_STATUS_MODIFIED
) &&
1126 strchr(filter
, DIFF_STATUS_FILTER_BROKEN
)) ||
1128 strchr(filter
, DIFF_STATUS_MODIFIED
)))) ||
1129 ((p
->status
!= DIFF_STATUS_MODIFIED
) &&
1130 strchr(filter
, p
->status
)))
1133 diff_free_filepair(p
);
1140 void diffcore_std(struct diff_options
*options
)
1142 if (options
->paths
&& options
->paths
[0])
1143 diffcore_pathspec(options
->paths
);
1144 if (options
->break_opt
!= -1)
1145 diffcore_break(options
->break_opt
);
1146 if (options
->detect_rename
)
1147 diffcore_rename(options
);
1148 if (options
->break_opt
!= -1)
1149 diffcore_merge_broken();
1150 if (options
->pickaxe
)
1151 diffcore_pickaxe(options
->pickaxe
, options
->pickaxe_opts
);
1152 if (options
->orderfile
)
1153 diffcore_order(options
->orderfile
);
1154 diff_resolve_rename_copy();
1155 diffcore_apply_filter(options
->filter
);
1159 void diffcore_std_no_resolve(struct diff_options
*options
)
1161 if (options
->pickaxe
)
1162 diffcore_pickaxe(options
->pickaxe
, options
->pickaxe_opts
);
1163 if (options
->orderfile
)
1164 diffcore_order(options
->orderfile
);
1165 diffcore_apply_filter(options
->filter
);
1168 void diff_addremove(struct diff_options
*options
,
1169 int addremove
, unsigned mode
,
1170 const unsigned char *sha1
,
1171 const char *base
, const char *path
)
1173 char concatpath
[PATH_MAX
];
1174 struct diff_filespec
*one
, *two
;
1176 /* This may look odd, but it is a preparation for
1177 * feeding "there are unchanged files which should
1178 * not produce diffs, but when you are doing copy
1179 * detection you would need them, so here they are"
1180 * entries to the diff-core. They will be prefixed
1181 * with something like '=' or '*' (I haven't decided
1182 * which but should not make any difference).
1183 * Feeding the same new and old to diff_change()
1184 * also has the same effect.
1185 * Before the final output happens, they are pruned after
1186 * merged into rename/copy pairs as appropriate.
1188 if (options
->reverse_diff
)
1189 addremove
= (addremove
== '+' ? '-' :
1190 addremove
== '-' ? '+' : addremove
);
1192 if (!path
) path
= "";
1193 sprintf(concatpath
, "%s%s", base
, path
);
1194 one
= alloc_filespec(concatpath
);
1195 two
= alloc_filespec(concatpath
);
1197 if (addremove
!= '+')
1198 fill_filespec(one
, sha1
, mode
);
1199 if (addremove
!= '-')
1200 fill_filespec(two
, sha1
, mode
);
1202 diff_queue(&diff_queued_diff
, one
, two
);
1205 void diff_change(struct diff_options
*options
,
1206 unsigned old_mode
, unsigned new_mode
,
1207 const unsigned char *old_sha1
,
1208 const unsigned char *new_sha1
,
1209 const char *base
, const char *path
)
1211 char concatpath
[PATH_MAX
];
1212 struct diff_filespec
*one
, *two
;
1214 if (options
->reverse_diff
) {
1216 const unsigned char *tmp_c
;
1217 tmp
= old_mode
; old_mode
= new_mode
; new_mode
= tmp
;
1218 tmp_c
= old_sha1
; old_sha1
= new_sha1
; new_sha1
= tmp_c
;
1220 if (!path
) path
= "";
1221 sprintf(concatpath
, "%s%s", base
, path
);
1222 one
= alloc_filespec(concatpath
);
1223 two
= alloc_filespec(concatpath
);
1224 fill_filespec(one
, old_sha1
, old_mode
);
1225 fill_filespec(two
, new_sha1
, new_mode
);
1227 diff_queue(&diff_queued_diff
, one
, two
);
1230 void diff_unmerge(struct diff_options
*options
,
1233 struct diff_filespec
*one
, *two
;
1234 one
= alloc_filespec(path
);
1235 two
= alloc_filespec(path
);
1236 diff_queue(&diff_queued_diff
, one
, two
);