2 * Copyright (C) 2005 Junio C Hamano
11 #include "xdiff-interface.h"
13 static int use_size_cache
;
15 int diff_rename_limit_default
= -1;
17 int git_diff_config(const char *var
, const char *value
)
19 if (!strcmp(var
, "diff.renamelimit")) {
20 diff_rename_limit_default
= git_config_int(var
, value
);
24 return git_default_config(var
, value
);
27 static char *quote_one(const char *str
)
34 needlen
= quote_c_style(str
, NULL
, NULL
, 0);
37 xp
= xmalloc(needlen
+ 1);
38 quote_c_style(str
, xp
, NULL
, 0);
42 static char *quote_two(const char *one
, const char *two
)
44 int need_one
= quote_c_style(one
, NULL
, NULL
, 1);
45 int need_two
= quote_c_style(two
, NULL
, NULL
, 1);
48 if (need_one
+ need_two
) {
49 if (!need_one
) need_one
= strlen(one
);
50 if (!need_two
) need_one
= strlen(two
);
52 xp
= xmalloc(need_one
+ need_two
+ 3);
54 quote_c_style(one
, xp
+ 1, NULL
, 1);
55 quote_c_style(two
, xp
+ need_one
+ 1, NULL
, 1);
56 strcpy(xp
+ need_one
+ need_two
+ 1, "\"");
59 need_one
= strlen(one
);
60 need_two
= strlen(two
);
61 xp
= xmalloc(need_one
+ need_two
+ 1);
63 strcpy(xp
+ need_one
, two
);
67 static const char *external_diff(void)
69 static const char *external_diff_cmd
= NULL
;
70 static int done_preparing
= 0;
73 return external_diff_cmd
;
74 external_diff_cmd
= getenv("GIT_EXTERNAL_DIFF");
76 return external_diff_cmd
;
79 #define TEMPFILE_PATH_LEN 50
81 static struct diff_tempfile
{
82 const char *name
; /* filename external diff should read from */
85 char tmp_path
[TEMPFILE_PATH_LEN
];
88 static int count_lines(const char *data
, int size
)
90 int count
, ch
, completely_empty
= 1, nl_just_seen
= 0;
101 completely_empty
= 0;
104 if (completely_empty
)
107 count
++; /* no trailing newline */
111 static void print_line_count(int count
)
121 printf("1,%d", count
);
126 static void copy_file(int prefix
, const char *data
, int size
)
128 int ch
, nl_just_seen
= 1;
140 printf("\n\\ No newline at end of file\n");
143 static void emit_rewrite_diff(const char *name_a
,
145 struct diff_filespec
*one
,
146 struct diff_filespec
*two
)
149 diff_populate_filespec(one
, 0);
150 diff_populate_filespec(two
, 0);
151 lc_a
= count_lines(one
->data
, one
->size
);
152 lc_b
= count_lines(two
->data
, two
->size
);
153 printf("--- %s\n+++ %s\n@@ -", name_a
, name_b
);
154 print_line_count(lc_a
);
156 print_line_count(lc_b
);
159 copy_file('-', one
->data
, one
->size
);
161 copy_file('+', two
->data
, two
->size
);
164 static int fill_mmfile(mmfile_t
*mf
, struct diff_filespec
*one
)
166 if (!DIFF_FILE_VALID(one
)) {
167 mf
->ptr
= ""; /* does not matter */
171 else if (diff_populate_filespec(one
, 0))
174 mf
->size
= one
->size
;
178 struct emit_callback
{
179 const char **label_path
;
182 static int fn_out(void *priv
, mmbuffer_t
*mb
, int nbuf
)
185 struct emit_callback
*ecbdata
= priv
;
187 if (ecbdata
->label_path
[0]) {
188 printf("--- %s\n", ecbdata
->label_path
[0]);
189 printf("+++ %s\n", ecbdata
->label_path
[1]);
190 ecbdata
->label_path
[0] = ecbdata
->label_path
[1] = NULL
;
192 for (i
= 0; i
< nbuf
; i
++)
193 if (!fwrite(mb
[i
].ptr
, mb
[i
].size
, 1, stdout
))
198 static char *pprint_rename(const char *a
, const char *b
)
203 int pfx_length
, sfx_length
;
204 int len_a
= strlen(a
);
205 int len_b
= strlen(b
);
207 /* Find common prefix */
209 while (*old
&& *new && *old
== *new) {
211 pfx_length
= old
- a
+ 1;
216 /* Find common suffix */
220 while (a
<= old
&& b
<= new && *old
== *new) {
222 sfx_length
= len_a
- (old
- a
);
228 * pfx{mid-a => mid-b}sfx
229 * {pfx-a => pfx-b}sfx
230 * pfx{sfx-a => sfx-b}
233 if (pfx_length
+ sfx_length
) {
234 name
= xmalloc(len_a
+ len_b
- pfx_length
- sfx_length
+ 7);
235 sprintf(name
, "%.*s{%.*s => %.*s}%s",
237 len_a
- pfx_length
- sfx_length
, a
+ pfx_length
,
238 len_b
- pfx_length
- sfx_length
, b
+ pfx_length
,
239 a
+ len_a
- sfx_length
);
242 name
= xmalloc(len_a
+ len_b
+ 5);
243 sprintf(name
, "%s => %s", a
, b
);
249 struct xdiff_emit_state xm
;
253 struct diffstat_file
{
255 unsigned is_unmerged
:1;
256 unsigned is_binary
:1;
257 unsigned is_renamed
:1;
258 unsigned int added
, deleted
;
262 static struct diffstat_file
*diffstat_add(struct diffstat_t
*diffstat
,
266 struct diffstat_file
*x
;
267 x
= xcalloc(sizeof (*x
), 1);
268 if (diffstat
->nr
== diffstat
->alloc
) {
269 diffstat
->alloc
= alloc_nr(diffstat
->alloc
);
270 diffstat
->files
= xrealloc(diffstat
->files
,
271 diffstat
->alloc
* sizeof(x
));
273 diffstat
->files
[diffstat
->nr
++] = x
;
275 x
->name
= pprint_rename(name_a
, name_b
);
279 x
->name
= strdup(name_a
);
283 static void diffstat_consume(void *priv
, char *line
, unsigned long len
)
285 struct diffstat_t
*diffstat
= priv
;
286 struct diffstat_file
*x
= diffstat
->files
[diffstat
->nr
- 1];
290 else if (line
[0] == '-')
294 static const char pluses
[] = "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++";
295 static const char minuses
[]= "----------------------------------------------------------------------";
297 static void show_stats(struct diffstat_t
* data
)
300 int i
, len
, add
, del
, total
, adds
= 0, dels
= 0;
301 int max
, max_change
= 0, max_len
= 0;
302 int total_files
= data
->nr
;
307 for (i
= 0; i
< data
->nr
; i
++) {
308 struct diffstat_file
*file
= data
->files
[i
];
310 len
= strlen(file
->name
);
314 if (file
->is_binary
|| file
->is_unmerged
)
316 if (max_change
< file
->added
+ file
->deleted
)
317 max_change
= file
->added
+ file
->deleted
;
320 for (i
= 0; i
< data
->nr
; i
++) {
321 char *name
= data
->files
[i
]->name
;
322 int added
= data
->files
[i
]->added
;
323 int deleted
= data
->files
[i
]->deleted
;
325 if (0 < (len
= quote_c_style(name
, NULL
, NULL
, 0))) {
326 char *qname
= xmalloc(len
+ 1);
327 quote_c_style(name
, qname
, NULL
, 0);
329 data
->files
[i
]->name
= name
= qname
;
333 * "scale" the filename
344 slash
= strchr(name
, '/');
351 * scale the add/delete
357 if (data
->files
[i
]->is_binary
) {
358 printf(" %s%-*s | Bin\n", prefix
, len
, name
);
359 goto free_diffstat_file
;
361 else if (data
->files
[i
]->is_unmerged
) {
362 printf(" %s%-*s | Unmerged\n", prefix
, len
, name
);
363 goto free_diffstat_file
;
365 else if (!data
->files
[i
]->is_renamed
&&
366 (added
+ deleted
== 0)) {
368 goto free_diffstat_file
;
377 if (max_change
> 0) {
378 total
= (total
* max
+ max_change
/ 2) / max_change
;
379 add
= (add
* max
+ max_change
/ 2) / max_change
;
382 printf(" %s%-*s |%5d %.*s%.*s\n", prefix
,
383 len
, name
, added
+ deleted
,
384 add
, pluses
, del
, minuses
);
386 free(data
->files
[i
]->name
);
387 free(data
->files
[i
]);
390 printf(" %d files changed, %d insertions(+), %d deletions(-)\n",
391 total_files
, adds
, dels
);
394 #define FIRST_FEW_BYTES 8000
395 static int mmfile_is_binary(mmfile_t
*mf
)
398 if (FIRST_FEW_BYTES
< sz
)
399 sz
= FIRST_FEW_BYTES
;
400 if (memchr(mf
->ptr
, 0, sz
))
405 static void builtin_diff(const char *name_a
,
407 struct diff_filespec
*one
,
408 struct diff_filespec
*two
,
409 const char *xfrm_msg
,
410 int complete_rewrite
)
416 a_one
= quote_two("a/", name_a
);
417 b_two
= quote_two("b/", name_b
);
418 lbl
[0] = DIFF_FILE_VALID(one
) ? a_one
: "/dev/null";
419 lbl
[1] = DIFF_FILE_VALID(two
) ? b_two
: "/dev/null";
420 printf("diff --git %s %s\n", a_one
, b_two
);
421 if (lbl
[0][0] == '/') {
423 printf("new file mode %06o\n", two
->mode
);
424 if (xfrm_msg
&& xfrm_msg
[0])
427 else if (lbl
[1][0] == '/') {
428 printf("deleted file mode %06o\n", one
->mode
);
429 if (xfrm_msg
&& xfrm_msg
[0])
433 if (one
->mode
!= two
->mode
) {
434 printf("old mode %06o\n", one
->mode
);
435 printf("new mode %06o\n", two
->mode
);
437 if (xfrm_msg
&& xfrm_msg
[0])
440 * we do not run diff between different kind
443 if ((one
->mode
^ two
->mode
) & S_IFMT
)
444 goto free_ab_and_return
;
445 if (complete_rewrite
) {
446 emit_rewrite_diff(name_a
, name_b
, one
, two
);
447 goto free_ab_and_return
;
451 if (fill_mmfile(&mf1
, one
) < 0 || fill_mmfile(&mf2
, two
) < 0)
452 die("unable to read files to diff");
454 if (mmfile_is_binary(&mf1
) || mmfile_is_binary(&mf2
))
455 printf("Binary files %s and %s differ\n", lbl
[0], lbl
[1]);
457 /* Crazy xdl interfaces.. */
458 const char *diffopts
= getenv("GIT_DIFF_OPTS");
462 struct emit_callback ecbdata
;
464 ecbdata
.label_path
= lbl
;
465 xpp
.flags
= XDF_NEED_MINIMAL
;
467 xecfg
.flags
= XDL_EMIT_FUNCNAMES
;
470 else if (!strncmp(diffopts
, "--unified=", 10))
471 xecfg
.ctxlen
= strtoul(diffopts
+ 10, NULL
, 10);
472 else if (!strncmp(diffopts
, "-u", 2))
473 xecfg
.ctxlen
= strtoul(diffopts
+ 2, NULL
, 10);
476 xdl_diff(&mf1
, &mf2
, &xpp
, &xecfg
, &ecb
);
485 static void builtin_diffstat(const char *name_a
, const char *name_b
,
486 struct diff_filespec
*one
,
487 struct diff_filespec
*two
,
488 struct diffstat_t
*diffstat
,
489 int complete_rewrite
)
492 struct diffstat_file
*data
;
494 data
= diffstat_add(diffstat
, name_a
, name_b
);
497 data
->is_unmerged
= 1;
500 if (complete_rewrite
) {
501 diff_populate_filespec(one
, 0);
502 diff_populate_filespec(two
, 0);
503 data
->deleted
= count_lines(one
->data
, one
->size
);
504 data
->added
= count_lines(two
->data
, two
->size
);
507 if (fill_mmfile(&mf1
, one
) < 0 || fill_mmfile(&mf2
, two
) < 0)
508 die("unable to read files to diff");
510 if (mmfile_is_binary(&mf1
) || mmfile_is_binary(&mf2
))
513 /* Crazy xdl interfaces.. */
518 xpp
.flags
= XDF_NEED_MINIMAL
;
521 ecb
.outf
= xdiff_outf
;
523 xdl_diff(&mf1
, &mf2
, &xpp
, &xecfg
, &ecb
);
527 struct diff_filespec
*alloc_filespec(const char *path
)
529 int namelen
= strlen(path
);
530 struct diff_filespec
*spec
= xmalloc(sizeof(*spec
) + namelen
+ 1);
532 memset(spec
, 0, sizeof(*spec
));
533 spec
->path
= (char *)(spec
+ 1);
534 memcpy(spec
->path
, path
, namelen
+1);
538 void fill_filespec(struct diff_filespec
*spec
, const unsigned char *sha1
,
542 spec
->mode
= canon_mode(mode
);
543 memcpy(spec
->sha1
, sha1
, 20);
544 spec
->sha1_valid
= !!memcmp(sha1
, null_sha1
, 20);
549 * Given a name and sha1 pair, if the dircache tells us the file in
550 * the work tree has that object contents, return true, so that
551 * prepare_temp_file() does not have to inflate and extract.
553 static int work_tree_matches(const char *name
, const unsigned char *sha1
)
555 struct cache_entry
*ce
;
559 /* We do not read the cache ourselves here, because the
560 * benchmark with my previous version that always reads cache
561 * shows that it makes things worse for diff-tree comparing
562 * two linux-2.6 kernel trees in an already checked out work
563 * tree. This is because most diff-tree comparisons deal with
564 * only a small number of files, while reading the cache is
565 * expensive for a large project, and its cost outweighs the
566 * savings we get by not inflating the object to a temporary
567 * file. Practically, this code only helps when we are used
568 * by diff-cache --cached, which does read the cache before
575 pos
= cache_name_pos(name
, len
);
578 ce
= active_cache
[pos
];
579 if ((lstat(name
, &st
) < 0) ||
580 !S_ISREG(st
.st_mode
) || /* careful! */
581 ce_match_stat(ce
, &st
, 0) ||
582 memcmp(sha1
, ce
->sha1
, 20))
584 /* we return 1 only when we can stat, it is a regular file,
585 * stat information matches, and sha1 recorded in the cache
586 * matches. I.e. we know the file in the work tree really is
587 * the same as the <name, sha1> pair.
592 static struct sha1_size_cache
{
593 unsigned char sha1
[20];
596 static int sha1_size_cache_nr
, sha1_size_cache_alloc
;
598 static struct sha1_size_cache
*locate_size_cache(unsigned char *sha1
,
603 struct sha1_size_cache
*e
;
606 last
= sha1_size_cache_nr
;
607 while (last
> first
) {
608 int cmp
, next
= (last
+ first
) >> 1;
609 e
= sha1_size_cache
[next
];
610 cmp
= memcmp(e
->sha1
, sha1
, 20);
622 /* insert to make it at "first" */
623 if (sha1_size_cache_alloc
<= sha1_size_cache_nr
) {
624 sha1_size_cache_alloc
= alloc_nr(sha1_size_cache_alloc
);
625 sha1_size_cache
= xrealloc(sha1_size_cache
,
626 sha1_size_cache_alloc
*
627 sizeof(*sha1_size_cache
));
629 sha1_size_cache_nr
++;
630 if (first
< sha1_size_cache_nr
)
631 memmove(sha1_size_cache
+ first
+ 1, sha1_size_cache
+ first
,
632 (sha1_size_cache_nr
- first
- 1) *
633 sizeof(*sha1_size_cache
));
634 e
= xmalloc(sizeof(struct sha1_size_cache
));
635 sha1_size_cache
[first
] = e
;
636 memcpy(e
->sha1
, sha1
, 20);
642 * While doing rename detection and pickaxe operation, we may need to
643 * grab the data for the blob (or file) for our own in-core comparison.
644 * diff_filespec has data and size fields for this purpose.
646 int diff_populate_filespec(struct diff_filespec
*s
, int size_only
)
649 if (!DIFF_FILE_VALID(s
))
650 die("internal error: asking to populate invalid file.");
651 if (S_ISDIR(s
->mode
))
659 if (!s
->sha1_valid
||
660 work_tree_matches(s
->path
, s
->sha1
)) {
663 if (lstat(s
->path
, &st
) < 0) {
664 if (errno
== ENOENT
) {
673 s
->size
= st
.st_size
;
678 if (S_ISLNK(st
.st_mode
)) {
680 s
->data
= xmalloc(s
->size
);
682 ret
= readlink(s
->path
, s
->data
, s
->size
);
689 fd
= open(s
->path
, O_RDONLY
);
692 s
->data
= mmap(NULL
, s
->size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
694 if (s
->data
== MAP_FAILED
)
696 s
->should_munmap
= 1;
700 struct sha1_size_cache
*e
;
703 e
= locate_size_cache(s
->sha1
, 1, 0);
708 if (!sha1_object_info(s
->sha1
, type
, &s
->size
))
709 locate_size_cache(s
->sha1
, 0, s
->size
);
712 s
->data
= read_sha1_file(s
->sha1
, type
, &s
->size
);
719 void diff_free_filespec_data(struct diff_filespec
*s
)
723 else if (s
->should_munmap
)
724 munmap(s
->data
, s
->size
);
725 s
->should_free
= s
->should_munmap
= 0;
731 static void prep_temp_blob(struct diff_tempfile
*temp
,
734 const unsigned char *sha1
,
739 fd
= git_mkstemp(temp
->tmp_path
, TEMPFILE_PATH_LEN
, ".diff_XXXXXX");
741 die("unable to create temp-file");
742 if (write(fd
, blob
, size
) != size
)
743 die("unable to write temp-file");
745 temp
->name
= temp
->tmp_path
;
746 strcpy(temp
->hex
, sha1_to_hex(sha1
));
748 sprintf(temp
->mode
, "%06o", mode
);
751 static void prepare_temp_file(const char *name
,
752 struct diff_tempfile
*temp
,
753 struct diff_filespec
*one
)
755 if (!DIFF_FILE_VALID(one
)) {
757 /* A '-' entry produces this for file-2, and
758 * a '+' entry produces this for file-1.
760 temp
->name
= "/dev/null";
761 strcpy(temp
->hex
, ".");
762 strcpy(temp
->mode
, ".");
766 if (!one
->sha1_valid
||
767 work_tree_matches(name
, one
->sha1
)) {
769 if (lstat(name
, &st
) < 0) {
771 goto not_a_valid_file
;
772 die("stat(%s): %s", name
, strerror(errno
));
774 if (S_ISLNK(st
.st_mode
)) {
776 char buf
[PATH_MAX
+ 1]; /* ought to be SYMLINK_MAX */
777 if (sizeof(buf
) <= st
.st_size
)
778 die("symlink too long: %s", name
);
779 ret
= readlink(name
, buf
, st
.st_size
);
781 die("readlink(%s)", name
);
782 prep_temp_blob(temp
, buf
, st
.st_size
,
784 one
->sha1
: null_sha1
),
786 one
->mode
: S_IFLNK
));
789 /* we can borrow from the file in the work tree */
791 if (!one
->sha1_valid
)
792 strcpy(temp
->hex
, sha1_to_hex(null_sha1
));
794 strcpy(temp
->hex
, sha1_to_hex(one
->sha1
));
795 /* Even though we may sometimes borrow the
796 * contents from the work tree, we always want
797 * one->mode. mode is trustworthy even when
798 * !(one->sha1_valid), as long as
799 * DIFF_FILE_VALID(one).
801 sprintf(temp
->mode
, "%06o", one
->mode
);
806 if (diff_populate_filespec(one
, 0))
807 die("cannot read data blob for %s", one
->path
);
808 prep_temp_blob(temp
, one
->data
, one
->size
,
809 one
->sha1
, one
->mode
);
813 static void remove_tempfile(void)
817 for (i
= 0; i
< 2; i
++)
818 if (diff_temp
[i
].name
== diff_temp
[i
].tmp_path
) {
819 unlink(diff_temp
[i
].name
);
820 diff_temp
[i
].name
= NULL
;
824 static void remove_tempfile_on_signal(int signo
)
827 signal(SIGINT
, SIG_DFL
);
831 static int spawn_prog(const char *pgm
, const char **arg
)
839 die("unable to fork");
841 execvp(pgm
, (char *const*) arg
);
845 while (waitpid(pid
, &status
, 0) < 0) {
851 /* Earlier we did not check the exit status because
852 * diff exits non-zero if files are different, and
853 * we are not interested in knowing that. It was a
854 * mistake which made it harder to quit a diff-*
855 * session that uses the git-apply-patch-script as
856 * the GIT_EXTERNAL_DIFF. A custom GIT_EXTERNAL_DIFF
857 * should also exit non-zero only when it wants to
858 * abort the entire diff-* session.
860 if (WIFEXITED(status
) && !WEXITSTATUS(status
))
865 /* An external diff command takes:
867 * diff-cmd name infile1 infile1-sha1 infile1-mode \
868 * infile2 infile2-sha1 infile2-mode [ rename-to ]
871 static void run_external_diff(const char *pgm
,
874 struct diff_filespec
*one
,
875 struct diff_filespec
*two
,
876 const char *xfrm_msg
,
877 int complete_rewrite
)
879 const char *spawn_arg
[10];
880 struct diff_tempfile
*temp
= diff_temp
;
882 static int atexit_asked
= 0;
883 const char *othername
;
884 const char **arg
= &spawn_arg
[0];
886 othername
= (other
? other
: name
);
888 prepare_temp_file(name
, &temp
[0], one
);
889 prepare_temp_file(othername
, &temp
[1], two
);
890 if (! atexit_asked
&&
891 (temp
[0].name
== temp
[0].tmp_path
||
892 temp
[1].name
== temp
[1].tmp_path
)) {
894 atexit(remove_tempfile
);
896 signal(SIGINT
, remove_tempfile_on_signal
);
902 *arg
++ = temp
[0].name
;
903 *arg
++ = temp
[0].hex
;
904 *arg
++ = temp
[0].mode
;
905 *arg
++ = temp
[1].name
;
906 *arg
++ = temp
[1].hex
;
907 *arg
++ = temp
[1].mode
;
917 retval
= spawn_prog(pgm
, spawn_arg
);
920 fprintf(stderr
, "external diff died, stopping at %s.\n", name
);
925 static void run_diff_cmd(const char *pgm
,
928 struct diff_filespec
*one
,
929 struct diff_filespec
*two
,
930 const char *xfrm_msg
,
931 int complete_rewrite
)
934 run_external_diff(pgm
, name
, other
, one
, two
, xfrm_msg
,
939 builtin_diff(name
, other
? other
: name
,
940 one
, two
, xfrm_msg
, complete_rewrite
);
942 printf("* Unmerged path %s\n", name
);
945 static void diff_fill_sha1_info(struct diff_filespec
*one
)
947 if (DIFF_FILE_VALID(one
)) {
948 if (!one
->sha1_valid
) {
950 if (lstat(one
->path
, &st
) < 0)
951 die("stat %s", one
->path
);
952 if (index_path(one
->sha1
, one
->path
, &st
, 0))
953 die("cannot hash %s\n", one
->path
);
957 memset(one
->sha1
, 0, 20);
960 static void run_diff(struct diff_filepair
*p
, struct diff_options
*o
)
962 const char *pgm
= external_diff();
963 char msg
[PATH_MAX
*2+300], *xfrm_msg
;
964 struct diff_filespec
*one
;
965 struct diff_filespec
*two
;
968 char *name_munged
, *other_munged
;
969 int complete_rewrite
= 0;
972 if (DIFF_PAIR_UNMERGED(p
)) {
974 run_diff_cmd(pgm
, p
->one
->path
, NULL
, NULL
, NULL
, NULL
, 0);
979 other
= (strcmp(name
, p
->two
->path
) ? p
->two
->path
: NULL
);
980 name_munged
= quote_one(name
);
981 other_munged
= quote_one(other
);
982 one
= p
->one
; two
= p
->two
;
984 diff_fill_sha1_info(one
);
985 diff_fill_sha1_info(two
);
989 case DIFF_STATUS_COPIED
:
990 len
+= snprintf(msg
+ len
, sizeof(msg
) - len
,
991 "similarity index %d%%\n"
994 (int)(0.5 + p
->score
* 100.0/MAX_SCORE
),
995 name_munged
, other_munged
);
997 case DIFF_STATUS_RENAMED
:
998 len
+= snprintf(msg
+ len
, sizeof(msg
) - len
,
999 "similarity index %d%%\n"
1002 (int)(0.5 + p
->score
* 100.0/MAX_SCORE
),
1003 name_munged
, other_munged
);
1005 case DIFF_STATUS_MODIFIED
:
1007 len
+= snprintf(msg
+ len
, sizeof(msg
) - len
,
1008 "dissimilarity index %d%%\n",
1009 (int)(0.5 + p
->score
*
1011 complete_rewrite
= 1;
1020 if (memcmp(one
->sha1
, two
->sha1
, 20)) {
1021 int abbrev
= o
->full_index
? 40 : DEFAULT_ABBREV
;
1023 len
+= snprintf(msg
+ len
, sizeof(msg
) - len
,
1025 abbrev
, sha1_to_hex(one
->sha1
),
1026 abbrev
, sha1_to_hex(two
->sha1
));
1027 if (one
->mode
== two
->mode
)
1028 len
+= snprintf(msg
+ len
, sizeof(msg
) - len
,
1029 " %06o", one
->mode
);
1030 len
+= snprintf(msg
+ len
, sizeof(msg
) - len
, "\n");
1035 xfrm_msg
= len
? msg
: NULL
;
1038 DIFF_FILE_VALID(one
) && DIFF_FILE_VALID(two
) &&
1039 (S_IFMT
& one
->mode
) != (S_IFMT
& two
->mode
)) {
1040 /* a filepair that changes between file and symlink
1041 * needs to be split into deletion and creation.
1043 struct diff_filespec
*null
= alloc_filespec(two
->path
);
1044 run_diff_cmd(NULL
, name
, other
, one
, null
, xfrm_msg
, 0);
1046 null
= alloc_filespec(one
->path
);
1047 run_diff_cmd(NULL
, name
, other
, null
, two
, xfrm_msg
, 0);
1051 run_diff_cmd(pgm
, name
, other
, one
, two
, xfrm_msg
,
1058 static void run_diffstat(struct diff_filepair
*p
, struct diff_options
*o
,
1059 struct diffstat_t
*diffstat
)
1063 int complete_rewrite
= 0;
1065 if (DIFF_PAIR_UNMERGED(p
)) {
1067 builtin_diffstat(p
->one
->path
, NULL
, NULL
, NULL
, diffstat
, 0);
1071 name
= p
->one
->path
;
1072 other
= (strcmp(name
, p
->two
->path
) ? p
->two
->path
: NULL
);
1074 diff_fill_sha1_info(p
->one
);
1075 diff_fill_sha1_info(p
->two
);
1077 if (p
->status
== DIFF_STATUS_MODIFIED
&& p
->score
)
1078 complete_rewrite
= 1;
1079 builtin_diffstat(name
, other
, p
->one
, p
->two
, diffstat
, complete_rewrite
);
1082 void diff_setup(struct diff_options
*options
)
1084 memset(options
, 0, sizeof(*options
));
1085 options
->output_format
= DIFF_FORMAT_RAW
;
1086 options
->line_termination
= '\n';
1087 options
->break_opt
= -1;
1088 options
->rename_limit
= -1;
1090 options
->change
= diff_change
;
1091 options
->add_remove
= diff_addremove
;
1094 int diff_setup_done(struct diff_options
*options
)
1096 if ((options
->find_copies_harder
&&
1097 options
->detect_rename
!= DIFF_DETECT_COPY
) ||
1098 (0 <= options
->rename_limit
&& !options
->detect_rename
))
1102 * These cases always need recursive; we do not drop caller-supplied
1103 * recursive bits for other formats here.
1105 if ((options
->output_format
== DIFF_FORMAT_PATCH
) ||
1106 (options
->output_format
== DIFF_FORMAT_DIFFSTAT
))
1107 options
->recursive
= 1;
1109 if (options
->detect_rename
&& options
->rename_limit
< 0)
1110 options
->rename_limit
= diff_rename_limit_default
;
1111 if (options
->setup
& DIFF_SETUP_USE_CACHE
) {
1113 /* read-cache does not die even when it fails
1114 * so it is safe for us to do this here. Also
1115 * it does not smudge active_cache or active_nr
1116 * when it fails, so we do not have to worry about
1117 * cleaning it up ourselves either.
1121 if (options
->setup
& DIFF_SETUP_USE_SIZE_CACHE
)
1123 if (options
->abbrev
<= 0 || 40 < options
->abbrev
)
1124 options
->abbrev
= 40; /* full */
1129 int diff_opt_parse(struct diff_options
*options
, const char **av
, int ac
)
1131 const char *arg
= av
[0];
1132 if (!strcmp(arg
, "-p") || !strcmp(arg
, "-u"))
1133 options
->output_format
= DIFF_FORMAT_PATCH
;
1134 else if (!strcmp(arg
, "--patch-with-raw")) {
1135 options
->output_format
= DIFF_FORMAT_PATCH
;
1136 options
->with_raw
= 1;
1138 else if (!strcmp(arg
, "--stat"))
1139 options
->output_format
= DIFF_FORMAT_DIFFSTAT
;
1140 else if (!strcmp(arg
, "--patch-with-stat")) {
1141 options
->output_format
= DIFF_FORMAT_PATCH
;
1142 options
->with_stat
= 1;
1144 else if (!strcmp(arg
, "-z"))
1145 options
->line_termination
= 0;
1146 else if (!strncmp(arg
, "-l", 2))
1147 options
->rename_limit
= strtoul(arg
+2, NULL
, 10);
1148 else if (!strcmp(arg
, "--full-index"))
1149 options
->full_index
= 1;
1150 else if (!strcmp(arg
, "--name-only"))
1151 options
->output_format
= DIFF_FORMAT_NAME
;
1152 else if (!strcmp(arg
, "--name-status"))
1153 options
->output_format
= DIFF_FORMAT_NAME_STATUS
;
1154 else if (!strcmp(arg
, "-R"))
1155 options
->reverse_diff
= 1;
1156 else if (!strncmp(arg
, "-S", 2))
1157 options
->pickaxe
= arg
+ 2;
1158 else if (!strcmp(arg
, "-s"))
1159 options
->output_format
= DIFF_FORMAT_NO_OUTPUT
;
1160 else if (!strncmp(arg
, "-O", 2))
1161 options
->orderfile
= arg
+ 2;
1162 else if (!strncmp(arg
, "--diff-filter=", 14))
1163 options
->filter
= arg
+ 14;
1164 else if (!strcmp(arg
, "--pickaxe-all"))
1165 options
->pickaxe_opts
= DIFF_PICKAXE_ALL
;
1166 else if (!strcmp(arg
, "--pickaxe-regex"))
1167 options
->pickaxe_opts
= DIFF_PICKAXE_REGEX
;
1168 else if (!strncmp(arg
, "-B", 2)) {
1169 if ((options
->break_opt
=
1170 diff_scoreopt_parse(arg
)) == -1)
1173 else if (!strncmp(arg
, "-M", 2)) {
1174 if ((options
->rename_score
=
1175 diff_scoreopt_parse(arg
)) == -1)
1177 options
->detect_rename
= DIFF_DETECT_RENAME
;
1179 else if (!strncmp(arg
, "-C", 2)) {
1180 if ((options
->rename_score
=
1181 diff_scoreopt_parse(arg
)) == -1)
1183 options
->detect_rename
= DIFF_DETECT_COPY
;
1185 else if (!strcmp(arg
, "--find-copies-harder"))
1186 options
->find_copies_harder
= 1;
1187 else if (!strcmp(arg
, "--abbrev"))
1188 options
->abbrev
= DEFAULT_ABBREV
;
1189 else if (!strncmp(arg
, "--abbrev=", 9)) {
1190 options
->abbrev
= strtoul(arg
+ 9, NULL
, 10);
1191 if (options
->abbrev
< MINIMUM_ABBREV
)
1192 options
->abbrev
= MINIMUM_ABBREV
;
1193 else if (40 < options
->abbrev
)
1194 options
->abbrev
= 40;
1201 static int parse_num(const char **cp_p
)
1203 unsigned long num
, scale
;
1205 const char *cp
= *cp_p
;
1212 if ( !dot
&& ch
== '.' ) {
1215 } else if ( ch
== '%' ) {
1216 scale
= dot
? scale
*100 : 100;
1217 cp
++; /* % is always at the end */
1219 } else if ( ch
>= '0' && ch
<= '9' ) {
1220 if ( scale
< 100000 ) {
1222 num
= (num
*10) + (ch
-'0');
1231 /* user says num divided by scale and we say internally that
1232 * is MAX_SCORE * num / scale.
1234 return (num
>= scale
) ? MAX_SCORE
: (MAX_SCORE
* num
/ scale
);
1237 int diff_scoreopt_parse(const char *opt
)
1239 int opt1
, opt2
, cmd
;
1244 if (cmd
!= 'M' && cmd
!= 'C' && cmd
!= 'B')
1245 return -1; /* that is not a -M, -C nor -B option */
1247 opt1
= parse_num(&opt
);
1253 else if (*opt
!= '/')
1254 return -1; /* we expect -B80/99 or -B80 */
1257 opt2
= parse_num(&opt
);
1262 return opt1
| (opt2
<< 16);
1265 struct diff_queue_struct diff_queued_diff
;
1267 void diff_q(struct diff_queue_struct
*queue
, struct diff_filepair
*dp
)
1269 if (queue
->alloc
<= queue
->nr
) {
1270 queue
->alloc
= alloc_nr(queue
->alloc
);
1271 queue
->queue
= xrealloc(queue
->queue
,
1272 sizeof(dp
) * queue
->alloc
);
1274 queue
->queue
[queue
->nr
++] = dp
;
1277 struct diff_filepair
*diff_queue(struct diff_queue_struct
*queue
,
1278 struct diff_filespec
*one
,
1279 struct diff_filespec
*two
)
1281 struct diff_filepair
*dp
= xmalloc(sizeof(*dp
));
1286 dp
->source_stays
= 0;
1287 dp
->broken_pair
= 0;
1293 void diff_free_filepair(struct diff_filepair
*p
)
1295 diff_free_filespec_data(p
->one
);
1296 diff_free_filespec_data(p
->two
);
1302 /* This is different from find_unique_abbrev() in that
1303 * it stuffs the result with dots for alignment.
1305 const char *diff_unique_abbrev(const unsigned char *sha1
, int len
)
1310 return sha1_to_hex(sha1
);
1312 abbrev
= find_unique_abbrev(sha1
, len
);
1314 return sha1_to_hex(sha1
);
1315 abblen
= strlen(abbrev
);
1317 static char hex
[41];
1318 if (len
< abblen
&& abblen
<= len
+ 2)
1319 sprintf(hex
, "%s%.*s", abbrev
, len
+3-abblen
, "..");
1321 sprintf(hex
, "%s...", abbrev
);
1324 return sha1_to_hex(sha1
);
1327 static void diff_flush_raw(struct diff_filepair
*p
,
1328 int line_termination
,
1329 int inter_name_termination
,
1330 struct diff_options
*options
,
1335 int abbrev
= options
->abbrev
;
1336 const char *path_one
, *path_two
;
1338 path_one
= p
->one
->path
;
1339 path_two
= p
->two
->path
;
1340 if (line_termination
) {
1341 path_one
= quote_one(path_one
);
1342 path_two
= quote_one(path_two
);
1346 sprintf(status
, "%c%03d", p
->status
,
1347 (int)(0.5 + p
->score
* 100.0/MAX_SCORE
));
1349 status
[0] = p
->status
;
1352 switch (p
->status
) {
1353 case DIFF_STATUS_COPIED
:
1354 case DIFF_STATUS_RENAMED
:
1357 case DIFF_STATUS_ADDED
:
1358 case DIFF_STATUS_DELETED
:
1365 if (output_format
!= DIFF_FORMAT_NAME_STATUS
) {
1366 printf(":%06o %06o %s ",
1367 p
->one
->mode
, p
->two
->mode
,
1368 diff_unique_abbrev(p
->one
->sha1
, abbrev
));
1370 diff_unique_abbrev(p
->two
->sha1
, abbrev
));
1372 printf("%s%c%s", status
, inter_name_termination
, path_one
);
1374 printf("%c%s", inter_name_termination
, path_two
);
1375 putchar(line_termination
);
1376 if (path_one
!= p
->one
->path
)
1377 free((void*)path_one
);
1378 if (path_two
!= p
->two
->path
)
1379 free((void*)path_two
);
1382 static void diff_flush_name(struct diff_filepair
*p
,
1383 int inter_name_termination
,
1384 int line_termination
)
1386 char *path
= p
->two
->path
;
1388 if (line_termination
)
1389 path
= quote_one(p
->two
->path
);
1391 path
= p
->two
->path
;
1392 printf("%s%c", path
, line_termination
);
1393 if (p
->two
->path
!= path
)
1397 int diff_unmodified_pair(struct diff_filepair
*p
)
1399 /* This function is written stricter than necessary to support
1400 * the currently implemented transformers, but the idea is to
1401 * let transformers to produce diff_filepairs any way they want,
1402 * and filter and clean them up here before producing the output.
1404 struct diff_filespec
*one
, *two
;
1406 if (DIFF_PAIR_UNMERGED(p
))
1407 return 0; /* unmerged is interesting */
1412 /* deletion, addition, mode or type change
1413 * and rename are all interesting.
1415 if (DIFF_FILE_VALID(one
) != DIFF_FILE_VALID(two
) ||
1416 DIFF_PAIR_MODE_CHANGED(p
) ||
1417 strcmp(one
->path
, two
->path
))
1420 /* both are valid and point at the same path. that is, we are
1421 * dealing with a change.
1423 if (one
->sha1_valid
&& two
->sha1_valid
&&
1424 !memcmp(one
->sha1
, two
->sha1
, sizeof(one
->sha1
)))
1425 return 1; /* no change */
1426 if (!one
->sha1_valid
&& !two
->sha1_valid
)
1427 return 1; /* both look at the same file on the filesystem. */
1431 static void diff_flush_patch(struct diff_filepair
*p
, struct diff_options
*o
)
1433 if (diff_unmodified_pair(p
))
1436 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
1437 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
1438 return; /* no tree diffs in patch format */
1443 static void diff_flush_stat(struct diff_filepair
*p
, struct diff_options
*o
,
1444 struct diffstat_t
*diffstat
)
1446 if (diff_unmodified_pair(p
))
1449 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
1450 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
1451 return; /* no tree diffs in patch format */
1453 run_diffstat(p
, o
, diffstat
);
1456 int diff_queue_is_empty(void)
1458 struct diff_queue_struct
*q
= &diff_queued_diff
;
1460 for (i
= 0; i
< q
->nr
; i
++)
1461 if (!diff_unmodified_pair(q
->queue
[i
]))
1467 void diff_debug_filespec(struct diff_filespec
*s
, int x
, const char *one
)
1469 fprintf(stderr
, "queue[%d] %s (%s) %s %06o %s\n",
1472 DIFF_FILE_VALID(s
) ? "valid" : "invalid",
1474 s
->sha1_valid
? sha1_to_hex(s
->sha1
) : "");
1475 fprintf(stderr
, "queue[%d] %s size %lu flags %d\n",
1477 s
->size
, s
->xfrm_flags
);
1480 void diff_debug_filepair(const struct diff_filepair
*p
, int i
)
1482 diff_debug_filespec(p
->one
, i
, "one");
1483 diff_debug_filespec(p
->two
, i
, "two");
1484 fprintf(stderr
, "score %d, status %c stays %d broken %d\n",
1485 p
->score
, p
->status
? p
->status
: '?',
1486 p
->source_stays
, p
->broken_pair
);
1489 void diff_debug_queue(const char *msg
, struct diff_queue_struct
*q
)
1493 fprintf(stderr
, "%s\n", msg
);
1494 fprintf(stderr
, "q->nr = %d\n", q
->nr
);
1495 for (i
= 0; i
< q
->nr
; i
++) {
1496 struct diff_filepair
*p
= q
->queue
[i
];
1497 diff_debug_filepair(p
, i
);
1502 static void diff_resolve_rename_copy(void)
1505 struct diff_filepair
*p
, *pp
;
1506 struct diff_queue_struct
*q
= &diff_queued_diff
;
1508 diff_debug_queue("resolve-rename-copy", q
);
1510 for (i
= 0; i
< q
->nr
; i
++) {
1512 p
->status
= 0; /* undecided */
1513 if (DIFF_PAIR_UNMERGED(p
))
1514 p
->status
= DIFF_STATUS_UNMERGED
;
1515 else if (!DIFF_FILE_VALID(p
->one
))
1516 p
->status
= DIFF_STATUS_ADDED
;
1517 else if (!DIFF_FILE_VALID(p
->two
))
1518 p
->status
= DIFF_STATUS_DELETED
;
1519 else if (DIFF_PAIR_TYPE_CHANGED(p
))
1520 p
->status
= DIFF_STATUS_TYPE_CHANGED
;
1522 /* from this point on, we are dealing with a pair
1523 * whose both sides are valid and of the same type, i.e.
1524 * either in-place edit or rename/copy edit.
1526 else if (DIFF_PAIR_RENAME(p
)) {
1527 if (p
->source_stays
) {
1528 p
->status
= DIFF_STATUS_COPIED
;
1531 /* See if there is some other filepair that
1532 * copies from the same source as us. If so
1533 * we are a copy. Otherwise we are either a
1534 * copy if the path stays, or a rename if it
1535 * does not, but we already handled "stays" case.
1537 for (j
= i
+ 1; j
< q
->nr
; j
++) {
1539 if (strcmp(pp
->one
->path
, p
->one
->path
))
1540 continue; /* not us */
1541 if (!DIFF_PAIR_RENAME(pp
))
1542 continue; /* not a rename/copy */
1543 /* pp is a rename/copy from the same source */
1544 p
->status
= DIFF_STATUS_COPIED
;
1548 p
->status
= DIFF_STATUS_RENAMED
;
1550 else if (memcmp(p
->one
->sha1
, p
->two
->sha1
, 20) ||
1551 p
->one
->mode
!= p
->two
->mode
)
1552 p
->status
= DIFF_STATUS_MODIFIED
;
1554 /* This is a "no-change" entry and should not
1555 * happen anymore, but prepare for broken callers.
1557 error("feeding unmodified %s to diffcore",
1559 p
->status
= DIFF_STATUS_UNKNOWN
;
1562 diff_debug_queue("resolve-rename-copy done", q
);
1565 static void flush_one_pair(struct diff_filepair
*p
,
1566 int diff_output_format
,
1567 struct diff_options
*options
,
1568 struct diffstat_t
*diffstat
)
1570 int inter_name_termination
= '\t';
1571 int line_termination
= options
->line_termination
;
1572 if (!line_termination
)
1573 inter_name_termination
= 0;
1575 switch (p
->status
) {
1576 case DIFF_STATUS_UNKNOWN
:
1579 die("internal error in diff-resolve-rename-copy");
1582 switch (diff_output_format
) {
1583 case DIFF_FORMAT_DIFFSTAT
:
1584 diff_flush_stat(p
, options
, diffstat
);
1586 case DIFF_FORMAT_PATCH
:
1587 diff_flush_patch(p
, options
);
1589 case DIFF_FORMAT_RAW
:
1590 case DIFF_FORMAT_NAME_STATUS
:
1591 diff_flush_raw(p
, line_termination
,
1592 inter_name_termination
,
1593 options
, diff_output_format
);
1595 case DIFF_FORMAT_NAME
:
1597 inter_name_termination
,
1600 case DIFF_FORMAT_NO_OUTPUT
:
1606 void diff_flush(struct diff_options
*options
)
1608 struct diff_queue_struct
*q
= &diff_queued_diff
;
1610 int diff_output_format
= options
->output_format
;
1611 struct diffstat_t
*diffstat
= NULL
;
1613 if (diff_output_format
== DIFF_FORMAT_DIFFSTAT
|| options
->with_stat
) {
1614 diffstat
= xcalloc(sizeof (struct diffstat_t
), 1);
1615 diffstat
->xm
.consume
= diffstat_consume
;
1618 if (options
->with_raw
) {
1619 for (i
= 0; i
< q
->nr
; i
++) {
1620 struct diff_filepair
*p
= q
->queue
[i
];
1621 flush_one_pair(p
, DIFF_FORMAT_RAW
, options
, NULL
);
1623 putchar(options
->line_termination
);
1625 if (options
->with_stat
) {
1626 for (i
= 0; i
< q
->nr
; i
++) {
1627 struct diff_filepair
*p
= q
->queue
[i
];
1628 flush_one_pair(p
, DIFF_FORMAT_DIFFSTAT
, options
,
1631 show_stats(diffstat
);
1634 putchar(options
->line_termination
);
1636 for (i
= 0; i
< q
->nr
; i
++) {
1637 struct diff_filepair
*p
= q
->queue
[i
];
1638 flush_one_pair(p
, diff_output_format
, options
, diffstat
);
1639 diff_free_filepair(p
);
1643 show_stats(diffstat
);
1649 q
->nr
= q
->alloc
= 0;
1652 static void diffcore_apply_filter(const char *filter
)
1655 struct diff_queue_struct
*q
= &diff_queued_diff
;
1656 struct diff_queue_struct outq
;
1658 outq
.nr
= outq
.alloc
= 0;
1663 if (strchr(filter
, DIFF_STATUS_FILTER_AON
)) {
1665 for (i
= found
= 0; !found
&& i
< q
->nr
; i
++) {
1666 struct diff_filepair
*p
= q
->queue
[i
];
1667 if (((p
->status
== DIFF_STATUS_MODIFIED
) &&
1669 strchr(filter
, DIFF_STATUS_FILTER_BROKEN
)) ||
1671 strchr(filter
, DIFF_STATUS_MODIFIED
)))) ||
1672 ((p
->status
!= DIFF_STATUS_MODIFIED
) &&
1673 strchr(filter
, p
->status
)))
1679 /* otherwise we will clear the whole queue
1680 * by copying the empty outq at the end of this
1681 * function, but first clear the current entries
1684 for (i
= 0; i
< q
->nr
; i
++)
1685 diff_free_filepair(q
->queue
[i
]);
1688 /* Only the matching ones */
1689 for (i
= 0; i
< q
->nr
; i
++) {
1690 struct diff_filepair
*p
= q
->queue
[i
];
1692 if (((p
->status
== DIFF_STATUS_MODIFIED
) &&
1694 strchr(filter
, DIFF_STATUS_FILTER_BROKEN
)) ||
1696 strchr(filter
, DIFF_STATUS_MODIFIED
)))) ||
1697 ((p
->status
!= DIFF_STATUS_MODIFIED
) &&
1698 strchr(filter
, p
->status
)))
1701 diff_free_filepair(p
);
1708 void diffcore_std(struct diff_options
*options
)
1710 if (options
->break_opt
!= -1)
1711 diffcore_break(options
->break_opt
);
1712 if (options
->detect_rename
)
1713 diffcore_rename(options
);
1714 if (options
->break_opt
!= -1)
1715 diffcore_merge_broken();
1716 if (options
->pickaxe
)
1717 diffcore_pickaxe(options
->pickaxe
, options
->pickaxe_opts
);
1718 if (options
->orderfile
)
1719 diffcore_order(options
->orderfile
);
1720 diff_resolve_rename_copy();
1721 diffcore_apply_filter(options
->filter
);
1725 void diffcore_std_no_resolve(struct diff_options
*options
)
1727 if (options
->pickaxe
)
1728 diffcore_pickaxe(options
->pickaxe
, options
->pickaxe_opts
);
1729 if (options
->orderfile
)
1730 diffcore_order(options
->orderfile
);
1731 diffcore_apply_filter(options
->filter
);
1734 void diff_addremove(struct diff_options
*options
,
1735 int addremove
, unsigned mode
,
1736 const unsigned char *sha1
,
1737 const char *base
, const char *path
)
1739 char concatpath
[PATH_MAX
];
1740 struct diff_filespec
*one
, *two
;
1742 /* This may look odd, but it is a preparation for
1743 * feeding "there are unchanged files which should
1744 * not produce diffs, but when you are doing copy
1745 * detection you would need them, so here they are"
1746 * entries to the diff-core. They will be prefixed
1747 * with something like '=' or '*' (I haven't decided
1748 * which but should not make any difference).
1749 * Feeding the same new and old to diff_change()
1750 * also has the same effect.
1751 * Before the final output happens, they are pruned after
1752 * merged into rename/copy pairs as appropriate.
1754 if (options
->reverse_diff
)
1755 addremove
= (addremove
== '+' ? '-' :
1756 addremove
== '-' ? '+' : addremove
);
1758 if (!path
) path
= "";
1759 sprintf(concatpath
, "%s%s", base
, path
);
1760 one
= alloc_filespec(concatpath
);
1761 two
= alloc_filespec(concatpath
);
1763 if (addremove
!= '+')
1764 fill_filespec(one
, sha1
, mode
);
1765 if (addremove
!= '-')
1766 fill_filespec(two
, sha1
, mode
);
1768 diff_queue(&diff_queued_diff
, one
, two
);
1771 void diff_change(struct diff_options
*options
,
1772 unsigned old_mode
, unsigned new_mode
,
1773 const unsigned char *old_sha1
,
1774 const unsigned char *new_sha1
,
1775 const char *base
, const char *path
)
1777 char concatpath
[PATH_MAX
];
1778 struct diff_filespec
*one
, *two
;
1780 if (options
->reverse_diff
) {
1782 const unsigned char *tmp_c
;
1783 tmp
= old_mode
; old_mode
= new_mode
; new_mode
= tmp
;
1784 tmp_c
= old_sha1
; old_sha1
= new_sha1
; new_sha1
= tmp_c
;
1786 if (!path
) path
= "";
1787 sprintf(concatpath
, "%s%s", base
, path
);
1788 one
= alloc_filespec(concatpath
);
1789 two
= alloc_filespec(concatpath
);
1790 fill_filespec(one
, old_sha1
, old_mode
);
1791 fill_filespec(two
, new_sha1
, new_mode
);
1793 diff_queue(&diff_queued_diff
, one
, two
);
1796 void diff_unmerge(struct diff_options
*options
,
1799 struct diff_filespec
*one
, *two
;
1800 one
= alloc_filespec(path
);
1801 two
= alloc_filespec(path
);
1802 diff_queue(&diff_queued_diff
, one
, two
);