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
;
1465 if ((!fill_mmfile(&mf
, one
) && mmfile_is_binary(&mf
)) ||
1466 (!fill_mmfile(&mf
, two
) && mmfile_is_binary(&mf
)))
1469 len
+= snprintf(msg
+ len
, sizeof(msg
) - len
,
1471 abbrev
, sha1_to_hex(one
->sha1
),
1472 abbrev
, sha1_to_hex(two
->sha1
));
1473 if (one
->mode
== two
->mode
)
1474 len
+= snprintf(msg
+ len
, sizeof(msg
) - len
,
1475 " %06o", one
->mode
);
1476 len
+= snprintf(msg
+ len
, sizeof(msg
) - len
, "\n");
1481 xfrm_msg
= len
? msg
: NULL
;
1484 DIFF_FILE_VALID(one
) && DIFF_FILE_VALID(two
) &&
1485 (S_IFMT
& one
->mode
) != (S_IFMT
& two
->mode
)) {
1486 /* a filepair that changes between file and symlink
1487 * needs to be split into deletion and creation.
1489 struct diff_filespec
*null
= alloc_filespec(two
->path
);
1490 run_diff_cmd(NULL
, name
, other
, one
, null
, xfrm_msg
, o
, 0);
1492 null
= alloc_filespec(one
->path
);
1493 run_diff_cmd(NULL
, name
, other
, null
, two
, xfrm_msg
, o
, 0);
1497 run_diff_cmd(pgm
, name
, other
, one
, two
, xfrm_msg
, o
,
1504 static void run_diffstat(struct diff_filepair
*p
, struct diff_options
*o
,
1505 struct diffstat_t
*diffstat
)
1509 int complete_rewrite
= 0;
1511 if (DIFF_PAIR_UNMERGED(p
)) {
1513 builtin_diffstat(p
->one
->path
, NULL
, NULL
, NULL
, diffstat
, o
, 0);
1517 name
= p
->one
->path
;
1518 other
= (strcmp(name
, p
->two
->path
) ? p
->two
->path
: NULL
);
1520 diff_fill_sha1_info(p
->one
);
1521 diff_fill_sha1_info(p
->two
);
1523 if (p
->status
== DIFF_STATUS_MODIFIED
&& p
->score
)
1524 complete_rewrite
= 1;
1525 builtin_diffstat(name
, other
, p
->one
, p
->two
, diffstat
, o
, complete_rewrite
);
1528 static void run_checkdiff(struct diff_filepair
*p
, struct diff_options
*o
)
1533 if (DIFF_PAIR_UNMERGED(p
)) {
1538 name
= p
->one
->path
;
1539 other
= (strcmp(name
, p
->two
->path
) ? p
->two
->path
: NULL
);
1541 diff_fill_sha1_info(p
->one
);
1542 diff_fill_sha1_info(p
->two
);
1544 builtin_checkdiff(name
, other
, p
->one
, p
->two
);
1547 void diff_setup(struct diff_options
*options
)
1549 memset(options
, 0, sizeof(*options
));
1550 options
->line_termination
= '\n';
1551 options
->break_opt
= -1;
1552 options
->rename_limit
= -1;
1553 options
->context
= 3;
1554 options
->msg_sep
= "";
1556 options
->change
= diff_change
;
1557 options
->add_remove
= diff_addremove
;
1558 options
->color_diff
= diff_use_color_default
;
1559 options
->detect_rename
= diff_detect_rename_default
;
1562 int diff_setup_done(struct diff_options
*options
)
1566 if (options
->output_format
& DIFF_FORMAT_NAME
)
1568 if (options
->output_format
& DIFF_FORMAT_NAME_STATUS
)
1570 if (options
->output_format
& DIFF_FORMAT_CHECKDIFF
)
1572 if (options
->output_format
& DIFF_FORMAT_NO_OUTPUT
)
1575 die("--name-only, --name-status, --check and -s are mutually exclusive");
1577 if (options
->find_copies_harder
)
1578 options
->detect_rename
= DIFF_DETECT_COPY
;
1580 if (options
->output_format
& (DIFF_FORMAT_NAME
|
1581 DIFF_FORMAT_NAME_STATUS
|
1582 DIFF_FORMAT_CHECKDIFF
|
1583 DIFF_FORMAT_NO_OUTPUT
))
1584 options
->output_format
&= ~(DIFF_FORMAT_RAW
|
1585 DIFF_FORMAT_DIFFSTAT
|
1586 DIFF_FORMAT_SUMMARY
|
1590 * These cases always need recursive; we do not drop caller-supplied
1591 * recursive bits for other formats here.
1593 if (options
->output_format
& (DIFF_FORMAT_PATCH
|
1594 DIFF_FORMAT_DIFFSTAT
|
1595 DIFF_FORMAT_CHECKDIFF
))
1596 options
->recursive
= 1;
1598 * Also pickaxe would not work very well if you do not say recursive
1600 if (options
->pickaxe
)
1601 options
->recursive
= 1;
1603 if (options
->detect_rename
&& options
->rename_limit
< 0)
1604 options
->rename_limit
= diff_rename_limit_default
;
1605 if (options
->setup
& DIFF_SETUP_USE_CACHE
) {
1607 /* read-cache does not die even when it fails
1608 * so it is safe for us to do this here. Also
1609 * it does not smudge active_cache or active_nr
1610 * when it fails, so we do not have to worry about
1611 * cleaning it up ourselves either.
1615 if (options
->setup
& DIFF_SETUP_USE_SIZE_CACHE
)
1617 if (options
->abbrev
<= 0 || 40 < options
->abbrev
)
1618 options
->abbrev
= 40; /* full */
1623 static int opt_arg(const char *arg
, int arg_short
, const char *arg_long
, int *val
)
1633 if (c
== arg_short
) {
1637 if (val
&& isdigit(c
)) {
1639 int n
= strtoul(arg
, &end
, 10);
1650 eq
= strchr(arg
, '=');
1655 if (!len
|| strncmp(arg
, arg_long
, len
))
1660 if (!isdigit(*++eq
))
1662 n
= strtoul(eq
, &end
, 10);
1670 int diff_opt_parse(struct diff_options
*options
, const char **av
, int ac
)
1672 const char *arg
= av
[0];
1673 if (!strcmp(arg
, "-p") || !strcmp(arg
, "-u"))
1674 options
->output_format
|= DIFF_FORMAT_PATCH
;
1675 else if (opt_arg(arg
, 'U', "unified", &options
->context
))
1676 options
->output_format
|= DIFF_FORMAT_PATCH
;
1677 else if (!strcmp(arg
, "--raw"))
1678 options
->output_format
|= DIFF_FORMAT_RAW
;
1679 else if (!strcmp(arg
, "--patch-with-raw")) {
1680 options
->output_format
|= DIFF_FORMAT_PATCH
| DIFF_FORMAT_RAW
;
1682 else if (!strcmp(arg
, "--stat"))
1683 options
->output_format
|= DIFF_FORMAT_DIFFSTAT
;
1684 else if (!strcmp(arg
, "--check"))
1685 options
->output_format
|= DIFF_FORMAT_CHECKDIFF
;
1686 else if (!strcmp(arg
, "--summary"))
1687 options
->output_format
|= DIFF_FORMAT_SUMMARY
;
1688 else if (!strcmp(arg
, "--patch-with-stat")) {
1689 options
->output_format
|= DIFF_FORMAT_PATCH
| DIFF_FORMAT_DIFFSTAT
;
1691 else if (!strcmp(arg
, "-z"))
1692 options
->line_termination
= 0;
1693 else if (!strncmp(arg
, "-l", 2))
1694 options
->rename_limit
= strtoul(arg
+2, NULL
, 10);
1695 else if (!strcmp(arg
, "--full-index"))
1696 options
->full_index
= 1;
1697 else if (!strcmp(arg
, "--binary")) {
1698 options
->output_format
|= DIFF_FORMAT_PATCH
;
1699 options
->binary
= 1;
1701 else if (!strcmp(arg
, "-a") || !strcmp(arg
, "--text")) {
1704 else if (!strcmp(arg
, "--name-only"))
1705 options
->output_format
|= DIFF_FORMAT_NAME
;
1706 else if (!strcmp(arg
, "--name-status"))
1707 options
->output_format
|= DIFF_FORMAT_NAME_STATUS
;
1708 else if (!strcmp(arg
, "-R"))
1709 options
->reverse_diff
= 1;
1710 else if (!strncmp(arg
, "-S", 2))
1711 options
->pickaxe
= arg
+ 2;
1712 else if (!strcmp(arg
, "-s")) {
1713 options
->output_format
|= DIFF_FORMAT_NO_OUTPUT
;
1715 else if (!strncmp(arg
, "-O", 2))
1716 options
->orderfile
= arg
+ 2;
1717 else if (!strncmp(arg
, "--diff-filter=", 14))
1718 options
->filter
= arg
+ 14;
1719 else if (!strcmp(arg
, "--pickaxe-all"))
1720 options
->pickaxe_opts
= DIFF_PICKAXE_ALL
;
1721 else if (!strcmp(arg
, "--pickaxe-regex"))
1722 options
->pickaxe_opts
= DIFF_PICKAXE_REGEX
;
1723 else if (!strncmp(arg
, "-B", 2)) {
1724 if ((options
->break_opt
=
1725 diff_scoreopt_parse(arg
)) == -1)
1728 else if (!strncmp(arg
, "-M", 2)) {
1729 if ((options
->rename_score
=
1730 diff_scoreopt_parse(arg
)) == -1)
1732 options
->detect_rename
= DIFF_DETECT_RENAME
;
1734 else if (!strncmp(arg
, "-C", 2)) {
1735 if ((options
->rename_score
=
1736 diff_scoreopt_parse(arg
)) == -1)
1738 options
->detect_rename
= DIFF_DETECT_COPY
;
1740 else if (!strcmp(arg
, "--find-copies-harder"))
1741 options
->find_copies_harder
= 1;
1742 else if (!strcmp(arg
, "--abbrev"))
1743 options
->abbrev
= DEFAULT_ABBREV
;
1744 else if (!strncmp(arg
, "--abbrev=", 9)) {
1745 options
->abbrev
= strtoul(arg
+ 9, NULL
, 10);
1746 if (options
->abbrev
< MINIMUM_ABBREV
)
1747 options
->abbrev
= MINIMUM_ABBREV
;
1748 else if (40 < options
->abbrev
)
1749 options
->abbrev
= 40;
1751 else if (!strcmp(arg
, "--color"))
1752 options
->color_diff
= 1;
1753 else if (!strcmp(arg
, "--no-color"))
1754 options
->color_diff
= 0;
1755 else if (!strcmp(arg
, "-w") || !strcmp(arg
, "--ignore-all-space"))
1756 options
->xdl_opts
|= XDF_IGNORE_WHITESPACE
;
1757 else if (!strcmp(arg
, "-b") || !strcmp(arg
, "--ignore-space-change"))
1758 options
->xdl_opts
|= XDF_IGNORE_WHITESPACE_CHANGE
;
1759 else if (!strcmp(arg
, "--color-words"))
1760 options
->color_diff
= options
->color_diff_words
= 1;
1761 else if (!strcmp(arg
, "--no-renames"))
1762 options
->detect_rename
= 0;
1768 static int parse_num(const char **cp_p
)
1770 unsigned long num
, scale
;
1772 const char *cp
= *cp_p
;
1779 if ( !dot
&& ch
== '.' ) {
1782 } else if ( ch
== '%' ) {
1783 scale
= dot
? scale
*100 : 100;
1784 cp
++; /* % is always at the end */
1786 } else if ( ch
>= '0' && ch
<= '9' ) {
1787 if ( scale
< 100000 ) {
1789 num
= (num
*10) + (ch
-'0');
1798 /* user says num divided by scale and we say internally that
1799 * is MAX_SCORE * num / scale.
1801 return (num
>= scale
) ? MAX_SCORE
: (MAX_SCORE
* num
/ scale
);
1804 int diff_scoreopt_parse(const char *opt
)
1806 int opt1
, opt2
, cmd
;
1811 if (cmd
!= 'M' && cmd
!= 'C' && cmd
!= 'B')
1812 return -1; /* that is not a -M, -C nor -B option */
1814 opt1
= parse_num(&opt
);
1820 else if (*opt
!= '/')
1821 return -1; /* we expect -B80/99 or -B80 */
1824 opt2
= parse_num(&opt
);
1829 return opt1
| (opt2
<< 16);
1832 struct diff_queue_struct diff_queued_diff
;
1834 void diff_q(struct diff_queue_struct
*queue
, struct diff_filepair
*dp
)
1836 if (queue
->alloc
<= queue
->nr
) {
1837 queue
->alloc
= alloc_nr(queue
->alloc
);
1838 queue
->queue
= xrealloc(queue
->queue
,
1839 sizeof(dp
) * queue
->alloc
);
1841 queue
->queue
[queue
->nr
++] = dp
;
1844 struct diff_filepair
*diff_queue(struct diff_queue_struct
*queue
,
1845 struct diff_filespec
*one
,
1846 struct diff_filespec
*two
)
1848 struct diff_filepair
*dp
= xcalloc(1, sizeof(*dp
));
1856 void diff_free_filepair(struct diff_filepair
*p
)
1858 diff_free_filespec_data(p
->one
);
1859 diff_free_filespec_data(p
->two
);
1865 /* This is different from find_unique_abbrev() in that
1866 * it stuffs the result with dots for alignment.
1868 const char *diff_unique_abbrev(const unsigned char *sha1
, int len
)
1873 return sha1_to_hex(sha1
);
1875 abbrev
= find_unique_abbrev(sha1
, len
);
1877 return sha1_to_hex(sha1
);
1878 abblen
= strlen(abbrev
);
1880 static char hex
[41];
1881 if (len
< abblen
&& abblen
<= len
+ 2)
1882 sprintf(hex
, "%s%.*s", abbrev
, len
+3-abblen
, "..");
1884 sprintf(hex
, "%s...", abbrev
);
1887 return sha1_to_hex(sha1
);
1890 static void diff_flush_raw(struct diff_filepair
*p
,
1891 struct diff_options
*options
)
1895 int abbrev
= options
->abbrev
;
1896 const char *path_one
, *path_two
;
1897 int inter_name_termination
= '\t';
1898 int line_termination
= options
->line_termination
;
1900 if (!line_termination
)
1901 inter_name_termination
= 0;
1903 path_one
= p
->one
->path
;
1904 path_two
= p
->two
->path
;
1905 if (line_termination
) {
1906 path_one
= quote_one(path_one
);
1907 path_two
= quote_one(path_two
);
1911 sprintf(status
, "%c%03d", p
->status
,
1912 (int)(0.5 + p
->score
* 100.0/MAX_SCORE
));
1914 status
[0] = p
->status
;
1917 switch (p
->status
) {
1918 case DIFF_STATUS_COPIED
:
1919 case DIFF_STATUS_RENAMED
:
1922 case DIFF_STATUS_ADDED
:
1923 case DIFF_STATUS_DELETED
:
1930 if (!(options
->output_format
& DIFF_FORMAT_NAME_STATUS
)) {
1931 printf(":%06o %06o %s ",
1932 p
->one
->mode
, p
->two
->mode
,
1933 diff_unique_abbrev(p
->one
->sha1
, abbrev
));
1935 diff_unique_abbrev(p
->two
->sha1
, abbrev
));
1937 printf("%s%c%s", status
, inter_name_termination
, path_one
);
1939 printf("%c%s", inter_name_termination
, path_two
);
1940 putchar(line_termination
);
1941 if (path_one
!= p
->one
->path
)
1942 free((void*)path_one
);
1943 if (path_two
!= p
->two
->path
)
1944 free((void*)path_two
);
1947 static void diff_flush_name(struct diff_filepair
*p
, int line_termination
)
1949 char *path
= p
->two
->path
;
1951 if (line_termination
)
1952 path
= quote_one(p
->two
->path
);
1953 printf("%s%c", path
, line_termination
);
1954 if (p
->two
->path
!= path
)
1958 int diff_unmodified_pair(struct diff_filepair
*p
)
1960 /* This function is written stricter than necessary to support
1961 * the currently implemented transformers, but the idea is to
1962 * let transformers to produce diff_filepairs any way they want,
1963 * and filter and clean them up here before producing the output.
1965 struct diff_filespec
*one
, *two
;
1967 if (DIFF_PAIR_UNMERGED(p
))
1968 return 0; /* unmerged is interesting */
1973 /* deletion, addition, mode or type change
1974 * and rename are all interesting.
1976 if (DIFF_FILE_VALID(one
) != DIFF_FILE_VALID(two
) ||
1977 DIFF_PAIR_MODE_CHANGED(p
) ||
1978 strcmp(one
->path
, two
->path
))
1981 /* both are valid and point at the same path. that is, we are
1982 * dealing with a change.
1984 if (one
->sha1_valid
&& two
->sha1_valid
&&
1985 !hashcmp(one
->sha1
, two
->sha1
))
1986 return 1; /* no change */
1987 if (!one
->sha1_valid
&& !two
->sha1_valid
)
1988 return 1; /* both look at the same file on the filesystem. */
1992 static void diff_flush_patch(struct diff_filepair
*p
, struct diff_options
*o
)
1994 if (diff_unmodified_pair(p
))
1997 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
1998 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
1999 return; /* no tree diffs in patch format */
2004 static void diff_flush_stat(struct diff_filepair
*p
, struct diff_options
*o
,
2005 struct diffstat_t
*diffstat
)
2007 if (diff_unmodified_pair(p
))
2010 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
2011 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
2012 return; /* no tree diffs in patch format */
2014 run_diffstat(p
, o
, diffstat
);
2017 static void diff_flush_checkdiff(struct diff_filepair
*p
,
2018 struct diff_options
*o
)
2020 if (diff_unmodified_pair(p
))
2023 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
2024 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
2025 return; /* no tree diffs in patch format */
2027 run_checkdiff(p
, o
);
2030 int diff_queue_is_empty(void)
2032 struct diff_queue_struct
*q
= &diff_queued_diff
;
2034 for (i
= 0; i
< q
->nr
; i
++)
2035 if (!diff_unmodified_pair(q
->queue
[i
]))
2041 void diff_debug_filespec(struct diff_filespec
*s
, int x
, const char *one
)
2043 fprintf(stderr
, "queue[%d] %s (%s) %s %06o %s\n",
2046 DIFF_FILE_VALID(s
) ? "valid" : "invalid",
2048 s
->sha1_valid
? sha1_to_hex(s
->sha1
) : "");
2049 fprintf(stderr
, "queue[%d] %s size %lu flags %d\n",
2051 s
->size
, s
->xfrm_flags
);
2054 void diff_debug_filepair(const struct diff_filepair
*p
, int i
)
2056 diff_debug_filespec(p
->one
, i
, "one");
2057 diff_debug_filespec(p
->two
, i
, "two");
2058 fprintf(stderr
, "score %d, status %c stays %d broken %d\n",
2059 p
->score
, p
->status
? p
->status
: '?',
2060 p
->source_stays
, p
->broken_pair
);
2063 void diff_debug_queue(const char *msg
, struct diff_queue_struct
*q
)
2067 fprintf(stderr
, "%s\n", msg
);
2068 fprintf(stderr
, "q->nr = %d\n", q
->nr
);
2069 for (i
= 0; i
< q
->nr
; i
++) {
2070 struct diff_filepair
*p
= q
->queue
[i
];
2071 diff_debug_filepair(p
, i
);
2076 static void diff_resolve_rename_copy(void)
2079 struct diff_filepair
*p
, *pp
;
2080 struct diff_queue_struct
*q
= &diff_queued_diff
;
2082 diff_debug_queue("resolve-rename-copy", q
);
2084 for (i
= 0; i
< q
->nr
; i
++) {
2086 p
->status
= 0; /* undecided */
2087 if (DIFF_PAIR_UNMERGED(p
))
2088 p
->status
= DIFF_STATUS_UNMERGED
;
2089 else if (!DIFF_FILE_VALID(p
->one
))
2090 p
->status
= DIFF_STATUS_ADDED
;
2091 else if (!DIFF_FILE_VALID(p
->two
))
2092 p
->status
= DIFF_STATUS_DELETED
;
2093 else if (DIFF_PAIR_TYPE_CHANGED(p
))
2094 p
->status
= DIFF_STATUS_TYPE_CHANGED
;
2096 /* from this point on, we are dealing with a pair
2097 * whose both sides are valid and of the same type, i.e.
2098 * either in-place edit or rename/copy edit.
2100 else if (DIFF_PAIR_RENAME(p
)) {
2101 if (p
->source_stays
) {
2102 p
->status
= DIFF_STATUS_COPIED
;
2105 /* See if there is some other filepair that
2106 * copies from the same source as us. If so
2107 * we are a copy. Otherwise we are either a
2108 * copy if the path stays, or a rename if it
2109 * does not, but we already handled "stays" case.
2111 for (j
= i
+ 1; j
< q
->nr
; j
++) {
2113 if (strcmp(pp
->one
->path
, p
->one
->path
))
2114 continue; /* not us */
2115 if (!DIFF_PAIR_RENAME(pp
))
2116 continue; /* not a rename/copy */
2117 /* pp is a rename/copy from the same source */
2118 p
->status
= DIFF_STATUS_COPIED
;
2122 p
->status
= DIFF_STATUS_RENAMED
;
2124 else if (hashcmp(p
->one
->sha1
, p
->two
->sha1
) ||
2125 p
->one
->mode
!= p
->two
->mode
)
2126 p
->status
= DIFF_STATUS_MODIFIED
;
2128 /* This is a "no-change" entry and should not
2129 * happen anymore, but prepare for broken callers.
2131 error("feeding unmodified %s to diffcore",
2133 p
->status
= DIFF_STATUS_UNKNOWN
;
2136 diff_debug_queue("resolve-rename-copy done", q
);
2139 static int check_pair_status(struct diff_filepair
*p
)
2141 switch (p
->status
) {
2142 case DIFF_STATUS_UNKNOWN
:
2145 die("internal error in diff-resolve-rename-copy");
2151 static void flush_one_pair(struct diff_filepair
*p
, struct diff_options
*opt
)
2153 int fmt
= opt
->output_format
;
2155 if (fmt
& DIFF_FORMAT_CHECKDIFF
)
2156 diff_flush_checkdiff(p
, opt
);
2157 else if (fmt
& (DIFF_FORMAT_RAW
| DIFF_FORMAT_NAME_STATUS
))
2158 diff_flush_raw(p
, opt
);
2159 else if (fmt
& DIFF_FORMAT_NAME
)
2160 diff_flush_name(p
, opt
->line_termination
);
2163 static void show_file_mode_name(const char *newdelete
, struct diff_filespec
*fs
)
2166 printf(" %s mode %06o %s\n", newdelete
, fs
->mode
, fs
->path
);
2168 printf(" %s %s\n", newdelete
, fs
->path
);
2172 static void show_mode_change(struct diff_filepair
*p
, int show_name
)
2174 if (p
->one
->mode
&& p
->two
->mode
&& p
->one
->mode
!= p
->two
->mode
) {
2176 printf(" mode change %06o => %06o %s\n",
2177 p
->one
->mode
, p
->two
->mode
, p
->two
->path
);
2179 printf(" mode change %06o => %06o\n",
2180 p
->one
->mode
, p
->two
->mode
);
2184 static void show_rename_copy(const char *renamecopy
, struct diff_filepair
*p
)
2186 const char *old
, *new;
2188 /* Find common prefix */
2192 const char *slash_old
, *slash_new
;
2193 slash_old
= strchr(old
, '/');
2194 slash_new
= strchr(new, '/');
2197 slash_old
- old
!= slash_new
- new ||
2198 memcmp(old
, new, slash_new
- new))
2200 old
= slash_old
+ 1;
2201 new = slash_new
+ 1;
2203 /* p->one->path thru old is the common prefix, and old and new
2204 * through the end of names are renames
2206 if (old
!= p
->one
->path
)
2207 printf(" %s %.*s{%s => %s} (%d%%)\n", renamecopy
,
2208 (int)(old
- p
->one
->path
), p
->one
->path
,
2209 old
, new, (int)(0.5 + p
->score
* 100.0/MAX_SCORE
));
2211 printf(" %s %s => %s (%d%%)\n", renamecopy
,
2212 p
->one
->path
, p
->two
->path
,
2213 (int)(0.5 + p
->score
* 100.0/MAX_SCORE
));
2214 show_mode_change(p
, 0);
2217 static void diff_summary(struct diff_filepair
*p
)
2220 case DIFF_STATUS_DELETED
:
2221 show_file_mode_name("delete", p
->one
);
2223 case DIFF_STATUS_ADDED
:
2224 show_file_mode_name("create", p
->two
);
2226 case DIFF_STATUS_COPIED
:
2227 show_rename_copy("copy", p
);
2229 case DIFF_STATUS_RENAMED
:
2230 show_rename_copy("rename", p
);
2234 printf(" rewrite %s (%d%%)\n", p
->two
->path
,
2235 (int)(0.5 + p
->score
* 100.0/MAX_SCORE
));
2236 show_mode_change(p
, 0);
2237 } else show_mode_change(p
, 1);
2243 struct xdiff_emit_state xm
;
2248 static int remove_space(char *line
, int len
)
2254 for (i
= 0; i
< len
; i
++)
2255 if (!isspace((c
= line
[i
])))
2261 static void patch_id_consume(void *priv
, char *line
, unsigned long len
)
2263 struct patch_id_t
*data
= priv
;
2266 /* Ignore line numbers when computing the SHA1 of the patch */
2267 if (!strncmp(line
, "@@ -", 4))
2270 new_len
= remove_space(line
, len
);
2272 SHA1_Update(data
->ctx
, line
, new_len
);
2273 data
->patchlen
+= new_len
;
2276 /* returns 0 upon success, and writes result into sha1 */
2277 static int diff_get_patch_id(struct diff_options
*options
, unsigned char *sha1
)
2279 struct diff_queue_struct
*q
= &diff_queued_diff
;
2282 struct patch_id_t data
;
2283 char buffer
[PATH_MAX
* 4 + 20];
2286 memset(&data
, 0, sizeof(struct patch_id_t
));
2288 data
.xm
.consume
= patch_id_consume
;
2290 for (i
= 0; i
< q
->nr
; i
++) {
2295 struct diff_filepair
*p
= q
->queue
[i
];
2299 return error("internal diff status error");
2300 if (p
->status
== DIFF_STATUS_UNKNOWN
)
2302 if (diff_unmodified_pair(p
))
2304 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
2305 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
2307 if (DIFF_PAIR_UNMERGED(p
))
2310 diff_fill_sha1_info(p
->one
);
2311 diff_fill_sha1_info(p
->two
);
2312 if (fill_mmfile(&mf1
, p
->one
) < 0 ||
2313 fill_mmfile(&mf2
, p
->two
) < 0)
2314 return error("unable to read files to diff");
2316 /* Maybe hash p->two? into the patch id? */
2317 if (mmfile_is_binary(&mf2
))
2320 len1
= remove_space(p
->one
->path
, strlen(p
->one
->path
));
2321 len2
= remove_space(p
->two
->path
, strlen(p
->two
->path
));
2322 if (p
->one
->mode
== 0)
2323 len1
= snprintf(buffer
, sizeof(buffer
),
2324 "diff--gita/%.*sb/%.*s"
2331 len2
, p
->two
->path
);
2332 else if (p
->two
->mode
== 0)
2333 len1
= snprintf(buffer
, sizeof(buffer
),
2334 "diff--gita/%.*sb/%.*s"
2335 "deletedfilemode%06o"
2341 len1
, p
->one
->path
);
2343 len1
= snprintf(buffer
, sizeof(buffer
),
2344 "diff--gita/%.*sb/%.*s"
2350 len2
, p
->two
->path
);
2351 SHA1_Update(&ctx
, buffer
, len1
);
2353 xpp
.flags
= XDF_NEED_MINIMAL
;
2355 xecfg
.flags
= XDL_EMIT_FUNCNAMES
;
2356 ecb
.outf
= xdiff_outf
;
2358 xdl_diff(&mf1
, &mf2
, &xpp
, &xecfg
, &ecb
);
2361 SHA1_Final(sha1
, &ctx
);
2365 int diff_flush_patch_id(struct diff_options
*options
, unsigned char *sha1
)
2367 struct diff_queue_struct
*q
= &diff_queued_diff
;
2369 int result
= diff_get_patch_id(options
, sha1
);
2371 for (i
= 0; i
< q
->nr
; i
++)
2372 diff_free_filepair(q
->queue
[i
]);
2376 q
->nr
= q
->alloc
= 0;
2381 static int is_summary_empty(const struct diff_queue_struct
*q
)
2385 for (i
= 0; i
< q
->nr
; i
++) {
2386 const struct diff_filepair
*p
= q
->queue
[i
];
2388 switch (p
->status
) {
2389 case DIFF_STATUS_DELETED
:
2390 case DIFF_STATUS_ADDED
:
2391 case DIFF_STATUS_COPIED
:
2392 case DIFF_STATUS_RENAMED
:
2397 if (p
->one
->mode
&& p
->two
->mode
&&
2398 p
->one
->mode
!= p
->two
->mode
)
2406 void diff_flush(struct diff_options
*options
)
2408 struct diff_queue_struct
*q
= &diff_queued_diff
;
2409 int i
, output_format
= options
->output_format
;
2413 * Order: raw, stat, summary, patch
2414 * or: name/name-status/checkdiff (other bits clear)
2419 if (output_format
& (DIFF_FORMAT_RAW
|
2421 DIFF_FORMAT_NAME_STATUS
|
2422 DIFF_FORMAT_CHECKDIFF
)) {
2423 for (i
= 0; i
< q
->nr
; i
++) {
2424 struct diff_filepair
*p
= q
->queue
[i
];
2425 if (check_pair_status(p
))
2426 flush_one_pair(p
, options
);
2431 if (output_format
& DIFF_FORMAT_DIFFSTAT
) {
2432 struct diffstat_t diffstat
;
2434 memset(&diffstat
, 0, sizeof(struct diffstat_t
));
2435 diffstat
.xm
.consume
= diffstat_consume
;
2436 for (i
= 0; i
< q
->nr
; i
++) {
2437 struct diff_filepair
*p
= q
->queue
[i
];
2438 if (check_pair_status(p
))
2439 diff_flush_stat(p
, options
, &diffstat
);
2441 show_stats(&diffstat
);
2445 if (output_format
& DIFF_FORMAT_SUMMARY
&& !is_summary_empty(q
)) {
2446 for (i
= 0; i
< q
->nr
; i
++)
2447 diff_summary(q
->queue
[i
]);
2451 if (output_format
& DIFF_FORMAT_PATCH
) {
2453 if (options
->stat_sep
) {
2454 /* attach patch instead of inline */
2455 fputs(options
->stat_sep
, stdout
);
2457 putchar(options
->line_termination
);
2461 for (i
= 0; i
< q
->nr
; i
++) {
2462 struct diff_filepair
*p
= q
->queue
[i
];
2463 if (check_pair_status(p
))
2464 diff_flush_patch(p
, options
);
2468 if (output_format
& DIFF_FORMAT_CALLBACK
)
2469 options
->format_callback(q
, options
, options
->format_callback_data
);
2471 for (i
= 0; i
< q
->nr
; i
++)
2472 diff_free_filepair(q
->queue
[i
]);
2476 q
->nr
= q
->alloc
= 0;
2479 static void diffcore_apply_filter(const char *filter
)
2482 struct diff_queue_struct
*q
= &diff_queued_diff
;
2483 struct diff_queue_struct outq
;
2485 outq
.nr
= outq
.alloc
= 0;
2490 if (strchr(filter
, DIFF_STATUS_FILTER_AON
)) {
2492 for (i
= found
= 0; !found
&& i
< q
->nr
; i
++) {
2493 struct diff_filepair
*p
= q
->queue
[i
];
2494 if (((p
->status
== DIFF_STATUS_MODIFIED
) &&
2496 strchr(filter
, DIFF_STATUS_FILTER_BROKEN
)) ||
2498 strchr(filter
, DIFF_STATUS_MODIFIED
)))) ||
2499 ((p
->status
!= DIFF_STATUS_MODIFIED
) &&
2500 strchr(filter
, p
->status
)))
2506 /* otherwise we will clear the whole queue
2507 * by copying the empty outq at the end of this
2508 * function, but first clear the current entries
2511 for (i
= 0; i
< q
->nr
; i
++)
2512 diff_free_filepair(q
->queue
[i
]);
2515 /* Only the matching ones */
2516 for (i
= 0; i
< q
->nr
; i
++) {
2517 struct diff_filepair
*p
= q
->queue
[i
];
2519 if (((p
->status
== DIFF_STATUS_MODIFIED
) &&
2521 strchr(filter
, DIFF_STATUS_FILTER_BROKEN
)) ||
2523 strchr(filter
, DIFF_STATUS_MODIFIED
)))) ||
2524 ((p
->status
!= DIFF_STATUS_MODIFIED
) &&
2525 strchr(filter
, p
->status
)))
2528 diff_free_filepair(p
);
2535 void diffcore_std(struct diff_options
*options
)
2537 if (options
->break_opt
!= -1)
2538 diffcore_break(options
->break_opt
);
2539 if (options
->detect_rename
)
2540 diffcore_rename(options
);
2541 if (options
->break_opt
!= -1)
2542 diffcore_merge_broken();
2543 if (options
->pickaxe
)
2544 diffcore_pickaxe(options
->pickaxe
, options
->pickaxe_opts
);
2545 if (options
->orderfile
)
2546 diffcore_order(options
->orderfile
);
2547 diff_resolve_rename_copy();
2548 diffcore_apply_filter(options
->filter
);
2552 void diffcore_std_no_resolve(struct diff_options
*options
)
2554 if (options
->pickaxe
)
2555 diffcore_pickaxe(options
->pickaxe
, options
->pickaxe_opts
);
2556 if (options
->orderfile
)
2557 diffcore_order(options
->orderfile
);
2558 diffcore_apply_filter(options
->filter
);
2561 void diff_addremove(struct diff_options
*options
,
2562 int addremove
, unsigned mode
,
2563 const unsigned char *sha1
,
2564 const char *base
, const char *path
)
2566 char concatpath
[PATH_MAX
];
2567 struct diff_filespec
*one
, *two
;
2569 /* This may look odd, but it is a preparation for
2570 * feeding "there are unchanged files which should
2571 * not produce diffs, but when you are doing copy
2572 * detection you would need them, so here they are"
2573 * entries to the diff-core. They will be prefixed
2574 * with something like '=' or '*' (I haven't decided
2575 * which but should not make any difference).
2576 * Feeding the same new and old to diff_change()
2577 * also has the same effect.
2578 * Before the final output happens, they are pruned after
2579 * merged into rename/copy pairs as appropriate.
2581 if (options
->reverse_diff
)
2582 addremove
= (addremove
== '+' ? '-' :
2583 addremove
== '-' ? '+' : addremove
);
2585 if (!path
) path
= "";
2586 sprintf(concatpath
, "%s%s", base
, path
);
2587 one
= alloc_filespec(concatpath
);
2588 two
= alloc_filespec(concatpath
);
2590 if (addremove
!= '+')
2591 fill_filespec(one
, sha1
, mode
);
2592 if (addremove
!= '-')
2593 fill_filespec(two
, sha1
, mode
);
2595 diff_queue(&diff_queued_diff
, one
, two
);
2598 void diff_change(struct diff_options
*options
,
2599 unsigned old_mode
, unsigned new_mode
,
2600 const unsigned char *old_sha1
,
2601 const unsigned char *new_sha1
,
2602 const char *base
, const char *path
)
2604 char concatpath
[PATH_MAX
];
2605 struct diff_filespec
*one
, *two
;
2607 if (options
->reverse_diff
) {
2609 const unsigned char *tmp_c
;
2610 tmp
= old_mode
; old_mode
= new_mode
; new_mode
= tmp
;
2611 tmp_c
= old_sha1
; old_sha1
= new_sha1
; new_sha1
= tmp_c
;
2613 if (!path
) path
= "";
2614 sprintf(concatpath
, "%s%s", base
, path
);
2615 one
= alloc_filespec(concatpath
);
2616 two
= alloc_filespec(concatpath
);
2617 fill_filespec(one
, old_sha1
, old_mode
);
2618 fill_filespec(two
, new_sha1
, new_mode
);
2620 diff_queue(&diff_queued_diff
, one
, two
);
2623 void diff_unmerge(struct diff_options
*options
,
2626 struct diff_filespec
*one
, *two
;
2627 one
= alloc_filespec(path
);
2628 two
= alloc_filespec(path
);
2629 diff_queue(&diff_queued_diff
, one
, two
);