2 * Copyright (C) 2005 Junio C Hamano
12 #include "xdiff-interface.h"
14 static int use_size_cache
;
16 int diff_rename_limit_default
= -1;
18 int git_diff_config(const char *var
, const char *value
)
20 if (!strcmp(var
, "diff.renamelimit")) {
21 diff_rename_limit_default
= git_config_int(var
, value
);
25 return git_default_config(var
, value
);
28 static char *quote_one(const char *str
)
35 needlen
= quote_c_style(str
, NULL
, NULL
, 0);
38 xp
= xmalloc(needlen
+ 1);
39 quote_c_style(str
, xp
, NULL
, 0);
43 static char *quote_two(const char *one
, const char *two
)
45 int need_one
= quote_c_style(one
, NULL
, NULL
, 1);
46 int need_two
= quote_c_style(two
, NULL
, NULL
, 1);
49 if (need_one
+ need_two
) {
50 if (!need_one
) need_one
= strlen(one
);
51 if (!need_two
) need_one
= strlen(two
);
53 xp
= xmalloc(need_one
+ need_two
+ 3);
55 quote_c_style(one
, xp
+ 1, NULL
, 1);
56 quote_c_style(two
, xp
+ need_one
+ 1, NULL
, 1);
57 strcpy(xp
+ need_one
+ need_two
+ 1, "\"");
60 need_one
= strlen(one
);
61 need_two
= strlen(two
);
62 xp
= xmalloc(need_one
+ need_two
+ 1);
64 strcpy(xp
+ need_one
, two
);
68 static const char *external_diff(void)
70 static const char *external_diff_cmd
= NULL
;
71 static int done_preparing
= 0;
74 return external_diff_cmd
;
75 external_diff_cmd
= getenv("GIT_EXTERNAL_DIFF");
77 return external_diff_cmd
;
80 #define TEMPFILE_PATH_LEN 50
82 static struct diff_tempfile
{
83 const char *name
; /* filename external diff should read from */
86 char tmp_path
[TEMPFILE_PATH_LEN
];
89 static int count_lines(const char *data
, int size
)
91 int count
, ch
, completely_empty
= 1, nl_just_seen
= 0;
102 completely_empty
= 0;
105 if (completely_empty
)
108 count
++; /* no trailing newline */
112 static void print_line_count(int count
)
122 printf("1,%d", count
);
127 static void copy_file(int prefix
, const char *data
, int size
)
129 int ch
, nl_just_seen
= 1;
141 printf("\n\\ No newline at end of file\n");
144 static void emit_rewrite_diff(const char *name_a
,
146 struct diff_filespec
*one
,
147 struct diff_filespec
*two
)
150 diff_populate_filespec(one
, 0);
151 diff_populate_filespec(two
, 0);
152 lc_a
= count_lines(one
->data
, one
->size
);
153 lc_b
= count_lines(two
->data
, two
->size
);
154 printf("--- %s\n+++ %s\n@@ -", name_a
, name_b
);
155 print_line_count(lc_a
);
157 print_line_count(lc_b
);
160 copy_file('-', one
->data
, one
->size
);
162 copy_file('+', two
->data
, two
->size
);
165 static int fill_mmfile(mmfile_t
*mf
, struct diff_filespec
*one
)
167 if (!DIFF_FILE_VALID(one
)) {
168 mf
->ptr
= ""; /* does not matter */
172 else if (diff_populate_filespec(one
, 0))
175 mf
->size
= one
->size
;
179 struct emit_callback
{
180 const char **label_path
;
183 static int fn_out(void *priv
, mmbuffer_t
*mb
, int nbuf
)
186 struct emit_callback
*ecbdata
= priv
;
188 if (ecbdata
->label_path
[0]) {
189 printf("--- %s\n", ecbdata
->label_path
[0]);
190 printf("+++ %s\n", ecbdata
->label_path
[1]);
191 ecbdata
->label_path
[0] = ecbdata
->label_path
[1] = NULL
;
193 for (i
= 0; i
< nbuf
; i
++)
194 if (!fwrite(mb
[i
].ptr
, mb
[i
].size
, 1, stdout
))
199 static char *pprint_rename(const char *a
, const char *b
)
204 int pfx_length
, sfx_length
;
205 int len_a
= strlen(a
);
206 int len_b
= strlen(b
);
208 /* Find common prefix */
210 while (*old
&& *new && *old
== *new) {
212 pfx_length
= old
- a
+ 1;
217 /* Find common suffix */
221 while (a
<= old
&& b
<= new && *old
== *new) {
223 sfx_length
= len_a
- (old
- a
);
229 * pfx{mid-a => mid-b}sfx
230 * {pfx-a => pfx-b}sfx
231 * pfx{sfx-a => sfx-b}
234 if (pfx_length
+ sfx_length
) {
235 int a_midlen
= len_a
- pfx_length
- sfx_length
;
236 int b_midlen
= len_b
- pfx_length
- sfx_length
;
237 if (a_midlen
< 0) a_midlen
= 0;
238 if (b_midlen
< 0) b_midlen
= 0;
240 name
= xmalloc(len_a
+ len_b
- pfx_length
- sfx_length
+ 7);
241 sprintf(name
, "%.*s{%.*s => %.*s}%s",
243 a_midlen
, a
+ pfx_length
,
244 b_midlen
, b
+ pfx_length
,
245 a
+ len_a
- sfx_length
);
248 name
= xmalloc(len_a
+ len_b
+ 5);
249 sprintf(name
, "%s => %s", a
, b
);
255 struct xdiff_emit_state xm
;
259 struct diffstat_file
{
261 unsigned is_unmerged
:1;
262 unsigned is_binary
:1;
263 unsigned is_renamed
:1;
264 unsigned int added
, deleted
;
268 static struct diffstat_file
*diffstat_add(struct diffstat_t
*diffstat
,
272 struct diffstat_file
*x
;
273 x
= xcalloc(sizeof (*x
), 1);
274 if (diffstat
->nr
== diffstat
->alloc
) {
275 diffstat
->alloc
= alloc_nr(diffstat
->alloc
);
276 diffstat
->files
= xrealloc(diffstat
->files
,
277 diffstat
->alloc
* sizeof(x
));
279 diffstat
->files
[diffstat
->nr
++] = x
;
281 x
->name
= pprint_rename(name_a
, name_b
);
285 x
->name
= strdup(name_a
);
289 static void diffstat_consume(void *priv
, char *line
, unsigned long len
)
291 struct diffstat_t
*diffstat
= priv
;
292 struct diffstat_file
*x
= diffstat
->files
[diffstat
->nr
- 1];
296 else if (line
[0] == '-')
300 static const char pluses
[] = "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++";
301 static const char minuses
[]= "----------------------------------------------------------------------";
303 static void show_stats(struct diffstat_t
* data
)
305 int i
, len
, add
, del
, total
, adds
= 0, dels
= 0;
306 int max
, max_change
= 0, max_len
= 0;
307 int total_files
= data
->nr
;
312 for (i
= 0; i
< data
->nr
; i
++) {
313 struct diffstat_file
*file
= data
->files
[i
];
315 len
= strlen(file
->name
);
319 if (file
->is_binary
|| file
->is_unmerged
)
321 if (max_change
< file
->added
+ file
->deleted
)
322 max_change
= file
->added
+ file
->deleted
;
325 for (i
= 0; i
< data
->nr
; i
++) {
327 char *name
= data
->files
[i
]->name
;
328 int added
= data
->files
[i
]->added
;
329 int deleted
= data
->files
[i
]->deleted
;
331 if (0 < (len
= quote_c_style(name
, NULL
, NULL
, 0))) {
332 char *qname
= xmalloc(len
+ 1);
333 quote_c_style(name
, qname
, NULL
, 0);
335 data
->files
[i
]->name
= name
= qname
;
339 * "scale" the filename
350 slash
= strchr(name
, '/');
357 * scale the add/delete
363 if (data
->files
[i
]->is_binary
) {
364 printf(" %s%-*s | Bin\n", prefix
, len
, name
);
365 goto free_diffstat_file
;
367 else if (data
->files
[i
]->is_unmerged
) {
368 printf(" %s%-*s | Unmerged\n", prefix
, len
, name
);
369 goto free_diffstat_file
;
371 else if (!data
->files
[i
]->is_renamed
&&
372 (added
+ deleted
== 0)) {
374 goto free_diffstat_file
;
383 if (max_change
> 0) {
384 total
= (total
* max
+ max_change
/ 2) / max_change
;
385 add
= (add
* max
+ max_change
/ 2) / max_change
;
388 printf(" %s%-*s |%5d %.*s%.*s\n", prefix
,
389 len
, name
, added
+ deleted
,
390 add
, pluses
, del
, minuses
);
392 free(data
->files
[i
]->name
);
393 free(data
->files
[i
]);
396 printf(" %d files changed, %d insertions(+), %d deletions(-)\n",
397 total_files
, adds
, dels
);
400 static unsigned char *deflate_it(char *data
,
402 unsigned long *result_size
)
405 unsigned char *deflated
;
408 memset(&stream
, 0, sizeof(stream
));
409 deflateInit(&stream
, Z_BEST_COMPRESSION
);
410 bound
= deflateBound(&stream
, size
);
411 deflated
= xmalloc(bound
);
412 stream
.next_out
= deflated
;
413 stream
.avail_out
= bound
;
415 stream
.next_in
= (unsigned char *)data
;
416 stream
.avail_in
= size
;
417 while (deflate(&stream
, Z_FINISH
) == Z_OK
)
420 *result_size
= stream
.total_out
;
424 static void emit_binary_diff(mmfile_t
*one
, mmfile_t
*two
)
430 unsigned long orig_size
;
431 unsigned long delta_size
;
432 unsigned long deflate_size
;
433 unsigned long data_size
;
435 printf("GIT binary patch\n");
436 /* We could do deflated delta, or we could do just deflated two,
437 * whichever is smaller.
440 deflated
= deflate_it(two
->ptr
, two
->size
, &deflate_size
);
441 if (one
->size
&& two
->size
) {
442 delta
= diff_delta(one
->ptr
, one
->size
,
444 &delta_size
, deflate_size
);
446 void *to_free
= delta
;
447 orig_size
= delta_size
;
448 delta
= deflate_it(delta
, delta_size
, &delta_size
);
453 if (delta
&& delta_size
< deflate_size
) {
454 printf("delta %lu\n", orig_size
);
457 data_size
= delta_size
;
460 printf("literal %lu\n", two
->size
);
463 data_size
= deflate_size
;
466 /* emit data encoded in base85 */
469 int bytes
= (52 < data_size
) ? 52 : data_size
;
473 line
[0] = bytes
+ 'A' - 1;
475 line
[0] = bytes
- 26 + 'a' - 1;
476 encode_85(line
+ 1, cp
, bytes
);
484 #define FIRST_FEW_BYTES 8000
485 static int mmfile_is_binary(mmfile_t
*mf
)
488 if (FIRST_FEW_BYTES
< sz
)
489 sz
= FIRST_FEW_BYTES
;
490 if (memchr(mf
->ptr
, 0, sz
))
495 static void builtin_diff(const char *name_a
,
497 struct diff_filespec
*one
,
498 struct diff_filespec
*two
,
499 const char *xfrm_msg
,
500 struct diff_options
*o
,
501 int complete_rewrite
)
507 a_one
= quote_two("a/", name_a
);
508 b_two
= quote_two("b/", name_b
);
509 lbl
[0] = DIFF_FILE_VALID(one
) ? a_one
: "/dev/null";
510 lbl
[1] = DIFF_FILE_VALID(two
) ? b_two
: "/dev/null";
511 printf("diff --git %s %s\n", a_one
, b_two
);
512 if (lbl
[0][0] == '/') {
514 printf("new file mode %06o\n", two
->mode
);
515 if (xfrm_msg
&& xfrm_msg
[0])
518 else if (lbl
[1][0] == '/') {
519 printf("deleted file mode %06o\n", one
->mode
);
520 if (xfrm_msg
&& xfrm_msg
[0])
524 if (one
->mode
!= two
->mode
) {
525 printf("old mode %06o\n", one
->mode
);
526 printf("new mode %06o\n", two
->mode
);
528 if (xfrm_msg
&& xfrm_msg
[0])
531 * we do not run diff between different kind
534 if ((one
->mode
^ two
->mode
) & S_IFMT
)
535 goto free_ab_and_return
;
536 if (complete_rewrite
) {
537 emit_rewrite_diff(name_a
, name_b
, one
, two
);
538 goto free_ab_and_return
;
542 if (fill_mmfile(&mf1
, one
) < 0 || fill_mmfile(&mf2
, two
) < 0)
543 die("unable to read files to diff");
545 if (mmfile_is_binary(&mf1
) || mmfile_is_binary(&mf2
)) {
546 /* Quite common confusing case */
547 if (mf1
.size
== mf2
.size
&&
548 !memcmp(mf1
.ptr
, mf2
.ptr
, mf1
.size
))
549 goto free_ab_and_return
;
551 emit_binary_diff(&mf1
, &mf2
);
553 printf("Binary files %s and %s differ\n",
557 /* Crazy xdl interfaces.. */
558 const char *diffopts
= getenv("GIT_DIFF_OPTS");
562 struct emit_callback ecbdata
;
564 ecbdata
.label_path
= lbl
;
565 xpp
.flags
= XDF_NEED_MINIMAL
;
567 xecfg
.flags
= XDL_EMIT_FUNCNAMES
;
570 else if (!strncmp(diffopts
, "--unified=", 10))
571 xecfg
.ctxlen
= strtoul(diffopts
+ 10, NULL
, 10);
572 else if (!strncmp(diffopts
, "-u", 2))
573 xecfg
.ctxlen
= strtoul(diffopts
+ 2, NULL
, 10);
576 xdl_diff(&mf1
, &mf2
, &xpp
, &xecfg
, &ecb
);
585 static void builtin_diffstat(const char *name_a
, const char *name_b
,
586 struct diff_filespec
*one
,
587 struct diff_filespec
*two
,
588 struct diffstat_t
*diffstat
,
589 int complete_rewrite
)
592 struct diffstat_file
*data
;
594 data
= diffstat_add(diffstat
, name_a
, name_b
);
597 data
->is_unmerged
= 1;
600 if (complete_rewrite
) {
601 diff_populate_filespec(one
, 0);
602 diff_populate_filespec(two
, 0);
603 data
->deleted
= count_lines(one
->data
, one
->size
);
604 data
->added
= count_lines(two
->data
, two
->size
);
607 if (fill_mmfile(&mf1
, one
) < 0 || fill_mmfile(&mf2
, two
) < 0)
608 die("unable to read files to diff");
610 if (mmfile_is_binary(&mf1
) || mmfile_is_binary(&mf2
))
613 /* Crazy xdl interfaces.. */
618 xpp
.flags
= XDF_NEED_MINIMAL
;
621 ecb
.outf
= xdiff_outf
;
623 xdl_diff(&mf1
, &mf2
, &xpp
, &xecfg
, &ecb
);
627 struct diff_filespec
*alloc_filespec(const char *path
)
629 int namelen
= strlen(path
);
630 struct diff_filespec
*spec
= xmalloc(sizeof(*spec
) + namelen
+ 1);
632 memset(spec
, 0, sizeof(*spec
));
633 spec
->path
= (char *)(spec
+ 1);
634 memcpy(spec
->path
, path
, namelen
+1);
638 void fill_filespec(struct diff_filespec
*spec
, const unsigned char *sha1
,
642 spec
->mode
= canon_mode(mode
);
643 memcpy(spec
->sha1
, sha1
, 20);
644 spec
->sha1_valid
= !!memcmp(sha1
, null_sha1
, 20);
649 * Given a name and sha1 pair, if the dircache tells us the file in
650 * the work tree has that object contents, return true, so that
651 * prepare_temp_file() does not have to inflate and extract.
653 static int work_tree_matches(const char *name
, const unsigned char *sha1
)
655 struct cache_entry
*ce
;
659 /* We do not read the cache ourselves here, because the
660 * benchmark with my previous version that always reads cache
661 * shows that it makes things worse for diff-tree comparing
662 * two linux-2.6 kernel trees in an already checked out work
663 * tree. This is because most diff-tree comparisons deal with
664 * only a small number of files, while reading the cache is
665 * expensive for a large project, and its cost outweighs the
666 * savings we get by not inflating the object to a temporary
667 * file. Practically, this code only helps when we are used
668 * by diff-cache --cached, which does read the cache before
675 pos
= cache_name_pos(name
, len
);
678 ce
= active_cache
[pos
];
679 if ((lstat(name
, &st
) < 0) ||
680 !S_ISREG(st
.st_mode
) || /* careful! */
681 ce_match_stat(ce
, &st
, 0) ||
682 memcmp(sha1
, ce
->sha1
, 20))
684 /* we return 1 only when we can stat, it is a regular file,
685 * stat information matches, and sha1 recorded in the cache
686 * matches. I.e. we know the file in the work tree really is
687 * the same as the <name, sha1> pair.
692 static struct sha1_size_cache
{
693 unsigned char sha1
[20];
696 static int sha1_size_cache_nr
, sha1_size_cache_alloc
;
698 static struct sha1_size_cache
*locate_size_cache(unsigned char *sha1
,
703 struct sha1_size_cache
*e
;
706 last
= sha1_size_cache_nr
;
707 while (last
> first
) {
708 int cmp
, next
= (last
+ first
) >> 1;
709 e
= sha1_size_cache
[next
];
710 cmp
= memcmp(e
->sha1
, sha1
, 20);
722 /* insert to make it at "first" */
723 if (sha1_size_cache_alloc
<= sha1_size_cache_nr
) {
724 sha1_size_cache_alloc
= alloc_nr(sha1_size_cache_alloc
);
725 sha1_size_cache
= xrealloc(sha1_size_cache
,
726 sha1_size_cache_alloc
*
727 sizeof(*sha1_size_cache
));
729 sha1_size_cache_nr
++;
730 if (first
< sha1_size_cache_nr
)
731 memmove(sha1_size_cache
+ first
+ 1, sha1_size_cache
+ first
,
732 (sha1_size_cache_nr
- first
- 1) *
733 sizeof(*sha1_size_cache
));
734 e
= xmalloc(sizeof(struct sha1_size_cache
));
735 sha1_size_cache
[first
] = e
;
736 memcpy(e
->sha1
, sha1
, 20);
742 * While doing rename detection and pickaxe operation, we may need to
743 * grab the data for the blob (or file) for our own in-core comparison.
744 * diff_filespec has data and size fields for this purpose.
746 int diff_populate_filespec(struct diff_filespec
*s
, int size_only
)
749 if (!DIFF_FILE_VALID(s
))
750 die("internal error: asking to populate invalid file.");
751 if (S_ISDIR(s
->mode
))
759 if (!s
->sha1_valid
||
760 work_tree_matches(s
->path
, s
->sha1
)) {
763 if (lstat(s
->path
, &st
) < 0) {
764 if (errno
== ENOENT
) {
773 s
->size
= st
.st_size
;
778 if (S_ISLNK(st
.st_mode
)) {
780 s
->data
= xmalloc(s
->size
);
782 ret
= readlink(s
->path
, s
->data
, s
->size
);
789 fd
= open(s
->path
, O_RDONLY
);
792 s
->data
= mmap(NULL
, s
->size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
794 if (s
->data
== MAP_FAILED
)
796 s
->should_munmap
= 1;
800 struct sha1_size_cache
*e
;
803 e
= locate_size_cache(s
->sha1
, 1, 0);
808 if (!sha1_object_info(s
->sha1
, type
, &s
->size
))
809 locate_size_cache(s
->sha1
, 0, s
->size
);
812 s
->data
= read_sha1_file(s
->sha1
, type
, &s
->size
);
819 void diff_free_filespec_data(struct diff_filespec
*s
)
823 else if (s
->should_munmap
)
824 munmap(s
->data
, s
->size
);
825 s
->should_free
= s
->should_munmap
= 0;
831 static void prep_temp_blob(struct diff_tempfile
*temp
,
834 const unsigned char *sha1
,
839 fd
= git_mkstemp(temp
->tmp_path
, TEMPFILE_PATH_LEN
, ".diff_XXXXXX");
841 die("unable to create temp-file");
842 if (write(fd
, blob
, size
) != size
)
843 die("unable to write temp-file");
845 temp
->name
= temp
->tmp_path
;
846 strcpy(temp
->hex
, sha1_to_hex(sha1
));
848 sprintf(temp
->mode
, "%06o", mode
);
851 static void prepare_temp_file(const char *name
,
852 struct diff_tempfile
*temp
,
853 struct diff_filespec
*one
)
855 if (!DIFF_FILE_VALID(one
)) {
857 /* A '-' entry produces this for file-2, and
858 * a '+' entry produces this for file-1.
860 temp
->name
= "/dev/null";
861 strcpy(temp
->hex
, ".");
862 strcpy(temp
->mode
, ".");
866 if (!one
->sha1_valid
||
867 work_tree_matches(name
, one
->sha1
)) {
869 if (lstat(name
, &st
) < 0) {
871 goto not_a_valid_file
;
872 die("stat(%s): %s", name
, strerror(errno
));
874 if (S_ISLNK(st
.st_mode
)) {
876 char buf
[PATH_MAX
+ 1]; /* ought to be SYMLINK_MAX */
877 if (sizeof(buf
) <= st
.st_size
)
878 die("symlink too long: %s", name
);
879 ret
= readlink(name
, buf
, st
.st_size
);
881 die("readlink(%s)", name
);
882 prep_temp_blob(temp
, buf
, st
.st_size
,
884 one
->sha1
: null_sha1
),
886 one
->mode
: S_IFLNK
));
889 /* we can borrow from the file in the work tree */
891 if (!one
->sha1_valid
)
892 strcpy(temp
->hex
, sha1_to_hex(null_sha1
));
894 strcpy(temp
->hex
, sha1_to_hex(one
->sha1
));
895 /* Even though we may sometimes borrow the
896 * contents from the work tree, we always want
897 * one->mode. mode is trustworthy even when
898 * !(one->sha1_valid), as long as
899 * DIFF_FILE_VALID(one).
901 sprintf(temp
->mode
, "%06o", one
->mode
);
906 if (diff_populate_filespec(one
, 0))
907 die("cannot read data blob for %s", one
->path
);
908 prep_temp_blob(temp
, one
->data
, one
->size
,
909 one
->sha1
, one
->mode
);
913 static void remove_tempfile(void)
917 for (i
= 0; i
< 2; i
++)
918 if (diff_temp
[i
].name
== diff_temp
[i
].tmp_path
) {
919 unlink(diff_temp
[i
].name
);
920 diff_temp
[i
].name
= NULL
;
924 static void remove_tempfile_on_signal(int signo
)
927 signal(SIGINT
, SIG_DFL
);
931 static int spawn_prog(const char *pgm
, const char **arg
)
939 die("unable to fork");
941 execvp(pgm
, (char *const*) arg
);
945 while (waitpid(pid
, &status
, 0) < 0) {
951 /* Earlier we did not check the exit status because
952 * diff exits non-zero if files are different, and
953 * we are not interested in knowing that. It was a
954 * mistake which made it harder to quit a diff-*
955 * session that uses the git-apply-patch-script as
956 * the GIT_EXTERNAL_DIFF. A custom GIT_EXTERNAL_DIFF
957 * should also exit non-zero only when it wants to
958 * abort the entire diff-* session.
960 if (WIFEXITED(status
) && !WEXITSTATUS(status
))
965 /* An external diff command takes:
967 * diff-cmd name infile1 infile1-sha1 infile1-mode \
968 * infile2 infile2-sha1 infile2-mode [ rename-to ]
971 static void run_external_diff(const char *pgm
,
974 struct diff_filespec
*one
,
975 struct diff_filespec
*two
,
976 const char *xfrm_msg
,
977 int complete_rewrite
)
979 const char *spawn_arg
[10];
980 struct diff_tempfile
*temp
= diff_temp
;
982 static int atexit_asked
= 0;
983 const char *othername
;
984 const char **arg
= &spawn_arg
[0];
986 othername
= (other
? other
: name
);
988 prepare_temp_file(name
, &temp
[0], one
);
989 prepare_temp_file(othername
, &temp
[1], two
);
990 if (! atexit_asked
&&
991 (temp
[0].name
== temp
[0].tmp_path
||
992 temp
[1].name
== temp
[1].tmp_path
)) {
994 atexit(remove_tempfile
);
996 signal(SIGINT
, remove_tempfile_on_signal
);
1002 *arg
++ = temp
[0].name
;
1003 *arg
++ = temp
[0].hex
;
1004 *arg
++ = temp
[0].mode
;
1005 *arg
++ = temp
[1].name
;
1006 *arg
++ = temp
[1].hex
;
1007 *arg
++ = temp
[1].mode
;
1017 retval
= spawn_prog(pgm
, spawn_arg
);
1020 fprintf(stderr
, "external diff died, stopping at %s.\n", name
);
1025 static void run_diff_cmd(const char *pgm
,
1028 struct diff_filespec
*one
,
1029 struct diff_filespec
*two
,
1030 const char *xfrm_msg
,
1031 struct diff_options
*o
,
1032 int complete_rewrite
)
1035 run_external_diff(pgm
, name
, other
, one
, two
, xfrm_msg
,
1040 builtin_diff(name
, other
? other
: name
,
1041 one
, two
, xfrm_msg
, o
, complete_rewrite
);
1043 printf("* Unmerged path %s\n", name
);
1046 static void diff_fill_sha1_info(struct diff_filespec
*one
)
1048 if (DIFF_FILE_VALID(one
)) {
1049 if (!one
->sha1_valid
) {
1051 if (lstat(one
->path
, &st
) < 0)
1052 die("stat %s", one
->path
);
1053 if (index_path(one
->sha1
, one
->path
, &st
, 0))
1054 die("cannot hash %s\n", one
->path
);
1058 memset(one
->sha1
, 0, 20);
1061 static void run_diff(struct diff_filepair
*p
, struct diff_options
*o
)
1063 const char *pgm
= external_diff();
1064 char msg
[PATH_MAX
*2+300], *xfrm_msg
;
1065 struct diff_filespec
*one
;
1066 struct diff_filespec
*two
;
1069 char *name_munged
, *other_munged
;
1070 int complete_rewrite
= 0;
1073 if (DIFF_PAIR_UNMERGED(p
)) {
1075 run_diff_cmd(pgm
, p
->one
->path
, NULL
, NULL
, NULL
, NULL
, o
, 0);
1079 name
= p
->one
->path
;
1080 other
= (strcmp(name
, p
->two
->path
) ? p
->two
->path
: NULL
);
1081 name_munged
= quote_one(name
);
1082 other_munged
= quote_one(other
);
1083 one
= p
->one
; two
= p
->two
;
1085 diff_fill_sha1_info(one
);
1086 diff_fill_sha1_info(two
);
1089 switch (p
->status
) {
1090 case DIFF_STATUS_COPIED
:
1091 len
+= snprintf(msg
+ len
, sizeof(msg
) - len
,
1092 "similarity index %d%%\n"
1095 (int)(0.5 + p
->score
* 100.0/MAX_SCORE
),
1096 name_munged
, other_munged
);
1098 case DIFF_STATUS_RENAMED
:
1099 len
+= snprintf(msg
+ len
, sizeof(msg
) - len
,
1100 "similarity index %d%%\n"
1103 (int)(0.5 + p
->score
* 100.0/MAX_SCORE
),
1104 name_munged
, other_munged
);
1106 case DIFF_STATUS_MODIFIED
:
1108 len
+= snprintf(msg
+ len
, sizeof(msg
) - len
,
1109 "dissimilarity index %d%%\n",
1110 (int)(0.5 + p
->score
*
1112 complete_rewrite
= 1;
1121 if (memcmp(one
->sha1
, two
->sha1
, 20)) {
1122 int abbrev
= o
->full_index
? 40 : DEFAULT_ABBREV
;
1124 len
+= snprintf(msg
+ len
, sizeof(msg
) - len
,
1126 abbrev
, sha1_to_hex(one
->sha1
),
1127 abbrev
, sha1_to_hex(two
->sha1
));
1128 if (one
->mode
== two
->mode
)
1129 len
+= snprintf(msg
+ len
, sizeof(msg
) - len
,
1130 " %06o", one
->mode
);
1131 len
+= snprintf(msg
+ len
, sizeof(msg
) - len
, "\n");
1136 xfrm_msg
= len
? msg
: NULL
;
1139 DIFF_FILE_VALID(one
) && DIFF_FILE_VALID(two
) &&
1140 (S_IFMT
& one
->mode
) != (S_IFMT
& two
->mode
)) {
1141 /* a filepair that changes between file and symlink
1142 * needs to be split into deletion and creation.
1144 struct diff_filespec
*null
= alloc_filespec(two
->path
);
1145 run_diff_cmd(NULL
, name
, other
, one
, null
, xfrm_msg
, o
, 0);
1147 null
= alloc_filespec(one
->path
);
1148 run_diff_cmd(NULL
, name
, other
, null
, two
, xfrm_msg
, o
, 0);
1152 run_diff_cmd(pgm
, name
, other
, one
, two
, xfrm_msg
, o
,
1159 static void run_diffstat(struct diff_filepair
*p
, struct diff_options
*o
,
1160 struct diffstat_t
*diffstat
)
1164 int complete_rewrite
= 0;
1166 if (DIFF_PAIR_UNMERGED(p
)) {
1168 builtin_diffstat(p
->one
->path
, NULL
, NULL
, NULL
, diffstat
, 0);
1172 name
= p
->one
->path
;
1173 other
= (strcmp(name
, p
->two
->path
) ? p
->two
->path
: NULL
);
1175 diff_fill_sha1_info(p
->one
);
1176 diff_fill_sha1_info(p
->two
);
1178 if (p
->status
== DIFF_STATUS_MODIFIED
&& p
->score
)
1179 complete_rewrite
= 1;
1180 builtin_diffstat(name
, other
, p
->one
, p
->two
, diffstat
, complete_rewrite
);
1183 void diff_setup(struct diff_options
*options
)
1185 memset(options
, 0, sizeof(*options
));
1186 options
->output_format
= DIFF_FORMAT_RAW
;
1187 options
->line_termination
= '\n';
1188 options
->break_opt
= -1;
1189 options
->rename_limit
= -1;
1191 options
->change
= diff_change
;
1192 options
->add_remove
= diff_addremove
;
1195 int diff_setup_done(struct diff_options
*options
)
1197 if ((options
->find_copies_harder
&&
1198 options
->detect_rename
!= DIFF_DETECT_COPY
) ||
1199 (0 <= options
->rename_limit
&& !options
->detect_rename
))
1203 * These cases always need recursive; we do not drop caller-supplied
1204 * recursive bits for other formats here.
1206 if ((options
->output_format
== DIFF_FORMAT_PATCH
) ||
1207 (options
->output_format
== DIFF_FORMAT_DIFFSTAT
))
1208 options
->recursive
= 1;
1210 if (options
->detect_rename
&& options
->rename_limit
< 0)
1211 options
->rename_limit
= diff_rename_limit_default
;
1212 if (options
->setup
& DIFF_SETUP_USE_CACHE
) {
1214 /* read-cache does not die even when it fails
1215 * so it is safe for us to do this here. Also
1216 * it does not smudge active_cache or active_nr
1217 * when it fails, so we do not have to worry about
1218 * cleaning it up ourselves either.
1222 if (options
->setup
& DIFF_SETUP_USE_SIZE_CACHE
)
1224 if (options
->abbrev
<= 0 || 40 < options
->abbrev
)
1225 options
->abbrev
= 40; /* full */
1230 int diff_opt_parse(struct diff_options
*options
, const char **av
, int ac
)
1232 const char *arg
= av
[0];
1233 if (!strcmp(arg
, "-p") || !strcmp(arg
, "-u"))
1234 options
->output_format
= DIFF_FORMAT_PATCH
;
1235 else if (!strcmp(arg
, "--patch-with-raw")) {
1236 options
->output_format
= DIFF_FORMAT_PATCH
;
1237 options
->with_raw
= 1;
1239 else if (!strcmp(arg
, "--stat"))
1240 options
->output_format
= DIFF_FORMAT_DIFFSTAT
;
1241 else if (!strcmp(arg
, "--patch-with-stat")) {
1242 options
->output_format
= DIFF_FORMAT_PATCH
;
1243 options
->with_stat
= 1;
1245 else if (!strcmp(arg
, "-z"))
1246 options
->line_termination
= 0;
1247 else if (!strncmp(arg
, "-l", 2))
1248 options
->rename_limit
= strtoul(arg
+2, NULL
, 10);
1249 else if (!strcmp(arg
, "--full-index"))
1250 options
->full_index
= 1;
1251 else if (!strcmp(arg
, "--binary")) {
1252 options
->output_format
= DIFF_FORMAT_PATCH
;
1253 options
->full_index
= options
->binary
= 1;
1255 else if (!strcmp(arg
, "--name-only"))
1256 options
->output_format
= DIFF_FORMAT_NAME
;
1257 else if (!strcmp(arg
, "--name-status"))
1258 options
->output_format
= DIFF_FORMAT_NAME_STATUS
;
1259 else if (!strcmp(arg
, "-R"))
1260 options
->reverse_diff
= 1;
1261 else if (!strncmp(arg
, "-S", 2))
1262 options
->pickaxe
= arg
+ 2;
1263 else if (!strcmp(arg
, "-s"))
1264 options
->output_format
= DIFF_FORMAT_NO_OUTPUT
;
1265 else if (!strncmp(arg
, "-O", 2))
1266 options
->orderfile
= arg
+ 2;
1267 else if (!strncmp(arg
, "--diff-filter=", 14))
1268 options
->filter
= arg
+ 14;
1269 else if (!strcmp(arg
, "--pickaxe-all"))
1270 options
->pickaxe_opts
= DIFF_PICKAXE_ALL
;
1271 else if (!strcmp(arg
, "--pickaxe-regex"))
1272 options
->pickaxe_opts
= DIFF_PICKAXE_REGEX
;
1273 else if (!strncmp(arg
, "-B", 2)) {
1274 if ((options
->break_opt
=
1275 diff_scoreopt_parse(arg
)) == -1)
1278 else if (!strncmp(arg
, "-M", 2)) {
1279 if ((options
->rename_score
=
1280 diff_scoreopt_parse(arg
)) == -1)
1282 options
->detect_rename
= DIFF_DETECT_RENAME
;
1284 else if (!strncmp(arg
, "-C", 2)) {
1285 if ((options
->rename_score
=
1286 diff_scoreopt_parse(arg
)) == -1)
1288 options
->detect_rename
= DIFF_DETECT_COPY
;
1290 else if (!strcmp(arg
, "--find-copies-harder"))
1291 options
->find_copies_harder
= 1;
1292 else if (!strcmp(arg
, "--abbrev"))
1293 options
->abbrev
= DEFAULT_ABBREV
;
1294 else if (!strncmp(arg
, "--abbrev=", 9)) {
1295 options
->abbrev
= strtoul(arg
+ 9, NULL
, 10);
1296 if (options
->abbrev
< MINIMUM_ABBREV
)
1297 options
->abbrev
= MINIMUM_ABBREV
;
1298 else if (40 < options
->abbrev
)
1299 options
->abbrev
= 40;
1306 static int parse_num(const char **cp_p
)
1308 unsigned long num
, scale
;
1310 const char *cp
= *cp_p
;
1317 if ( !dot
&& ch
== '.' ) {
1320 } else if ( ch
== '%' ) {
1321 scale
= dot
? scale
*100 : 100;
1322 cp
++; /* % is always at the end */
1324 } else if ( ch
>= '0' && ch
<= '9' ) {
1325 if ( scale
< 100000 ) {
1327 num
= (num
*10) + (ch
-'0');
1336 /* user says num divided by scale and we say internally that
1337 * is MAX_SCORE * num / scale.
1339 return (num
>= scale
) ? MAX_SCORE
: (MAX_SCORE
* num
/ scale
);
1342 int diff_scoreopt_parse(const char *opt
)
1344 int opt1
, opt2
, cmd
;
1349 if (cmd
!= 'M' && cmd
!= 'C' && cmd
!= 'B')
1350 return -1; /* that is not a -M, -C nor -B option */
1352 opt1
= parse_num(&opt
);
1358 else if (*opt
!= '/')
1359 return -1; /* we expect -B80/99 or -B80 */
1362 opt2
= parse_num(&opt
);
1367 return opt1
| (opt2
<< 16);
1370 struct diff_queue_struct diff_queued_diff
;
1372 void diff_q(struct diff_queue_struct
*queue
, struct diff_filepair
*dp
)
1374 if (queue
->alloc
<= queue
->nr
) {
1375 queue
->alloc
= alloc_nr(queue
->alloc
);
1376 queue
->queue
= xrealloc(queue
->queue
,
1377 sizeof(dp
) * queue
->alloc
);
1379 queue
->queue
[queue
->nr
++] = dp
;
1382 struct diff_filepair
*diff_queue(struct diff_queue_struct
*queue
,
1383 struct diff_filespec
*one
,
1384 struct diff_filespec
*two
)
1386 struct diff_filepair
*dp
= xmalloc(sizeof(*dp
));
1391 dp
->source_stays
= 0;
1392 dp
->broken_pair
= 0;
1398 void diff_free_filepair(struct diff_filepair
*p
)
1400 diff_free_filespec_data(p
->one
);
1401 diff_free_filespec_data(p
->two
);
1407 /* This is different from find_unique_abbrev() in that
1408 * it stuffs the result with dots for alignment.
1410 const char *diff_unique_abbrev(const unsigned char *sha1
, int len
)
1415 return sha1_to_hex(sha1
);
1417 abbrev
= find_unique_abbrev(sha1
, len
);
1419 return sha1_to_hex(sha1
);
1420 abblen
= strlen(abbrev
);
1422 static char hex
[41];
1423 if (len
< abblen
&& abblen
<= len
+ 2)
1424 sprintf(hex
, "%s%.*s", abbrev
, len
+3-abblen
, "..");
1426 sprintf(hex
, "%s...", abbrev
);
1429 return sha1_to_hex(sha1
);
1432 static void diff_flush_raw(struct diff_filepair
*p
,
1433 int line_termination
,
1434 int inter_name_termination
,
1435 struct diff_options
*options
,
1440 int abbrev
= options
->abbrev
;
1441 const char *path_one
, *path_two
;
1443 path_one
= p
->one
->path
;
1444 path_two
= p
->two
->path
;
1445 if (line_termination
) {
1446 path_one
= quote_one(path_one
);
1447 path_two
= quote_one(path_two
);
1451 sprintf(status
, "%c%03d", p
->status
,
1452 (int)(0.5 + p
->score
* 100.0/MAX_SCORE
));
1454 status
[0] = p
->status
;
1457 switch (p
->status
) {
1458 case DIFF_STATUS_COPIED
:
1459 case DIFF_STATUS_RENAMED
:
1462 case DIFF_STATUS_ADDED
:
1463 case DIFF_STATUS_DELETED
:
1470 if (output_format
!= DIFF_FORMAT_NAME_STATUS
) {
1471 printf(":%06o %06o %s ",
1472 p
->one
->mode
, p
->two
->mode
,
1473 diff_unique_abbrev(p
->one
->sha1
, abbrev
));
1475 diff_unique_abbrev(p
->two
->sha1
, abbrev
));
1477 printf("%s%c%s", status
, inter_name_termination
, path_one
);
1479 printf("%c%s", inter_name_termination
, path_two
);
1480 putchar(line_termination
);
1481 if (path_one
!= p
->one
->path
)
1482 free((void*)path_one
);
1483 if (path_two
!= p
->two
->path
)
1484 free((void*)path_two
);
1487 static void diff_flush_name(struct diff_filepair
*p
,
1488 int inter_name_termination
,
1489 int line_termination
)
1491 char *path
= p
->two
->path
;
1493 if (line_termination
)
1494 path
= quote_one(p
->two
->path
);
1496 path
= p
->two
->path
;
1497 printf("%s%c", path
, line_termination
);
1498 if (p
->two
->path
!= path
)
1502 int diff_unmodified_pair(struct diff_filepair
*p
)
1504 /* This function is written stricter than necessary to support
1505 * the currently implemented transformers, but the idea is to
1506 * let transformers to produce diff_filepairs any way they want,
1507 * and filter and clean them up here before producing the output.
1509 struct diff_filespec
*one
, *two
;
1511 if (DIFF_PAIR_UNMERGED(p
))
1512 return 0; /* unmerged is interesting */
1517 /* deletion, addition, mode or type change
1518 * and rename are all interesting.
1520 if (DIFF_FILE_VALID(one
) != DIFF_FILE_VALID(two
) ||
1521 DIFF_PAIR_MODE_CHANGED(p
) ||
1522 strcmp(one
->path
, two
->path
))
1525 /* both are valid and point at the same path. that is, we are
1526 * dealing with a change.
1528 if (one
->sha1_valid
&& two
->sha1_valid
&&
1529 !memcmp(one
->sha1
, two
->sha1
, sizeof(one
->sha1
)))
1530 return 1; /* no change */
1531 if (!one
->sha1_valid
&& !two
->sha1_valid
)
1532 return 1; /* both look at the same file on the filesystem. */
1536 static void diff_flush_patch(struct diff_filepair
*p
, struct diff_options
*o
)
1538 if (diff_unmodified_pair(p
))
1541 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
1542 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
1543 return; /* no tree diffs in patch format */
1548 static void diff_flush_stat(struct diff_filepair
*p
, struct diff_options
*o
,
1549 struct diffstat_t
*diffstat
)
1551 if (diff_unmodified_pair(p
))
1554 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
1555 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
1556 return; /* no tree diffs in patch format */
1558 run_diffstat(p
, o
, diffstat
);
1561 int diff_queue_is_empty(void)
1563 struct diff_queue_struct
*q
= &diff_queued_diff
;
1565 for (i
= 0; i
< q
->nr
; i
++)
1566 if (!diff_unmodified_pair(q
->queue
[i
]))
1572 void diff_debug_filespec(struct diff_filespec
*s
, int x
, const char *one
)
1574 fprintf(stderr
, "queue[%d] %s (%s) %s %06o %s\n",
1577 DIFF_FILE_VALID(s
) ? "valid" : "invalid",
1579 s
->sha1_valid
? sha1_to_hex(s
->sha1
) : "");
1580 fprintf(stderr
, "queue[%d] %s size %lu flags %d\n",
1582 s
->size
, s
->xfrm_flags
);
1585 void diff_debug_filepair(const struct diff_filepair
*p
, int i
)
1587 diff_debug_filespec(p
->one
, i
, "one");
1588 diff_debug_filespec(p
->two
, i
, "two");
1589 fprintf(stderr
, "score %d, status %c stays %d broken %d\n",
1590 p
->score
, p
->status
? p
->status
: '?',
1591 p
->source_stays
, p
->broken_pair
);
1594 void diff_debug_queue(const char *msg
, struct diff_queue_struct
*q
)
1598 fprintf(stderr
, "%s\n", msg
);
1599 fprintf(stderr
, "q->nr = %d\n", q
->nr
);
1600 for (i
= 0; i
< q
->nr
; i
++) {
1601 struct diff_filepair
*p
= q
->queue
[i
];
1602 diff_debug_filepair(p
, i
);
1607 static void diff_resolve_rename_copy(void)
1610 struct diff_filepair
*p
, *pp
;
1611 struct diff_queue_struct
*q
= &diff_queued_diff
;
1613 diff_debug_queue("resolve-rename-copy", q
);
1615 for (i
= 0; i
< q
->nr
; i
++) {
1617 p
->status
= 0; /* undecided */
1618 if (DIFF_PAIR_UNMERGED(p
))
1619 p
->status
= DIFF_STATUS_UNMERGED
;
1620 else if (!DIFF_FILE_VALID(p
->one
))
1621 p
->status
= DIFF_STATUS_ADDED
;
1622 else if (!DIFF_FILE_VALID(p
->two
))
1623 p
->status
= DIFF_STATUS_DELETED
;
1624 else if (DIFF_PAIR_TYPE_CHANGED(p
))
1625 p
->status
= DIFF_STATUS_TYPE_CHANGED
;
1627 /* from this point on, we are dealing with a pair
1628 * whose both sides are valid and of the same type, i.e.
1629 * either in-place edit or rename/copy edit.
1631 else if (DIFF_PAIR_RENAME(p
)) {
1632 if (p
->source_stays
) {
1633 p
->status
= DIFF_STATUS_COPIED
;
1636 /* See if there is some other filepair that
1637 * copies from the same source as us. If so
1638 * we are a copy. Otherwise we are either a
1639 * copy if the path stays, or a rename if it
1640 * does not, but we already handled "stays" case.
1642 for (j
= i
+ 1; j
< q
->nr
; j
++) {
1644 if (strcmp(pp
->one
->path
, p
->one
->path
))
1645 continue; /* not us */
1646 if (!DIFF_PAIR_RENAME(pp
))
1647 continue; /* not a rename/copy */
1648 /* pp is a rename/copy from the same source */
1649 p
->status
= DIFF_STATUS_COPIED
;
1653 p
->status
= DIFF_STATUS_RENAMED
;
1655 else if (memcmp(p
->one
->sha1
, p
->two
->sha1
, 20) ||
1656 p
->one
->mode
!= p
->two
->mode
)
1657 p
->status
= DIFF_STATUS_MODIFIED
;
1659 /* This is a "no-change" entry and should not
1660 * happen anymore, but prepare for broken callers.
1662 error("feeding unmodified %s to diffcore",
1664 p
->status
= DIFF_STATUS_UNKNOWN
;
1667 diff_debug_queue("resolve-rename-copy done", q
);
1670 static void flush_one_pair(struct diff_filepair
*p
,
1671 int diff_output_format
,
1672 struct diff_options
*options
,
1673 struct diffstat_t
*diffstat
)
1675 int inter_name_termination
= '\t';
1676 int line_termination
= options
->line_termination
;
1677 if (!line_termination
)
1678 inter_name_termination
= 0;
1680 switch (p
->status
) {
1681 case DIFF_STATUS_UNKNOWN
:
1684 die("internal error in diff-resolve-rename-copy");
1687 switch (diff_output_format
) {
1688 case DIFF_FORMAT_DIFFSTAT
:
1689 diff_flush_stat(p
, options
, diffstat
);
1691 case DIFF_FORMAT_PATCH
:
1692 diff_flush_patch(p
, options
);
1694 case DIFF_FORMAT_RAW
:
1695 case DIFF_FORMAT_NAME_STATUS
:
1696 diff_flush_raw(p
, line_termination
,
1697 inter_name_termination
,
1698 options
, diff_output_format
);
1700 case DIFF_FORMAT_NAME
:
1702 inter_name_termination
,
1705 case DIFF_FORMAT_NO_OUTPUT
:
1711 void diff_flush(struct diff_options
*options
)
1713 struct diff_queue_struct
*q
= &diff_queued_diff
;
1715 int diff_output_format
= options
->output_format
;
1716 struct diffstat_t
*diffstat
= NULL
;
1718 if (diff_output_format
== DIFF_FORMAT_DIFFSTAT
|| options
->with_stat
) {
1719 diffstat
= xcalloc(sizeof (struct diffstat_t
), 1);
1720 diffstat
->xm
.consume
= diffstat_consume
;
1723 if (options
->with_raw
) {
1724 for (i
= 0; i
< q
->nr
; i
++) {
1725 struct diff_filepair
*p
= q
->queue
[i
];
1726 flush_one_pair(p
, DIFF_FORMAT_RAW
, options
, NULL
);
1728 putchar(options
->line_termination
);
1730 if (options
->with_stat
) {
1731 for (i
= 0; i
< q
->nr
; i
++) {
1732 struct diff_filepair
*p
= q
->queue
[i
];
1733 flush_one_pair(p
, DIFF_FORMAT_DIFFSTAT
, options
,
1736 show_stats(diffstat
);
1739 putchar(options
->line_termination
);
1741 for (i
= 0; i
< q
->nr
; i
++) {
1742 struct diff_filepair
*p
= q
->queue
[i
];
1743 flush_one_pair(p
, diff_output_format
, options
, diffstat
);
1744 diff_free_filepair(p
);
1748 show_stats(diffstat
);
1754 q
->nr
= q
->alloc
= 0;
1757 static void diffcore_apply_filter(const char *filter
)
1760 struct diff_queue_struct
*q
= &diff_queued_diff
;
1761 struct diff_queue_struct outq
;
1763 outq
.nr
= outq
.alloc
= 0;
1768 if (strchr(filter
, DIFF_STATUS_FILTER_AON
)) {
1770 for (i
= found
= 0; !found
&& i
< q
->nr
; i
++) {
1771 struct diff_filepair
*p
= q
->queue
[i
];
1772 if (((p
->status
== DIFF_STATUS_MODIFIED
) &&
1774 strchr(filter
, DIFF_STATUS_FILTER_BROKEN
)) ||
1776 strchr(filter
, DIFF_STATUS_MODIFIED
)))) ||
1777 ((p
->status
!= DIFF_STATUS_MODIFIED
) &&
1778 strchr(filter
, p
->status
)))
1784 /* otherwise we will clear the whole queue
1785 * by copying the empty outq at the end of this
1786 * function, but first clear the current entries
1789 for (i
= 0; i
< q
->nr
; i
++)
1790 diff_free_filepair(q
->queue
[i
]);
1793 /* Only the matching ones */
1794 for (i
= 0; i
< q
->nr
; i
++) {
1795 struct diff_filepair
*p
= q
->queue
[i
];
1797 if (((p
->status
== DIFF_STATUS_MODIFIED
) &&
1799 strchr(filter
, DIFF_STATUS_FILTER_BROKEN
)) ||
1801 strchr(filter
, DIFF_STATUS_MODIFIED
)))) ||
1802 ((p
->status
!= DIFF_STATUS_MODIFIED
) &&
1803 strchr(filter
, p
->status
)))
1806 diff_free_filepair(p
);
1813 void diffcore_std(struct diff_options
*options
)
1815 if (options
->break_opt
!= -1)
1816 diffcore_break(options
->break_opt
);
1817 if (options
->detect_rename
)
1818 diffcore_rename(options
);
1819 if (options
->break_opt
!= -1)
1820 diffcore_merge_broken();
1821 if (options
->pickaxe
)
1822 diffcore_pickaxe(options
->pickaxe
, options
->pickaxe_opts
);
1823 if (options
->orderfile
)
1824 diffcore_order(options
->orderfile
);
1825 diff_resolve_rename_copy();
1826 diffcore_apply_filter(options
->filter
);
1830 void diffcore_std_no_resolve(struct diff_options
*options
)
1832 if (options
->pickaxe
)
1833 diffcore_pickaxe(options
->pickaxe
, options
->pickaxe_opts
);
1834 if (options
->orderfile
)
1835 diffcore_order(options
->orderfile
);
1836 diffcore_apply_filter(options
->filter
);
1839 void diff_addremove(struct diff_options
*options
,
1840 int addremove
, unsigned mode
,
1841 const unsigned char *sha1
,
1842 const char *base
, const char *path
)
1844 char concatpath
[PATH_MAX
];
1845 struct diff_filespec
*one
, *two
;
1847 /* This may look odd, but it is a preparation for
1848 * feeding "there are unchanged files which should
1849 * not produce diffs, but when you are doing copy
1850 * detection you would need them, so here they are"
1851 * entries to the diff-core. They will be prefixed
1852 * with something like '=' or '*' (I haven't decided
1853 * which but should not make any difference).
1854 * Feeding the same new and old to diff_change()
1855 * also has the same effect.
1856 * Before the final output happens, they are pruned after
1857 * merged into rename/copy pairs as appropriate.
1859 if (options
->reverse_diff
)
1860 addremove
= (addremove
== '+' ? '-' :
1861 addremove
== '-' ? '+' : addremove
);
1863 if (!path
) path
= "";
1864 sprintf(concatpath
, "%s%s", base
, path
);
1865 one
= alloc_filespec(concatpath
);
1866 two
= alloc_filespec(concatpath
);
1868 if (addremove
!= '+')
1869 fill_filespec(one
, sha1
, mode
);
1870 if (addremove
!= '-')
1871 fill_filespec(two
, sha1
, mode
);
1873 diff_queue(&diff_queued_diff
, one
, two
);
1876 void diff_change(struct diff_options
*options
,
1877 unsigned old_mode
, unsigned new_mode
,
1878 const unsigned char *old_sha1
,
1879 const unsigned char *new_sha1
,
1880 const char *base
, const char *path
)
1882 char concatpath
[PATH_MAX
];
1883 struct diff_filespec
*one
, *two
;
1885 if (options
->reverse_diff
) {
1887 const unsigned char *tmp_c
;
1888 tmp
= old_mode
; old_mode
= new_mode
; new_mode
= tmp
;
1889 tmp_c
= old_sha1
; old_sha1
= new_sha1
; new_sha1
= tmp_c
;
1891 if (!path
) path
= "";
1892 sprintf(concatpath
, "%s%s", base
, path
);
1893 one
= alloc_filespec(concatpath
);
1894 two
= alloc_filespec(concatpath
);
1895 fill_filespec(one
, old_sha1
, old_mode
);
1896 fill_filespec(two
, new_sha1
, new_mode
);
1898 diff_queue(&diff_queued_diff
, one
, two
);
1901 void diff_unmerge(struct diff_options
*options
,
1904 struct diff_filespec
*one
, *two
;
1905 one
= alloc_filespec(path
);
1906 two
= alloc_filespec(path
);
1907 diff_queue(&diff_queued_diff
, one
, two
);