2 * Copyright (C) 2005 Junio C Hamano
12 static const char *diff_opts
= "-pu";
14 static int use_size_cache
;
16 static char *quote_one(const char *str
)
23 needlen
= quote_c_style(str
, NULL
, NULL
, 0);
26 xp
= xmalloc(needlen
+ 1);
27 quote_c_style(str
, xp
, NULL
, 0);
31 static char *quote_two(const char *one
, const char *two
)
33 int need_one
= quote_c_style(one
, NULL
, NULL
, 1);
34 int need_two
= quote_c_style(two
, NULL
, NULL
, 1);
37 if (need_one
+ need_two
) {
38 if (!need_one
) need_one
= strlen(one
);
39 if (!need_two
) need_one
= strlen(two
);
41 xp
= xmalloc(need_one
+ need_two
+ 3);
43 quote_c_style(one
, xp
+ 1, NULL
, 1);
44 quote_c_style(two
, xp
+ need_one
+ 1, NULL
, 1);
45 strcpy(xp
+ need_one
+ need_two
+ 1, "\"");
48 need_one
= strlen(one
);
49 need_two
= strlen(two
);
50 xp
= xmalloc(need_one
+ need_two
+ 1);
52 strcpy(xp
+ need_one
, two
);
56 static const char *external_diff(void)
58 static const char *external_diff_cmd
= NULL
;
59 static int done_preparing
= 0;
60 const char *env_diff_opts
;
63 return external_diff_cmd
;
66 * Default values above are meant to match the
67 * Linux kernel development style. Examples of
68 * alternative styles you can specify via environment
73 external_diff_cmd
= getenv("GIT_EXTERNAL_DIFF");
75 /* In case external diff fails... */
76 env_diff_opts
= getenv("GIT_DIFF_OPTS");
77 if (env_diff_opts
) diff_opts
= env_diff_opts
;
80 return external_diff_cmd
;
83 #define TEMPFILE_PATH_LEN 50
85 static struct diff_tempfile
{
86 const char *name
; /* filename external diff should read from */
89 char tmp_path
[TEMPFILE_PATH_LEN
];
92 static int count_lines(const char *filename
)
95 int count
, ch
, completely_empty
= 1, nl_just_seen
= 0;
96 in
= fopen(filename
, "r");
98 while ((ch
= fgetc(in
)) != EOF
)
102 completely_empty
= 0;
106 completely_empty
= 0;
109 if (completely_empty
)
112 count
++; /* no trailing newline */
116 static void print_line_count(int count
)
126 printf("1,%d", count
);
131 static void copy_file(int prefix
, const char *filename
)
134 int ch
, nl_just_seen
= 1;
135 in
= fopen(filename
, "r");
136 while ((ch
= fgetc(in
)) != EOF
) {
147 printf("\n\\ No newline at end of file\n");
150 static void emit_rewrite_diff(const char *name_a
,
152 struct diff_tempfile
*temp
)
154 /* Use temp[i].name as input, name_a and name_b as labels */
156 lc_a
= count_lines(temp
[0].name
);
157 lc_b
= count_lines(temp
[1].name
);
158 printf("--- %s\n+++ %s\n@@ -", name_a
, name_b
);
159 print_line_count(lc_a
);
161 print_line_count(lc_b
);
164 copy_file('-', temp
[0].name
);
166 copy_file('+', temp
[1].name
);
169 static void builtin_diff(const char *name_a
,
171 struct diff_tempfile
*temp
,
172 const char *xfrm_msg
,
173 int complete_rewrite
)
175 int i
, next_at
, cmd_size
;
176 const char *const diff_cmd
= "diff -L%s -L%s";
177 const char *const diff_arg
= "-- %s %s||:"; /* "||:" is to return 0 */
178 const char *input_name_sq
[2];
179 const char *label_path
[2];
182 /* diff_cmd and diff_arg have 4 %s in total which makes
183 * the sum of these strings 8 bytes larger than required.
184 * we use 2 spaces around diff-opts, and we need to count
185 * terminating NUL; we used to subtract 5 here, but we do not
186 * care about small leaks in this subprocess that is about
187 * to exec "diff" anymore.
189 cmd_size
= (strlen(diff_cmd
) + strlen(diff_opts
) + strlen(diff_arg
)
192 for (i
= 0; i
< 2; i
++) {
193 input_name_sq
[i
] = sq_quote(temp
[i
].name
);
194 if (!strcmp(temp
[i
].name
, "/dev/null"))
195 label_path
[i
] = "/dev/null";
197 label_path
[i
] = sq_quote(quote_two("a/", name_a
));
199 label_path
[i
] = sq_quote(quote_two("b/", name_b
));
200 cmd_size
+= (strlen(label_path
[i
]) + strlen(input_name_sq
[i
]));
203 cmd
= xmalloc(cmd_size
);
206 next_at
+= snprintf(cmd
+next_at
, cmd_size
-next_at
,
207 diff_cmd
, label_path
[0], label_path
[1]);
208 next_at
+= snprintf(cmd
+next_at
, cmd_size
-next_at
,
210 next_at
+= snprintf(cmd
+next_at
, cmd_size
-next_at
,
211 diff_arg
, input_name_sq
[0], input_name_sq
[1]);
213 printf("diff --git %s %s\n",
214 quote_two("a/", name_a
), quote_two("b/", name_b
));
215 if (label_path
[0][0] == '/') {
217 printf("new file mode %s\n", temp
[1].mode
);
218 if (xfrm_msg
&& xfrm_msg
[0])
221 else if (label_path
[1][0] == '/') {
222 printf("deleted file mode %s\n", temp
[0].mode
);
223 if (xfrm_msg
&& xfrm_msg
[0])
227 if (strcmp(temp
[0].mode
, temp
[1].mode
)) {
228 printf("old mode %s\n", temp
[0].mode
);
229 printf("new mode %s\n", temp
[1].mode
);
231 if (xfrm_msg
&& xfrm_msg
[0])
233 if (strncmp(temp
[0].mode
, temp
[1].mode
, 3))
234 /* we do not run diff between different kind
238 if (complete_rewrite
) {
240 emit_rewrite_diff(name_a
, name_b
, temp
);
245 execlp("/bin/sh","sh", "-c", cmd
, NULL
);
248 struct diff_filespec
*alloc_filespec(const char *path
)
250 int namelen
= strlen(path
);
251 struct diff_filespec
*spec
= xmalloc(sizeof(*spec
) + namelen
+ 1);
253 memset(spec
, 0, sizeof(*spec
));
254 spec
->path
= (char *)(spec
+ 1);
255 memcpy(spec
->path
, path
, namelen
+1);
259 void fill_filespec(struct diff_filespec
*spec
, const unsigned char *sha1
,
263 spec
->mode
= DIFF_FILE_CANON_MODE(mode
);
264 memcpy(spec
->sha1
, sha1
, 20);
265 spec
->sha1_valid
= !!memcmp(sha1
, null_sha1
, 20);
270 * Given a name and sha1 pair, if the dircache tells us the file in
271 * the work tree has that object contents, return true, so that
272 * prepare_temp_file() does not have to inflate and extract.
274 static int work_tree_matches(const char *name
, const unsigned char *sha1
)
276 struct cache_entry
*ce
;
280 /* We do not read the cache ourselves here, because the
281 * benchmark with my previous version that always reads cache
282 * shows that it makes things worse for diff-tree comparing
283 * two linux-2.6 kernel trees in an already checked out work
284 * tree. This is because most diff-tree comparisons deal with
285 * only a small number of files, while reading the cache is
286 * expensive for a large project, and its cost outweighs the
287 * savings we get by not inflating the object to a temporary
288 * file. Practically, this code only helps when we are used
289 * by diff-cache --cached, which does read the cache before
296 pos
= cache_name_pos(name
, len
);
299 ce
= active_cache
[pos
];
300 if ((lstat(name
, &st
) < 0) ||
301 !S_ISREG(st
.st_mode
) || /* careful! */
302 ce_match_stat(ce
, &st
) ||
303 memcmp(sha1
, ce
->sha1
, 20))
305 /* we return 1 only when we can stat, it is a regular file,
306 * stat information matches, and sha1 recorded in the cache
307 * matches. I.e. we know the file in the work tree really is
308 * the same as the <name, sha1> pair.
313 static struct sha1_size_cache
{
314 unsigned char sha1
[20];
317 static int sha1_size_cache_nr
, sha1_size_cache_alloc
;
319 static struct sha1_size_cache
*locate_size_cache(unsigned char *sha1
,
324 struct sha1_size_cache
*e
;
327 last
= sha1_size_cache_nr
;
328 while (last
> first
) {
329 int cmp
, next
= (last
+ first
) >> 1;
330 e
= sha1_size_cache
[next
];
331 cmp
= memcmp(e
->sha1
, sha1
, 20);
343 /* insert to make it at "first" */
344 if (sha1_size_cache_alloc
<= sha1_size_cache_nr
) {
345 sha1_size_cache_alloc
= alloc_nr(sha1_size_cache_alloc
);
346 sha1_size_cache
= xrealloc(sha1_size_cache
,
347 sha1_size_cache_alloc
*
348 sizeof(*sha1_size_cache
));
350 sha1_size_cache_nr
++;
351 if (first
< sha1_size_cache_nr
)
352 memmove(sha1_size_cache
+ first
+ 1, sha1_size_cache
+ first
,
353 (sha1_size_cache_nr
- first
- 1) *
354 sizeof(*sha1_size_cache
));
355 e
= xmalloc(sizeof(struct sha1_size_cache
));
356 sha1_size_cache
[first
] = e
;
357 memcpy(e
->sha1
, sha1
, 20);
363 * While doing rename detection and pickaxe operation, we may need to
364 * grab the data for the blob (or file) for our own in-core comparison.
365 * diff_filespec has data and size fields for this purpose.
367 int diff_populate_filespec(struct diff_filespec
*s
, int size_only
)
370 if (!DIFF_FILE_VALID(s
))
371 die("internal error: asking to populate invalid file.");
372 if (S_ISDIR(s
->mode
))
380 if (!s
->sha1_valid
||
381 work_tree_matches(s
->path
, s
->sha1
)) {
384 if (lstat(s
->path
, &st
) < 0) {
385 if (errno
== ENOENT
) {
394 s
->size
= st
.st_size
;
399 if (S_ISLNK(st
.st_mode
)) {
401 s
->data
= xmalloc(s
->size
);
403 ret
= readlink(s
->path
, s
->data
, s
->size
);
410 fd
= open(s
->path
, O_RDONLY
);
413 s
->data
= mmap(NULL
, s
->size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
415 if (s
->data
== MAP_FAILED
)
417 s
->should_munmap
= 1;
421 struct sha1_size_cache
*e
;
424 e
= locate_size_cache(s
->sha1
, 1, 0);
429 if (!sha1_object_info(s
->sha1
, type
, &s
->size
))
430 locate_size_cache(s
->sha1
, 0, s
->size
);
433 s
->data
= read_sha1_file(s
->sha1
, type
, &s
->size
);
440 void diff_free_filespec_data(struct diff_filespec
*s
)
444 else if (s
->should_munmap
)
445 munmap(s
->data
, s
->size
);
446 s
->should_free
= s
->should_munmap
= 0;
450 static void prep_temp_blob(struct diff_tempfile
*temp
,
453 const unsigned char *sha1
,
458 fd
= git_mkstemp(temp
->tmp_path
, TEMPFILE_PATH_LEN
, ".diff_XXXXXX");
460 die("unable to create temp-file");
461 if (write(fd
, blob
, size
) != size
)
462 die("unable to write temp-file");
464 temp
->name
= temp
->tmp_path
;
465 strcpy(temp
->hex
, sha1_to_hex(sha1
));
467 sprintf(temp
->mode
, "%06o", mode
);
470 static void prepare_temp_file(const char *name
,
471 struct diff_tempfile
*temp
,
472 struct diff_filespec
*one
)
474 if (!DIFF_FILE_VALID(one
)) {
476 /* A '-' entry produces this for file-2, and
477 * a '+' entry produces this for file-1.
479 temp
->name
= "/dev/null";
480 strcpy(temp
->hex
, ".");
481 strcpy(temp
->mode
, ".");
485 if (!one
->sha1_valid
||
486 work_tree_matches(name
, one
->sha1
)) {
488 if (lstat(name
, &st
) < 0) {
490 goto not_a_valid_file
;
491 die("stat(%s): %s", name
, strerror(errno
));
493 if (S_ISLNK(st
.st_mode
)) {
495 char *buf
, buf_
[1024];
496 buf
= ((sizeof(buf_
) < st
.st_size
) ?
497 xmalloc(st
.st_size
) : buf_
);
498 ret
= readlink(name
, buf
, st
.st_size
);
500 die("readlink(%s)", name
);
501 prep_temp_blob(temp
, buf
, st
.st_size
,
503 one
->sha1
: null_sha1
),
505 one
->mode
: S_IFLNK
));
508 /* we can borrow from the file in the work tree */
510 if (!one
->sha1_valid
)
511 strcpy(temp
->hex
, sha1_to_hex(null_sha1
));
513 strcpy(temp
->hex
, sha1_to_hex(one
->sha1
));
514 /* Even though we may sometimes borrow the
515 * contents from the work tree, we always want
516 * one->mode. mode is trustworthy even when
517 * !(one->sha1_valid), as long as
518 * DIFF_FILE_VALID(one).
520 sprintf(temp
->mode
, "%06o", one
->mode
);
525 if (diff_populate_filespec(one
, 0))
526 die("cannot read data blob for %s", one
->path
);
527 prep_temp_blob(temp
, one
->data
, one
->size
,
528 one
->sha1
, one
->mode
);
532 static void remove_tempfile(void)
536 for (i
= 0; i
< 2; i
++)
537 if (diff_temp
[i
].name
== diff_temp
[i
].tmp_path
) {
538 unlink(diff_temp
[i
].name
);
539 diff_temp
[i
].name
= NULL
;
543 static void remove_tempfile_on_signal(int signo
)
548 /* An external diff command takes:
550 * diff-cmd name infile1 infile1-sha1 infile1-mode \
551 * infile2 infile2-sha1 infile2-mode [ rename-to ]
554 static void run_external_diff(const char *pgm
,
557 struct diff_filespec
*one
,
558 struct diff_filespec
*two
,
559 const char *xfrm_msg
,
560 int complete_rewrite
)
562 struct diff_tempfile
*temp
= diff_temp
;
565 static int atexit_asked
= 0;
566 const char *othername
;
568 othername
= (other
? other
: name
);
570 prepare_temp_file(name
, &temp
[0], one
);
571 prepare_temp_file(othername
, &temp
[1], two
);
572 if (! atexit_asked
&&
573 (temp
[0].name
== temp
[0].tmp_path
||
574 temp
[1].name
== temp
[1].tmp_path
)) {
576 atexit(remove_tempfile
);
578 signal(SIGINT
, remove_tempfile_on_signal
);
584 die("unable to fork");
588 const char *exec_arg
[10];
589 const char **arg
= &exec_arg
[0];
592 *arg
++ = temp
[0].name
;
593 *arg
++ = temp
[0].hex
;
594 *arg
++ = temp
[0].mode
;
595 *arg
++ = temp
[1].name
;
596 *arg
++ = temp
[1].hex
;
597 *arg
++ = temp
[1].mode
;
603 execvp(pgm
, (char *const*) exec_arg
);
606 execlp(pgm
, pgm
, name
, NULL
);
609 * otherwise we use the built-in one.
612 builtin_diff(name
, othername
, temp
, xfrm_msg
,
615 printf("* Unmerged path %s\n", name
);
618 if (waitpid(pid
, &status
, 0) < 0 ||
619 !WIFEXITED(status
) || WEXITSTATUS(status
)) {
620 /* Earlier we did not check the exit status because
621 * diff exits non-zero if files are different, and
622 * we are not interested in knowing that. It was a
623 * mistake which made it harder to quit a diff-*
624 * session that uses the git-apply-patch-script as
625 * the GIT_EXTERNAL_DIFF. A custom GIT_EXTERNAL_DIFF
626 * should also exit non-zero only when it wants to
627 * abort the entire diff-* session.
630 fprintf(stderr
, "external diff died, stopping at %s.\n", name
);
636 static void diff_fill_sha1_info(struct diff_filespec
*one
)
638 if (DIFF_FILE_VALID(one
)) {
639 if (!one
->sha1_valid
) {
641 if (stat(one
->path
, &st
) < 0)
642 die("stat %s", one
->path
);
643 if (index_path(one
->sha1
, one
->path
, &st
, 0))
644 die("cannot hash %s\n", one
->path
);
648 memset(one
->sha1
, 0, 20);
651 static void run_diff(struct diff_filepair
*p
)
653 const char *pgm
= external_diff();
654 char msg
[PATH_MAX
*2+300], *xfrm_msg
;
655 struct diff_filespec
*one
;
656 struct diff_filespec
*two
;
659 char *name_munged
, *other_munged
;
660 int complete_rewrite
= 0;
663 if (DIFF_PAIR_UNMERGED(p
)) {
665 run_external_diff(pgm
, p
->one
->path
, NULL
, NULL
, NULL
, NULL
,
671 other
= (strcmp(name
, p
->two
->path
) ? p
->two
->path
: NULL
);
672 name_munged
= quote_one(name
);
673 other_munged
= quote_one(other
);
674 one
= p
->one
; two
= p
->two
;
676 diff_fill_sha1_info(one
);
677 diff_fill_sha1_info(two
);
681 case DIFF_STATUS_COPIED
:
682 len
+= snprintf(msg
+ len
, sizeof(msg
) - len
,
683 "similarity index %d%%\n"
686 (int)(0.5 + p
->score
* 100.0/MAX_SCORE
),
687 name_munged
, other_munged
);
689 case DIFF_STATUS_RENAMED
:
690 len
+= snprintf(msg
+ len
, sizeof(msg
) - len
,
691 "similarity index %d%%\n"
694 (int)(0.5 + p
->score
* 100.0/MAX_SCORE
),
695 name_munged
, other_munged
);
697 case DIFF_STATUS_MODIFIED
:
699 len
+= snprintf(msg
+ len
, sizeof(msg
) - len
,
700 "dissimilarity index %d%%\n",
701 (int)(0.5 + p
->score
*
703 complete_rewrite
= 1;
712 if (memcmp(one
->sha1
, two
->sha1
, 20)) {
714 memcpy(one_sha1
, sha1_to_hex(one
->sha1
), 41);
716 len
+= snprintf(msg
+ len
, sizeof(msg
) - len
,
717 "index %.7s..%.7s", one_sha1
,
718 sha1_to_hex(two
->sha1
));
719 if (one
->mode
== two
->mode
)
720 len
+= snprintf(msg
+ len
, sizeof(msg
) - len
,
722 len
+= snprintf(msg
+ len
, sizeof(msg
) - len
, "\n");
727 xfrm_msg
= len
? msg
: NULL
;
730 DIFF_FILE_VALID(one
) && DIFF_FILE_VALID(two
) &&
731 (S_IFMT
& one
->mode
) != (S_IFMT
& two
->mode
)) {
732 /* a filepair that changes between file and symlink
733 * needs to be split into deletion and creation.
735 struct diff_filespec
*null
= alloc_filespec(two
->path
);
736 run_external_diff(NULL
, name
, other
, one
, null
, xfrm_msg
, 0);
738 null
= alloc_filespec(one
->path
);
739 run_external_diff(NULL
, name
, other
, null
, two
, xfrm_msg
, 0);
743 run_external_diff(pgm
, name
, other
, one
, two
, xfrm_msg
,
750 void diff_setup(struct diff_options
*options
)
752 memset(options
, 0, sizeof(*options
));
753 options
->output_format
= DIFF_FORMAT_RAW
;
754 options
->line_termination
= '\n';
755 options
->break_opt
= -1;
756 options
->rename_limit
= -1;
759 int diff_setup_done(struct diff_options
*options
)
761 if ((options
->find_copies_harder
|| 0 <= options
->rename_limit
) &&
762 options
->detect_rename
!= DIFF_DETECT_COPY
)
764 if (options
->setup
& DIFF_SETUP_USE_CACHE
) {
766 /* read-cache does not die even when it fails
767 * so it is safe for us to do this here. Also
768 * it does not smudge active_cache or active_nr
769 * when it fails, so we do not have to worry about
770 * cleaning it up oufselves either.
774 if (options
->setup
& DIFF_SETUP_USE_SIZE_CACHE
)
780 int diff_opt_parse(struct diff_options
*options
, const char **av
, int ac
)
782 const char *arg
= av
[0];
783 if (!strcmp(arg
, "-p") || !strcmp(arg
, "-u"))
784 options
->output_format
= DIFF_FORMAT_PATCH
;
785 else if (!strcmp(arg
, "-z"))
786 options
->line_termination
= 0;
787 else if (!strncmp(arg
, "-l", 2))
788 options
->rename_limit
= strtoul(arg
+2, NULL
, 10);
789 else if (!strcmp(arg
, "--name-only"))
790 options
->output_format
= DIFF_FORMAT_NAME
;
791 else if (!strcmp(arg
, "--name-status"))
792 options
->output_format
= DIFF_FORMAT_NAME_STATUS
;
793 else if (!strcmp(arg
, "-R"))
794 options
->reverse_diff
= 1;
795 else if (!strncmp(arg
, "-S", 2))
796 options
->pickaxe
= arg
+ 2;
797 else if (!strcmp(arg
, "-s"))
798 options
->output_format
= DIFF_FORMAT_NO_OUTPUT
;
799 else if (!strncmp(arg
, "-O", 2))
800 options
->orderfile
= arg
+ 2;
801 else if (!strncmp(arg
, "--diff-filter=", 14))
802 options
->filter
= arg
+ 14;
803 else if (!strcmp(arg
, "--pickaxe-all"))
804 options
->pickaxe_opts
= DIFF_PICKAXE_ALL
;
805 else if (!strncmp(arg
, "-B", 2)) {
806 if ((options
->break_opt
=
807 diff_scoreopt_parse(arg
)) == -1)
810 else if (!strncmp(arg
, "-M", 2)) {
811 if ((options
->rename_score
=
812 diff_scoreopt_parse(arg
)) == -1)
814 options
->detect_rename
= DIFF_DETECT_RENAME
;
816 else if (!strncmp(arg
, "-C", 2)) {
817 if ((options
->rename_score
=
818 diff_scoreopt_parse(arg
)) == -1)
820 options
->detect_rename
= DIFF_DETECT_COPY
;
822 else if (!strcmp(arg
, "--find-copies-harder"))
823 options
->find_copies_harder
= 1;
829 static int parse_num(const char **cp_p
)
831 int num
, scale
, ch
, cnt
;
832 const char *cp
= *cp_p
;
836 while ('0' <= (ch
= *cp
) && ch
<= '9') {
838 /* We simply ignore more than 5 digits precision. */
840 num
= num
* 10 + ch
- '0';
846 /* user says num divided by scale and we say internally that
847 * is MAX_SCORE * num / scale.
849 return (MAX_SCORE
* num
/ scale
);
852 int diff_scoreopt_parse(const char *opt
)
859 if (cmd
!= 'M' && cmd
!= 'C' && cmd
!= 'B')
860 return -1; /* that is not a -M, -C nor -B option */
862 opt1
= parse_num(&opt
);
868 else if (*opt
!= '/')
869 return -1; /* we expect -B80/99 or -B80 */
872 opt2
= parse_num(&opt
);
877 return opt1
| (opt2
<< 16);
880 struct diff_queue_struct diff_queued_diff
;
882 void diff_q(struct diff_queue_struct
*queue
, struct diff_filepair
*dp
)
884 if (queue
->alloc
<= queue
->nr
) {
885 queue
->alloc
= alloc_nr(queue
->alloc
);
886 queue
->queue
= xrealloc(queue
->queue
,
887 sizeof(dp
) * queue
->alloc
);
889 queue
->queue
[queue
->nr
++] = dp
;
892 struct diff_filepair
*diff_queue(struct diff_queue_struct
*queue
,
893 struct diff_filespec
*one
,
894 struct diff_filespec
*two
)
896 struct diff_filepair
*dp
= xmalloc(sizeof(*dp
));
901 dp
->source_stays
= 0;
908 void diff_free_filepair(struct diff_filepair
*p
)
910 diff_free_filespec_data(p
->one
);
911 diff_free_filespec_data(p
->two
);
917 static void diff_flush_raw(struct diff_filepair
*p
,
918 int line_termination
,
919 int inter_name_termination
,
924 const char *path_one
, *path_two
;
926 path_one
= p
->one
->path
;
927 path_two
= p
->two
->path
;
928 if (line_termination
) {
929 path_one
= quote_one(path_one
);
930 path_two
= quote_one(path_two
);
934 sprintf(status
, "%c%03d", p
->status
,
935 (int)(0.5 + p
->score
* 100.0/MAX_SCORE
));
937 status
[0] = p
->status
;
941 case DIFF_STATUS_COPIED
:
942 case DIFF_STATUS_RENAMED
:
945 case DIFF_STATUS_ADDED
:
946 case DIFF_STATUS_DELETED
:
953 if (output_format
!= DIFF_FORMAT_NAME_STATUS
) {
954 printf(":%06o %06o %s ",
955 p
->one
->mode
, p
->two
->mode
, sha1_to_hex(p
->one
->sha1
));
956 printf("%s ", sha1_to_hex(p
->two
->sha1
));
958 printf("%s%c%s", status
, inter_name_termination
, path_one
);
960 printf("%c%s", inter_name_termination
, path_two
);
961 putchar(line_termination
);
962 if (path_one
!= p
->one
->path
)
963 free((void*)path_one
);
964 if (path_two
!= p
->two
->path
)
965 free((void*)path_two
);
968 static void diff_flush_name(struct diff_filepair
*p
,
969 int inter_name_termination
,
970 int line_termination
)
972 char *path
= p
->two
->path
;
974 if (line_termination
)
975 path
= quote_one(p
->two
->path
);
978 printf("%s%c", path
, line_termination
);
979 if (p
->two
->path
!= path
)
983 int diff_unmodified_pair(struct diff_filepair
*p
)
985 /* This function is written stricter than necessary to support
986 * the currently implemented transformers, but the idea is to
987 * let transformers to produce diff_filepairs any way they want,
988 * and filter and clean them up here before producing the output.
990 struct diff_filespec
*one
, *two
;
992 if (DIFF_PAIR_UNMERGED(p
))
993 return 0; /* unmerged is interesting */
998 /* deletion, addition, mode or type change
999 * and rename are all interesting.
1001 if (DIFF_FILE_VALID(one
) != DIFF_FILE_VALID(two
) ||
1002 DIFF_PAIR_MODE_CHANGED(p
) ||
1003 strcmp(one
->path
, two
->path
))
1006 /* both are valid and point at the same path. that is, we are
1007 * dealing with a change.
1009 if (one
->sha1_valid
&& two
->sha1_valid
&&
1010 !memcmp(one
->sha1
, two
->sha1
, sizeof(one
->sha1
)))
1011 return 1; /* no change */
1012 if (!one
->sha1_valid
&& !two
->sha1_valid
)
1013 return 1; /* both look at the same file on the filesystem. */
1017 static void diff_flush_patch(struct diff_filepair
*p
)
1019 if (diff_unmodified_pair(p
))
1022 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
1023 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
1024 return; /* no tree diffs in patch format */
1029 int diff_queue_is_empty(void)
1031 struct diff_queue_struct
*q
= &diff_queued_diff
;
1033 for (i
= 0; i
< q
->nr
; i
++)
1034 if (!diff_unmodified_pair(q
->queue
[i
]))
1040 void diff_debug_filespec(struct diff_filespec
*s
, int x
, const char *one
)
1042 fprintf(stderr
, "queue[%d] %s (%s) %s %06o %s\n",
1045 DIFF_FILE_VALID(s
) ? "valid" : "invalid",
1047 s
->sha1_valid
? sha1_to_hex(s
->sha1
) : "");
1048 fprintf(stderr
, "queue[%d] %s size %lu flags %d\n",
1050 s
->size
, s
->xfrm_flags
);
1053 void diff_debug_filepair(const struct diff_filepair
*p
, int i
)
1055 diff_debug_filespec(p
->one
, i
, "one");
1056 diff_debug_filespec(p
->two
, i
, "two");
1057 fprintf(stderr
, "score %d, status %c stays %d broken %d\n",
1058 p
->score
, p
->status
? p
->status
: '?',
1059 p
->source_stays
, p
->broken_pair
);
1062 void diff_debug_queue(const char *msg
, struct diff_queue_struct
*q
)
1066 fprintf(stderr
, "%s\n", msg
);
1067 fprintf(stderr
, "q->nr = %d\n", q
->nr
);
1068 for (i
= 0; i
< q
->nr
; i
++) {
1069 struct diff_filepair
*p
= q
->queue
[i
];
1070 diff_debug_filepair(p
, i
);
1075 static void diff_resolve_rename_copy(void)
1078 struct diff_filepair
*p
, *pp
;
1079 struct diff_queue_struct
*q
= &diff_queued_diff
;
1081 diff_debug_queue("resolve-rename-copy", q
);
1083 for (i
= 0; i
< q
->nr
; i
++) {
1085 p
->status
= 0; /* undecided */
1086 if (DIFF_PAIR_UNMERGED(p
))
1087 p
->status
= DIFF_STATUS_UNMERGED
;
1088 else if (!DIFF_FILE_VALID(p
->one
))
1089 p
->status
= DIFF_STATUS_ADDED
;
1090 else if (!DIFF_FILE_VALID(p
->two
))
1091 p
->status
= DIFF_STATUS_DELETED
;
1092 else if (DIFF_PAIR_TYPE_CHANGED(p
))
1093 p
->status
= DIFF_STATUS_TYPE_CHANGED
;
1095 /* from this point on, we are dealing with a pair
1096 * whose both sides are valid and of the same type, i.e.
1097 * either in-place edit or rename/copy edit.
1099 else if (DIFF_PAIR_RENAME(p
)) {
1100 if (p
->source_stays
) {
1101 p
->status
= DIFF_STATUS_COPIED
;
1104 /* See if there is some other filepair that
1105 * copies from the same source as us. If so
1106 * we are a copy. Otherwise we are either a
1107 * copy if the path stays, or a rename if it
1108 * does not, but we already handled "stays" case.
1110 for (j
= i
+ 1; j
< q
->nr
; j
++) {
1112 if (strcmp(pp
->one
->path
, p
->one
->path
))
1113 continue; /* not us */
1114 if (!DIFF_PAIR_RENAME(pp
))
1115 continue; /* not a rename/copy */
1116 /* pp is a rename/copy from the same source */
1117 p
->status
= DIFF_STATUS_COPIED
;
1121 p
->status
= DIFF_STATUS_RENAMED
;
1123 else if (memcmp(p
->one
->sha1
, p
->two
->sha1
, 20) ||
1124 p
->one
->mode
!= p
->two
->mode
)
1125 p
->status
= DIFF_STATUS_MODIFIED
;
1127 /* This is a "no-change" entry and should not
1128 * happen anymore, but prepare for broken callers.
1130 error("feeding unmodified %s to diffcore",
1132 p
->status
= DIFF_STATUS_UNKNOWN
;
1135 diff_debug_queue("resolve-rename-copy done", q
);
1138 void diff_flush(struct diff_options
*options
)
1140 struct diff_queue_struct
*q
= &diff_queued_diff
;
1142 int inter_name_termination
= '\t';
1143 int diff_output_format
= options
->output_format
;
1144 int line_termination
= options
->line_termination
;
1146 if (!line_termination
)
1147 inter_name_termination
= 0;
1149 for (i
= 0; i
< q
->nr
; i
++) {
1150 struct diff_filepair
*p
= q
->queue
[i
];
1151 if ((diff_output_format
== DIFF_FORMAT_NO_OUTPUT
) ||
1152 (p
->status
== DIFF_STATUS_UNKNOWN
))
1155 die("internal error in diff-resolve-rename-copy");
1156 switch (diff_output_format
) {
1157 case DIFF_FORMAT_PATCH
:
1158 diff_flush_patch(p
);
1160 case DIFF_FORMAT_RAW
:
1161 case DIFF_FORMAT_NAME_STATUS
:
1162 diff_flush_raw(p
, line_termination
,
1163 inter_name_termination
,
1164 diff_output_format
);
1166 case DIFF_FORMAT_NAME
:
1168 inter_name_termination
,
1172 diff_free_filepair(q
->queue
[i
]);
1176 q
->nr
= q
->alloc
= 0;
1179 static void diffcore_apply_filter(const char *filter
)
1182 struct diff_queue_struct
*q
= &diff_queued_diff
;
1183 struct diff_queue_struct outq
;
1185 outq
.nr
= outq
.alloc
= 0;
1190 if (strchr(filter
, DIFF_STATUS_FILTER_AON
)) {
1192 for (i
= found
= 0; !found
&& i
< q
->nr
; i
++) {
1193 struct diff_filepair
*p
= q
->queue
[i
];
1194 if (((p
->status
== DIFF_STATUS_MODIFIED
) &&
1196 strchr(filter
, DIFF_STATUS_FILTER_BROKEN
)) ||
1198 strchr(filter
, DIFF_STATUS_MODIFIED
)))) ||
1199 ((p
->status
!= DIFF_STATUS_MODIFIED
) &&
1200 strchr(filter
, p
->status
)))
1206 /* otherwise we will clear the whole queue
1207 * by copying the empty outq at the end of this
1208 * function, but first clear the current entries
1211 for (i
= 0; i
< q
->nr
; i
++)
1212 diff_free_filepair(q
->queue
[i
]);
1215 /* Only the matching ones */
1216 for (i
= 0; i
< q
->nr
; i
++) {
1217 struct diff_filepair
*p
= q
->queue
[i
];
1219 if (((p
->status
== DIFF_STATUS_MODIFIED
) &&
1221 strchr(filter
, DIFF_STATUS_FILTER_BROKEN
)) ||
1223 strchr(filter
, DIFF_STATUS_MODIFIED
)))) ||
1224 ((p
->status
!= DIFF_STATUS_MODIFIED
) &&
1225 strchr(filter
, p
->status
)))
1228 diff_free_filepair(p
);
1235 void diffcore_std(struct diff_options
*options
)
1237 if (options
->paths
&& options
->paths
[0])
1238 diffcore_pathspec(options
->paths
);
1239 if (options
->break_opt
!= -1)
1240 diffcore_break(options
->break_opt
);
1241 if (options
->detect_rename
)
1242 diffcore_rename(options
);
1243 if (options
->break_opt
!= -1)
1244 diffcore_merge_broken();
1245 if (options
->pickaxe
)
1246 diffcore_pickaxe(options
->pickaxe
, options
->pickaxe_opts
);
1247 if (options
->orderfile
)
1248 diffcore_order(options
->orderfile
);
1249 diff_resolve_rename_copy();
1250 diffcore_apply_filter(options
->filter
);
1254 void diffcore_std_no_resolve(struct diff_options
*options
)
1256 if (options
->pickaxe
)
1257 diffcore_pickaxe(options
->pickaxe
, options
->pickaxe_opts
);
1258 if (options
->orderfile
)
1259 diffcore_order(options
->orderfile
);
1260 diffcore_apply_filter(options
->filter
);
1263 void diff_addremove(struct diff_options
*options
,
1264 int addremove
, unsigned mode
,
1265 const unsigned char *sha1
,
1266 const char *base
, const char *path
)
1268 char concatpath
[PATH_MAX
];
1269 struct diff_filespec
*one
, *two
;
1271 /* This may look odd, but it is a preparation for
1272 * feeding "there are unchanged files which should
1273 * not produce diffs, but when you are doing copy
1274 * detection you would need them, so here they are"
1275 * entries to the diff-core. They will be prefixed
1276 * with something like '=' or '*' (I haven't decided
1277 * which but should not make any difference).
1278 * Feeding the same new and old to diff_change()
1279 * also has the same effect.
1280 * Before the final output happens, they are pruned after
1281 * merged into rename/copy pairs as appropriate.
1283 if (options
->reverse_diff
)
1284 addremove
= (addremove
== '+' ? '-' :
1285 addremove
== '-' ? '+' : addremove
);
1287 if (!path
) path
= "";
1288 sprintf(concatpath
, "%s%s", base
, path
);
1289 one
= alloc_filespec(concatpath
);
1290 two
= alloc_filespec(concatpath
);
1292 if (addremove
!= '+')
1293 fill_filespec(one
, sha1
, mode
);
1294 if (addremove
!= '-')
1295 fill_filespec(two
, sha1
, mode
);
1297 diff_queue(&diff_queued_diff
, one
, two
);
1300 void diff_change(struct diff_options
*options
,
1301 unsigned old_mode
, unsigned new_mode
,
1302 const unsigned char *old_sha1
,
1303 const unsigned char *new_sha1
,
1304 const char *base
, const char *path
)
1306 char concatpath
[PATH_MAX
];
1307 struct diff_filespec
*one
, *two
;
1309 if (options
->reverse_diff
) {
1311 const unsigned char *tmp_c
;
1312 tmp
= old_mode
; old_mode
= new_mode
; new_mode
= tmp
;
1313 tmp_c
= old_sha1
; old_sha1
= new_sha1
; new_sha1
= tmp_c
;
1315 if (!path
) path
= "";
1316 sprintf(concatpath
, "%s%s", base
, path
);
1317 one
= alloc_filespec(concatpath
);
1318 two
= alloc_filespec(concatpath
);
1319 fill_filespec(one
, old_sha1
, old_mode
);
1320 fill_filespec(two
, new_sha1
, new_mode
);
1322 diff_queue(&diff_queued_diff
, one
, two
);
1325 void diff_unmerge(struct diff_options
*options
,
1328 struct diff_filespec
*one
, *two
;
1329 one
= alloc_filespec(path
);
1330 two
= alloc_filespec(path
);
1331 diff_queue(&diff_queued_diff
, one
, two
);