2 * Copyright (C) 2005 Junio C Hamano
12 static const char *diff_opts
= "-pu";
14 static int use_size_cache
;
16 int diff_rename_limit_default
= -1;
18 static char *quote_one(const char *str
)
25 needlen
= quote_c_style(str
, NULL
, NULL
, 0);
28 xp
= xmalloc(needlen
+ 1);
29 quote_c_style(str
, xp
, NULL
, 0);
33 static char *quote_two(const char *one
, const char *two
)
35 int need_one
= quote_c_style(one
, NULL
, NULL
, 1);
36 int need_two
= quote_c_style(two
, NULL
, NULL
, 1);
39 if (need_one
+ need_two
) {
40 if (!need_one
) need_one
= strlen(one
);
41 if (!need_two
) need_one
= strlen(two
);
43 xp
= xmalloc(need_one
+ need_two
+ 3);
45 quote_c_style(one
, xp
+ 1, NULL
, 1);
46 quote_c_style(two
, xp
+ need_one
+ 1, NULL
, 1);
47 strcpy(xp
+ need_one
+ need_two
+ 1, "\"");
50 need_one
= strlen(one
);
51 need_two
= strlen(two
);
52 xp
= xmalloc(need_one
+ need_two
+ 1);
54 strcpy(xp
+ need_one
, two
);
58 static const char *external_diff(void)
60 static const char *external_diff_cmd
= NULL
;
61 static int done_preparing
= 0;
62 const char *env_diff_opts
;
65 return external_diff_cmd
;
68 * Default values above are meant to match the
69 * Linux kernel development style. Examples of
70 * alternative styles you can specify via environment
75 external_diff_cmd
= getenv("GIT_EXTERNAL_DIFF");
77 /* In case external diff fails... */
78 env_diff_opts
= getenv("GIT_DIFF_OPTS");
79 if (env_diff_opts
) diff_opts
= env_diff_opts
;
82 return external_diff_cmd
;
85 #define TEMPFILE_PATH_LEN 50
87 static struct diff_tempfile
{
88 const char *name
; /* filename external diff should read from */
91 char tmp_path
[TEMPFILE_PATH_LEN
];
94 static int count_lines(const char *filename
)
97 int count
, ch
, completely_empty
= 1, nl_just_seen
= 0;
98 in
= fopen(filename
, "r");
100 while ((ch
= fgetc(in
)) != EOF
)
104 completely_empty
= 0;
108 completely_empty
= 0;
111 if (completely_empty
)
114 count
++; /* no trailing newline */
118 static void print_line_count(int count
)
128 printf("1,%d", count
);
133 static void copy_file(int prefix
, const char *filename
)
136 int ch
, nl_just_seen
= 1;
137 in
= fopen(filename
, "r");
138 while ((ch
= fgetc(in
)) != EOF
) {
149 printf("\n\\ No newline at end of file\n");
152 static void emit_rewrite_diff(const char *name_a
,
154 struct diff_tempfile
*temp
)
156 /* Use temp[i].name as input, name_a and name_b as labels */
158 lc_a
= count_lines(temp
[0].name
);
159 lc_b
= count_lines(temp
[1].name
);
160 printf("--- %s\n+++ %s\n@@ -", name_a
, name_b
);
161 print_line_count(lc_a
);
163 print_line_count(lc_b
);
166 copy_file('-', temp
[0].name
);
168 copy_file('+', temp
[1].name
);
171 static void builtin_diff(const char *name_a
,
173 struct diff_tempfile
*temp
,
174 const char *xfrm_msg
,
175 int complete_rewrite
)
177 int i
, next_at
, cmd_size
;
178 const char *const diff_cmd
= "diff -L%s -L%s";
179 const char *const diff_arg
= "-- %s %s||:"; /* "||:" is to return 0 */
180 const char *input_name_sq
[2];
181 const char *label_path
[2];
184 /* diff_cmd and diff_arg have 4 %s in total which makes
185 * the sum of these strings 8 bytes larger than required.
186 * we use 2 spaces around diff-opts, and we need to count
187 * terminating NUL; we used to subtract 5 here, but we do not
188 * care about small leaks in this subprocess that is about
189 * to exec "diff" anymore.
191 cmd_size
= (strlen(diff_cmd
) + strlen(diff_opts
) + strlen(diff_arg
)
194 for (i
= 0; i
< 2; i
++) {
195 input_name_sq
[i
] = sq_quote(temp
[i
].name
);
196 if (!strcmp(temp
[i
].name
, "/dev/null"))
197 label_path
[i
] = "/dev/null";
199 label_path
[i
] = sq_quote(quote_two("a/", name_a
));
201 label_path
[i
] = sq_quote(quote_two("b/", name_b
));
202 cmd_size
+= (strlen(label_path
[i
]) + strlen(input_name_sq
[i
]));
205 cmd
= xmalloc(cmd_size
);
208 next_at
+= snprintf(cmd
+next_at
, cmd_size
-next_at
,
209 diff_cmd
, label_path
[0], label_path
[1]);
210 next_at
+= snprintf(cmd
+next_at
, cmd_size
-next_at
,
212 next_at
+= snprintf(cmd
+next_at
, cmd_size
-next_at
,
213 diff_arg
, input_name_sq
[0], input_name_sq
[1]);
215 printf("diff --git %s %s\n",
216 quote_two("a/", name_a
), quote_two("b/", name_b
));
217 if (label_path
[0][0] == '/') {
219 printf("new file mode %s\n", temp
[1].mode
);
220 if (xfrm_msg
&& xfrm_msg
[0])
223 else if (label_path
[1][0] == '/') {
224 printf("deleted file mode %s\n", temp
[0].mode
);
225 if (xfrm_msg
&& xfrm_msg
[0])
229 if (strcmp(temp
[0].mode
, temp
[1].mode
)) {
230 printf("old mode %s\n", temp
[0].mode
);
231 printf("new mode %s\n", temp
[1].mode
);
233 if (xfrm_msg
&& xfrm_msg
[0])
235 if (strncmp(temp
[0].mode
, temp
[1].mode
, 3))
236 /* we do not run diff between different kind
240 if (complete_rewrite
) {
242 emit_rewrite_diff(name_a
, name_b
, temp
);
247 execlp("/bin/sh","sh", "-c", cmd
, NULL
);
250 struct diff_filespec
*alloc_filespec(const char *path
)
252 int namelen
= strlen(path
);
253 struct diff_filespec
*spec
= xmalloc(sizeof(*spec
) + namelen
+ 1);
255 memset(spec
, 0, sizeof(*spec
));
256 spec
->path
= (char *)(spec
+ 1);
257 memcpy(spec
->path
, path
, namelen
+1);
261 void fill_filespec(struct diff_filespec
*spec
, const unsigned char *sha1
,
265 spec
->mode
= DIFF_FILE_CANON_MODE(mode
);
266 memcpy(spec
->sha1
, sha1
, 20);
267 spec
->sha1_valid
= !!memcmp(sha1
, null_sha1
, 20);
272 * Given a name and sha1 pair, if the dircache tells us the file in
273 * the work tree has that object contents, return true, so that
274 * prepare_temp_file() does not have to inflate and extract.
276 static int work_tree_matches(const char *name
, const unsigned char *sha1
)
278 struct cache_entry
*ce
;
282 /* We do not read the cache ourselves here, because the
283 * benchmark with my previous version that always reads cache
284 * shows that it makes things worse for diff-tree comparing
285 * two linux-2.6 kernel trees in an already checked out work
286 * tree. This is because most diff-tree comparisons deal with
287 * only a small number of files, while reading the cache is
288 * expensive for a large project, and its cost outweighs the
289 * savings we get by not inflating the object to a temporary
290 * file. Practically, this code only helps when we are used
291 * by diff-cache --cached, which does read the cache before
298 pos
= cache_name_pos(name
, len
);
301 ce
= active_cache
[pos
];
302 if ((lstat(name
, &st
) < 0) ||
303 !S_ISREG(st
.st_mode
) || /* careful! */
304 ce_match_stat(ce
, &st
) ||
305 memcmp(sha1
, ce
->sha1
, 20))
307 /* we return 1 only when we can stat, it is a regular file,
308 * stat information matches, and sha1 recorded in the cache
309 * matches. I.e. we know the file in the work tree really is
310 * the same as the <name, sha1> pair.
315 static struct sha1_size_cache
{
316 unsigned char sha1
[20];
319 static int sha1_size_cache_nr
, sha1_size_cache_alloc
;
321 static struct sha1_size_cache
*locate_size_cache(unsigned char *sha1
,
326 struct sha1_size_cache
*e
;
329 last
= sha1_size_cache_nr
;
330 while (last
> first
) {
331 int cmp
, next
= (last
+ first
) >> 1;
332 e
= sha1_size_cache
[next
];
333 cmp
= memcmp(e
->sha1
, sha1
, 20);
345 /* insert to make it at "first" */
346 if (sha1_size_cache_alloc
<= sha1_size_cache_nr
) {
347 sha1_size_cache_alloc
= alloc_nr(sha1_size_cache_alloc
);
348 sha1_size_cache
= xrealloc(sha1_size_cache
,
349 sha1_size_cache_alloc
*
350 sizeof(*sha1_size_cache
));
352 sha1_size_cache_nr
++;
353 if (first
< sha1_size_cache_nr
)
354 memmove(sha1_size_cache
+ first
+ 1, sha1_size_cache
+ first
,
355 (sha1_size_cache_nr
- first
- 1) *
356 sizeof(*sha1_size_cache
));
357 e
= xmalloc(sizeof(struct sha1_size_cache
));
358 sha1_size_cache
[first
] = e
;
359 memcpy(e
->sha1
, sha1
, 20);
365 * While doing rename detection and pickaxe operation, we may need to
366 * grab the data for the blob (or file) for our own in-core comparison.
367 * diff_filespec has data and size fields for this purpose.
369 int diff_populate_filespec(struct diff_filespec
*s
, int size_only
)
372 if (!DIFF_FILE_VALID(s
))
373 die("internal error: asking to populate invalid file.");
374 if (S_ISDIR(s
->mode
))
382 if (!s
->sha1_valid
||
383 work_tree_matches(s
->path
, s
->sha1
)) {
386 if (lstat(s
->path
, &st
) < 0) {
387 if (errno
== ENOENT
) {
396 s
->size
= st
.st_size
;
401 if (S_ISLNK(st
.st_mode
)) {
403 s
->data
= xmalloc(s
->size
);
405 ret
= readlink(s
->path
, s
->data
, s
->size
);
412 fd
= open(s
->path
, O_RDONLY
);
415 s
->data
= mmap(NULL
, s
->size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
417 if (s
->data
== MAP_FAILED
)
419 s
->should_munmap
= 1;
423 struct sha1_size_cache
*e
;
426 e
= locate_size_cache(s
->sha1
, 1, 0);
431 if (!sha1_object_info(s
->sha1
, type
, &s
->size
))
432 locate_size_cache(s
->sha1
, 0, s
->size
);
435 s
->data
= read_sha1_file(s
->sha1
, type
, &s
->size
);
442 void diff_free_filespec_data(struct diff_filespec
*s
)
446 else if (s
->should_munmap
)
447 munmap(s
->data
, s
->size
);
448 s
->should_free
= s
->should_munmap
= 0;
452 static void prep_temp_blob(struct diff_tempfile
*temp
,
455 const unsigned char *sha1
,
460 fd
= git_mkstemp(temp
->tmp_path
, TEMPFILE_PATH_LEN
, ".diff_XXXXXX");
462 die("unable to create temp-file");
463 if (write(fd
, blob
, size
) != size
)
464 die("unable to write temp-file");
466 temp
->name
= temp
->tmp_path
;
467 strcpy(temp
->hex
, sha1_to_hex(sha1
));
469 sprintf(temp
->mode
, "%06o", mode
);
472 static void prepare_temp_file(const char *name
,
473 struct diff_tempfile
*temp
,
474 struct diff_filespec
*one
)
476 if (!DIFF_FILE_VALID(one
)) {
478 /* A '-' entry produces this for file-2, and
479 * a '+' entry produces this for file-1.
481 temp
->name
= "/dev/null";
482 strcpy(temp
->hex
, ".");
483 strcpy(temp
->mode
, ".");
487 if (!one
->sha1_valid
||
488 work_tree_matches(name
, one
->sha1
)) {
490 if (lstat(name
, &st
) < 0) {
492 goto not_a_valid_file
;
493 die("stat(%s): %s", name
, strerror(errno
));
495 if (S_ISLNK(st
.st_mode
)) {
497 char *buf
, buf_
[1024];
498 buf
= ((sizeof(buf_
) < st
.st_size
) ?
499 xmalloc(st
.st_size
) : buf_
);
500 ret
= readlink(name
, buf
, st
.st_size
);
502 die("readlink(%s)", name
);
503 prep_temp_blob(temp
, buf
, st
.st_size
,
505 one
->sha1
: null_sha1
),
507 one
->mode
: S_IFLNK
));
510 /* we can borrow from the file in the work tree */
512 if (!one
->sha1_valid
)
513 strcpy(temp
->hex
, sha1_to_hex(null_sha1
));
515 strcpy(temp
->hex
, sha1_to_hex(one
->sha1
));
516 /* Even though we may sometimes borrow the
517 * contents from the work tree, we always want
518 * one->mode. mode is trustworthy even when
519 * !(one->sha1_valid), as long as
520 * DIFF_FILE_VALID(one).
522 sprintf(temp
->mode
, "%06o", one
->mode
);
527 if (diff_populate_filespec(one
, 0))
528 die("cannot read data blob for %s", one
->path
);
529 prep_temp_blob(temp
, one
->data
, one
->size
,
530 one
->sha1
, one
->mode
);
534 static void remove_tempfile(void)
538 for (i
= 0; i
< 2; i
++)
539 if (diff_temp
[i
].name
== diff_temp
[i
].tmp_path
) {
540 unlink(diff_temp
[i
].name
);
541 diff_temp
[i
].name
= NULL
;
545 static void remove_tempfile_on_signal(int signo
)
550 /* An external diff command takes:
552 * diff-cmd name infile1 infile1-sha1 infile1-mode \
553 * infile2 infile2-sha1 infile2-mode [ rename-to ]
556 static void run_external_diff(const char *pgm
,
559 struct diff_filespec
*one
,
560 struct diff_filespec
*two
,
561 const char *xfrm_msg
,
562 int complete_rewrite
)
564 struct diff_tempfile
*temp
= diff_temp
;
567 static int atexit_asked
= 0;
568 const char *othername
;
570 othername
= (other
? other
: name
);
572 prepare_temp_file(name
, &temp
[0], one
);
573 prepare_temp_file(othername
, &temp
[1], two
);
574 if (! atexit_asked
&&
575 (temp
[0].name
== temp
[0].tmp_path
||
576 temp
[1].name
== temp
[1].tmp_path
)) {
578 atexit(remove_tempfile
);
580 signal(SIGINT
, remove_tempfile_on_signal
);
586 die("unable to fork");
590 const char *exec_arg
[10];
591 const char **arg
= &exec_arg
[0];
594 *arg
++ = temp
[0].name
;
595 *arg
++ = temp
[0].hex
;
596 *arg
++ = temp
[0].mode
;
597 *arg
++ = temp
[1].name
;
598 *arg
++ = temp
[1].hex
;
599 *arg
++ = temp
[1].mode
;
605 execvp(pgm
, (char *const*) exec_arg
);
608 execlp(pgm
, pgm
, name
, NULL
);
611 * otherwise we use the built-in one.
614 builtin_diff(name
, othername
, temp
, xfrm_msg
,
617 printf("* Unmerged path %s\n", name
);
620 if (waitpid(pid
, &status
, 0) < 0 ||
621 !WIFEXITED(status
) || WEXITSTATUS(status
)) {
622 /* Earlier we did not check the exit status because
623 * diff exits non-zero if files are different, and
624 * we are not interested in knowing that. It was a
625 * mistake which made it harder to quit a diff-*
626 * session that uses the git-apply-patch-script as
627 * the GIT_EXTERNAL_DIFF. A custom GIT_EXTERNAL_DIFF
628 * should also exit non-zero only when it wants to
629 * abort the entire diff-* session.
632 fprintf(stderr
, "external diff died, stopping at %s.\n", name
);
638 static void diff_fill_sha1_info(struct diff_filespec
*one
)
640 if (DIFF_FILE_VALID(one
)) {
641 if (!one
->sha1_valid
) {
643 if (stat(one
->path
, &st
) < 0)
644 die("stat %s", one
->path
);
645 if (index_path(one
->sha1
, one
->path
, &st
, 0))
646 die("cannot hash %s\n", one
->path
);
650 memset(one
->sha1
, 0, 20);
653 static void run_diff(struct diff_filepair
*p
)
655 const char *pgm
= external_diff();
656 char msg
[PATH_MAX
*2+300], *xfrm_msg
;
657 struct diff_filespec
*one
;
658 struct diff_filespec
*two
;
661 char *name_munged
, *other_munged
;
662 int complete_rewrite
= 0;
665 if (DIFF_PAIR_UNMERGED(p
)) {
667 run_external_diff(pgm
, p
->one
->path
, NULL
, NULL
, NULL
, NULL
,
673 other
= (strcmp(name
, p
->two
->path
) ? p
->two
->path
: NULL
);
674 name_munged
= quote_one(name
);
675 other_munged
= quote_one(other
);
676 one
= p
->one
; two
= p
->two
;
678 diff_fill_sha1_info(one
);
679 diff_fill_sha1_info(two
);
683 case DIFF_STATUS_COPIED
:
684 len
+= snprintf(msg
+ len
, sizeof(msg
) - len
,
685 "similarity index %d%%\n"
688 (int)(0.5 + p
->score
* 100.0/MAX_SCORE
),
689 name_munged
, other_munged
);
691 case DIFF_STATUS_RENAMED
:
692 len
+= snprintf(msg
+ len
, sizeof(msg
) - len
,
693 "similarity index %d%%\n"
696 (int)(0.5 + p
->score
* 100.0/MAX_SCORE
),
697 name_munged
, other_munged
);
699 case DIFF_STATUS_MODIFIED
:
701 len
+= snprintf(msg
+ len
, sizeof(msg
) - len
,
702 "dissimilarity index %d%%\n",
703 (int)(0.5 + p
->score
*
705 complete_rewrite
= 1;
714 if (memcmp(one
->sha1
, two
->sha1
, 20)) {
716 memcpy(one_sha1
, sha1_to_hex(one
->sha1
), 41);
718 len
+= snprintf(msg
+ len
, sizeof(msg
) - len
,
719 "index %.7s..%.7s", one_sha1
,
720 sha1_to_hex(two
->sha1
));
721 if (one
->mode
== two
->mode
)
722 len
+= snprintf(msg
+ len
, sizeof(msg
) - len
,
724 len
+= snprintf(msg
+ len
, sizeof(msg
) - len
, "\n");
729 xfrm_msg
= len
? msg
: NULL
;
732 DIFF_FILE_VALID(one
) && DIFF_FILE_VALID(two
) &&
733 (S_IFMT
& one
->mode
) != (S_IFMT
& two
->mode
)) {
734 /* a filepair that changes between file and symlink
735 * needs to be split into deletion and creation.
737 struct diff_filespec
*null
= alloc_filespec(two
->path
);
738 run_external_diff(NULL
, name
, other
, one
, null
, xfrm_msg
, 0);
740 null
= alloc_filespec(one
->path
);
741 run_external_diff(NULL
, name
, other
, null
, two
, xfrm_msg
, 0);
745 run_external_diff(pgm
, name
, other
, one
, two
, xfrm_msg
,
752 void diff_setup(struct diff_options
*options
)
754 memset(options
, 0, sizeof(*options
));
755 options
->output_format
= DIFF_FORMAT_RAW
;
756 options
->line_termination
= '\n';
757 options
->break_opt
= -1;
758 options
->rename_limit
= -1;
760 options
->change
= diff_change
;
761 options
->add_remove
= diff_addremove
;
764 int diff_setup_done(struct diff_options
*options
)
766 if ((options
->find_copies_harder
&&
767 options
->detect_rename
!= DIFF_DETECT_COPY
) ||
768 (0 <= options
->rename_limit
&& !options
->detect_rename
))
770 if (options
->detect_rename
&& options
->rename_limit
< 0)
771 options
->rename_limit
= diff_rename_limit_default
;
772 if (options
->setup
& DIFF_SETUP_USE_CACHE
) {
774 /* read-cache does not die even when it fails
775 * so it is safe for us to do this here. Also
776 * it does not smudge active_cache or active_nr
777 * when it fails, so we do not have to worry about
778 * cleaning it up oufselves either.
782 if (options
->setup
& DIFF_SETUP_USE_SIZE_CACHE
)
788 int diff_opt_parse(struct diff_options
*options
, const char **av
, int ac
)
790 const char *arg
= av
[0];
791 if (!strcmp(arg
, "-p") || !strcmp(arg
, "-u"))
792 options
->output_format
= DIFF_FORMAT_PATCH
;
793 else if (!strcmp(arg
, "-z"))
794 options
->line_termination
= 0;
795 else if (!strncmp(arg
, "-l", 2))
796 options
->rename_limit
= strtoul(arg
+2, NULL
, 10);
797 else if (!strcmp(arg
, "--name-only"))
798 options
->output_format
= DIFF_FORMAT_NAME
;
799 else if (!strcmp(arg
, "--name-status"))
800 options
->output_format
= DIFF_FORMAT_NAME_STATUS
;
801 else if (!strcmp(arg
, "-R"))
802 options
->reverse_diff
= 1;
803 else if (!strncmp(arg
, "-S", 2))
804 options
->pickaxe
= arg
+ 2;
805 else if (!strcmp(arg
, "-s"))
806 options
->output_format
= DIFF_FORMAT_NO_OUTPUT
;
807 else if (!strncmp(arg
, "-O", 2))
808 options
->orderfile
= arg
+ 2;
809 else if (!strncmp(arg
, "--diff-filter=", 14))
810 options
->filter
= arg
+ 14;
811 else if (!strcmp(arg
, "--pickaxe-all"))
812 options
->pickaxe_opts
= DIFF_PICKAXE_ALL
;
813 else if (!strncmp(arg
, "-B", 2)) {
814 if ((options
->break_opt
=
815 diff_scoreopt_parse(arg
)) == -1)
818 else if (!strncmp(arg
, "-M", 2)) {
819 if ((options
->rename_score
=
820 diff_scoreopt_parse(arg
)) == -1)
822 options
->detect_rename
= DIFF_DETECT_RENAME
;
824 else if (!strncmp(arg
, "-C", 2)) {
825 if ((options
->rename_score
=
826 diff_scoreopt_parse(arg
)) == -1)
828 options
->detect_rename
= DIFF_DETECT_COPY
;
830 else if (!strcmp(arg
, "--find-copies-harder"))
831 options
->find_copies_harder
= 1;
837 static int parse_num(const char **cp_p
)
839 int num
, scale
, ch
, cnt
;
840 const char *cp
= *cp_p
;
844 while ('0' <= (ch
= *cp
) && ch
<= '9') {
846 /* We simply ignore more than 5 digits precision. */
848 num
= num
* 10 + ch
- '0';
854 /* user says num divided by scale and we say internally that
855 * is MAX_SCORE * num / scale.
857 return (MAX_SCORE
* num
/ scale
);
860 int diff_scoreopt_parse(const char *opt
)
867 if (cmd
!= 'M' && cmd
!= 'C' && cmd
!= 'B')
868 return -1; /* that is not a -M, -C nor -B option */
870 opt1
= parse_num(&opt
);
876 else if (*opt
!= '/')
877 return -1; /* we expect -B80/99 or -B80 */
880 opt2
= parse_num(&opt
);
885 return opt1
| (opt2
<< 16);
888 struct diff_queue_struct diff_queued_diff
;
890 void diff_q(struct diff_queue_struct
*queue
, struct diff_filepair
*dp
)
892 if (queue
->alloc
<= queue
->nr
) {
893 queue
->alloc
= alloc_nr(queue
->alloc
);
894 queue
->queue
= xrealloc(queue
->queue
,
895 sizeof(dp
) * queue
->alloc
);
897 queue
->queue
[queue
->nr
++] = dp
;
900 struct diff_filepair
*diff_queue(struct diff_queue_struct
*queue
,
901 struct diff_filespec
*one
,
902 struct diff_filespec
*two
)
904 struct diff_filepair
*dp
= xmalloc(sizeof(*dp
));
909 dp
->source_stays
= 0;
916 void diff_free_filepair(struct diff_filepair
*p
)
918 diff_free_filespec_data(p
->one
);
919 diff_free_filespec_data(p
->two
);
925 static void diff_flush_raw(struct diff_filepair
*p
,
926 int line_termination
,
927 int inter_name_termination
,
932 const char *path_one
, *path_two
;
934 path_one
= p
->one
->path
;
935 path_two
= p
->two
->path
;
936 if (line_termination
) {
937 path_one
= quote_one(path_one
);
938 path_two
= quote_one(path_two
);
942 sprintf(status
, "%c%03d", p
->status
,
943 (int)(0.5 + p
->score
* 100.0/MAX_SCORE
));
945 status
[0] = p
->status
;
949 case DIFF_STATUS_COPIED
:
950 case DIFF_STATUS_RENAMED
:
953 case DIFF_STATUS_ADDED
:
954 case DIFF_STATUS_DELETED
:
961 if (output_format
!= DIFF_FORMAT_NAME_STATUS
) {
962 printf(":%06o %06o %s ",
963 p
->one
->mode
, p
->two
->mode
, sha1_to_hex(p
->one
->sha1
));
964 printf("%s ", sha1_to_hex(p
->two
->sha1
));
966 printf("%s%c%s", status
, inter_name_termination
, path_one
);
968 printf("%c%s", inter_name_termination
, path_two
);
969 putchar(line_termination
);
970 if (path_one
!= p
->one
->path
)
971 free((void*)path_one
);
972 if (path_two
!= p
->two
->path
)
973 free((void*)path_two
);
976 static void diff_flush_name(struct diff_filepair
*p
,
977 int inter_name_termination
,
978 int line_termination
)
980 char *path
= p
->two
->path
;
982 if (line_termination
)
983 path
= quote_one(p
->two
->path
);
986 printf("%s%c", path
, line_termination
);
987 if (p
->two
->path
!= path
)
991 int diff_unmodified_pair(struct diff_filepair
*p
)
993 /* This function is written stricter than necessary to support
994 * the currently implemented transformers, but the idea is to
995 * let transformers to produce diff_filepairs any way they want,
996 * and filter and clean them up here before producing the output.
998 struct diff_filespec
*one
, *two
;
1000 if (DIFF_PAIR_UNMERGED(p
))
1001 return 0; /* unmerged is interesting */
1006 /* deletion, addition, mode or type change
1007 * and rename are all interesting.
1009 if (DIFF_FILE_VALID(one
) != DIFF_FILE_VALID(two
) ||
1010 DIFF_PAIR_MODE_CHANGED(p
) ||
1011 strcmp(one
->path
, two
->path
))
1014 /* both are valid and point at the same path. that is, we are
1015 * dealing with a change.
1017 if (one
->sha1_valid
&& two
->sha1_valid
&&
1018 !memcmp(one
->sha1
, two
->sha1
, sizeof(one
->sha1
)))
1019 return 1; /* no change */
1020 if (!one
->sha1_valid
&& !two
->sha1_valid
)
1021 return 1; /* both look at the same file on the filesystem. */
1025 static void diff_flush_patch(struct diff_filepair
*p
)
1027 if (diff_unmodified_pair(p
))
1030 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
1031 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
1032 return; /* no tree diffs in patch format */
1037 int diff_queue_is_empty(void)
1039 struct diff_queue_struct
*q
= &diff_queued_diff
;
1041 for (i
= 0; i
< q
->nr
; i
++)
1042 if (!diff_unmodified_pair(q
->queue
[i
]))
1048 void diff_debug_filespec(struct diff_filespec
*s
, int x
, const char *one
)
1050 fprintf(stderr
, "queue[%d] %s (%s) %s %06o %s\n",
1053 DIFF_FILE_VALID(s
) ? "valid" : "invalid",
1055 s
->sha1_valid
? sha1_to_hex(s
->sha1
) : "");
1056 fprintf(stderr
, "queue[%d] %s size %lu flags %d\n",
1058 s
->size
, s
->xfrm_flags
);
1061 void diff_debug_filepair(const struct diff_filepair
*p
, int i
)
1063 diff_debug_filespec(p
->one
, i
, "one");
1064 diff_debug_filespec(p
->two
, i
, "two");
1065 fprintf(stderr
, "score %d, status %c stays %d broken %d\n",
1066 p
->score
, p
->status
? p
->status
: '?',
1067 p
->source_stays
, p
->broken_pair
);
1070 void diff_debug_queue(const char *msg
, struct diff_queue_struct
*q
)
1074 fprintf(stderr
, "%s\n", msg
);
1075 fprintf(stderr
, "q->nr = %d\n", q
->nr
);
1076 for (i
= 0; i
< q
->nr
; i
++) {
1077 struct diff_filepair
*p
= q
->queue
[i
];
1078 diff_debug_filepair(p
, i
);
1083 static void diff_resolve_rename_copy(void)
1086 struct diff_filepair
*p
, *pp
;
1087 struct diff_queue_struct
*q
= &diff_queued_diff
;
1089 diff_debug_queue("resolve-rename-copy", q
);
1091 for (i
= 0; i
< q
->nr
; i
++) {
1093 p
->status
= 0; /* undecided */
1094 if (DIFF_PAIR_UNMERGED(p
))
1095 p
->status
= DIFF_STATUS_UNMERGED
;
1096 else if (!DIFF_FILE_VALID(p
->one
))
1097 p
->status
= DIFF_STATUS_ADDED
;
1098 else if (!DIFF_FILE_VALID(p
->two
))
1099 p
->status
= DIFF_STATUS_DELETED
;
1100 else if (DIFF_PAIR_TYPE_CHANGED(p
))
1101 p
->status
= DIFF_STATUS_TYPE_CHANGED
;
1103 /* from this point on, we are dealing with a pair
1104 * whose both sides are valid and of the same type, i.e.
1105 * either in-place edit or rename/copy edit.
1107 else if (DIFF_PAIR_RENAME(p
)) {
1108 if (p
->source_stays
) {
1109 p
->status
= DIFF_STATUS_COPIED
;
1112 /* See if there is some other filepair that
1113 * copies from the same source as us. If so
1114 * we are a copy. Otherwise we are either a
1115 * copy if the path stays, or a rename if it
1116 * does not, but we already handled "stays" case.
1118 for (j
= i
+ 1; j
< q
->nr
; j
++) {
1120 if (strcmp(pp
->one
->path
, p
->one
->path
))
1121 continue; /* not us */
1122 if (!DIFF_PAIR_RENAME(pp
))
1123 continue; /* not a rename/copy */
1124 /* pp is a rename/copy from the same source */
1125 p
->status
= DIFF_STATUS_COPIED
;
1129 p
->status
= DIFF_STATUS_RENAMED
;
1131 else if (memcmp(p
->one
->sha1
, p
->two
->sha1
, 20) ||
1132 p
->one
->mode
!= p
->two
->mode
)
1133 p
->status
= DIFF_STATUS_MODIFIED
;
1135 /* This is a "no-change" entry and should not
1136 * happen anymore, but prepare for broken callers.
1138 error("feeding unmodified %s to diffcore",
1140 p
->status
= DIFF_STATUS_UNKNOWN
;
1143 diff_debug_queue("resolve-rename-copy done", q
);
1146 void diff_flush(struct diff_options
*options
)
1148 struct diff_queue_struct
*q
= &diff_queued_diff
;
1150 int inter_name_termination
= '\t';
1151 int diff_output_format
= options
->output_format
;
1152 int line_termination
= options
->line_termination
;
1154 if (!line_termination
)
1155 inter_name_termination
= 0;
1157 for (i
= 0; i
< q
->nr
; i
++) {
1158 struct diff_filepair
*p
= q
->queue
[i
];
1159 if ((diff_output_format
== DIFF_FORMAT_NO_OUTPUT
) ||
1160 (p
->status
== DIFF_STATUS_UNKNOWN
))
1163 die("internal error in diff-resolve-rename-copy");
1164 switch (diff_output_format
) {
1165 case DIFF_FORMAT_PATCH
:
1166 diff_flush_patch(p
);
1168 case DIFF_FORMAT_RAW
:
1169 case DIFF_FORMAT_NAME_STATUS
:
1170 diff_flush_raw(p
, line_termination
,
1171 inter_name_termination
,
1172 diff_output_format
);
1174 case DIFF_FORMAT_NAME
:
1176 inter_name_termination
,
1180 diff_free_filepair(q
->queue
[i
]);
1184 q
->nr
= q
->alloc
= 0;
1187 static void diffcore_apply_filter(const char *filter
)
1190 struct diff_queue_struct
*q
= &diff_queued_diff
;
1191 struct diff_queue_struct outq
;
1193 outq
.nr
= outq
.alloc
= 0;
1198 if (strchr(filter
, DIFF_STATUS_FILTER_AON
)) {
1200 for (i
= found
= 0; !found
&& i
< q
->nr
; i
++) {
1201 struct diff_filepair
*p
= q
->queue
[i
];
1202 if (((p
->status
== DIFF_STATUS_MODIFIED
) &&
1204 strchr(filter
, DIFF_STATUS_FILTER_BROKEN
)) ||
1206 strchr(filter
, DIFF_STATUS_MODIFIED
)))) ||
1207 ((p
->status
!= DIFF_STATUS_MODIFIED
) &&
1208 strchr(filter
, p
->status
)))
1214 /* otherwise we will clear the whole queue
1215 * by copying the empty outq at the end of this
1216 * function, but first clear the current entries
1219 for (i
= 0; i
< q
->nr
; i
++)
1220 diff_free_filepair(q
->queue
[i
]);
1223 /* Only the matching ones */
1224 for (i
= 0; i
< q
->nr
; i
++) {
1225 struct diff_filepair
*p
= q
->queue
[i
];
1227 if (((p
->status
== DIFF_STATUS_MODIFIED
) &&
1229 strchr(filter
, DIFF_STATUS_FILTER_BROKEN
)) ||
1231 strchr(filter
, DIFF_STATUS_MODIFIED
)))) ||
1232 ((p
->status
!= DIFF_STATUS_MODIFIED
) &&
1233 strchr(filter
, p
->status
)))
1236 diff_free_filepair(p
);
1243 void diffcore_std(struct diff_options
*options
)
1245 if (options
->paths
&& options
->paths
[0])
1246 diffcore_pathspec(options
->paths
);
1247 if (options
->break_opt
!= -1)
1248 diffcore_break(options
->break_opt
);
1249 if (options
->detect_rename
)
1250 diffcore_rename(options
);
1251 if (options
->break_opt
!= -1)
1252 diffcore_merge_broken();
1253 if (options
->pickaxe
)
1254 diffcore_pickaxe(options
->pickaxe
, options
->pickaxe_opts
);
1255 if (options
->orderfile
)
1256 diffcore_order(options
->orderfile
);
1257 diff_resolve_rename_copy();
1258 diffcore_apply_filter(options
->filter
);
1262 void diffcore_std_no_resolve(struct diff_options
*options
)
1264 if (options
->pickaxe
)
1265 diffcore_pickaxe(options
->pickaxe
, options
->pickaxe_opts
);
1266 if (options
->orderfile
)
1267 diffcore_order(options
->orderfile
);
1268 diffcore_apply_filter(options
->filter
);
1271 void diff_addremove(struct diff_options
*options
,
1272 int addremove
, unsigned mode
,
1273 const unsigned char *sha1
,
1274 const char *base
, const char *path
)
1276 char concatpath
[PATH_MAX
];
1277 struct diff_filespec
*one
, *two
;
1279 /* This may look odd, but it is a preparation for
1280 * feeding "there are unchanged files which should
1281 * not produce diffs, but when you are doing copy
1282 * detection you would need them, so here they are"
1283 * entries to the diff-core. They will be prefixed
1284 * with something like '=' or '*' (I haven't decided
1285 * which but should not make any difference).
1286 * Feeding the same new and old to diff_change()
1287 * also has the same effect.
1288 * Before the final output happens, they are pruned after
1289 * merged into rename/copy pairs as appropriate.
1291 if (options
->reverse_diff
)
1292 addremove
= (addremove
== '+' ? '-' :
1293 addremove
== '-' ? '+' : addremove
);
1295 if (!path
) path
= "";
1296 sprintf(concatpath
, "%s%s", base
, path
);
1297 one
= alloc_filespec(concatpath
);
1298 two
= alloc_filespec(concatpath
);
1300 if (addremove
!= '+')
1301 fill_filespec(one
, sha1
, mode
);
1302 if (addremove
!= '-')
1303 fill_filespec(two
, sha1
, mode
);
1305 diff_queue(&diff_queued_diff
, one
, two
);
1308 void diff_change(struct diff_options
*options
,
1309 unsigned old_mode
, unsigned new_mode
,
1310 const unsigned char *old_sha1
,
1311 const unsigned char *new_sha1
,
1312 const char *base
, const char *path
)
1314 char concatpath
[PATH_MAX
];
1315 struct diff_filespec
*one
, *two
;
1317 if (options
->reverse_diff
) {
1319 const unsigned char *tmp_c
;
1320 tmp
= old_mode
; old_mode
= new_mode
; new_mode
= tmp
;
1321 tmp_c
= old_sha1
; old_sha1
= new_sha1
; new_sha1
= tmp_c
;
1323 if (!path
) path
= "";
1324 sprintf(concatpath
, "%s%s", base
, path
);
1325 one
= alloc_filespec(concatpath
);
1326 two
= alloc_filespec(concatpath
);
1327 fill_filespec(one
, old_sha1
, old_mode
);
1328 fill_filespec(two
, new_sha1
, new_mode
);
1330 diff_queue(&diff_queued_diff
, one
, two
);
1333 void diff_unmerge(struct diff_options
*options
,
1336 struct diff_filespec
*one
, *two
;
1337 one
= alloc_filespec(path
);
1338 two
= alloc_filespec(path
);
1339 diff_queue(&diff_queued_diff
, one
, two
);