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
))
199 struct xdiff_emit_state xm
;
203 struct diffstat_file
{
205 unsigned is_unmerged
:1;
206 unsigned is_binary
:1;
207 unsigned int added
, deleted
;
211 static struct diffstat_file
*diffstat_add(struct diffstat_t
*diffstat
,
214 struct diffstat_file
*x
;
215 x
= xcalloc(sizeof (*x
), 1);
216 if (diffstat
->nr
== diffstat
->alloc
) {
217 diffstat
->alloc
= alloc_nr(diffstat
->alloc
);
218 diffstat
->files
= xrealloc(diffstat
->files
,
219 diffstat
->alloc
* sizeof(x
));
221 diffstat
->files
[diffstat
->nr
++] = x
;
222 x
->name
= strdup(name
);
226 static void diffstat_consume(void *priv
, char *line
, unsigned long len
)
228 struct diffstat_t
*diffstat
= priv
;
229 struct diffstat_file
*x
= diffstat
->files
[diffstat
->nr
- 1];
233 else if (line
[0] == '-')
237 static const char pluses
[] = "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++";
238 static const char minuses
[]= "----------------------------------------------------------------------";
240 static void show_stats(struct diffstat_t
* data
)
243 int i
, len
, add
, del
, total
, adds
= 0, dels
= 0;
244 int max
, max_change
= 0, max_len
= 0;
245 int total_files
= data
->nr
;
250 for (i
= 0; i
< data
->nr
; i
++) {
251 struct diffstat_file
*file
= data
->files
[i
];
253 if (file
->is_binary
|| file
->is_unmerged
)
255 if (max_change
< file
->added
+ file
->deleted
)
256 max_change
= file
->added
+ file
->deleted
;
257 len
= strlen(file
->name
);
262 for (i
= 0; i
< data
->nr
; i
++) {
263 char *name
= data
->files
[i
]->name
;
264 int added
= data
->files
[i
]->added
;
265 int deleted
= data
->files
[i
]->deleted
;
267 if (0 < (len
= quote_c_style(name
, NULL
, NULL
, 0))) {
268 char *qname
= xmalloc(len
+ 1);
269 quote_c_style(name
, qname
, NULL
, 0);
271 data
->files
[i
]->name
= name
= qname
;
275 * "scale" the filename
286 slash
= strchr(name
, '/');
293 * scale the add/delete
299 if (data
->files
[i
]->is_binary
) {
300 printf(" %s%-*s | Bin\n", prefix
, len
, name
);
301 goto free_diffstat_file
;
303 else if (data
->files
[i
]->is_unmerged
) {
304 printf(" %s%-*s | Unmerged\n", prefix
, len
, name
);
305 goto free_diffstat_file
;
307 else if (added
+ deleted
== 0) {
309 goto free_diffstat_file
;
318 if (max_change
> 0) {
319 total
= (total
* max
+ max_change
/ 2) / max_change
;
320 add
= (add
* max
+ max_change
/ 2) / max_change
;
323 printf(" %s%-*s |%5d %.*s%.*s\n", prefix
,
324 len
, name
, added
+ deleted
,
325 add
, pluses
, del
, minuses
);
327 free(data
->files
[i
]->name
);
328 free(data
->files
[i
]);
331 printf(" %d files changed, %d insertions(+), %d deletions(-)\n",
332 total_files
, adds
, dels
);
335 #define FIRST_FEW_BYTES 8000
336 static int mmfile_is_binary(mmfile_t
*mf
)
339 if (FIRST_FEW_BYTES
< sz
)
340 sz
= FIRST_FEW_BYTES
;
341 if (memchr(mf
->ptr
, 0, sz
))
346 static void builtin_diff(const char *name_a
,
348 struct diff_filespec
*one
,
349 struct diff_filespec
*two
,
350 const char *xfrm_msg
,
351 int complete_rewrite
)
357 a_one
= quote_two("a/", name_a
);
358 b_two
= quote_two("b/", name_b
);
359 lbl
[0] = DIFF_FILE_VALID(one
) ? a_one
: "/dev/null";
360 lbl
[1] = DIFF_FILE_VALID(two
) ? b_two
: "/dev/null";
361 printf("diff --git %s %s\n", a_one
, b_two
);
362 if (lbl
[0][0] == '/') {
364 printf("new file mode %06o\n", two
->mode
);
365 if (xfrm_msg
&& xfrm_msg
[0])
368 else if (lbl
[1][0] == '/') {
369 printf("deleted file mode %06o\n", one
->mode
);
370 if (xfrm_msg
&& xfrm_msg
[0])
374 if (one
->mode
!= two
->mode
) {
375 printf("old mode %06o\n", one
->mode
);
376 printf("new mode %06o\n", two
->mode
);
378 if (xfrm_msg
&& xfrm_msg
[0])
381 * we do not run diff between different kind
384 if ((one
->mode
^ two
->mode
) & S_IFMT
)
385 goto free_ab_and_return
;
386 if (complete_rewrite
) {
387 emit_rewrite_diff(name_a
, name_b
, one
, two
);
388 goto free_ab_and_return
;
392 if (fill_mmfile(&mf1
, one
) < 0 || fill_mmfile(&mf2
, two
) < 0)
393 die("unable to read files to diff");
395 if (mmfile_is_binary(&mf1
) || mmfile_is_binary(&mf2
))
396 printf("Binary files %s and %s differ\n", lbl
[0], lbl
[1]);
398 /* Crazy xdl interfaces.. */
399 const char *diffopts
= getenv("GIT_DIFF_OPTS");
403 struct emit_callback ecbdata
;
405 ecbdata
.label_path
= lbl
;
406 xpp
.flags
= XDF_NEED_MINIMAL
;
408 xecfg
.flags
= XDL_EMIT_FUNCNAMES
;
411 else if (!strncmp(diffopts
, "--unified=", 10))
412 xecfg
.ctxlen
= strtoul(diffopts
+ 10, NULL
, 10);
413 else if (!strncmp(diffopts
, "-u", 2))
414 xecfg
.ctxlen
= strtoul(diffopts
+ 2, NULL
, 10);
417 xdl_diff(&mf1
, &mf2
, &xpp
, &xecfg
, &ecb
);
426 static void builtin_diffstat(const char *name_a
, const char *name_b
,
427 struct diff_filespec
*one
, struct diff_filespec
*two
,
428 struct diffstat_t
*diffstat
)
431 struct diffstat_file
*data
;
433 data
= diffstat_add(diffstat
, name_a
? name_a
: name_b
);
436 data
->is_unmerged
= 1;
440 if (fill_mmfile(&mf1
, one
) < 0 || fill_mmfile(&mf2
, two
) < 0)
441 die("unable to read files to diff");
443 if (mmfile_is_binary(&mf1
) || mmfile_is_binary(&mf2
))
446 /* Crazy xdl interfaces.. */
451 xpp
.flags
= XDF_NEED_MINIMAL
;
454 ecb
.outf
= xdiff_outf
;
456 xdl_diff(&mf1
, &mf2
, &xpp
, &xecfg
, &ecb
);
460 struct diff_filespec
*alloc_filespec(const char *path
)
462 int namelen
= strlen(path
);
463 struct diff_filespec
*spec
= xmalloc(sizeof(*spec
) + namelen
+ 1);
465 memset(spec
, 0, sizeof(*spec
));
466 spec
->path
= (char *)(spec
+ 1);
467 memcpy(spec
->path
, path
, namelen
+1);
471 void fill_filespec(struct diff_filespec
*spec
, const unsigned char *sha1
,
475 spec
->mode
= canon_mode(mode
);
476 memcpy(spec
->sha1
, sha1
, 20);
477 spec
->sha1_valid
= !!memcmp(sha1
, null_sha1
, 20);
482 * Given a name and sha1 pair, if the dircache tells us the file in
483 * the work tree has that object contents, return true, so that
484 * prepare_temp_file() does not have to inflate and extract.
486 static int work_tree_matches(const char *name
, const unsigned char *sha1
)
488 struct cache_entry
*ce
;
492 /* We do not read the cache ourselves here, because the
493 * benchmark with my previous version that always reads cache
494 * shows that it makes things worse for diff-tree comparing
495 * two linux-2.6 kernel trees in an already checked out work
496 * tree. This is because most diff-tree comparisons deal with
497 * only a small number of files, while reading the cache is
498 * expensive for a large project, and its cost outweighs the
499 * savings we get by not inflating the object to a temporary
500 * file. Practically, this code only helps when we are used
501 * by diff-cache --cached, which does read the cache before
508 pos
= cache_name_pos(name
, len
);
511 ce
= active_cache
[pos
];
512 if ((lstat(name
, &st
) < 0) ||
513 !S_ISREG(st
.st_mode
) || /* careful! */
514 ce_match_stat(ce
, &st
, 0) ||
515 memcmp(sha1
, ce
->sha1
, 20))
517 /* we return 1 only when we can stat, it is a regular file,
518 * stat information matches, and sha1 recorded in the cache
519 * matches. I.e. we know the file in the work tree really is
520 * the same as the <name, sha1> pair.
525 static struct sha1_size_cache
{
526 unsigned char sha1
[20];
529 static int sha1_size_cache_nr
, sha1_size_cache_alloc
;
531 static struct sha1_size_cache
*locate_size_cache(unsigned char *sha1
,
536 struct sha1_size_cache
*e
;
539 last
= sha1_size_cache_nr
;
540 while (last
> first
) {
541 int cmp
, next
= (last
+ first
) >> 1;
542 e
= sha1_size_cache
[next
];
543 cmp
= memcmp(e
->sha1
, sha1
, 20);
555 /* insert to make it at "first" */
556 if (sha1_size_cache_alloc
<= sha1_size_cache_nr
) {
557 sha1_size_cache_alloc
= alloc_nr(sha1_size_cache_alloc
);
558 sha1_size_cache
= xrealloc(sha1_size_cache
,
559 sha1_size_cache_alloc
*
560 sizeof(*sha1_size_cache
));
562 sha1_size_cache_nr
++;
563 if (first
< sha1_size_cache_nr
)
564 memmove(sha1_size_cache
+ first
+ 1, sha1_size_cache
+ first
,
565 (sha1_size_cache_nr
- first
- 1) *
566 sizeof(*sha1_size_cache
));
567 e
= xmalloc(sizeof(struct sha1_size_cache
));
568 sha1_size_cache
[first
] = e
;
569 memcpy(e
->sha1
, sha1
, 20);
575 * While doing rename detection and pickaxe operation, we may need to
576 * grab the data for the blob (or file) for our own in-core comparison.
577 * diff_filespec has data and size fields for this purpose.
579 int diff_populate_filespec(struct diff_filespec
*s
, int size_only
)
582 if (!DIFF_FILE_VALID(s
))
583 die("internal error: asking to populate invalid file.");
584 if (S_ISDIR(s
->mode
))
592 if (!s
->sha1_valid
||
593 work_tree_matches(s
->path
, s
->sha1
)) {
596 if (lstat(s
->path
, &st
) < 0) {
597 if (errno
== ENOENT
) {
606 s
->size
= st
.st_size
;
611 if (S_ISLNK(st
.st_mode
)) {
613 s
->data
= xmalloc(s
->size
);
615 ret
= readlink(s
->path
, s
->data
, s
->size
);
622 fd
= open(s
->path
, O_RDONLY
);
625 s
->data
= mmap(NULL
, s
->size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
627 if (s
->data
== MAP_FAILED
)
629 s
->should_munmap
= 1;
633 struct sha1_size_cache
*e
;
636 e
= locate_size_cache(s
->sha1
, 1, 0);
641 if (!sha1_object_info(s
->sha1
, type
, &s
->size
))
642 locate_size_cache(s
->sha1
, 0, s
->size
);
645 s
->data
= read_sha1_file(s
->sha1
, type
, &s
->size
);
652 void diff_free_filespec_data(struct diff_filespec
*s
)
656 else if (s
->should_munmap
)
657 munmap(s
->data
, s
->size
);
658 s
->should_free
= s
->should_munmap
= 0;
664 static void prep_temp_blob(struct diff_tempfile
*temp
,
667 const unsigned char *sha1
,
672 fd
= git_mkstemp(temp
->tmp_path
, TEMPFILE_PATH_LEN
, ".diff_XXXXXX");
674 die("unable to create temp-file");
675 if (write(fd
, blob
, size
) != size
)
676 die("unable to write temp-file");
678 temp
->name
= temp
->tmp_path
;
679 strcpy(temp
->hex
, sha1_to_hex(sha1
));
681 sprintf(temp
->mode
, "%06o", mode
);
684 static void prepare_temp_file(const char *name
,
685 struct diff_tempfile
*temp
,
686 struct diff_filespec
*one
)
688 if (!DIFF_FILE_VALID(one
)) {
690 /* A '-' entry produces this for file-2, and
691 * a '+' entry produces this for file-1.
693 temp
->name
= "/dev/null";
694 strcpy(temp
->hex
, ".");
695 strcpy(temp
->mode
, ".");
699 if (!one
->sha1_valid
||
700 work_tree_matches(name
, one
->sha1
)) {
702 if (lstat(name
, &st
) < 0) {
704 goto not_a_valid_file
;
705 die("stat(%s): %s", name
, strerror(errno
));
707 if (S_ISLNK(st
.st_mode
)) {
709 char buf
[PATH_MAX
+ 1]; /* ought to be SYMLINK_MAX */
710 if (sizeof(buf
) <= st
.st_size
)
711 die("symlink too long: %s", name
);
712 ret
= readlink(name
, buf
, st
.st_size
);
714 die("readlink(%s)", name
);
715 prep_temp_blob(temp
, buf
, st
.st_size
,
717 one
->sha1
: null_sha1
),
719 one
->mode
: S_IFLNK
));
722 /* we can borrow from the file in the work tree */
724 if (!one
->sha1_valid
)
725 strcpy(temp
->hex
, sha1_to_hex(null_sha1
));
727 strcpy(temp
->hex
, sha1_to_hex(one
->sha1
));
728 /* Even though we may sometimes borrow the
729 * contents from the work tree, we always want
730 * one->mode. mode is trustworthy even when
731 * !(one->sha1_valid), as long as
732 * DIFF_FILE_VALID(one).
734 sprintf(temp
->mode
, "%06o", one
->mode
);
739 if (diff_populate_filespec(one
, 0))
740 die("cannot read data blob for %s", one
->path
);
741 prep_temp_blob(temp
, one
->data
, one
->size
,
742 one
->sha1
, one
->mode
);
746 static void remove_tempfile(void)
750 for (i
= 0; i
< 2; i
++)
751 if (diff_temp
[i
].name
== diff_temp
[i
].tmp_path
) {
752 unlink(diff_temp
[i
].name
);
753 diff_temp
[i
].name
= NULL
;
757 static void remove_tempfile_on_signal(int signo
)
760 signal(SIGINT
, SIG_DFL
);
764 static int spawn_prog(const char *pgm
, const char **arg
)
772 die("unable to fork");
774 execvp(pgm
, (char *const*) arg
);
778 while (waitpid(pid
, &status
, 0) < 0) {
784 /* Earlier we did not check the exit status because
785 * diff exits non-zero if files are different, and
786 * we are not interested in knowing that. It was a
787 * mistake which made it harder to quit a diff-*
788 * session that uses the git-apply-patch-script as
789 * the GIT_EXTERNAL_DIFF. A custom GIT_EXTERNAL_DIFF
790 * should also exit non-zero only when it wants to
791 * abort the entire diff-* session.
793 if (WIFEXITED(status
) && !WEXITSTATUS(status
))
798 /* An external diff command takes:
800 * diff-cmd name infile1 infile1-sha1 infile1-mode \
801 * infile2 infile2-sha1 infile2-mode [ rename-to ]
804 static void run_external_diff(const char *pgm
,
807 struct diff_filespec
*one
,
808 struct diff_filespec
*two
,
809 const char *xfrm_msg
,
810 int complete_rewrite
)
812 const char *spawn_arg
[10];
813 struct diff_tempfile
*temp
= diff_temp
;
815 static int atexit_asked
= 0;
816 const char *othername
;
817 const char **arg
= &spawn_arg
[0];
819 othername
= (other
? other
: name
);
821 prepare_temp_file(name
, &temp
[0], one
);
822 prepare_temp_file(othername
, &temp
[1], two
);
823 if (! atexit_asked
&&
824 (temp
[0].name
== temp
[0].tmp_path
||
825 temp
[1].name
== temp
[1].tmp_path
)) {
827 atexit(remove_tempfile
);
829 signal(SIGINT
, remove_tempfile_on_signal
);
835 *arg
++ = temp
[0].name
;
836 *arg
++ = temp
[0].hex
;
837 *arg
++ = temp
[0].mode
;
838 *arg
++ = temp
[1].name
;
839 *arg
++ = temp
[1].hex
;
840 *arg
++ = temp
[1].mode
;
850 retval
= spawn_prog(pgm
, spawn_arg
);
853 fprintf(stderr
, "external diff died, stopping at %s.\n", name
);
858 static void run_diff_cmd(const char *pgm
,
861 struct diff_filespec
*one
,
862 struct diff_filespec
*two
,
863 const char *xfrm_msg
,
864 int complete_rewrite
)
867 run_external_diff(pgm
, name
, other
, one
, two
, xfrm_msg
,
872 builtin_diff(name
, other
? other
: name
,
873 one
, two
, xfrm_msg
, complete_rewrite
);
875 printf("* Unmerged path %s\n", name
);
878 static void diff_fill_sha1_info(struct diff_filespec
*one
)
880 if (DIFF_FILE_VALID(one
)) {
881 if (!one
->sha1_valid
) {
883 if (lstat(one
->path
, &st
) < 0)
884 die("stat %s", one
->path
);
885 if (index_path(one
->sha1
, one
->path
, &st
, 0))
886 die("cannot hash %s\n", one
->path
);
890 memset(one
->sha1
, 0, 20);
893 static void run_diff(struct diff_filepair
*p
, struct diff_options
*o
)
895 const char *pgm
= external_diff();
896 char msg
[PATH_MAX
*2+300], *xfrm_msg
;
897 struct diff_filespec
*one
;
898 struct diff_filespec
*two
;
901 char *name_munged
, *other_munged
;
902 int complete_rewrite
= 0;
905 if (DIFF_PAIR_UNMERGED(p
)) {
907 run_diff_cmd(pgm
, p
->one
->path
, NULL
, NULL
, NULL
, NULL
, 0);
912 other
= (strcmp(name
, p
->two
->path
) ? p
->two
->path
: NULL
);
913 name_munged
= quote_one(name
);
914 other_munged
= quote_one(other
);
915 one
= p
->one
; two
= p
->two
;
917 diff_fill_sha1_info(one
);
918 diff_fill_sha1_info(two
);
922 case DIFF_STATUS_COPIED
:
923 len
+= snprintf(msg
+ len
, sizeof(msg
) - len
,
924 "similarity index %d%%\n"
927 (int)(0.5 + p
->score
* 100.0/MAX_SCORE
),
928 name_munged
, other_munged
);
930 case DIFF_STATUS_RENAMED
:
931 len
+= snprintf(msg
+ len
, sizeof(msg
) - len
,
932 "similarity index %d%%\n"
935 (int)(0.5 + p
->score
* 100.0/MAX_SCORE
),
936 name_munged
, other_munged
);
938 case DIFF_STATUS_MODIFIED
:
940 len
+= snprintf(msg
+ len
, sizeof(msg
) - len
,
941 "dissimilarity index %d%%\n",
942 (int)(0.5 + p
->score
*
944 complete_rewrite
= 1;
953 if (memcmp(one
->sha1
, two
->sha1
, 20)) {
955 int abbrev
= o
->full_index
? 40 : DEFAULT_ABBREV
;
956 memcpy(one_sha1
, sha1_to_hex(one
->sha1
), 41);
958 len
+= snprintf(msg
+ len
, sizeof(msg
) - len
,
960 abbrev
, one_sha1
, abbrev
,
961 sha1_to_hex(two
->sha1
));
962 if (one
->mode
== two
->mode
)
963 len
+= snprintf(msg
+ len
, sizeof(msg
) - len
,
965 len
+= snprintf(msg
+ len
, sizeof(msg
) - len
, "\n");
970 xfrm_msg
= len
? msg
: NULL
;
973 DIFF_FILE_VALID(one
) && DIFF_FILE_VALID(two
) &&
974 (S_IFMT
& one
->mode
) != (S_IFMT
& two
->mode
)) {
975 /* a filepair that changes between file and symlink
976 * needs to be split into deletion and creation.
978 struct diff_filespec
*null
= alloc_filespec(two
->path
);
979 run_diff_cmd(NULL
, name
, other
, one
, null
, xfrm_msg
, 0);
981 null
= alloc_filespec(one
->path
);
982 run_diff_cmd(NULL
, name
, other
, null
, two
, xfrm_msg
, 0);
986 run_diff_cmd(pgm
, name
, other
, one
, two
, xfrm_msg
,
993 static void run_diffstat(struct diff_filepair
*p
, struct diff_options
*o
,
994 struct diffstat_t
*diffstat
)
999 if (DIFF_PAIR_UNMERGED(p
)) {
1001 builtin_diffstat(p
->one
->path
, NULL
, NULL
, NULL
, diffstat
);
1005 name
= p
->one
->path
;
1006 other
= (strcmp(name
, p
->two
->path
) ? p
->two
->path
: NULL
);
1008 diff_fill_sha1_info(p
->one
);
1009 diff_fill_sha1_info(p
->two
);
1011 builtin_diffstat(name
, other
, p
->one
, p
->two
, diffstat
);
1014 void diff_setup(struct diff_options
*options
)
1016 memset(options
, 0, sizeof(*options
));
1017 options
->output_format
= DIFF_FORMAT_RAW
;
1018 options
->line_termination
= '\n';
1019 options
->break_opt
= -1;
1020 options
->rename_limit
= -1;
1022 options
->change
= diff_change
;
1023 options
->add_remove
= diff_addremove
;
1026 int diff_setup_done(struct diff_options
*options
)
1028 if ((options
->find_copies_harder
&&
1029 options
->detect_rename
!= DIFF_DETECT_COPY
) ||
1030 (0 <= options
->rename_limit
&& !options
->detect_rename
))
1032 if (options
->detect_rename
&& options
->rename_limit
< 0)
1033 options
->rename_limit
= diff_rename_limit_default
;
1034 if (options
->setup
& DIFF_SETUP_USE_CACHE
) {
1036 /* read-cache does not die even when it fails
1037 * so it is safe for us to do this here. Also
1038 * it does not smudge active_cache or active_nr
1039 * when it fails, so we do not have to worry about
1040 * cleaning it up ourselves either.
1044 if (options
->setup
& DIFF_SETUP_USE_SIZE_CACHE
)
1046 if (options
->abbrev
<= 0 || 40 < options
->abbrev
)
1047 options
->abbrev
= 40; /* full */
1052 int diff_opt_parse(struct diff_options
*options
, const char **av
, int ac
)
1054 const char *arg
= av
[0];
1055 if (!strcmp(arg
, "-p") || !strcmp(arg
, "-u"))
1056 options
->output_format
= DIFF_FORMAT_PATCH
;
1057 else if (!strcmp(arg
, "--patch-with-raw")) {
1058 options
->output_format
= DIFF_FORMAT_PATCH
;
1059 options
->with_raw
= 1;
1061 else if (!strcmp(arg
, "--stat"))
1062 options
->output_format
= DIFF_FORMAT_DIFFSTAT
;
1063 else if (!strcmp(arg
, "--patch-with-stat")) {
1064 options
->output_format
= DIFF_FORMAT_PATCH
;
1065 options
->with_stat
= 1;
1067 else if (!strcmp(arg
, "-z"))
1068 options
->line_termination
= 0;
1069 else if (!strncmp(arg
, "-l", 2))
1070 options
->rename_limit
= strtoul(arg
+2, NULL
, 10);
1071 else if (!strcmp(arg
, "--full-index"))
1072 options
->full_index
= 1;
1073 else if (!strcmp(arg
, "--name-only"))
1074 options
->output_format
= DIFF_FORMAT_NAME
;
1075 else if (!strcmp(arg
, "--name-status"))
1076 options
->output_format
= DIFF_FORMAT_NAME_STATUS
;
1077 else if (!strcmp(arg
, "-R"))
1078 options
->reverse_diff
= 1;
1079 else if (!strncmp(arg
, "-S", 2))
1080 options
->pickaxe
= arg
+ 2;
1081 else if (!strcmp(arg
, "-s"))
1082 options
->output_format
= DIFF_FORMAT_NO_OUTPUT
;
1083 else if (!strncmp(arg
, "-O", 2))
1084 options
->orderfile
= arg
+ 2;
1085 else if (!strncmp(arg
, "--diff-filter=", 14))
1086 options
->filter
= arg
+ 14;
1087 else if (!strcmp(arg
, "--pickaxe-all"))
1088 options
->pickaxe_opts
= DIFF_PICKAXE_ALL
;
1089 else if (!strcmp(arg
, "--pickaxe-regex"))
1090 options
->pickaxe_opts
= DIFF_PICKAXE_REGEX
;
1091 else if (!strncmp(arg
, "-B", 2)) {
1092 if ((options
->break_opt
=
1093 diff_scoreopt_parse(arg
)) == -1)
1096 else if (!strncmp(arg
, "-M", 2)) {
1097 if ((options
->rename_score
=
1098 diff_scoreopt_parse(arg
)) == -1)
1100 options
->detect_rename
= DIFF_DETECT_RENAME
;
1102 else if (!strncmp(arg
, "-C", 2)) {
1103 if ((options
->rename_score
=
1104 diff_scoreopt_parse(arg
)) == -1)
1106 options
->detect_rename
= DIFF_DETECT_COPY
;
1108 else if (!strcmp(arg
, "--find-copies-harder"))
1109 options
->find_copies_harder
= 1;
1110 else if (!strcmp(arg
, "--abbrev"))
1111 options
->abbrev
= DEFAULT_ABBREV
;
1112 else if (!strncmp(arg
, "--abbrev=", 9)) {
1113 options
->abbrev
= strtoul(arg
+ 9, NULL
, 10);
1114 if (options
->abbrev
< MINIMUM_ABBREV
)
1115 options
->abbrev
= MINIMUM_ABBREV
;
1116 else if (40 < options
->abbrev
)
1117 options
->abbrev
= 40;
1124 static int parse_num(const char **cp_p
)
1126 unsigned long num
, scale
;
1128 const char *cp
= *cp_p
;
1135 if ( !dot
&& ch
== '.' ) {
1138 } else if ( ch
== '%' ) {
1139 scale
= dot
? scale
*100 : 100;
1140 cp
++; /* % is always at the end */
1142 } else if ( ch
>= '0' && ch
<= '9' ) {
1143 if ( scale
< 100000 ) {
1145 num
= (num
*10) + (ch
-'0');
1154 /* user says num divided by scale and we say internally that
1155 * is MAX_SCORE * num / scale.
1157 return (num
>= scale
) ? MAX_SCORE
: (MAX_SCORE
* num
/ scale
);
1160 int diff_scoreopt_parse(const char *opt
)
1162 int opt1
, opt2
, cmd
;
1167 if (cmd
!= 'M' && cmd
!= 'C' && cmd
!= 'B')
1168 return -1; /* that is not a -M, -C nor -B option */
1170 opt1
= parse_num(&opt
);
1176 else if (*opt
!= '/')
1177 return -1; /* we expect -B80/99 or -B80 */
1180 opt2
= parse_num(&opt
);
1185 return opt1
| (opt2
<< 16);
1188 struct diff_queue_struct diff_queued_diff
;
1190 void diff_q(struct diff_queue_struct
*queue
, struct diff_filepair
*dp
)
1192 if (queue
->alloc
<= queue
->nr
) {
1193 queue
->alloc
= alloc_nr(queue
->alloc
);
1194 queue
->queue
= xrealloc(queue
->queue
,
1195 sizeof(dp
) * queue
->alloc
);
1197 queue
->queue
[queue
->nr
++] = dp
;
1200 struct diff_filepair
*diff_queue(struct diff_queue_struct
*queue
,
1201 struct diff_filespec
*one
,
1202 struct diff_filespec
*two
)
1204 struct diff_filepair
*dp
= xmalloc(sizeof(*dp
));
1209 dp
->source_stays
= 0;
1210 dp
->broken_pair
= 0;
1216 void diff_free_filepair(struct diff_filepair
*p
)
1218 diff_free_filespec_data(p
->one
);
1219 diff_free_filespec_data(p
->two
);
1225 /* This is different from find_unique_abbrev() in that
1226 * it stuffs the result with dots for alignment.
1228 const char *diff_unique_abbrev(const unsigned char *sha1
, int len
)
1233 return sha1_to_hex(sha1
);
1235 abbrev
= find_unique_abbrev(sha1
, len
);
1237 return sha1_to_hex(sha1
);
1238 abblen
= strlen(abbrev
);
1240 static char hex
[41];
1241 if (len
< abblen
&& abblen
<= len
+ 2)
1242 sprintf(hex
, "%s%.*s", abbrev
, len
+3-abblen
, "..");
1244 sprintf(hex
, "%s...", abbrev
);
1247 return sha1_to_hex(sha1
);
1250 static void diff_flush_raw(struct diff_filepair
*p
,
1251 int line_termination
,
1252 int inter_name_termination
,
1253 struct diff_options
*options
,
1258 int abbrev
= options
->abbrev
;
1259 const char *path_one
, *path_two
;
1261 path_one
= p
->one
->path
;
1262 path_two
= p
->two
->path
;
1263 if (line_termination
) {
1264 path_one
= quote_one(path_one
);
1265 path_two
= quote_one(path_two
);
1269 sprintf(status
, "%c%03d", p
->status
,
1270 (int)(0.5 + p
->score
* 100.0/MAX_SCORE
));
1272 status
[0] = p
->status
;
1275 switch (p
->status
) {
1276 case DIFF_STATUS_COPIED
:
1277 case DIFF_STATUS_RENAMED
:
1280 case DIFF_STATUS_ADDED
:
1281 case DIFF_STATUS_DELETED
:
1288 if (output_format
!= DIFF_FORMAT_NAME_STATUS
) {
1289 printf(":%06o %06o %s ",
1290 p
->one
->mode
, p
->two
->mode
,
1291 diff_unique_abbrev(p
->one
->sha1
, abbrev
));
1293 diff_unique_abbrev(p
->two
->sha1
, abbrev
));
1295 printf("%s%c%s", status
, inter_name_termination
, path_one
);
1297 printf("%c%s", inter_name_termination
, path_two
);
1298 putchar(line_termination
);
1299 if (path_one
!= p
->one
->path
)
1300 free((void*)path_one
);
1301 if (path_two
!= p
->two
->path
)
1302 free((void*)path_two
);
1305 static void diff_flush_name(struct diff_filepair
*p
,
1306 int inter_name_termination
,
1307 int line_termination
)
1309 char *path
= p
->two
->path
;
1311 if (line_termination
)
1312 path
= quote_one(p
->two
->path
);
1314 path
= p
->two
->path
;
1315 printf("%s%c", path
, line_termination
);
1316 if (p
->two
->path
!= path
)
1320 int diff_unmodified_pair(struct diff_filepair
*p
)
1322 /* This function is written stricter than necessary to support
1323 * the currently implemented transformers, but the idea is to
1324 * let transformers to produce diff_filepairs any way they want,
1325 * and filter and clean them up here before producing the output.
1327 struct diff_filespec
*one
, *two
;
1329 if (DIFF_PAIR_UNMERGED(p
))
1330 return 0; /* unmerged is interesting */
1335 /* deletion, addition, mode or type change
1336 * and rename are all interesting.
1338 if (DIFF_FILE_VALID(one
) != DIFF_FILE_VALID(two
) ||
1339 DIFF_PAIR_MODE_CHANGED(p
) ||
1340 strcmp(one
->path
, two
->path
))
1343 /* both are valid and point at the same path. that is, we are
1344 * dealing with a change.
1346 if (one
->sha1_valid
&& two
->sha1_valid
&&
1347 !memcmp(one
->sha1
, two
->sha1
, sizeof(one
->sha1
)))
1348 return 1; /* no change */
1349 if (!one
->sha1_valid
&& !two
->sha1_valid
)
1350 return 1; /* both look at the same file on the filesystem. */
1354 static void diff_flush_patch(struct diff_filepair
*p
, struct diff_options
*o
)
1356 if (diff_unmodified_pair(p
))
1359 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
1360 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
1361 return; /* no tree diffs in patch format */
1366 static void diff_flush_stat(struct diff_filepair
*p
, struct diff_options
*o
,
1367 struct diffstat_t
*diffstat
)
1369 if (diff_unmodified_pair(p
))
1372 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
1373 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
1374 return; /* no tree diffs in patch format */
1376 run_diffstat(p
, o
, diffstat
);
1379 int diff_queue_is_empty(void)
1381 struct diff_queue_struct
*q
= &diff_queued_diff
;
1383 for (i
= 0; i
< q
->nr
; i
++)
1384 if (!diff_unmodified_pair(q
->queue
[i
]))
1390 void diff_debug_filespec(struct diff_filespec
*s
, int x
, const char *one
)
1392 fprintf(stderr
, "queue[%d] %s (%s) %s %06o %s\n",
1395 DIFF_FILE_VALID(s
) ? "valid" : "invalid",
1397 s
->sha1_valid
? sha1_to_hex(s
->sha1
) : "");
1398 fprintf(stderr
, "queue[%d] %s size %lu flags %d\n",
1400 s
->size
, s
->xfrm_flags
);
1403 void diff_debug_filepair(const struct diff_filepair
*p
, int i
)
1405 diff_debug_filespec(p
->one
, i
, "one");
1406 diff_debug_filespec(p
->two
, i
, "two");
1407 fprintf(stderr
, "score %d, status %c stays %d broken %d\n",
1408 p
->score
, p
->status
? p
->status
: '?',
1409 p
->source_stays
, p
->broken_pair
);
1412 void diff_debug_queue(const char *msg
, struct diff_queue_struct
*q
)
1416 fprintf(stderr
, "%s\n", msg
);
1417 fprintf(stderr
, "q->nr = %d\n", q
->nr
);
1418 for (i
= 0; i
< q
->nr
; i
++) {
1419 struct diff_filepair
*p
= q
->queue
[i
];
1420 diff_debug_filepair(p
, i
);
1425 static void diff_resolve_rename_copy(void)
1428 struct diff_filepair
*p
, *pp
;
1429 struct diff_queue_struct
*q
= &diff_queued_diff
;
1431 diff_debug_queue("resolve-rename-copy", q
);
1433 for (i
= 0; i
< q
->nr
; i
++) {
1435 p
->status
= 0; /* undecided */
1436 if (DIFF_PAIR_UNMERGED(p
))
1437 p
->status
= DIFF_STATUS_UNMERGED
;
1438 else if (!DIFF_FILE_VALID(p
->one
))
1439 p
->status
= DIFF_STATUS_ADDED
;
1440 else if (!DIFF_FILE_VALID(p
->two
))
1441 p
->status
= DIFF_STATUS_DELETED
;
1442 else if (DIFF_PAIR_TYPE_CHANGED(p
))
1443 p
->status
= DIFF_STATUS_TYPE_CHANGED
;
1445 /* from this point on, we are dealing with a pair
1446 * whose both sides are valid and of the same type, i.e.
1447 * either in-place edit or rename/copy edit.
1449 else if (DIFF_PAIR_RENAME(p
)) {
1450 if (p
->source_stays
) {
1451 p
->status
= DIFF_STATUS_COPIED
;
1454 /* See if there is some other filepair that
1455 * copies from the same source as us. If so
1456 * we are a copy. Otherwise we are either a
1457 * copy if the path stays, or a rename if it
1458 * does not, but we already handled "stays" case.
1460 for (j
= i
+ 1; j
< q
->nr
; j
++) {
1462 if (strcmp(pp
->one
->path
, p
->one
->path
))
1463 continue; /* not us */
1464 if (!DIFF_PAIR_RENAME(pp
))
1465 continue; /* not a rename/copy */
1466 /* pp is a rename/copy from the same source */
1467 p
->status
= DIFF_STATUS_COPIED
;
1471 p
->status
= DIFF_STATUS_RENAMED
;
1473 else if (memcmp(p
->one
->sha1
, p
->two
->sha1
, 20) ||
1474 p
->one
->mode
!= p
->two
->mode
)
1475 p
->status
= DIFF_STATUS_MODIFIED
;
1477 /* This is a "no-change" entry and should not
1478 * happen anymore, but prepare for broken callers.
1480 error("feeding unmodified %s to diffcore",
1482 p
->status
= DIFF_STATUS_UNKNOWN
;
1485 diff_debug_queue("resolve-rename-copy done", q
);
1488 static void flush_one_pair(struct diff_filepair
*p
,
1489 int diff_output_format
,
1490 struct diff_options
*options
,
1491 struct diffstat_t
*diffstat
)
1493 int inter_name_termination
= '\t';
1494 int line_termination
= options
->line_termination
;
1495 if (!line_termination
)
1496 inter_name_termination
= 0;
1498 switch (p
->status
) {
1499 case DIFF_STATUS_UNKNOWN
:
1502 die("internal error in diff-resolve-rename-copy");
1505 switch (diff_output_format
) {
1506 case DIFF_FORMAT_DIFFSTAT
:
1507 diff_flush_stat(p
, options
, diffstat
);
1509 case DIFF_FORMAT_PATCH
:
1510 diff_flush_patch(p
, options
);
1512 case DIFF_FORMAT_RAW
:
1513 case DIFF_FORMAT_NAME_STATUS
:
1514 diff_flush_raw(p
, line_termination
,
1515 inter_name_termination
,
1516 options
, diff_output_format
);
1518 case DIFF_FORMAT_NAME
:
1520 inter_name_termination
,
1523 case DIFF_FORMAT_NO_OUTPUT
:
1529 void diff_flush(struct diff_options
*options
)
1531 struct diff_queue_struct
*q
= &diff_queued_diff
;
1533 int diff_output_format
= options
->output_format
;
1534 struct diffstat_t
*diffstat
= NULL
;
1536 if (diff_output_format
== DIFF_FORMAT_DIFFSTAT
|| options
->with_stat
) {
1537 diffstat
= xcalloc(sizeof (struct diffstat_t
), 1);
1538 diffstat
->xm
.consume
= diffstat_consume
;
1541 if (options
->with_raw
) {
1542 for (i
= 0; i
< q
->nr
; i
++) {
1543 struct diff_filepair
*p
= q
->queue
[i
];
1544 flush_one_pair(p
, DIFF_FORMAT_RAW
, options
, NULL
);
1546 putchar(options
->line_termination
);
1548 if (options
->with_stat
) {
1549 for (i
= 0; i
< q
->nr
; i
++) {
1550 struct diff_filepair
*p
= q
->queue
[i
];
1551 flush_one_pair(p
, DIFF_FORMAT_DIFFSTAT
, options
,
1554 show_stats(diffstat
);
1557 putchar(options
->line_termination
);
1559 for (i
= 0; i
< q
->nr
; i
++) {
1560 struct diff_filepair
*p
= q
->queue
[i
];
1561 flush_one_pair(p
, diff_output_format
, options
, diffstat
);
1562 diff_free_filepair(p
);
1566 show_stats(diffstat
);
1572 q
->nr
= q
->alloc
= 0;
1575 static void diffcore_apply_filter(const char *filter
)
1578 struct diff_queue_struct
*q
= &diff_queued_diff
;
1579 struct diff_queue_struct outq
;
1581 outq
.nr
= outq
.alloc
= 0;
1586 if (strchr(filter
, DIFF_STATUS_FILTER_AON
)) {
1588 for (i
= found
= 0; !found
&& i
< q
->nr
; i
++) {
1589 struct diff_filepair
*p
= q
->queue
[i
];
1590 if (((p
->status
== DIFF_STATUS_MODIFIED
) &&
1592 strchr(filter
, DIFF_STATUS_FILTER_BROKEN
)) ||
1594 strchr(filter
, DIFF_STATUS_MODIFIED
)))) ||
1595 ((p
->status
!= DIFF_STATUS_MODIFIED
) &&
1596 strchr(filter
, p
->status
)))
1602 /* otherwise we will clear the whole queue
1603 * by copying the empty outq at the end of this
1604 * function, but first clear the current entries
1607 for (i
= 0; i
< q
->nr
; i
++)
1608 diff_free_filepair(q
->queue
[i
]);
1611 /* Only the matching ones */
1612 for (i
= 0; i
< q
->nr
; i
++) {
1613 struct diff_filepair
*p
= q
->queue
[i
];
1615 if (((p
->status
== DIFF_STATUS_MODIFIED
) &&
1617 strchr(filter
, DIFF_STATUS_FILTER_BROKEN
)) ||
1619 strchr(filter
, DIFF_STATUS_MODIFIED
)))) ||
1620 ((p
->status
!= DIFF_STATUS_MODIFIED
) &&
1621 strchr(filter
, p
->status
)))
1624 diff_free_filepair(p
);
1631 void diffcore_std(struct diff_options
*options
)
1633 if (options
->break_opt
!= -1)
1634 diffcore_break(options
->break_opt
);
1635 if (options
->detect_rename
)
1636 diffcore_rename(options
);
1637 if (options
->break_opt
!= -1)
1638 diffcore_merge_broken();
1639 if (options
->pickaxe
)
1640 diffcore_pickaxe(options
->pickaxe
, options
->pickaxe_opts
);
1641 if (options
->orderfile
)
1642 diffcore_order(options
->orderfile
);
1643 diff_resolve_rename_copy();
1644 diffcore_apply_filter(options
->filter
);
1648 void diffcore_std_no_resolve(struct diff_options
*options
)
1650 if (options
->pickaxe
)
1651 diffcore_pickaxe(options
->pickaxe
, options
->pickaxe_opts
);
1652 if (options
->orderfile
)
1653 diffcore_order(options
->orderfile
);
1654 diffcore_apply_filter(options
->filter
);
1657 void diff_addremove(struct diff_options
*options
,
1658 int addremove
, unsigned mode
,
1659 const unsigned char *sha1
,
1660 const char *base
, const char *path
)
1662 char concatpath
[PATH_MAX
];
1663 struct diff_filespec
*one
, *two
;
1665 /* This may look odd, but it is a preparation for
1666 * feeding "there are unchanged files which should
1667 * not produce diffs, but when you are doing copy
1668 * detection you would need them, so here they are"
1669 * entries to the diff-core. They will be prefixed
1670 * with something like '=' or '*' (I haven't decided
1671 * which but should not make any difference).
1672 * Feeding the same new and old to diff_change()
1673 * also has the same effect.
1674 * Before the final output happens, they are pruned after
1675 * merged into rename/copy pairs as appropriate.
1677 if (options
->reverse_diff
)
1678 addremove
= (addremove
== '+' ? '-' :
1679 addremove
== '-' ? '+' : addremove
);
1681 if (!path
) path
= "";
1682 sprintf(concatpath
, "%s%s", base
, path
);
1683 one
= alloc_filespec(concatpath
);
1684 two
= alloc_filespec(concatpath
);
1686 if (addremove
!= '+')
1687 fill_filespec(one
, sha1
, mode
);
1688 if (addremove
!= '-')
1689 fill_filespec(two
, sha1
, mode
);
1691 diff_queue(&diff_queued_diff
, one
, two
);
1694 void diff_change(struct diff_options
*options
,
1695 unsigned old_mode
, unsigned new_mode
,
1696 const unsigned char *old_sha1
,
1697 const unsigned char *new_sha1
,
1698 const char *base
, const char *path
)
1700 char concatpath
[PATH_MAX
];
1701 struct diff_filespec
*one
, *two
;
1703 if (options
->reverse_diff
) {
1705 const unsigned char *tmp_c
;
1706 tmp
= old_mode
; old_mode
= new_mode
; new_mode
= tmp
;
1707 tmp_c
= old_sha1
; old_sha1
= new_sha1
; new_sha1
= tmp_c
;
1709 if (!path
) path
= "";
1710 sprintf(concatpath
, "%s%s", base
, path
);
1711 one
= alloc_filespec(concatpath
);
1712 two
= alloc_filespec(concatpath
);
1713 fill_filespec(one
, old_sha1
, old_mode
);
1714 fill_filespec(two
, new_sha1
, new_mode
);
1716 diff_queue(&diff_queued_diff
, one
, two
);
1719 void diff_unmerge(struct diff_options
*options
,
1722 struct diff_filespec
*one
, *two
;
1723 one
= alloc_filespec(path
);
1724 two
= alloc_filespec(path
);
1725 diff_queue(&diff_queued_diff
, one
, two
);