2 * Copyright (C) 2005 Junio C Hamano
12 #include "xdiff-interface.h"
15 static int use_size_cache
;
17 static int diff_detect_rename_default
;
18 static int diff_rename_limit_default
= -1;
19 static int diff_use_color_default
;
21 static char diff_colors
[][COLOR_MAXLEN
] = {
25 "\033[36m", /* cyan */
27 "\033[32m", /* green */
28 "\033[33m" /* yellow */
31 static int parse_diff_color_slot(const char *var
, int ofs
)
33 if (!strcasecmp(var
+ofs
, "plain"))
35 if (!strcasecmp(var
+ofs
, "meta"))
37 if (!strcasecmp(var
+ofs
, "frag"))
39 if (!strcasecmp(var
+ofs
, "old"))
41 if (!strcasecmp(var
+ofs
, "new"))
43 if (!strcasecmp(var
+ofs
, "commit"))
45 die("bad config variable '%s'", var
);
49 * These are to give UI layer defaults.
50 * The core-level commands such as git-diff-files should
51 * never be affected by the setting of diff.renames
52 * the user happens to have in the configuration file.
54 int git_diff_ui_config(const char *var
, const char *value
)
56 if (!strcmp(var
, "diff.renamelimit")) {
57 diff_rename_limit_default
= git_config_int(var
, value
);
60 if (!strcmp(var
, "diff.color")) {
61 diff_use_color_default
= git_config_colorbool(var
, value
);
64 if (!strcmp(var
, "diff.renames")) {
66 diff_detect_rename_default
= DIFF_DETECT_RENAME
;
67 else if (!strcasecmp(value
, "copies") ||
68 !strcasecmp(value
, "copy"))
69 diff_detect_rename_default
= DIFF_DETECT_COPY
;
70 else if (git_config_bool(var
,value
))
71 diff_detect_rename_default
= DIFF_DETECT_RENAME
;
74 if (!strncmp(var
, "diff.color.", 11)) {
75 int slot
= parse_diff_color_slot(var
, 11);
76 color_parse(value
, var
, diff_colors
[slot
]);
79 return git_default_config(var
, value
);
82 static char *quote_one(const char *str
)
89 needlen
= quote_c_style(str
, NULL
, NULL
, 0);
92 xp
= xmalloc(needlen
+ 1);
93 quote_c_style(str
, xp
, NULL
, 0);
97 static char *quote_two(const char *one
, const char *two
)
99 int need_one
= quote_c_style(one
, NULL
, NULL
, 1);
100 int need_two
= quote_c_style(two
, NULL
, NULL
, 1);
103 if (need_one
+ need_two
) {
104 if (!need_one
) need_one
= strlen(one
);
105 if (!need_two
) need_one
= strlen(two
);
107 xp
= xmalloc(need_one
+ need_two
+ 3);
109 quote_c_style(one
, xp
+ 1, NULL
, 1);
110 quote_c_style(two
, xp
+ need_one
+ 1, NULL
, 1);
111 strcpy(xp
+ need_one
+ need_two
+ 1, "\"");
114 need_one
= strlen(one
);
115 need_two
= strlen(two
);
116 xp
= xmalloc(need_one
+ need_two
+ 1);
118 strcpy(xp
+ need_one
, two
);
122 static const char *external_diff(void)
124 static const char *external_diff_cmd
= NULL
;
125 static int done_preparing
= 0;
128 return external_diff_cmd
;
129 external_diff_cmd
= getenv("GIT_EXTERNAL_DIFF");
131 return external_diff_cmd
;
134 #define TEMPFILE_PATH_LEN 50
136 static struct diff_tempfile
{
137 const char *name
; /* filename external diff should read from */
140 char tmp_path
[TEMPFILE_PATH_LEN
];
143 static int count_lines(const char *data
, int size
)
145 int count
, ch
, completely_empty
= 1, nl_just_seen
= 0;
152 completely_empty
= 0;
156 completely_empty
= 0;
159 if (completely_empty
)
162 count
++; /* no trailing newline */
166 static void print_line_count(int count
)
176 printf("1,%d", count
);
181 static void copy_file(int prefix
, const char *data
, int size
)
183 int ch
, nl_just_seen
= 1;
195 printf("\n\\ No newline at end of file\n");
198 static void emit_rewrite_diff(const char *name_a
,
200 struct diff_filespec
*one
,
201 struct diff_filespec
*two
)
204 diff_populate_filespec(one
, 0);
205 diff_populate_filespec(two
, 0);
206 lc_a
= count_lines(one
->data
, one
->size
);
207 lc_b
= count_lines(two
->data
, two
->size
);
208 printf("--- %s\n+++ %s\n@@ -", name_a
, name_b
);
209 print_line_count(lc_a
);
211 print_line_count(lc_b
);
214 copy_file('-', one
->data
, one
->size
);
216 copy_file('+', two
->data
, two
->size
);
219 static int fill_mmfile(mmfile_t
*mf
, struct diff_filespec
*one
)
221 if (!DIFF_FILE_VALID(one
)) {
222 mf
->ptr
= (char *)""; /* does not matter */
226 else if (diff_populate_filespec(one
, 0))
229 mf
->size
= one
->size
;
233 struct diff_words_buffer
{
236 long current
; /* output pointer */
237 int suppressed_newline
;
240 static void diff_words_append(char *line
, unsigned long len
,
241 struct diff_words_buffer
*buffer
)
243 if (buffer
->text
.size
+ len
> buffer
->alloc
) {
244 buffer
->alloc
= (buffer
->text
.size
+ len
) * 3 / 2;
245 buffer
->text
.ptr
= xrealloc(buffer
->text
.ptr
, buffer
->alloc
);
249 memcpy(buffer
->text
.ptr
+ buffer
->text
.size
, line
, len
);
250 buffer
->text
.size
+= len
;
253 struct diff_words_data
{
254 struct xdiff_emit_state xm
;
255 struct diff_words_buffer minus
, plus
;
258 static void print_word(struct diff_words_buffer
*buffer
, int len
, int color
,
259 int suppress_newline
)
267 ptr
= buffer
->text
.ptr
+ buffer
->current
;
268 buffer
->current
+= len
;
270 if (ptr
[len
- 1] == '\n') {
275 fputs(diff_get_color(1, color
), stdout
);
276 fwrite(ptr
, len
, 1, stdout
);
277 fputs(diff_get_color(1, DIFF_RESET
), stdout
);
280 if (suppress_newline
)
281 buffer
->suppressed_newline
= 1;
287 static void fn_out_diff_words_aux(void *priv
, char *line
, unsigned long len
)
289 struct diff_words_data
*diff_words
= priv
;
291 if (diff_words
->minus
.suppressed_newline
) {
294 diff_words
->minus
.suppressed_newline
= 0;
300 print_word(&diff_words
->minus
, len
, DIFF_FILE_OLD
, 1);
303 print_word(&diff_words
->plus
, len
, DIFF_FILE_NEW
, 0);
306 print_word(&diff_words
->plus
, len
, DIFF_PLAIN
, 0);
307 diff_words
->minus
.current
+= len
;
312 /* this executes the word diff on the accumulated buffers */
313 static void diff_words_show(struct diff_words_data
*diff_words
)
318 mmfile_t minus
, plus
;
321 minus
.size
= diff_words
->minus
.text
.size
;
322 minus
.ptr
= xmalloc(minus
.size
);
323 memcpy(minus
.ptr
, diff_words
->minus
.text
.ptr
, minus
.size
);
324 for (i
= 0; i
< minus
.size
; i
++)
325 if (isspace(minus
.ptr
[i
]))
327 diff_words
->minus
.current
= 0;
329 plus
.size
= diff_words
->plus
.text
.size
;
330 plus
.ptr
= xmalloc(plus
.size
);
331 memcpy(plus
.ptr
, diff_words
->plus
.text
.ptr
, plus
.size
);
332 for (i
= 0; i
< plus
.size
; i
++)
333 if (isspace(plus
.ptr
[i
]))
335 diff_words
->plus
.current
= 0;
337 xpp
.flags
= XDF_NEED_MINIMAL
;
338 xecfg
.ctxlen
= diff_words
->minus
.alloc
+ diff_words
->plus
.alloc
;
340 ecb
.outf
= xdiff_outf
;
341 ecb
.priv
= diff_words
;
342 diff_words
->xm
.consume
= fn_out_diff_words_aux
;
343 xdl_diff(&minus
, &plus
, &xpp
, &xecfg
, &ecb
);
347 diff_words
->minus
.text
.size
= diff_words
->plus
.text
.size
= 0;
349 if (diff_words
->minus
.suppressed_newline
) {
351 diff_words
->minus
.suppressed_newline
= 0;
355 struct emit_callback
{
356 struct xdiff_emit_state xm
;
357 int nparents
, color_diff
;
358 const char **label_path
;
359 struct diff_words_data
*diff_words
;
362 static void free_diff_words_data(struct emit_callback
*ecbdata
)
364 if (ecbdata
->diff_words
) {
366 if (ecbdata
->diff_words
->minus
.text
.size
||
367 ecbdata
->diff_words
->plus
.text
.size
)
368 diff_words_show(ecbdata
->diff_words
);
370 if (ecbdata
->diff_words
->minus
.text
.ptr
)
371 free (ecbdata
->diff_words
->minus
.text
.ptr
);
372 if (ecbdata
->diff_words
->plus
.text
.ptr
)
373 free (ecbdata
->diff_words
->plus
.text
.ptr
);
374 free(ecbdata
->diff_words
);
375 ecbdata
->diff_words
= NULL
;
379 const char *diff_get_color(int diff_use_color
, enum color_diff ix
)
382 return diff_colors
[ix
];
386 static void fn_out_consume(void *priv
, char *line
, unsigned long len
)
389 struct emit_callback
*ecbdata
= priv
;
390 const char *set
= diff_get_color(ecbdata
->color_diff
, DIFF_METAINFO
);
391 const char *reset
= diff_get_color(ecbdata
->color_diff
, DIFF_RESET
);
393 if (ecbdata
->label_path
[0]) {
394 printf("%s--- %s%s\n", set
, ecbdata
->label_path
[0], reset
);
395 printf("%s+++ %s%s\n", set
, ecbdata
->label_path
[1], reset
);
396 ecbdata
->label_path
[0] = ecbdata
->label_path
[1] = NULL
;
399 /* This is not really necessary for now because
400 * this codepath only deals with two-way diffs.
402 for (i
= 0; i
< len
&& line
[i
] == '@'; i
++)
404 if (2 <= i
&& i
< len
&& line
[i
] == ' ') {
405 ecbdata
->nparents
= i
- 1;
406 set
= diff_get_color(ecbdata
->color_diff
, DIFF_FRAGINFO
);
408 else if (len
< ecbdata
->nparents
)
411 int nparents
= ecbdata
->nparents
;
412 int color
= DIFF_PLAIN
;
413 if (ecbdata
->diff_words
&& nparents
!= 1)
414 /* fall back to normal diff */
415 free_diff_words_data(ecbdata
);
416 if (ecbdata
->diff_words
) {
417 if (line
[0] == '-') {
418 diff_words_append(line
, len
,
419 &ecbdata
->diff_words
->minus
);
421 } else if (line
[0] == '+') {
422 diff_words_append(line
, len
,
423 &ecbdata
->diff_words
->plus
);
426 if (ecbdata
->diff_words
->minus
.text
.size
||
427 ecbdata
->diff_words
->plus
.text
.size
)
428 diff_words_show(ecbdata
->diff_words
);
432 for (i
= 0; i
< nparents
&& len
; i
++) {
434 color
= DIFF_FILE_OLD
;
435 else if (line
[i
] == '+')
436 color
= DIFF_FILE_NEW
;
438 set
= diff_get_color(ecbdata
->color_diff
, color
);
440 if (len
> 0 && line
[len
-1] == '\n')
443 fwrite (line
, len
, 1, stdout
);
447 static char *pprint_rename(const char *a
, const char *b
)
452 int pfx_length
, sfx_length
;
453 int len_a
= strlen(a
);
454 int len_b
= strlen(b
);
456 /* Find common prefix */
458 while (*old
&& *new && *old
== *new) {
460 pfx_length
= old
- a
+ 1;
465 /* Find common suffix */
469 while (a
<= old
&& b
<= new && *old
== *new) {
471 sfx_length
= len_a
- (old
- a
);
477 * pfx{mid-a => mid-b}sfx
478 * {pfx-a => pfx-b}sfx
479 * pfx{sfx-a => sfx-b}
482 if (pfx_length
+ sfx_length
) {
483 int a_midlen
= len_a
- pfx_length
- sfx_length
;
484 int b_midlen
= len_b
- pfx_length
- sfx_length
;
485 if (a_midlen
< 0) a_midlen
= 0;
486 if (b_midlen
< 0) b_midlen
= 0;
488 name
= xmalloc(pfx_length
+ a_midlen
+ b_midlen
+ sfx_length
+ 7);
489 sprintf(name
, "%.*s{%.*s => %.*s}%s",
491 a_midlen
, a
+ pfx_length
,
492 b_midlen
, b
+ pfx_length
,
493 a
+ len_a
- sfx_length
);
496 name
= xmalloc(len_a
+ len_b
+ 5);
497 sprintf(name
, "%s => %s", a
, b
);
503 struct xdiff_emit_state xm
;
507 struct diffstat_file
{
509 unsigned is_unmerged
:1;
510 unsigned is_binary
:1;
511 unsigned is_renamed
:1;
512 unsigned int added
, deleted
;
516 static struct diffstat_file
*diffstat_add(struct diffstat_t
*diffstat
,
520 struct diffstat_file
*x
;
521 x
= xcalloc(sizeof (*x
), 1);
522 if (diffstat
->nr
== diffstat
->alloc
) {
523 diffstat
->alloc
= alloc_nr(diffstat
->alloc
);
524 diffstat
->files
= xrealloc(diffstat
->files
,
525 diffstat
->alloc
* sizeof(x
));
527 diffstat
->files
[diffstat
->nr
++] = x
;
529 x
->name
= pprint_rename(name_a
, name_b
);
533 x
->name
= xstrdup(name_a
);
537 static void diffstat_consume(void *priv
, char *line
, unsigned long len
)
539 struct diffstat_t
*diffstat
= priv
;
540 struct diffstat_file
*x
= diffstat
->files
[diffstat
->nr
- 1];
544 else if (line
[0] == '-')
548 static const char pluses
[] = "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++";
549 static const char minuses
[]= "----------------------------------------------------------------------";
550 const char mime_boundary_leader
[] = "------------";
552 static void show_stats(struct diffstat_t
* data
)
554 int i
, len
, add
, del
, total
, adds
= 0, dels
= 0;
555 int max
, max_change
= 0, max_len
= 0;
556 int total_files
= data
->nr
;
561 for (i
= 0; i
< data
->nr
; i
++) {
562 struct diffstat_file
*file
= data
->files
[i
];
564 len
= strlen(file
->name
);
568 if (file
->is_binary
|| file
->is_unmerged
)
570 if (max_change
< file
->added
+ file
->deleted
)
571 max_change
= file
->added
+ file
->deleted
;
574 for (i
= 0; i
< data
->nr
; i
++) {
575 const char *prefix
= "";
576 char *name
= data
->files
[i
]->name
;
577 int added
= data
->files
[i
]->added
;
578 int deleted
= data
->files
[i
]->deleted
;
580 if (0 < (len
= quote_c_style(name
, NULL
, NULL
, 0))) {
581 char *qname
= xmalloc(len
+ 1);
582 quote_c_style(name
, qname
, NULL
, 0);
584 data
->files
[i
]->name
= name
= qname
;
588 * "scale" the filename
599 slash
= strchr(name
, '/');
606 * scale the add/delete
612 if (data
->files
[i
]->is_binary
) {
613 printf(" %s%-*s | Bin\n", prefix
, len
, name
);
614 goto free_diffstat_file
;
616 else if (data
->files
[i
]->is_unmerged
) {
617 printf(" %s%-*s | Unmerged\n", prefix
, len
, name
);
618 goto free_diffstat_file
;
620 else if (!data
->files
[i
]->is_renamed
&&
621 (added
+ deleted
== 0)) {
623 goto free_diffstat_file
;
632 if (max_change
> 0) {
633 total
= (total
* max
+ max_change
/ 2) / max_change
;
634 add
= (add
* max
+ max_change
/ 2) / max_change
;
637 printf(" %s%-*s |%5d %.*s%.*s\n", prefix
,
638 len
, name
, added
+ deleted
,
639 add
, pluses
, del
, minuses
);
641 free(data
->files
[i
]->name
);
642 free(data
->files
[i
]);
645 printf(" %d files changed, %d insertions(+), %d deletions(-)\n",
646 total_files
, adds
, dels
);
650 struct xdiff_emit_state xm
;
651 const char *filename
;
655 static void checkdiff_consume(void *priv
, char *line
, unsigned long len
)
657 struct checkdiff_t
*data
= priv
;
659 if (line
[0] == '+') {
664 /* check space before tab */
665 for (i
= 1; i
< len
&& (line
[i
] == ' ' || line
[i
] == '\t'); i
++)
668 if (line
[i
- 1] == '\t' && spaces
)
669 printf("%s:%d: space before tab:%.*s\n",
670 data
->filename
, data
->lineno
, (int)len
, line
);
672 /* check white space at line end */
673 if (line
[len
- 1] == '\n')
675 if (isspace(line
[len
- 1]))
676 printf("%s:%d: white space at end: %.*s\n",
677 data
->filename
, data
->lineno
, (int)len
, line
);
678 } else if (line
[0] == ' ')
680 else if (line
[0] == '@') {
681 char *plus
= strchr(line
, '+');
683 data
->lineno
= strtol(plus
, NULL
, 10);
689 static unsigned char *deflate_it(char *data
,
691 unsigned long *result_size
)
694 unsigned char *deflated
;
697 memset(&stream
, 0, sizeof(stream
));
698 deflateInit(&stream
, zlib_compression_level
);
699 bound
= deflateBound(&stream
, size
);
700 deflated
= xmalloc(bound
);
701 stream
.next_out
= deflated
;
702 stream
.avail_out
= bound
;
704 stream
.next_in
= (unsigned char *)data
;
705 stream
.avail_in
= size
;
706 while (deflate(&stream
, Z_FINISH
) == Z_OK
)
709 *result_size
= stream
.total_out
;
713 static void emit_binary_diff_body(mmfile_t
*one
, mmfile_t
*two
)
719 unsigned long orig_size
;
720 unsigned long delta_size
;
721 unsigned long deflate_size
;
722 unsigned long data_size
;
724 /* We could do deflated delta, or we could do just deflated two,
725 * whichever is smaller.
728 deflated
= deflate_it(two
->ptr
, two
->size
, &deflate_size
);
729 if (one
->size
&& two
->size
) {
730 delta
= diff_delta(one
->ptr
, one
->size
,
732 &delta_size
, deflate_size
);
734 void *to_free
= delta
;
735 orig_size
= delta_size
;
736 delta
= deflate_it(delta
, delta_size
, &delta_size
);
741 if (delta
&& delta_size
< deflate_size
) {
742 printf("delta %lu\n", orig_size
);
745 data_size
= delta_size
;
748 printf("literal %lu\n", two
->size
);
751 data_size
= deflate_size
;
754 /* emit data encoded in base85 */
757 int bytes
= (52 < data_size
) ? 52 : data_size
;
761 line
[0] = bytes
+ 'A' - 1;
763 line
[0] = bytes
- 26 + 'a' - 1;
764 encode_85(line
+ 1, cp
, bytes
);
765 cp
= (char *) cp
+ bytes
;
772 static void emit_binary_diff(mmfile_t
*one
, mmfile_t
*two
)
774 printf("GIT binary patch\n");
775 emit_binary_diff_body(one
, two
);
776 emit_binary_diff_body(two
, one
);
779 #define FIRST_FEW_BYTES 8000
780 static int mmfile_is_binary(mmfile_t
*mf
)
783 if (FIRST_FEW_BYTES
< sz
)
784 sz
= FIRST_FEW_BYTES
;
785 return !!memchr(mf
->ptr
, 0, sz
);
788 static void builtin_diff(const char *name_a
,
790 struct diff_filespec
*one
,
791 struct diff_filespec
*two
,
792 const char *xfrm_msg
,
793 struct diff_options
*o
,
794 int complete_rewrite
)
799 const char *set
= diff_get_color(o
->color_diff
, DIFF_METAINFO
);
800 const char *reset
= diff_get_color(o
->color_diff
, DIFF_RESET
);
802 a_one
= quote_two("a/", name_a
);
803 b_two
= quote_two("b/", name_b
);
804 lbl
[0] = DIFF_FILE_VALID(one
) ? a_one
: "/dev/null";
805 lbl
[1] = DIFF_FILE_VALID(two
) ? b_two
: "/dev/null";
806 printf("%sdiff --git %s %s%s\n", set
, a_one
, b_two
, reset
);
807 if (lbl
[0][0] == '/') {
809 printf("%snew file mode %06o%s\n", set
, two
->mode
, reset
);
810 if (xfrm_msg
&& xfrm_msg
[0])
811 printf("%s%s%s\n", set
, xfrm_msg
, reset
);
813 else if (lbl
[1][0] == '/') {
814 printf("%sdeleted file mode %06o%s\n", set
, one
->mode
, reset
);
815 if (xfrm_msg
&& xfrm_msg
[0])
816 printf("%s%s%s\n", set
, xfrm_msg
, reset
);
819 if (one
->mode
!= two
->mode
) {
820 printf("%sold mode %06o%s\n", set
, one
->mode
, reset
);
821 printf("%snew mode %06o%s\n", set
, two
->mode
, reset
);
823 if (xfrm_msg
&& xfrm_msg
[0])
824 printf("%s%s%s\n", set
, xfrm_msg
, reset
);
826 * we do not run diff between different kind
829 if ((one
->mode
^ two
->mode
) & S_IFMT
)
830 goto free_ab_and_return
;
831 if (complete_rewrite
) {
832 emit_rewrite_diff(name_a
, name_b
, one
, two
);
833 goto free_ab_and_return
;
837 if (fill_mmfile(&mf1
, one
) < 0 || fill_mmfile(&mf2
, two
) < 0)
838 die("unable to read files to diff");
840 if (!o
->text
&& (mmfile_is_binary(&mf1
) || mmfile_is_binary(&mf2
))) {
841 /* Quite common confusing case */
842 if (mf1
.size
== mf2
.size
&&
843 !memcmp(mf1
.ptr
, mf2
.ptr
, mf1
.size
))
844 goto free_ab_and_return
;
846 emit_binary_diff(&mf1
, &mf2
);
848 printf("Binary files %s and %s differ\n",
852 /* Crazy xdl interfaces.. */
853 const char *diffopts
= getenv("GIT_DIFF_OPTS");
857 struct emit_callback ecbdata
;
859 memset(&ecbdata
, 0, sizeof(ecbdata
));
860 ecbdata
.label_path
= lbl
;
861 ecbdata
.color_diff
= o
->color_diff
;
862 xpp
.flags
= XDF_NEED_MINIMAL
| o
->xdl_opts
;
863 xecfg
.ctxlen
= o
->context
;
864 xecfg
.flags
= XDL_EMIT_FUNCNAMES
;
867 else if (!strncmp(diffopts
, "--unified=", 10))
868 xecfg
.ctxlen
= strtoul(diffopts
+ 10, NULL
, 10);
869 else if (!strncmp(diffopts
, "-u", 2))
870 xecfg
.ctxlen
= strtoul(diffopts
+ 2, NULL
, 10);
871 ecb
.outf
= xdiff_outf
;
873 ecbdata
.xm
.consume
= fn_out_consume
;
874 if (o
->color_diff_words
)
876 xcalloc(1, sizeof(struct diff_words_data
));
877 xdl_diff(&mf1
, &mf2
, &xpp
, &xecfg
, &ecb
);
878 if (o
->color_diff_words
)
879 free_diff_words_data(&ecbdata
);
888 static void builtin_diffstat(const char *name_a
, const char *name_b
,
889 struct diff_filespec
*one
,
890 struct diff_filespec
*two
,
891 struct diffstat_t
*diffstat
,
892 struct diff_options
*o
,
893 int complete_rewrite
)
896 struct diffstat_file
*data
;
898 data
= diffstat_add(diffstat
, name_a
, name_b
);
901 data
->is_unmerged
= 1;
904 if (complete_rewrite
) {
905 diff_populate_filespec(one
, 0);
906 diff_populate_filespec(two
, 0);
907 data
->deleted
= count_lines(one
->data
, one
->size
);
908 data
->added
= count_lines(two
->data
, two
->size
);
911 if (fill_mmfile(&mf1
, one
) < 0 || fill_mmfile(&mf2
, two
) < 0)
912 die("unable to read files to diff");
914 if (mmfile_is_binary(&mf1
) || mmfile_is_binary(&mf2
))
917 /* Crazy xdl interfaces.. */
922 xpp
.flags
= XDF_NEED_MINIMAL
| o
->xdl_opts
;
925 ecb
.outf
= xdiff_outf
;
927 xdl_diff(&mf1
, &mf2
, &xpp
, &xecfg
, &ecb
);
931 static void builtin_checkdiff(const char *name_a
, const char *name_b
,
932 struct diff_filespec
*one
,
933 struct diff_filespec
*two
)
936 struct checkdiff_t data
;
941 memset(&data
, 0, sizeof(data
));
942 data
.xm
.consume
= checkdiff_consume
;
943 data
.filename
= name_b
? name_b
: name_a
;
946 if (fill_mmfile(&mf1
, one
) < 0 || fill_mmfile(&mf2
, two
) < 0)
947 die("unable to read files to diff");
949 if (mmfile_is_binary(&mf2
))
952 /* Crazy xdl interfaces.. */
957 xpp
.flags
= XDF_NEED_MINIMAL
;
960 ecb
.outf
= xdiff_outf
;
962 xdl_diff(&mf1
, &mf2
, &xpp
, &xecfg
, &ecb
);
966 struct diff_filespec
*alloc_filespec(const char *path
)
968 int namelen
= strlen(path
);
969 struct diff_filespec
*spec
= xmalloc(sizeof(*spec
) + namelen
+ 1);
971 memset(spec
, 0, sizeof(*spec
));
972 spec
->path
= (char *)(spec
+ 1);
973 memcpy(spec
->path
, path
, namelen
+1);
977 void fill_filespec(struct diff_filespec
*spec
, const unsigned char *sha1
,
981 spec
->mode
= canon_mode(mode
);
982 hashcpy(spec
->sha1
, sha1
);
983 spec
->sha1_valid
= !is_null_sha1(sha1
);
988 * Given a name and sha1 pair, if the dircache tells us the file in
989 * the work tree has that object contents, return true, so that
990 * prepare_temp_file() does not have to inflate and extract.
992 static int work_tree_matches(const char *name
, const unsigned char *sha1
)
994 struct cache_entry
*ce
;
998 /* We do not read the cache ourselves here, because the
999 * benchmark with my previous version that always reads cache
1000 * shows that it makes things worse for diff-tree comparing
1001 * two linux-2.6 kernel trees in an already checked out work
1002 * tree. This is because most diff-tree comparisons deal with
1003 * only a small number of files, while reading the cache is
1004 * expensive for a large project, and its cost outweighs the
1005 * savings we get by not inflating the object to a temporary
1006 * file. Practically, this code only helps when we are used
1007 * by diff-cache --cached, which does read the cache before
1014 pos
= cache_name_pos(name
, len
);
1017 ce
= active_cache
[pos
];
1018 if ((lstat(name
, &st
) < 0) ||
1019 !S_ISREG(st
.st_mode
) || /* careful! */
1020 ce_match_stat(ce
, &st
, 0) ||
1021 hashcmp(sha1
, ce
->sha1
))
1023 /* we return 1 only when we can stat, it is a regular file,
1024 * stat information matches, and sha1 recorded in the cache
1025 * matches. I.e. we know the file in the work tree really is
1026 * the same as the <name, sha1> pair.
1031 static struct sha1_size_cache
{
1032 unsigned char sha1
[20];
1034 } **sha1_size_cache
;
1035 static int sha1_size_cache_nr
, sha1_size_cache_alloc
;
1037 static struct sha1_size_cache
*locate_size_cache(unsigned char *sha1
,
1042 struct sha1_size_cache
*e
;
1045 last
= sha1_size_cache_nr
;
1046 while (last
> first
) {
1047 int cmp
, next
= (last
+ first
) >> 1;
1048 e
= sha1_size_cache
[next
];
1049 cmp
= hashcmp(e
->sha1
, sha1
);
1061 /* insert to make it at "first" */
1062 if (sha1_size_cache_alloc
<= sha1_size_cache_nr
) {
1063 sha1_size_cache_alloc
= alloc_nr(sha1_size_cache_alloc
);
1064 sha1_size_cache
= xrealloc(sha1_size_cache
,
1065 sha1_size_cache_alloc
*
1066 sizeof(*sha1_size_cache
));
1068 sha1_size_cache_nr
++;
1069 if (first
< sha1_size_cache_nr
)
1070 memmove(sha1_size_cache
+ first
+ 1, sha1_size_cache
+ first
,
1071 (sha1_size_cache_nr
- first
- 1) *
1072 sizeof(*sha1_size_cache
));
1073 e
= xmalloc(sizeof(struct sha1_size_cache
));
1074 sha1_size_cache
[first
] = e
;
1075 hashcpy(e
->sha1
, sha1
);
1081 * While doing rename detection and pickaxe operation, we may need to
1082 * grab the data for the blob (or file) for our own in-core comparison.
1083 * diff_filespec has data and size fields for this purpose.
1085 int diff_populate_filespec(struct diff_filespec
*s
, int size_only
)
1088 if (!DIFF_FILE_VALID(s
))
1089 die("internal error: asking to populate invalid file.");
1090 if (S_ISDIR(s
->mode
))
1093 if (!use_size_cache
)
1098 if (!s
->sha1_valid
||
1099 work_tree_matches(s
->path
, s
->sha1
)) {
1102 if (lstat(s
->path
, &st
) < 0) {
1103 if (errno
== ENOENT
) {
1107 s
->data
= (char *)"";
1112 s
->size
= st
.st_size
;
1117 if (S_ISLNK(st
.st_mode
)) {
1119 s
->data
= xmalloc(s
->size
);
1121 ret
= readlink(s
->path
, s
->data
, s
->size
);
1128 fd
= open(s
->path
, O_RDONLY
);
1131 s
->data
= mmap(NULL
, s
->size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
1133 if (s
->data
== MAP_FAILED
)
1135 s
->should_munmap
= 1;
1139 struct sha1_size_cache
*e
;
1142 e
= locate_size_cache(s
->sha1
, 1, 0);
1147 if (!sha1_object_info(s
->sha1
, type
, &s
->size
))
1148 locate_size_cache(s
->sha1
, 0, s
->size
);
1151 s
->data
= read_sha1_file(s
->sha1
, type
, &s
->size
);
1158 void diff_free_filespec_data(struct diff_filespec
*s
)
1162 else if (s
->should_munmap
)
1163 munmap(s
->data
, s
->size
);
1164 s
->should_free
= s
->should_munmap
= 0;
1170 static void prep_temp_blob(struct diff_tempfile
*temp
,
1173 const unsigned char *sha1
,
1178 fd
= git_mkstemp(temp
->tmp_path
, TEMPFILE_PATH_LEN
, ".diff_XXXXXX");
1180 die("unable to create temp-file");
1181 if (write(fd
, blob
, size
) != size
)
1182 die("unable to write temp-file");
1184 temp
->name
= temp
->tmp_path
;
1185 strcpy(temp
->hex
, sha1_to_hex(sha1
));
1187 sprintf(temp
->mode
, "%06o", mode
);
1190 static void prepare_temp_file(const char *name
,
1191 struct diff_tempfile
*temp
,
1192 struct diff_filespec
*one
)
1194 if (!DIFF_FILE_VALID(one
)) {
1196 /* A '-' entry produces this for file-2, and
1197 * a '+' entry produces this for file-1.
1199 temp
->name
= "/dev/null";
1200 strcpy(temp
->hex
, ".");
1201 strcpy(temp
->mode
, ".");
1205 if (!one
->sha1_valid
||
1206 work_tree_matches(name
, one
->sha1
)) {
1208 if (lstat(name
, &st
) < 0) {
1209 if (errno
== ENOENT
)
1210 goto not_a_valid_file
;
1211 die("stat(%s): %s", name
, strerror(errno
));
1213 if (S_ISLNK(st
.st_mode
)) {
1215 char buf
[PATH_MAX
+ 1]; /* ought to be SYMLINK_MAX */
1216 if (sizeof(buf
) <= st
.st_size
)
1217 die("symlink too long: %s", name
);
1218 ret
= readlink(name
, buf
, st
.st_size
);
1220 die("readlink(%s)", name
);
1221 prep_temp_blob(temp
, buf
, st
.st_size
,
1223 one
->sha1
: null_sha1
),
1225 one
->mode
: S_IFLNK
));
1228 /* we can borrow from the file in the work tree */
1230 if (!one
->sha1_valid
)
1231 strcpy(temp
->hex
, sha1_to_hex(null_sha1
));
1233 strcpy(temp
->hex
, sha1_to_hex(one
->sha1
));
1234 /* Even though we may sometimes borrow the
1235 * contents from the work tree, we always want
1236 * one->mode. mode is trustworthy even when
1237 * !(one->sha1_valid), as long as
1238 * DIFF_FILE_VALID(one).
1240 sprintf(temp
->mode
, "%06o", one
->mode
);
1245 if (diff_populate_filespec(one
, 0))
1246 die("cannot read data blob for %s", one
->path
);
1247 prep_temp_blob(temp
, one
->data
, one
->size
,
1248 one
->sha1
, one
->mode
);
1252 static void remove_tempfile(void)
1256 for (i
= 0; i
< 2; i
++)
1257 if (diff_temp
[i
].name
== diff_temp
[i
].tmp_path
) {
1258 unlink(diff_temp
[i
].name
);
1259 diff_temp
[i
].name
= NULL
;
1263 static void remove_tempfile_on_signal(int signo
)
1266 signal(SIGINT
, SIG_DFL
);
1270 static int spawn_prog(const char *pgm
, const char **arg
)
1278 die("unable to fork");
1280 execvp(pgm
, (char *const*) arg
);
1284 while (waitpid(pid
, &status
, 0) < 0) {
1290 /* Earlier we did not check the exit status because
1291 * diff exits non-zero if files are different, and
1292 * we are not interested in knowing that. It was a
1293 * mistake which made it harder to quit a diff-*
1294 * session that uses the git-apply-patch-script as
1295 * the GIT_EXTERNAL_DIFF. A custom GIT_EXTERNAL_DIFF
1296 * should also exit non-zero only when it wants to
1297 * abort the entire diff-* session.
1299 if (WIFEXITED(status
) && !WEXITSTATUS(status
))
1304 /* An external diff command takes:
1306 * diff-cmd name infile1 infile1-sha1 infile1-mode \
1307 * infile2 infile2-sha1 infile2-mode [ rename-to ]
1310 static void run_external_diff(const char *pgm
,
1313 struct diff_filespec
*one
,
1314 struct diff_filespec
*two
,
1315 const char *xfrm_msg
,
1316 int complete_rewrite
)
1318 const char *spawn_arg
[10];
1319 struct diff_tempfile
*temp
= diff_temp
;
1321 static int atexit_asked
= 0;
1322 const char *othername
;
1323 const char **arg
= &spawn_arg
[0];
1325 othername
= (other
? other
: name
);
1327 prepare_temp_file(name
, &temp
[0], one
);
1328 prepare_temp_file(othername
, &temp
[1], two
);
1329 if (! atexit_asked
&&
1330 (temp
[0].name
== temp
[0].tmp_path
||
1331 temp
[1].name
== temp
[1].tmp_path
)) {
1333 atexit(remove_tempfile
);
1335 signal(SIGINT
, remove_tempfile_on_signal
);
1341 *arg
++ = temp
[0].name
;
1342 *arg
++ = temp
[0].hex
;
1343 *arg
++ = temp
[0].mode
;
1344 *arg
++ = temp
[1].name
;
1345 *arg
++ = temp
[1].hex
;
1346 *arg
++ = temp
[1].mode
;
1356 retval
= spawn_prog(pgm
, spawn_arg
);
1359 fprintf(stderr
, "external diff died, stopping at %s.\n", name
);
1364 static void run_diff_cmd(const char *pgm
,
1367 struct diff_filespec
*one
,
1368 struct diff_filespec
*two
,
1369 const char *xfrm_msg
,
1370 struct diff_options
*o
,
1371 int complete_rewrite
)
1374 run_external_diff(pgm
, name
, other
, one
, two
, xfrm_msg
,
1379 builtin_diff(name
, other
? other
: name
,
1380 one
, two
, xfrm_msg
, o
, complete_rewrite
);
1382 printf("* Unmerged path %s\n", name
);
1385 static void diff_fill_sha1_info(struct diff_filespec
*one
)
1387 if (DIFF_FILE_VALID(one
)) {
1388 if (!one
->sha1_valid
) {
1390 if (lstat(one
->path
, &st
) < 0)
1391 die("stat %s", one
->path
);
1392 if (index_path(one
->sha1
, one
->path
, &st
, 0))
1393 die("cannot hash %s\n", one
->path
);
1400 static void run_diff(struct diff_filepair
*p
, struct diff_options
*o
)
1402 const char *pgm
= external_diff();
1403 char msg
[PATH_MAX
*2+300], *xfrm_msg
;
1404 struct diff_filespec
*one
;
1405 struct diff_filespec
*two
;
1408 char *name_munged
, *other_munged
;
1409 int complete_rewrite
= 0;
1412 if (DIFF_PAIR_UNMERGED(p
)) {
1414 run_diff_cmd(pgm
, p
->one
->path
, NULL
, NULL
, NULL
, NULL
, o
, 0);
1418 name
= p
->one
->path
;
1419 other
= (strcmp(name
, p
->two
->path
) ? p
->two
->path
: NULL
);
1420 name_munged
= quote_one(name
);
1421 other_munged
= quote_one(other
);
1422 one
= p
->one
; two
= p
->two
;
1424 diff_fill_sha1_info(one
);
1425 diff_fill_sha1_info(two
);
1428 switch (p
->status
) {
1429 case DIFF_STATUS_COPIED
:
1430 len
+= snprintf(msg
+ len
, sizeof(msg
) - len
,
1431 "similarity index %d%%\n"
1434 (int)(0.5 + p
->score
* 100.0/MAX_SCORE
),
1435 name_munged
, other_munged
);
1437 case DIFF_STATUS_RENAMED
:
1438 len
+= snprintf(msg
+ len
, sizeof(msg
) - len
,
1439 "similarity index %d%%\n"
1442 (int)(0.5 + p
->score
* 100.0/MAX_SCORE
),
1443 name_munged
, other_munged
);
1445 case DIFF_STATUS_MODIFIED
:
1447 len
+= snprintf(msg
+ len
, sizeof(msg
) - len
,
1448 "dissimilarity index %d%%\n",
1449 (int)(0.5 + p
->score
*
1451 complete_rewrite
= 1;
1460 if (hashcmp(one
->sha1
, two
->sha1
)) {
1461 int abbrev
= o
->full_index
? 40 : DEFAULT_ABBREV
;
1463 len
+= snprintf(msg
+ len
, sizeof(msg
) - len
,
1465 abbrev
, sha1_to_hex(one
->sha1
),
1466 abbrev
, sha1_to_hex(two
->sha1
));
1467 if (one
->mode
== two
->mode
)
1468 len
+= snprintf(msg
+ len
, sizeof(msg
) - len
,
1469 " %06o", one
->mode
);
1470 len
+= snprintf(msg
+ len
, sizeof(msg
) - len
, "\n");
1475 xfrm_msg
= len
? msg
: NULL
;
1478 DIFF_FILE_VALID(one
) && DIFF_FILE_VALID(two
) &&
1479 (S_IFMT
& one
->mode
) != (S_IFMT
& two
->mode
)) {
1480 /* a filepair that changes between file and symlink
1481 * needs to be split into deletion and creation.
1483 struct diff_filespec
*null
= alloc_filespec(two
->path
);
1484 run_diff_cmd(NULL
, name
, other
, one
, null
, xfrm_msg
, o
, 0);
1486 null
= alloc_filespec(one
->path
);
1487 run_diff_cmd(NULL
, name
, other
, null
, two
, xfrm_msg
, o
, 0);
1491 run_diff_cmd(pgm
, name
, other
, one
, two
, xfrm_msg
, o
,
1498 static void run_diffstat(struct diff_filepair
*p
, struct diff_options
*o
,
1499 struct diffstat_t
*diffstat
)
1503 int complete_rewrite
= 0;
1505 if (DIFF_PAIR_UNMERGED(p
)) {
1507 builtin_diffstat(p
->one
->path
, NULL
, NULL
, NULL
, diffstat
, o
, 0);
1511 name
= p
->one
->path
;
1512 other
= (strcmp(name
, p
->two
->path
) ? p
->two
->path
: NULL
);
1514 diff_fill_sha1_info(p
->one
);
1515 diff_fill_sha1_info(p
->two
);
1517 if (p
->status
== DIFF_STATUS_MODIFIED
&& p
->score
)
1518 complete_rewrite
= 1;
1519 builtin_diffstat(name
, other
, p
->one
, p
->two
, diffstat
, o
, complete_rewrite
);
1522 static void run_checkdiff(struct diff_filepair
*p
, struct diff_options
*o
)
1527 if (DIFF_PAIR_UNMERGED(p
)) {
1532 name
= p
->one
->path
;
1533 other
= (strcmp(name
, p
->two
->path
) ? p
->two
->path
: NULL
);
1535 diff_fill_sha1_info(p
->one
);
1536 diff_fill_sha1_info(p
->two
);
1538 builtin_checkdiff(name
, other
, p
->one
, p
->two
);
1541 void diff_setup(struct diff_options
*options
)
1543 memset(options
, 0, sizeof(*options
));
1544 options
->line_termination
= '\n';
1545 options
->break_opt
= -1;
1546 options
->rename_limit
= -1;
1547 options
->context
= 3;
1548 options
->msg_sep
= "";
1550 options
->change
= diff_change
;
1551 options
->add_remove
= diff_addremove
;
1552 options
->color_diff
= diff_use_color_default
;
1553 options
->detect_rename
= diff_detect_rename_default
;
1556 int diff_setup_done(struct diff_options
*options
)
1560 if (options
->output_format
& DIFF_FORMAT_NAME
)
1562 if (options
->output_format
& DIFF_FORMAT_NAME_STATUS
)
1564 if (options
->output_format
& DIFF_FORMAT_CHECKDIFF
)
1566 if (options
->output_format
& DIFF_FORMAT_NO_OUTPUT
)
1569 die("--name-only, --name-status, --check and -s are mutually exclusive");
1571 if (options
->find_copies_harder
)
1572 options
->detect_rename
= DIFF_DETECT_COPY
;
1574 if (options
->output_format
& (DIFF_FORMAT_NAME
|
1575 DIFF_FORMAT_NAME_STATUS
|
1576 DIFF_FORMAT_CHECKDIFF
|
1577 DIFF_FORMAT_NO_OUTPUT
))
1578 options
->output_format
&= ~(DIFF_FORMAT_RAW
|
1579 DIFF_FORMAT_DIFFSTAT
|
1580 DIFF_FORMAT_SUMMARY
|
1584 * These cases always need recursive; we do not drop caller-supplied
1585 * recursive bits for other formats here.
1587 if (options
->output_format
& (DIFF_FORMAT_PATCH
|
1588 DIFF_FORMAT_DIFFSTAT
|
1589 DIFF_FORMAT_CHECKDIFF
))
1590 options
->recursive
= 1;
1592 * Also pickaxe would not work very well if you do not say recursive
1594 if (options
->pickaxe
)
1595 options
->recursive
= 1;
1597 if (options
->detect_rename
&& options
->rename_limit
< 0)
1598 options
->rename_limit
= diff_rename_limit_default
;
1599 if (options
->setup
& DIFF_SETUP_USE_CACHE
) {
1601 /* read-cache does not die even when it fails
1602 * so it is safe for us to do this here. Also
1603 * it does not smudge active_cache or active_nr
1604 * when it fails, so we do not have to worry about
1605 * cleaning it up ourselves either.
1609 if (options
->setup
& DIFF_SETUP_USE_SIZE_CACHE
)
1611 if (options
->abbrev
<= 0 || 40 < options
->abbrev
)
1612 options
->abbrev
= 40; /* full */
1617 static int opt_arg(const char *arg
, int arg_short
, const char *arg_long
, int *val
)
1627 if (c
== arg_short
) {
1631 if (val
&& isdigit(c
)) {
1633 int n
= strtoul(arg
, &end
, 10);
1644 eq
= strchr(arg
, '=');
1649 if (!len
|| strncmp(arg
, arg_long
, len
))
1654 if (!isdigit(*++eq
))
1656 n
= strtoul(eq
, &end
, 10);
1664 int diff_opt_parse(struct diff_options
*options
, const char **av
, int ac
)
1666 const char *arg
= av
[0];
1667 if (!strcmp(arg
, "-p") || !strcmp(arg
, "-u"))
1668 options
->output_format
|= DIFF_FORMAT_PATCH
;
1669 else if (opt_arg(arg
, 'U', "unified", &options
->context
))
1670 options
->output_format
|= DIFF_FORMAT_PATCH
;
1671 else if (!strcmp(arg
, "--raw"))
1672 options
->output_format
|= DIFF_FORMAT_RAW
;
1673 else if (!strcmp(arg
, "--patch-with-raw")) {
1674 options
->output_format
|= DIFF_FORMAT_PATCH
| DIFF_FORMAT_RAW
;
1676 else if (!strcmp(arg
, "--stat"))
1677 options
->output_format
|= DIFF_FORMAT_DIFFSTAT
;
1678 else if (!strcmp(arg
, "--check"))
1679 options
->output_format
|= DIFF_FORMAT_CHECKDIFF
;
1680 else if (!strcmp(arg
, "--summary"))
1681 options
->output_format
|= DIFF_FORMAT_SUMMARY
;
1682 else if (!strcmp(arg
, "--patch-with-stat")) {
1683 options
->output_format
|= DIFF_FORMAT_PATCH
| DIFF_FORMAT_DIFFSTAT
;
1685 else if (!strcmp(arg
, "-z"))
1686 options
->line_termination
= 0;
1687 else if (!strncmp(arg
, "-l", 2))
1688 options
->rename_limit
= strtoul(arg
+2, NULL
, 10);
1689 else if (!strcmp(arg
, "--full-index"))
1690 options
->full_index
= 1;
1691 else if (!strcmp(arg
, "--binary")) {
1692 options
->output_format
|= DIFF_FORMAT_PATCH
;
1693 options
->full_index
= options
->binary
= 1;
1695 else if (!strcmp(arg
, "-a") || !strcmp(arg
, "--text")) {
1698 else if (!strcmp(arg
, "--name-only"))
1699 options
->output_format
|= DIFF_FORMAT_NAME
;
1700 else if (!strcmp(arg
, "--name-status"))
1701 options
->output_format
|= DIFF_FORMAT_NAME_STATUS
;
1702 else if (!strcmp(arg
, "-R"))
1703 options
->reverse_diff
= 1;
1704 else if (!strncmp(arg
, "-S", 2))
1705 options
->pickaxe
= arg
+ 2;
1706 else if (!strcmp(arg
, "-s")) {
1707 options
->output_format
|= DIFF_FORMAT_NO_OUTPUT
;
1709 else if (!strncmp(arg
, "-O", 2))
1710 options
->orderfile
= arg
+ 2;
1711 else if (!strncmp(arg
, "--diff-filter=", 14))
1712 options
->filter
= arg
+ 14;
1713 else if (!strcmp(arg
, "--pickaxe-all"))
1714 options
->pickaxe_opts
= DIFF_PICKAXE_ALL
;
1715 else if (!strcmp(arg
, "--pickaxe-regex"))
1716 options
->pickaxe_opts
= DIFF_PICKAXE_REGEX
;
1717 else if (!strncmp(arg
, "-B", 2)) {
1718 if ((options
->break_opt
=
1719 diff_scoreopt_parse(arg
)) == -1)
1722 else if (!strncmp(arg
, "-M", 2)) {
1723 if ((options
->rename_score
=
1724 diff_scoreopt_parse(arg
)) == -1)
1726 options
->detect_rename
= DIFF_DETECT_RENAME
;
1728 else if (!strncmp(arg
, "-C", 2)) {
1729 if ((options
->rename_score
=
1730 diff_scoreopt_parse(arg
)) == -1)
1732 options
->detect_rename
= DIFF_DETECT_COPY
;
1734 else if (!strcmp(arg
, "--find-copies-harder"))
1735 options
->find_copies_harder
= 1;
1736 else if (!strcmp(arg
, "--abbrev"))
1737 options
->abbrev
= DEFAULT_ABBREV
;
1738 else if (!strncmp(arg
, "--abbrev=", 9)) {
1739 options
->abbrev
= strtoul(arg
+ 9, NULL
, 10);
1740 if (options
->abbrev
< MINIMUM_ABBREV
)
1741 options
->abbrev
= MINIMUM_ABBREV
;
1742 else if (40 < options
->abbrev
)
1743 options
->abbrev
= 40;
1745 else if (!strcmp(arg
, "--color"))
1746 options
->color_diff
= 1;
1747 else if (!strcmp(arg
, "--no-color"))
1748 options
->color_diff
= 0;
1749 else if (!strcmp(arg
, "-w") || !strcmp(arg
, "--ignore-all-space"))
1750 options
->xdl_opts
|= XDF_IGNORE_WHITESPACE
;
1751 else if (!strcmp(arg
, "-b") || !strcmp(arg
, "--ignore-space-change"))
1752 options
->xdl_opts
|= XDF_IGNORE_WHITESPACE_CHANGE
;
1753 else if (!strcmp(arg
, "--color-words"))
1754 options
->color_diff
= options
->color_diff_words
= 1;
1755 else if (!strcmp(arg
, "--no-renames"))
1756 options
->detect_rename
= 0;
1762 static int parse_num(const char **cp_p
)
1764 unsigned long num
, scale
;
1766 const char *cp
= *cp_p
;
1773 if ( !dot
&& ch
== '.' ) {
1776 } else if ( ch
== '%' ) {
1777 scale
= dot
? scale
*100 : 100;
1778 cp
++; /* % is always at the end */
1780 } else if ( ch
>= '0' && ch
<= '9' ) {
1781 if ( scale
< 100000 ) {
1783 num
= (num
*10) + (ch
-'0');
1792 /* user says num divided by scale and we say internally that
1793 * is MAX_SCORE * num / scale.
1795 return (num
>= scale
) ? MAX_SCORE
: (MAX_SCORE
* num
/ scale
);
1798 int diff_scoreopt_parse(const char *opt
)
1800 int opt1
, opt2
, cmd
;
1805 if (cmd
!= 'M' && cmd
!= 'C' && cmd
!= 'B')
1806 return -1; /* that is not a -M, -C nor -B option */
1808 opt1
= parse_num(&opt
);
1814 else if (*opt
!= '/')
1815 return -1; /* we expect -B80/99 or -B80 */
1818 opt2
= parse_num(&opt
);
1823 return opt1
| (opt2
<< 16);
1826 struct diff_queue_struct diff_queued_diff
;
1828 void diff_q(struct diff_queue_struct
*queue
, struct diff_filepair
*dp
)
1830 if (queue
->alloc
<= queue
->nr
) {
1831 queue
->alloc
= alloc_nr(queue
->alloc
);
1832 queue
->queue
= xrealloc(queue
->queue
,
1833 sizeof(dp
) * queue
->alloc
);
1835 queue
->queue
[queue
->nr
++] = dp
;
1838 struct diff_filepair
*diff_queue(struct diff_queue_struct
*queue
,
1839 struct diff_filespec
*one
,
1840 struct diff_filespec
*two
)
1842 struct diff_filepair
*dp
= xcalloc(1, sizeof(*dp
));
1850 void diff_free_filepair(struct diff_filepair
*p
)
1852 diff_free_filespec_data(p
->one
);
1853 diff_free_filespec_data(p
->two
);
1859 /* This is different from find_unique_abbrev() in that
1860 * it stuffs the result with dots for alignment.
1862 const char *diff_unique_abbrev(const unsigned char *sha1
, int len
)
1867 return sha1_to_hex(sha1
);
1869 abbrev
= find_unique_abbrev(sha1
, len
);
1871 return sha1_to_hex(sha1
);
1872 abblen
= strlen(abbrev
);
1874 static char hex
[41];
1875 if (len
< abblen
&& abblen
<= len
+ 2)
1876 sprintf(hex
, "%s%.*s", abbrev
, len
+3-abblen
, "..");
1878 sprintf(hex
, "%s...", abbrev
);
1881 return sha1_to_hex(sha1
);
1884 static void diff_flush_raw(struct diff_filepair
*p
,
1885 struct diff_options
*options
)
1889 int abbrev
= options
->abbrev
;
1890 const char *path_one
, *path_two
;
1891 int inter_name_termination
= '\t';
1892 int line_termination
= options
->line_termination
;
1894 if (!line_termination
)
1895 inter_name_termination
= 0;
1897 path_one
= p
->one
->path
;
1898 path_two
= p
->two
->path
;
1899 if (line_termination
) {
1900 path_one
= quote_one(path_one
);
1901 path_two
= quote_one(path_two
);
1905 sprintf(status
, "%c%03d", p
->status
,
1906 (int)(0.5 + p
->score
* 100.0/MAX_SCORE
));
1908 status
[0] = p
->status
;
1911 switch (p
->status
) {
1912 case DIFF_STATUS_COPIED
:
1913 case DIFF_STATUS_RENAMED
:
1916 case DIFF_STATUS_ADDED
:
1917 case DIFF_STATUS_DELETED
:
1924 if (!(options
->output_format
& DIFF_FORMAT_NAME_STATUS
)) {
1925 printf(":%06o %06o %s ",
1926 p
->one
->mode
, p
->two
->mode
,
1927 diff_unique_abbrev(p
->one
->sha1
, abbrev
));
1929 diff_unique_abbrev(p
->two
->sha1
, abbrev
));
1931 printf("%s%c%s", status
, inter_name_termination
, path_one
);
1933 printf("%c%s", inter_name_termination
, path_two
);
1934 putchar(line_termination
);
1935 if (path_one
!= p
->one
->path
)
1936 free((void*)path_one
);
1937 if (path_two
!= p
->two
->path
)
1938 free((void*)path_two
);
1941 static void diff_flush_name(struct diff_filepair
*p
, int line_termination
)
1943 char *path
= p
->two
->path
;
1945 if (line_termination
)
1946 path
= quote_one(p
->two
->path
);
1947 printf("%s%c", path
, line_termination
);
1948 if (p
->two
->path
!= path
)
1952 int diff_unmodified_pair(struct diff_filepair
*p
)
1954 /* This function is written stricter than necessary to support
1955 * the currently implemented transformers, but the idea is to
1956 * let transformers to produce diff_filepairs any way they want,
1957 * and filter and clean them up here before producing the output.
1959 struct diff_filespec
*one
, *two
;
1961 if (DIFF_PAIR_UNMERGED(p
))
1962 return 0; /* unmerged is interesting */
1967 /* deletion, addition, mode or type change
1968 * and rename are all interesting.
1970 if (DIFF_FILE_VALID(one
) != DIFF_FILE_VALID(two
) ||
1971 DIFF_PAIR_MODE_CHANGED(p
) ||
1972 strcmp(one
->path
, two
->path
))
1975 /* both are valid and point at the same path. that is, we are
1976 * dealing with a change.
1978 if (one
->sha1_valid
&& two
->sha1_valid
&&
1979 !hashcmp(one
->sha1
, two
->sha1
))
1980 return 1; /* no change */
1981 if (!one
->sha1_valid
&& !two
->sha1_valid
)
1982 return 1; /* both look at the same file on the filesystem. */
1986 static void diff_flush_patch(struct diff_filepair
*p
, struct diff_options
*o
)
1988 if (diff_unmodified_pair(p
))
1991 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
1992 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
1993 return; /* no tree diffs in patch format */
1998 static void diff_flush_stat(struct diff_filepair
*p
, struct diff_options
*o
,
1999 struct diffstat_t
*diffstat
)
2001 if (diff_unmodified_pair(p
))
2004 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
2005 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
2006 return; /* no tree diffs in patch format */
2008 run_diffstat(p
, o
, diffstat
);
2011 static void diff_flush_checkdiff(struct diff_filepair
*p
,
2012 struct diff_options
*o
)
2014 if (diff_unmodified_pair(p
))
2017 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
2018 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
2019 return; /* no tree diffs in patch format */
2021 run_checkdiff(p
, o
);
2024 int diff_queue_is_empty(void)
2026 struct diff_queue_struct
*q
= &diff_queued_diff
;
2028 for (i
= 0; i
< q
->nr
; i
++)
2029 if (!diff_unmodified_pair(q
->queue
[i
]))
2035 void diff_debug_filespec(struct diff_filespec
*s
, int x
, const char *one
)
2037 fprintf(stderr
, "queue[%d] %s (%s) %s %06o %s\n",
2040 DIFF_FILE_VALID(s
) ? "valid" : "invalid",
2042 s
->sha1_valid
? sha1_to_hex(s
->sha1
) : "");
2043 fprintf(stderr
, "queue[%d] %s size %lu flags %d\n",
2045 s
->size
, s
->xfrm_flags
);
2048 void diff_debug_filepair(const struct diff_filepair
*p
, int i
)
2050 diff_debug_filespec(p
->one
, i
, "one");
2051 diff_debug_filespec(p
->two
, i
, "two");
2052 fprintf(stderr
, "score %d, status %c stays %d broken %d\n",
2053 p
->score
, p
->status
? p
->status
: '?',
2054 p
->source_stays
, p
->broken_pair
);
2057 void diff_debug_queue(const char *msg
, struct diff_queue_struct
*q
)
2061 fprintf(stderr
, "%s\n", msg
);
2062 fprintf(stderr
, "q->nr = %d\n", q
->nr
);
2063 for (i
= 0; i
< q
->nr
; i
++) {
2064 struct diff_filepair
*p
= q
->queue
[i
];
2065 diff_debug_filepair(p
, i
);
2070 static void diff_resolve_rename_copy(void)
2073 struct diff_filepair
*p
, *pp
;
2074 struct diff_queue_struct
*q
= &diff_queued_diff
;
2076 diff_debug_queue("resolve-rename-copy", q
);
2078 for (i
= 0; i
< q
->nr
; i
++) {
2080 p
->status
= 0; /* undecided */
2081 if (DIFF_PAIR_UNMERGED(p
))
2082 p
->status
= DIFF_STATUS_UNMERGED
;
2083 else if (!DIFF_FILE_VALID(p
->one
))
2084 p
->status
= DIFF_STATUS_ADDED
;
2085 else if (!DIFF_FILE_VALID(p
->two
))
2086 p
->status
= DIFF_STATUS_DELETED
;
2087 else if (DIFF_PAIR_TYPE_CHANGED(p
))
2088 p
->status
= DIFF_STATUS_TYPE_CHANGED
;
2090 /* from this point on, we are dealing with a pair
2091 * whose both sides are valid and of the same type, i.e.
2092 * either in-place edit or rename/copy edit.
2094 else if (DIFF_PAIR_RENAME(p
)) {
2095 if (p
->source_stays
) {
2096 p
->status
= DIFF_STATUS_COPIED
;
2099 /* See if there is some other filepair that
2100 * copies from the same source as us. If so
2101 * we are a copy. Otherwise we are either a
2102 * copy if the path stays, or a rename if it
2103 * does not, but we already handled "stays" case.
2105 for (j
= i
+ 1; j
< q
->nr
; j
++) {
2107 if (strcmp(pp
->one
->path
, p
->one
->path
))
2108 continue; /* not us */
2109 if (!DIFF_PAIR_RENAME(pp
))
2110 continue; /* not a rename/copy */
2111 /* pp is a rename/copy from the same source */
2112 p
->status
= DIFF_STATUS_COPIED
;
2116 p
->status
= DIFF_STATUS_RENAMED
;
2118 else if (hashcmp(p
->one
->sha1
, p
->two
->sha1
) ||
2119 p
->one
->mode
!= p
->two
->mode
)
2120 p
->status
= DIFF_STATUS_MODIFIED
;
2122 /* This is a "no-change" entry and should not
2123 * happen anymore, but prepare for broken callers.
2125 error("feeding unmodified %s to diffcore",
2127 p
->status
= DIFF_STATUS_UNKNOWN
;
2130 diff_debug_queue("resolve-rename-copy done", q
);
2133 static int check_pair_status(struct diff_filepair
*p
)
2135 switch (p
->status
) {
2136 case DIFF_STATUS_UNKNOWN
:
2139 die("internal error in diff-resolve-rename-copy");
2145 static void flush_one_pair(struct diff_filepair
*p
, struct diff_options
*opt
)
2147 int fmt
= opt
->output_format
;
2149 if (fmt
& DIFF_FORMAT_CHECKDIFF
)
2150 diff_flush_checkdiff(p
, opt
);
2151 else if (fmt
& (DIFF_FORMAT_RAW
| DIFF_FORMAT_NAME_STATUS
))
2152 diff_flush_raw(p
, opt
);
2153 else if (fmt
& DIFF_FORMAT_NAME
)
2154 diff_flush_name(p
, opt
->line_termination
);
2157 static void show_file_mode_name(const char *newdelete
, struct diff_filespec
*fs
)
2160 printf(" %s mode %06o %s\n", newdelete
, fs
->mode
, fs
->path
);
2162 printf(" %s %s\n", newdelete
, fs
->path
);
2166 static void show_mode_change(struct diff_filepair
*p
, int show_name
)
2168 if (p
->one
->mode
&& p
->two
->mode
&& p
->one
->mode
!= p
->two
->mode
) {
2170 printf(" mode change %06o => %06o %s\n",
2171 p
->one
->mode
, p
->two
->mode
, p
->two
->path
);
2173 printf(" mode change %06o => %06o\n",
2174 p
->one
->mode
, p
->two
->mode
);
2178 static void show_rename_copy(const char *renamecopy
, struct diff_filepair
*p
)
2180 const char *old
, *new;
2182 /* Find common prefix */
2186 const char *slash_old
, *slash_new
;
2187 slash_old
= strchr(old
, '/');
2188 slash_new
= strchr(new, '/');
2191 slash_old
- old
!= slash_new
- new ||
2192 memcmp(old
, new, slash_new
- new))
2194 old
= slash_old
+ 1;
2195 new = slash_new
+ 1;
2197 /* p->one->path thru old is the common prefix, and old and new
2198 * through the end of names are renames
2200 if (old
!= p
->one
->path
)
2201 printf(" %s %.*s{%s => %s} (%d%%)\n", renamecopy
,
2202 (int)(old
- p
->one
->path
), p
->one
->path
,
2203 old
, new, (int)(0.5 + p
->score
* 100.0/MAX_SCORE
));
2205 printf(" %s %s => %s (%d%%)\n", renamecopy
,
2206 p
->one
->path
, p
->two
->path
,
2207 (int)(0.5 + p
->score
* 100.0/MAX_SCORE
));
2208 show_mode_change(p
, 0);
2211 static void diff_summary(struct diff_filepair
*p
)
2214 case DIFF_STATUS_DELETED
:
2215 show_file_mode_name("delete", p
->one
);
2217 case DIFF_STATUS_ADDED
:
2218 show_file_mode_name("create", p
->two
);
2220 case DIFF_STATUS_COPIED
:
2221 show_rename_copy("copy", p
);
2223 case DIFF_STATUS_RENAMED
:
2224 show_rename_copy("rename", p
);
2228 printf(" rewrite %s (%d%%)\n", p
->two
->path
,
2229 (int)(0.5 + p
->score
* 100.0/MAX_SCORE
));
2230 show_mode_change(p
, 0);
2231 } else show_mode_change(p
, 1);
2237 struct xdiff_emit_state xm
;
2242 static int remove_space(char *line
, int len
)
2248 for (i
= 0; i
< len
; i
++)
2249 if (!isspace((c
= line
[i
])))
2255 static void patch_id_consume(void *priv
, char *line
, unsigned long len
)
2257 struct patch_id_t
*data
= priv
;
2260 /* Ignore line numbers when computing the SHA1 of the patch */
2261 if (!strncmp(line
, "@@ -", 4))
2264 new_len
= remove_space(line
, len
);
2266 SHA1_Update(data
->ctx
, line
, new_len
);
2267 data
->patchlen
+= new_len
;
2270 /* returns 0 upon success, and writes result into sha1 */
2271 static int diff_get_patch_id(struct diff_options
*options
, unsigned char *sha1
)
2273 struct diff_queue_struct
*q
= &diff_queued_diff
;
2276 struct patch_id_t data
;
2277 char buffer
[PATH_MAX
* 4 + 20];
2280 memset(&data
, 0, sizeof(struct patch_id_t
));
2282 data
.xm
.consume
= patch_id_consume
;
2284 for (i
= 0; i
< q
->nr
; i
++) {
2289 struct diff_filepair
*p
= q
->queue
[i
];
2293 return error("internal diff status error");
2294 if (p
->status
== DIFF_STATUS_UNKNOWN
)
2296 if (diff_unmodified_pair(p
))
2298 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
2299 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
2301 if (DIFF_PAIR_UNMERGED(p
))
2304 diff_fill_sha1_info(p
->one
);
2305 diff_fill_sha1_info(p
->two
);
2306 if (fill_mmfile(&mf1
, p
->one
) < 0 ||
2307 fill_mmfile(&mf2
, p
->two
) < 0)
2308 return error("unable to read files to diff");
2310 /* Maybe hash p->two? into the patch id? */
2311 if (mmfile_is_binary(&mf2
))
2314 len1
= remove_space(p
->one
->path
, strlen(p
->one
->path
));
2315 len2
= remove_space(p
->two
->path
, strlen(p
->two
->path
));
2316 if (p
->one
->mode
== 0)
2317 len1
= snprintf(buffer
, sizeof(buffer
),
2318 "diff--gita/%.*sb/%.*s"
2325 len2
, p
->two
->path
);
2326 else if (p
->two
->mode
== 0)
2327 len1
= snprintf(buffer
, sizeof(buffer
),
2328 "diff--gita/%.*sb/%.*s"
2329 "deletedfilemode%06o"
2335 len1
, p
->one
->path
);
2337 len1
= snprintf(buffer
, sizeof(buffer
),
2338 "diff--gita/%.*sb/%.*s"
2344 len2
, p
->two
->path
);
2345 SHA1_Update(&ctx
, buffer
, len1
);
2347 xpp
.flags
= XDF_NEED_MINIMAL
;
2349 xecfg
.flags
= XDL_EMIT_FUNCNAMES
;
2350 ecb
.outf
= xdiff_outf
;
2352 xdl_diff(&mf1
, &mf2
, &xpp
, &xecfg
, &ecb
);
2355 SHA1_Final(sha1
, &ctx
);
2359 int diff_flush_patch_id(struct diff_options
*options
, unsigned char *sha1
)
2361 struct diff_queue_struct
*q
= &diff_queued_diff
;
2363 int result
= diff_get_patch_id(options
, sha1
);
2365 for (i
= 0; i
< q
->nr
; i
++)
2366 diff_free_filepair(q
->queue
[i
]);
2370 q
->nr
= q
->alloc
= 0;
2375 static int is_summary_empty(const struct diff_queue_struct
*q
)
2379 for (i
= 0; i
< q
->nr
; i
++) {
2380 const struct diff_filepair
*p
= q
->queue
[i
];
2382 switch (p
->status
) {
2383 case DIFF_STATUS_DELETED
:
2384 case DIFF_STATUS_ADDED
:
2385 case DIFF_STATUS_COPIED
:
2386 case DIFF_STATUS_RENAMED
:
2391 if (p
->one
->mode
&& p
->two
->mode
&&
2392 p
->one
->mode
!= p
->two
->mode
)
2400 void diff_flush(struct diff_options
*options
)
2402 struct diff_queue_struct
*q
= &diff_queued_diff
;
2403 int i
, output_format
= options
->output_format
;
2407 * Order: raw, stat, summary, patch
2408 * or: name/name-status/checkdiff (other bits clear)
2413 if (output_format
& (DIFF_FORMAT_RAW
|
2415 DIFF_FORMAT_NAME_STATUS
|
2416 DIFF_FORMAT_CHECKDIFF
)) {
2417 for (i
= 0; i
< q
->nr
; i
++) {
2418 struct diff_filepair
*p
= q
->queue
[i
];
2419 if (check_pair_status(p
))
2420 flush_one_pair(p
, options
);
2425 if (output_format
& DIFF_FORMAT_DIFFSTAT
) {
2426 struct diffstat_t diffstat
;
2428 memset(&diffstat
, 0, sizeof(struct diffstat_t
));
2429 diffstat
.xm
.consume
= diffstat_consume
;
2430 for (i
= 0; i
< q
->nr
; i
++) {
2431 struct diff_filepair
*p
= q
->queue
[i
];
2432 if (check_pair_status(p
))
2433 diff_flush_stat(p
, options
, &diffstat
);
2435 show_stats(&diffstat
);
2439 if (output_format
& DIFF_FORMAT_SUMMARY
&& !is_summary_empty(q
)) {
2440 for (i
= 0; i
< q
->nr
; i
++)
2441 diff_summary(q
->queue
[i
]);
2445 if (output_format
& DIFF_FORMAT_PATCH
) {
2447 if (options
->stat_sep
) {
2448 /* attach patch instead of inline */
2449 fputs(options
->stat_sep
, stdout
);
2451 putchar(options
->line_termination
);
2455 for (i
= 0; i
< q
->nr
; i
++) {
2456 struct diff_filepair
*p
= q
->queue
[i
];
2457 if (check_pair_status(p
))
2458 diff_flush_patch(p
, options
);
2462 if (output_format
& DIFF_FORMAT_CALLBACK
)
2463 options
->format_callback(q
, options
, options
->format_callback_data
);
2465 for (i
= 0; i
< q
->nr
; i
++)
2466 diff_free_filepair(q
->queue
[i
]);
2470 q
->nr
= q
->alloc
= 0;
2473 static void diffcore_apply_filter(const char *filter
)
2476 struct diff_queue_struct
*q
= &diff_queued_diff
;
2477 struct diff_queue_struct outq
;
2479 outq
.nr
= outq
.alloc
= 0;
2484 if (strchr(filter
, DIFF_STATUS_FILTER_AON
)) {
2486 for (i
= found
= 0; !found
&& i
< q
->nr
; i
++) {
2487 struct diff_filepair
*p
= q
->queue
[i
];
2488 if (((p
->status
== DIFF_STATUS_MODIFIED
) &&
2490 strchr(filter
, DIFF_STATUS_FILTER_BROKEN
)) ||
2492 strchr(filter
, DIFF_STATUS_MODIFIED
)))) ||
2493 ((p
->status
!= DIFF_STATUS_MODIFIED
) &&
2494 strchr(filter
, p
->status
)))
2500 /* otherwise we will clear the whole queue
2501 * by copying the empty outq at the end of this
2502 * function, but first clear the current entries
2505 for (i
= 0; i
< q
->nr
; i
++)
2506 diff_free_filepair(q
->queue
[i
]);
2509 /* Only the matching ones */
2510 for (i
= 0; i
< q
->nr
; i
++) {
2511 struct diff_filepair
*p
= q
->queue
[i
];
2513 if (((p
->status
== DIFF_STATUS_MODIFIED
) &&
2515 strchr(filter
, DIFF_STATUS_FILTER_BROKEN
)) ||
2517 strchr(filter
, DIFF_STATUS_MODIFIED
)))) ||
2518 ((p
->status
!= DIFF_STATUS_MODIFIED
) &&
2519 strchr(filter
, p
->status
)))
2522 diff_free_filepair(p
);
2529 void diffcore_std(struct diff_options
*options
)
2531 if (options
->break_opt
!= -1)
2532 diffcore_break(options
->break_opt
);
2533 if (options
->detect_rename
)
2534 diffcore_rename(options
);
2535 if (options
->break_opt
!= -1)
2536 diffcore_merge_broken();
2537 if (options
->pickaxe
)
2538 diffcore_pickaxe(options
->pickaxe
, options
->pickaxe_opts
);
2539 if (options
->orderfile
)
2540 diffcore_order(options
->orderfile
);
2541 diff_resolve_rename_copy();
2542 diffcore_apply_filter(options
->filter
);
2546 void diffcore_std_no_resolve(struct diff_options
*options
)
2548 if (options
->pickaxe
)
2549 diffcore_pickaxe(options
->pickaxe
, options
->pickaxe_opts
);
2550 if (options
->orderfile
)
2551 diffcore_order(options
->orderfile
);
2552 diffcore_apply_filter(options
->filter
);
2555 void diff_addremove(struct diff_options
*options
,
2556 int addremove
, unsigned mode
,
2557 const unsigned char *sha1
,
2558 const char *base
, const char *path
)
2560 char concatpath
[PATH_MAX
];
2561 struct diff_filespec
*one
, *two
;
2563 /* This may look odd, but it is a preparation for
2564 * feeding "there are unchanged files which should
2565 * not produce diffs, but when you are doing copy
2566 * detection you would need them, so here they are"
2567 * entries to the diff-core. They will be prefixed
2568 * with something like '=' or '*' (I haven't decided
2569 * which but should not make any difference).
2570 * Feeding the same new and old to diff_change()
2571 * also has the same effect.
2572 * Before the final output happens, they are pruned after
2573 * merged into rename/copy pairs as appropriate.
2575 if (options
->reverse_diff
)
2576 addremove
= (addremove
== '+' ? '-' :
2577 addremove
== '-' ? '+' : addremove
);
2579 if (!path
) path
= "";
2580 sprintf(concatpath
, "%s%s", base
, path
);
2581 one
= alloc_filespec(concatpath
);
2582 two
= alloc_filespec(concatpath
);
2584 if (addremove
!= '+')
2585 fill_filespec(one
, sha1
, mode
);
2586 if (addremove
!= '-')
2587 fill_filespec(two
, sha1
, mode
);
2589 diff_queue(&diff_queued_diff
, one
, two
);
2592 void diff_change(struct diff_options
*options
,
2593 unsigned old_mode
, unsigned new_mode
,
2594 const unsigned char *old_sha1
,
2595 const unsigned char *new_sha1
,
2596 const char *base
, const char *path
)
2598 char concatpath
[PATH_MAX
];
2599 struct diff_filespec
*one
, *two
;
2601 if (options
->reverse_diff
) {
2603 const unsigned char *tmp_c
;
2604 tmp
= old_mode
; old_mode
= new_mode
; new_mode
= tmp
;
2605 tmp_c
= old_sha1
; old_sha1
= new_sha1
; new_sha1
= tmp_c
;
2607 if (!path
) path
= "";
2608 sprintf(concatpath
, "%s%s", base
, path
);
2609 one
= alloc_filespec(concatpath
);
2610 two
= alloc_filespec(concatpath
);
2611 fill_filespec(one
, old_sha1
, old_mode
);
2612 fill_filespec(two
, new_sha1
, new_mode
);
2614 diff_queue(&diff_queued_diff
, one
, two
);
2617 void diff_unmerge(struct diff_options
*options
,
2620 struct diff_filespec
*one
, *two
;
2621 one
= alloc_filespec(path
);
2622 two
= alloc_filespec(path
);
2623 diff_queue(&diff_queued_diff
, one
, two
);