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 const char mime_boundary_leader
[] = "------------";
550 static int scale_linear(int it
, int width
, int max_change
)
553 * round(width * it / max_change);
555 return (it
* width
* 2 + max_change
) / (max_change
* 2);
558 static void show_name(const char *prefix
, const char *name
, int len
)
560 printf(" %s%-*s |", prefix
, len
, name
);
563 static void show_graph(char ch
, int cnt
)
571 static void show_stats(struct diffstat_t
* data
, struct diff_options
*options
)
573 int i
, len
, add
, del
, total
, adds
= 0, dels
= 0;
574 int max_change
= 0, max_len
= 0;
575 int total_files
= data
->nr
;
576 int width
, name_width
;
581 width
= options
->stat_width
? options
->stat_width
: 80;
582 name_width
= options
->stat_name_width
? options
->stat_name_width
: 50;
584 /* Sanity: give at least 5 columns to the graph,
585 * but leave at least 10 columns for the name.
587 if (width
< name_width
+ 15) {
588 if (name_width
<= 25)
589 width
= name_width
+ 15;
591 name_width
= width
- 15;
594 /* Find the longest filename and max number of changes */
595 for (i
= 0; i
< data
->nr
; i
++) {
596 struct diffstat_file
*file
= data
->files
[i
];
597 int change
= file
->added
+ file
->deleted
;
599 len
= quote_c_style(file
->name
, NULL
, NULL
, 0);
601 char *qname
= xmalloc(len
+ 1);
602 quote_c_style(file
->name
, qname
, NULL
, 0);
607 len
= strlen(file
->name
);
611 if (file
->is_binary
|| file
->is_unmerged
)
613 if (max_change
< change
)
617 /* Compute the width of the graph part;
618 * 10 is for one blank at the beginning of the line plus
619 * " | count " between the name and the graph.
621 * From here on, name_width is the width of the name area,
622 * and width is the width of the graph area.
624 name_width
= (name_width
< max_len
) ? name_width
: max_len
;
625 if (width
< (name_width
+ 10) + max_change
)
626 width
= width
- (name_width
+ 10);
630 for (i
= 0; i
< data
->nr
; i
++) {
631 const char *prefix
= "";
632 char *name
= data
->files
[i
]->name
;
633 int added
= data
->files
[i
]->added
;
634 int deleted
= data
->files
[i
]->deleted
;
638 * "scale" the filename
641 name_len
= strlen(name
);
642 if (name_width
< name_len
) {
646 name
+= name_len
- len
;
647 slash
= strchr(name
, '/');
652 if (data
->files
[i
]->is_binary
) {
653 show_name(prefix
, name
, len
);
655 goto free_diffstat_file
;
657 else if (data
->files
[i
]->is_unmerged
) {
658 show_name(prefix
, name
, len
);
659 printf(" Unmerged\n");
660 goto free_diffstat_file
;
662 else if (!data
->files
[i
]->is_renamed
&&
663 (added
+ deleted
== 0)) {
665 goto free_diffstat_file
;
669 * scale the add/delete
677 if (width
<= max_change
) {
678 total
= scale_linear(total
, width
, max_change
);
679 add
= scale_linear(add
, width
, max_change
);
682 show_name(prefix
, name
, len
);
683 printf("%5d ", added
+ deleted
);
684 show_graph('+', add
);
685 show_graph('-', del
);
688 free(data
->files
[i
]->name
);
689 free(data
->files
[i
]);
692 printf(" %d files changed, %d insertions(+), %d deletions(-)\n",
693 total_files
, adds
, dels
);
697 struct xdiff_emit_state xm
;
698 const char *filename
;
702 static void checkdiff_consume(void *priv
, char *line
, unsigned long len
)
704 struct checkdiff_t
*data
= priv
;
706 if (line
[0] == '+') {
711 /* check space before tab */
712 for (i
= 1; i
< len
&& (line
[i
] == ' ' || line
[i
] == '\t'); i
++)
715 if (line
[i
- 1] == '\t' && spaces
)
716 printf("%s:%d: space before tab:%.*s\n",
717 data
->filename
, data
->lineno
, (int)len
, line
);
719 /* check white space at line end */
720 if (line
[len
- 1] == '\n')
722 if (isspace(line
[len
- 1]))
723 printf("%s:%d: white space at end: %.*s\n",
724 data
->filename
, data
->lineno
, (int)len
, line
);
725 } else if (line
[0] == ' ')
727 else if (line
[0] == '@') {
728 char *plus
= strchr(line
, '+');
730 data
->lineno
= strtol(plus
, NULL
, 10);
736 static unsigned char *deflate_it(char *data
,
738 unsigned long *result_size
)
741 unsigned char *deflated
;
744 memset(&stream
, 0, sizeof(stream
));
745 deflateInit(&stream
, zlib_compression_level
);
746 bound
= deflateBound(&stream
, size
);
747 deflated
= xmalloc(bound
);
748 stream
.next_out
= deflated
;
749 stream
.avail_out
= bound
;
751 stream
.next_in
= (unsigned char *)data
;
752 stream
.avail_in
= size
;
753 while (deflate(&stream
, Z_FINISH
) == Z_OK
)
756 *result_size
= stream
.total_out
;
760 static void emit_binary_diff_body(mmfile_t
*one
, mmfile_t
*two
)
766 unsigned long orig_size
;
767 unsigned long delta_size
;
768 unsigned long deflate_size
;
769 unsigned long data_size
;
771 /* We could do deflated delta, or we could do just deflated two,
772 * whichever is smaller.
775 deflated
= deflate_it(two
->ptr
, two
->size
, &deflate_size
);
776 if (one
->size
&& two
->size
) {
777 delta
= diff_delta(one
->ptr
, one
->size
,
779 &delta_size
, deflate_size
);
781 void *to_free
= delta
;
782 orig_size
= delta_size
;
783 delta
= deflate_it(delta
, delta_size
, &delta_size
);
788 if (delta
&& delta_size
< deflate_size
) {
789 printf("delta %lu\n", orig_size
);
792 data_size
= delta_size
;
795 printf("literal %lu\n", two
->size
);
798 data_size
= deflate_size
;
801 /* emit data encoded in base85 */
804 int bytes
= (52 < data_size
) ? 52 : data_size
;
808 line
[0] = bytes
+ 'A' - 1;
810 line
[0] = bytes
- 26 + 'a' - 1;
811 encode_85(line
+ 1, cp
, bytes
);
812 cp
= (char *) cp
+ bytes
;
819 static void emit_binary_diff(mmfile_t
*one
, mmfile_t
*two
)
821 printf("GIT binary patch\n");
822 emit_binary_diff_body(one
, two
);
823 emit_binary_diff_body(two
, one
);
826 #define FIRST_FEW_BYTES 8000
827 static int mmfile_is_binary(mmfile_t
*mf
)
830 if (FIRST_FEW_BYTES
< sz
)
831 sz
= FIRST_FEW_BYTES
;
832 return !!memchr(mf
->ptr
, 0, sz
);
835 static void builtin_diff(const char *name_a
,
837 struct diff_filespec
*one
,
838 struct diff_filespec
*two
,
839 const char *xfrm_msg
,
840 struct diff_options
*o
,
841 int complete_rewrite
)
846 const char *set
= diff_get_color(o
->color_diff
, DIFF_METAINFO
);
847 const char *reset
= diff_get_color(o
->color_diff
, DIFF_RESET
);
849 a_one
= quote_two("a/", name_a
);
850 b_two
= quote_two("b/", name_b
);
851 lbl
[0] = DIFF_FILE_VALID(one
) ? a_one
: "/dev/null";
852 lbl
[1] = DIFF_FILE_VALID(two
) ? b_two
: "/dev/null";
853 printf("%sdiff --git %s %s%s\n", set
, a_one
, b_two
, reset
);
854 if (lbl
[0][0] == '/') {
856 printf("%snew file mode %06o%s\n", set
, two
->mode
, reset
);
857 if (xfrm_msg
&& xfrm_msg
[0])
858 printf("%s%s%s\n", set
, xfrm_msg
, reset
);
860 else if (lbl
[1][0] == '/') {
861 printf("%sdeleted file mode %06o%s\n", set
, one
->mode
, reset
);
862 if (xfrm_msg
&& xfrm_msg
[0])
863 printf("%s%s%s\n", set
, xfrm_msg
, reset
);
866 if (one
->mode
!= two
->mode
) {
867 printf("%sold mode %06o%s\n", set
, one
->mode
, reset
);
868 printf("%snew mode %06o%s\n", set
, two
->mode
, reset
);
870 if (xfrm_msg
&& xfrm_msg
[0])
871 printf("%s%s%s\n", set
, xfrm_msg
, reset
);
873 * we do not run diff between different kind
876 if ((one
->mode
^ two
->mode
) & S_IFMT
)
877 goto free_ab_and_return
;
878 if (complete_rewrite
) {
879 emit_rewrite_diff(name_a
, name_b
, one
, two
);
880 goto free_ab_and_return
;
884 if (fill_mmfile(&mf1
, one
) < 0 || fill_mmfile(&mf2
, two
) < 0)
885 die("unable to read files to diff");
887 if (!o
->text
&& (mmfile_is_binary(&mf1
) || mmfile_is_binary(&mf2
))) {
888 /* Quite common confusing case */
889 if (mf1
.size
== mf2
.size
&&
890 !memcmp(mf1
.ptr
, mf2
.ptr
, mf1
.size
))
891 goto free_ab_and_return
;
893 emit_binary_diff(&mf1
, &mf2
);
895 printf("Binary files %s and %s differ\n",
899 /* Crazy xdl interfaces.. */
900 const char *diffopts
= getenv("GIT_DIFF_OPTS");
904 struct emit_callback ecbdata
;
906 memset(&ecbdata
, 0, sizeof(ecbdata
));
907 ecbdata
.label_path
= lbl
;
908 ecbdata
.color_diff
= o
->color_diff
;
909 xpp
.flags
= XDF_NEED_MINIMAL
| o
->xdl_opts
;
910 xecfg
.ctxlen
= o
->context
;
911 xecfg
.flags
= XDL_EMIT_FUNCNAMES
;
914 else if (!strncmp(diffopts
, "--unified=", 10))
915 xecfg
.ctxlen
= strtoul(diffopts
+ 10, NULL
, 10);
916 else if (!strncmp(diffopts
, "-u", 2))
917 xecfg
.ctxlen
= strtoul(diffopts
+ 2, NULL
, 10);
918 ecb
.outf
= xdiff_outf
;
920 ecbdata
.xm
.consume
= fn_out_consume
;
921 if (o
->color_diff_words
)
923 xcalloc(1, sizeof(struct diff_words_data
));
924 xdl_diff(&mf1
, &mf2
, &xpp
, &xecfg
, &ecb
);
925 if (o
->color_diff_words
)
926 free_diff_words_data(&ecbdata
);
935 static void builtin_diffstat(const char *name_a
, const char *name_b
,
936 struct diff_filespec
*one
,
937 struct diff_filespec
*two
,
938 struct diffstat_t
*diffstat
,
939 struct diff_options
*o
,
940 int complete_rewrite
)
943 struct diffstat_file
*data
;
945 data
= diffstat_add(diffstat
, name_a
, name_b
);
948 data
->is_unmerged
= 1;
951 if (complete_rewrite
) {
952 diff_populate_filespec(one
, 0);
953 diff_populate_filespec(two
, 0);
954 data
->deleted
= count_lines(one
->data
, one
->size
);
955 data
->added
= count_lines(two
->data
, two
->size
);
958 if (fill_mmfile(&mf1
, one
) < 0 || fill_mmfile(&mf2
, two
) < 0)
959 die("unable to read files to diff");
961 if (mmfile_is_binary(&mf1
) || mmfile_is_binary(&mf2
))
964 /* Crazy xdl interfaces.. */
969 xpp
.flags
= XDF_NEED_MINIMAL
| o
->xdl_opts
;
972 ecb
.outf
= xdiff_outf
;
974 xdl_diff(&mf1
, &mf2
, &xpp
, &xecfg
, &ecb
);
978 static void builtin_checkdiff(const char *name_a
, const char *name_b
,
979 struct diff_filespec
*one
,
980 struct diff_filespec
*two
)
983 struct checkdiff_t data
;
988 memset(&data
, 0, sizeof(data
));
989 data
.xm
.consume
= checkdiff_consume
;
990 data
.filename
= name_b
? name_b
: name_a
;
993 if (fill_mmfile(&mf1
, one
) < 0 || fill_mmfile(&mf2
, two
) < 0)
994 die("unable to read files to diff");
996 if (mmfile_is_binary(&mf2
))
999 /* Crazy xdl interfaces.. */
1004 xpp
.flags
= XDF_NEED_MINIMAL
;
1007 ecb
.outf
= xdiff_outf
;
1009 xdl_diff(&mf1
, &mf2
, &xpp
, &xecfg
, &ecb
);
1013 struct diff_filespec
*alloc_filespec(const char *path
)
1015 int namelen
= strlen(path
);
1016 struct diff_filespec
*spec
= xmalloc(sizeof(*spec
) + namelen
+ 1);
1018 memset(spec
, 0, sizeof(*spec
));
1019 spec
->path
= (char *)(spec
+ 1);
1020 memcpy(spec
->path
, path
, namelen
+1);
1024 void fill_filespec(struct diff_filespec
*spec
, const unsigned char *sha1
,
1025 unsigned short mode
)
1028 spec
->mode
= canon_mode(mode
);
1029 hashcpy(spec
->sha1
, sha1
);
1030 spec
->sha1_valid
= !is_null_sha1(sha1
);
1035 * Given a name and sha1 pair, if the dircache tells us the file in
1036 * the work tree has that object contents, return true, so that
1037 * prepare_temp_file() does not have to inflate and extract.
1039 static int work_tree_matches(const char *name
, const unsigned char *sha1
)
1041 struct cache_entry
*ce
;
1045 /* We do not read the cache ourselves here, because the
1046 * benchmark with my previous version that always reads cache
1047 * shows that it makes things worse for diff-tree comparing
1048 * two linux-2.6 kernel trees in an already checked out work
1049 * tree. This is because most diff-tree comparisons deal with
1050 * only a small number of files, while reading the cache is
1051 * expensive for a large project, and its cost outweighs the
1052 * savings we get by not inflating the object to a temporary
1053 * file. Practically, this code only helps when we are used
1054 * by diff-cache --cached, which does read the cache before
1061 pos
= cache_name_pos(name
, len
);
1064 ce
= active_cache
[pos
];
1065 if ((lstat(name
, &st
) < 0) ||
1066 !S_ISREG(st
.st_mode
) || /* careful! */
1067 ce_match_stat(ce
, &st
, 0) ||
1068 hashcmp(sha1
, ce
->sha1
))
1070 /* we return 1 only when we can stat, it is a regular file,
1071 * stat information matches, and sha1 recorded in the cache
1072 * matches. I.e. we know the file in the work tree really is
1073 * the same as the <name, sha1> pair.
1078 static struct sha1_size_cache
{
1079 unsigned char sha1
[20];
1081 } **sha1_size_cache
;
1082 static int sha1_size_cache_nr
, sha1_size_cache_alloc
;
1084 static struct sha1_size_cache
*locate_size_cache(unsigned char *sha1
,
1089 struct sha1_size_cache
*e
;
1092 last
= sha1_size_cache_nr
;
1093 while (last
> first
) {
1094 int cmp
, next
= (last
+ first
) >> 1;
1095 e
= sha1_size_cache
[next
];
1096 cmp
= hashcmp(e
->sha1
, sha1
);
1108 /* insert to make it at "first" */
1109 if (sha1_size_cache_alloc
<= sha1_size_cache_nr
) {
1110 sha1_size_cache_alloc
= alloc_nr(sha1_size_cache_alloc
);
1111 sha1_size_cache
= xrealloc(sha1_size_cache
,
1112 sha1_size_cache_alloc
*
1113 sizeof(*sha1_size_cache
));
1115 sha1_size_cache_nr
++;
1116 if (first
< sha1_size_cache_nr
)
1117 memmove(sha1_size_cache
+ first
+ 1, sha1_size_cache
+ first
,
1118 (sha1_size_cache_nr
- first
- 1) *
1119 sizeof(*sha1_size_cache
));
1120 e
= xmalloc(sizeof(struct sha1_size_cache
));
1121 sha1_size_cache
[first
] = e
;
1122 hashcpy(e
->sha1
, sha1
);
1128 * While doing rename detection and pickaxe operation, we may need to
1129 * grab the data for the blob (or file) for our own in-core comparison.
1130 * diff_filespec has data and size fields for this purpose.
1132 int diff_populate_filespec(struct diff_filespec
*s
, int size_only
)
1135 if (!DIFF_FILE_VALID(s
))
1136 die("internal error: asking to populate invalid file.");
1137 if (S_ISDIR(s
->mode
))
1140 if (!use_size_cache
)
1145 if (!s
->sha1_valid
||
1146 work_tree_matches(s
->path
, s
->sha1
)) {
1149 if (lstat(s
->path
, &st
) < 0) {
1150 if (errno
== ENOENT
) {
1154 s
->data
= (char *)"";
1159 s
->size
= st
.st_size
;
1164 if (S_ISLNK(st
.st_mode
)) {
1166 s
->data
= xmalloc(s
->size
);
1168 ret
= readlink(s
->path
, s
->data
, s
->size
);
1175 fd
= open(s
->path
, O_RDONLY
);
1178 s
->data
= mmap(NULL
, s
->size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
1180 if (s
->data
== MAP_FAILED
)
1182 s
->should_munmap
= 1;
1186 struct sha1_size_cache
*e
;
1189 e
= locate_size_cache(s
->sha1
, 1, 0);
1194 if (!sha1_object_info(s
->sha1
, type
, &s
->size
))
1195 locate_size_cache(s
->sha1
, 0, s
->size
);
1198 s
->data
= read_sha1_file(s
->sha1
, type
, &s
->size
);
1205 void diff_free_filespec_data(struct diff_filespec
*s
)
1209 else if (s
->should_munmap
)
1210 munmap(s
->data
, s
->size
);
1211 s
->should_free
= s
->should_munmap
= 0;
1217 static void prep_temp_blob(struct diff_tempfile
*temp
,
1220 const unsigned char *sha1
,
1225 fd
= git_mkstemp(temp
->tmp_path
, TEMPFILE_PATH_LEN
, ".diff_XXXXXX");
1227 die("unable to create temp-file");
1228 if (write(fd
, blob
, size
) != size
)
1229 die("unable to write temp-file");
1231 temp
->name
= temp
->tmp_path
;
1232 strcpy(temp
->hex
, sha1_to_hex(sha1
));
1234 sprintf(temp
->mode
, "%06o", mode
);
1237 static void prepare_temp_file(const char *name
,
1238 struct diff_tempfile
*temp
,
1239 struct diff_filespec
*one
)
1241 if (!DIFF_FILE_VALID(one
)) {
1243 /* A '-' entry produces this for file-2, and
1244 * a '+' entry produces this for file-1.
1246 temp
->name
= "/dev/null";
1247 strcpy(temp
->hex
, ".");
1248 strcpy(temp
->mode
, ".");
1252 if (!one
->sha1_valid
||
1253 work_tree_matches(name
, one
->sha1
)) {
1255 if (lstat(name
, &st
) < 0) {
1256 if (errno
== ENOENT
)
1257 goto not_a_valid_file
;
1258 die("stat(%s): %s", name
, strerror(errno
));
1260 if (S_ISLNK(st
.st_mode
)) {
1262 char buf
[PATH_MAX
+ 1]; /* ought to be SYMLINK_MAX */
1263 if (sizeof(buf
) <= st
.st_size
)
1264 die("symlink too long: %s", name
);
1265 ret
= readlink(name
, buf
, st
.st_size
);
1267 die("readlink(%s)", name
);
1268 prep_temp_blob(temp
, buf
, st
.st_size
,
1270 one
->sha1
: null_sha1
),
1272 one
->mode
: S_IFLNK
));
1275 /* we can borrow from the file in the work tree */
1277 if (!one
->sha1_valid
)
1278 strcpy(temp
->hex
, sha1_to_hex(null_sha1
));
1280 strcpy(temp
->hex
, sha1_to_hex(one
->sha1
));
1281 /* Even though we may sometimes borrow the
1282 * contents from the work tree, we always want
1283 * one->mode. mode is trustworthy even when
1284 * !(one->sha1_valid), as long as
1285 * DIFF_FILE_VALID(one).
1287 sprintf(temp
->mode
, "%06o", one
->mode
);
1292 if (diff_populate_filespec(one
, 0))
1293 die("cannot read data blob for %s", one
->path
);
1294 prep_temp_blob(temp
, one
->data
, one
->size
,
1295 one
->sha1
, one
->mode
);
1299 static void remove_tempfile(void)
1303 for (i
= 0; i
< 2; i
++)
1304 if (diff_temp
[i
].name
== diff_temp
[i
].tmp_path
) {
1305 unlink(diff_temp
[i
].name
);
1306 diff_temp
[i
].name
= NULL
;
1310 static void remove_tempfile_on_signal(int signo
)
1313 signal(SIGINT
, SIG_DFL
);
1317 static int spawn_prog(const char *pgm
, const char **arg
)
1325 die("unable to fork");
1327 execvp(pgm
, (char *const*) arg
);
1331 while (waitpid(pid
, &status
, 0) < 0) {
1337 /* Earlier we did not check the exit status because
1338 * diff exits non-zero if files are different, and
1339 * we are not interested in knowing that. It was a
1340 * mistake which made it harder to quit a diff-*
1341 * session that uses the git-apply-patch-script as
1342 * the GIT_EXTERNAL_DIFF. A custom GIT_EXTERNAL_DIFF
1343 * should also exit non-zero only when it wants to
1344 * abort the entire diff-* session.
1346 if (WIFEXITED(status
) && !WEXITSTATUS(status
))
1351 /* An external diff command takes:
1353 * diff-cmd name infile1 infile1-sha1 infile1-mode \
1354 * infile2 infile2-sha1 infile2-mode [ rename-to ]
1357 static void run_external_diff(const char *pgm
,
1360 struct diff_filespec
*one
,
1361 struct diff_filespec
*two
,
1362 const char *xfrm_msg
,
1363 int complete_rewrite
)
1365 const char *spawn_arg
[10];
1366 struct diff_tempfile
*temp
= diff_temp
;
1368 static int atexit_asked
= 0;
1369 const char *othername
;
1370 const char **arg
= &spawn_arg
[0];
1372 othername
= (other
? other
: name
);
1374 prepare_temp_file(name
, &temp
[0], one
);
1375 prepare_temp_file(othername
, &temp
[1], two
);
1376 if (! atexit_asked
&&
1377 (temp
[0].name
== temp
[0].tmp_path
||
1378 temp
[1].name
== temp
[1].tmp_path
)) {
1380 atexit(remove_tempfile
);
1382 signal(SIGINT
, remove_tempfile_on_signal
);
1388 *arg
++ = temp
[0].name
;
1389 *arg
++ = temp
[0].hex
;
1390 *arg
++ = temp
[0].mode
;
1391 *arg
++ = temp
[1].name
;
1392 *arg
++ = temp
[1].hex
;
1393 *arg
++ = temp
[1].mode
;
1403 retval
= spawn_prog(pgm
, spawn_arg
);
1406 fprintf(stderr
, "external diff died, stopping at %s.\n", name
);
1411 static void run_diff_cmd(const char *pgm
,
1414 struct diff_filespec
*one
,
1415 struct diff_filespec
*two
,
1416 const char *xfrm_msg
,
1417 struct diff_options
*o
,
1418 int complete_rewrite
)
1421 run_external_diff(pgm
, name
, other
, one
, two
, xfrm_msg
,
1426 builtin_diff(name
, other
? other
: name
,
1427 one
, two
, xfrm_msg
, o
, complete_rewrite
);
1429 printf("* Unmerged path %s\n", name
);
1432 static void diff_fill_sha1_info(struct diff_filespec
*one
)
1434 if (DIFF_FILE_VALID(one
)) {
1435 if (!one
->sha1_valid
) {
1437 if (lstat(one
->path
, &st
) < 0)
1438 die("stat %s", one
->path
);
1439 if (index_path(one
->sha1
, one
->path
, &st
, 0))
1440 die("cannot hash %s\n", one
->path
);
1447 static void run_diff(struct diff_filepair
*p
, struct diff_options
*o
)
1449 const char *pgm
= external_diff();
1450 char msg
[PATH_MAX
*2+300], *xfrm_msg
;
1451 struct diff_filespec
*one
;
1452 struct diff_filespec
*two
;
1455 char *name_munged
, *other_munged
;
1456 int complete_rewrite
= 0;
1459 if (DIFF_PAIR_UNMERGED(p
)) {
1461 run_diff_cmd(pgm
, p
->one
->path
, NULL
, NULL
, NULL
, NULL
, o
, 0);
1465 name
= p
->one
->path
;
1466 other
= (strcmp(name
, p
->two
->path
) ? p
->two
->path
: NULL
);
1467 name_munged
= quote_one(name
);
1468 other_munged
= quote_one(other
);
1469 one
= p
->one
; two
= p
->two
;
1471 diff_fill_sha1_info(one
);
1472 diff_fill_sha1_info(two
);
1475 switch (p
->status
) {
1476 case DIFF_STATUS_COPIED
:
1477 len
+= snprintf(msg
+ len
, sizeof(msg
) - len
,
1478 "similarity index %d%%\n"
1481 (int)(0.5 + p
->score
* 100.0/MAX_SCORE
),
1482 name_munged
, other_munged
);
1484 case DIFF_STATUS_RENAMED
:
1485 len
+= snprintf(msg
+ len
, sizeof(msg
) - len
,
1486 "similarity index %d%%\n"
1489 (int)(0.5 + p
->score
* 100.0/MAX_SCORE
),
1490 name_munged
, other_munged
);
1492 case DIFF_STATUS_MODIFIED
:
1494 len
+= snprintf(msg
+ len
, sizeof(msg
) - len
,
1495 "dissimilarity index %d%%\n",
1496 (int)(0.5 + p
->score
*
1498 complete_rewrite
= 1;
1507 if (hashcmp(one
->sha1
, two
->sha1
)) {
1508 int abbrev
= o
->full_index
? 40 : DEFAULT_ABBREV
;
1512 if ((!fill_mmfile(&mf
, one
) && mmfile_is_binary(&mf
)) ||
1513 (!fill_mmfile(&mf
, two
) && mmfile_is_binary(&mf
)))
1516 len
+= snprintf(msg
+ len
, sizeof(msg
) - len
,
1518 abbrev
, sha1_to_hex(one
->sha1
),
1519 abbrev
, sha1_to_hex(two
->sha1
));
1520 if (one
->mode
== two
->mode
)
1521 len
+= snprintf(msg
+ len
, sizeof(msg
) - len
,
1522 " %06o", one
->mode
);
1523 len
+= snprintf(msg
+ len
, sizeof(msg
) - len
, "\n");
1528 xfrm_msg
= len
? msg
: NULL
;
1531 DIFF_FILE_VALID(one
) && DIFF_FILE_VALID(two
) &&
1532 (S_IFMT
& one
->mode
) != (S_IFMT
& two
->mode
)) {
1533 /* a filepair that changes between file and symlink
1534 * needs to be split into deletion and creation.
1536 struct diff_filespec
*null
= alloc_filespec(two
->path
);
1537 run_diff_cmd(NULL
, name
, other
, one
, null
, xfrm_msg
, o
, 0);
1539 null
= alloc_filespec(one
->path
);
1540 run_diff_cmd(NULL
, name
, other
, null
, two
, xfrm_msg
, o
, 0);
1544 run_diff_cmd(pgm
, name
, other
, one
, two
, xfrm_msg
, o
,
1551 static void run_diffstat(struct diff_filepair
*p
, struct diff_options
*o
,
1552 struct diffstat_t
*diffstat
)
1556 int complete_rewrite
= 0;
1558 if (DIFF_PAIR_UNMERGED(p
)) {
1560 builtin_diffstat(p
->one
->path
, NULL
, NULL
, NULL
, diffstat
, o
, 0);
1564 name
= p
->one
->path
;
1565 other
= (strcmp(name
, p
->two
->path
) ? p
->two
->path
: NULL
);
1567 diff_fill_sha1_info(p
->one
);
1568 diff_fill_sha1_info(p
->two
);
1570 if (p
->status
== DIFF_STATUS_MODIFIED
&& p
->score
)
1571 complete_rewrite
= 1;
1572 builtin_diffstat(name
, other
, p
->one
, p
->two
, diffstat
, o
, complete_rewrite
);
1575 static void run_checkdiff(struct diff_filepair
*p
, struct diff_options
*o
)
1580 if (DIFF_PAIR_UNMERGED(p
)) {
1585 name
= p
->one
->path
;
1586 other
= (strcmp(name
, p
->two
->path
) ? p
->two
->path
: NULL
);
1588 diff_fill_sha1_info(p
->one
);
1589 diff_fill_sha1_info(p
->two
);
1591 builtin_checkdiff(name
, other
, p
->one
, p
->two
);
1594 void diff_setup(struct diff_options
*options
)
1596 memset(options
, 0, sizeof(*options
));
1597 options
->line_termination
= '\n';
1598 options
->break_opt
= -1;
1599 options
->rename_limit
= -1;
1600 options
->context
= 3;
1601 options
->msg_sep
= "";
1603 options
->change
= diff_change
;
1604 options
->add_remove
= diff_addremove
;
1605 options
->color_diff
= diff_use_color_default
;
1606 options
->detect_rename
= diff_detect_rename_default
;
1609 int diff_setup_done(struct diff_options
*options
)
1613 if (options
->output_format
& DIFF_FORMAT_NAME
)
1615 if (options
->output_format
& DIFF_FORMAT_NAME_STATUS
)
1617 if (options
->output_format
& DIFF_FORMAT_CHECKDIFF
)
1619 if (options
->output_format
& DIFF_FORMAT_NO_OUTPUT
)
1622 die("--name-only, --name-status, --check and -s are mutually exclusive");
1624 if (options
->find_copies_harder
)
1625 options
->detect_rename
= DIFF_DETECT_COPY
;
1627 if (options
->output_format
& (DIFF_FORMAT_NAME
|
1628 DIFF_FORMAT_NAME_STATUS
|
1629 DIFF_FORMAT_CHECKDIFF
|
1630 DIFF_FORMAT_NO_OUTPUT
))
1631 options
->output_format
&= ~(DIFF_FORMAT_RAW
|
1632 DIFF_FORMAT_DIFFSTAT
|
1633 DIFF_FORMAT_SUMMARY
|
1637 * These cases always need recursive; we do not drop caller-supplied
1638 * recursive bits for other formats here.
1640 if (options
->output_format
& (DIFF_FORMAT_PATCH
|
1641 DIFF_FORMAT_DIFFSTAT
|
1642 DIFF_FORMAT_CHECKDIFF
))
1643 options
->recursive
= 1;
1645 * Also pickaxe would not work very well if you do not say recursive
1647 if (options
->pickaxe
)
1648 options
->recursive
= 1;
1650 if (options
->detect_rename
&& options
->rename_limit
< 0)
1651 options
->rename_limit
= diff_rename_limit_default
;
1652 if (options
->setup
& DIFF_SETUP_USE_CACHE
) {
1654 /* read-cache does not die even when it fails
1655 * so it is safe for us to do this here. Also
1656 * it does not smudge active_cache or active_nr
1657 * when it fails, so we do not have to worry about
1658 * cleaning it up ourselves either.
1662 if (options
->setup
& DIFF_SETUP_USE_SIZE_CACHE
)
1664 if (options
->abbrev
<= 0 || 40 < options
->abbrev
)
1665 options
->abbrev
= 40; /* full */
1670 static int opt_arg(const char *arg
, int arg_short
, const char *arg_long
, int *val
)
1680 if (c
== arg_short
) {
1684 if (val
&& isdigit(c
)) {
1686 int n
= strtoul(arg
, &end
, 10);
1697 eq
= strchr(arg
, '=');
1702 if (!len
|| strncmp(arg
, arg_long
, len
))
1707 if (!isdigit(*++eq
))
1709 n
= strtoul(eq
, &end
, 10);
1717 int diff_opt_parse(struct diff_options
*options
, const char **av
, int ac
)
1719 const char *arg
= av
[0];
1720 if (!strcmp(arg
, "-p") || !strcmp(arg
, "-u"))
1721 options
->output_format
|= DIFF_FORMAT_PATCH
;
1722 else if (opt_arg(arg
, 'U', "unified", &options
->context
))
1723 options
->output_format
|= DIFF_FORMAT_PATCH
;
1724 else if (!strcmp(arg
, "--raw"))
1725 options
->output_format
|= DIFF_FORMAT_RAW
;
1726 else if (!strcmp(arg
, "--patch-with-raw")) {
1727 options
->output_format
|= DIFF_FORMAT_PATCH
| DIFF_FORMAT_RAW
;
1729 else if (!strcmp(arg
, "--stat"))
1730 options
->output_format
|= DIFF_FORMAT_DIFFSTAT
;
1731 else if (!strncmp(arg
, "--stat-width=", 13)) {
1732 options
->stat_width
= strtoul(arg
+ 13, NULL
, 10);
1733 options
->output_format
|= DIFF_FORMAT_DIFFSTAT
;
1735 else if (!strncmp(arg
, "--stat-name-width=", 18)) {
1736 options
->stat_name_width
= strtoul(arg
+ 18, NULL
, 10);
1737 options
->output_format
|= DIFF_FORMAT_DIFFSTAT
;
1739 else if (!strcmp(arg
, "--check"))
1740 options
->output_format
|= DIFF_FORMAT_CHECKDIFF
;
1741 else if (!strcmp(arg
, "--summary"))
1742 options
->output_format
|= DIFF_FORMAT_SUMMARY
;
1743 else if (!strcmp(arg
, "--patch-with-stat")) {
1744 options
->output_format
|= DIFF_FORMAT_PATCH
| DIFF_FORMAT_DIFFSTAT
;
1746 else if (!strcmp(arg
, "-z"))
1747 options
->line_termination
= 0;
1748 else if (!strncmp(arg
, "-l", 2))
1749 options
->rename_limit
= strtoul(arg
+2, NULL
, 10);
1750 else if (!strcmp(arg
, "--full-index"))
1751 options
->full_index
= 1;
1752 else if (!strcmp(arg
, "--binary")) {
1753 options
->output_format
|= DIFF_FORMAT_PATCH
;
1754 options
->binary
= 1;
1756 else if (!strcmp(arg
, "-a") || !strcmp(arg
, "--text")) {
1759 else if (!strcmp(arg
, "--name-only"))
1760 options
->output_format
|= DIFF_FORMAT_NAME
;
1761 else if (!strcmp(arg
, "--name-status"))
1762 options
->output_format
|= DIFF_FORMAT_NAME_STATUS
;
1763 else if (!strcmp(arg
, "-R"))
1764 options
->reverse_diff
= 1;
1765 else if (!strncmp(arg
, "-S", 2))
1766 options
->pickaxe
= arg
+ 2;
1767 else if (!strcmp(arg
, "-s")) {
1768 options
->output_format
|= DIFF_FORMAT_NO_OUTPUT
;
1770 else if (!strncmp(arg
, "-O", 2))
1771 options
->orderfile
= arg
+ 2;
1772 else if (!strncmp(arg
, "--diff-filter=", 14))
1773 options
->filter
= arg
+ 14;
1774 else if (!strcmp(arg
, "--pickaxe-all"))
1775 options
->pickaxe_opts
= DIFF_PICKAXE_ALL
;
1776 else if (!strcmp(arg
, "--pickaxe-regex"))
1777 options
->pickaxe_opts
= DIFF_PICKAXE_REGEX
;
1778 else if (!strncmp(arg
, "-B", 2)) {
1779 if ((options
->break_opt
=
1780 diff_scoreopt_parse(arg
)) == -1)
1783 else if (!strncmp(arg
, "-M", 2)) {
1784 if ((options
->rename_score
=
1785 diff_scoreopt_parse(arg
)) == -1)
1787 options
->detect_rename
= DIFF_DETECT_RENAME
;
1789 else if (!strncmp(arg
, "-C", 2)) {
1790 if ((options
->rename_score
=
1791 diff_scoreopt_parse(arg
)) == -1)
1793 options
->detect_rename
= DIFF_DETECT_COPY
;
1795 else if (!strcmp(arg
, "--find-copies-harder"))
1796 options
->find_copies_harder
= 1;
1797 else if (!strcmp(arg
, "--abbrev"))
1798 options
->abbrev
= DEFAULT_ABBREV
;
1799 else if (!strncmp(arg
, "--abbrev=", 9)) {
1800 options
->abbrev
= strtoul(arg
+ 9, NULL
, 10);
1801 if (options
->abbrev
< MINIMUM_ABBREV
)
1802 options
->abbrev
= MINIMUM_ABBREV
;
1803 else if (40 < options
->abbrev
)
1804 options
->abbrev
= 40;
1806 else if (!strcmp(arg
, "--color"))
1807 options
->color_diff
= 1;
1808 else if (!strcmp(arg
, "--no-color"))
1809 options
->color_diff
= 0;
1810 else if (!strcmp(arg
, "-w") || !strcmp(arg
, "--ignore-all-space"))
1811 options
->xdl_opts
|= XDF_IGNORE_WHITESPACE
;
1812 else if (!strcmp(arg
, "-b") || !strcmp(arg
, "--ignore-space-change"))
1813 options
->xdl_opts
|= XDF_IGNORE_WHITESPACE_CHANGE
;
1814 else if (!strcmp(arg
, "--color-words"))
1815 options
->color_diff
= options
->color_diff_words
= 1;
1816 else if (!strcmp(arg
, "--no-renames"))
1817 options
->detect_rename
= 0;
1823 static int parse_num(const char **cp_p
)
1825 unsigned long num
, scale
;
1827 const char *cp
= *cp_p
;
1834 if ( !dot
&& ch
== '.' ) {
1837 } else if ( ch
== '%' ) {
1838 scale
= dot
? scale
*100 : 100;
1839 cp
++; /* % is always at the end */
1841 } else if ( ch
>= '0' && ch
<= '9' ) {
1842 if ( scale
< 100000 ) {
1844 num
= (num
*10) + (ch
-'0');
1853 /* user says num divided by scale and we say internally that
1854 * is MAX_SCORE * num / scale.
1856 return (num
>= scale
) ? MAX_SCORE
: (MAX_SCORE
* num
/ scale
);
1859 int diff_scoreopt_parse(const char *opt
)
1861 int opt1
, opt2
, cmd
;
1866 if (cmd
!= 'M' && cmd
!= 'C' && cmd
!= 'B')
1867 return -1; /* that is not a -M, -C nor -B option */
1869 opt1
= parse_num(&opt
);
1875 else if (*opt
!= '/')
1876 return -1; /* we expect -B80/99 or -B80 */
1879 opt2
= parse_num(&opt
);
1884 return opt1
| (opt2
<< 16);
1887 struct diff_queue_struct diff_queued_diff
;
1889 void diff_q(struct diff_queue_struct
*queue
, struct diff_filepair
*dp
)
1891 if (queue
->alloc
<= queue
->nr
) {
1892 queue
->alloc
= alloc_nr(queue
->alloc
);
1893 queue
->queue
= xrealloc(queue
->queue
,
1894 sizeof(dp
) * queue
->alloc
);
1896 queue
->queue
[queue
->nr
++] = dp
;
1899 struct diff_filepair
*diff_queue(struct diff_queue_struct
*queue
,
1900 struct diff_filespec
*one
,
1901 struct diff_filespec
*two
)
1903 struct diff_filepair
*dp
= xcalloc(1, sizeof(*dp
));
1911 void diff_free_filepair(struct diff_filepair
*p
)
1913 diff_free_filespec_data(p
->one
);
1914 diff_free_filespec_data(p
->two
);
1920 /* This is different from find_unique_abbrev() in that
1921 * it stuffs the result with dots for alignment.
1923 const char *diff_unique_abbrev(const unsigned char *sha1
, int len
)
1928 return sha1_to_hex(sha1
);
1930 abbrev
= find_unique_abbrev(sha1
, len
);
1932 return sha1_to_hex(sha1
);
1933 abblen
= strlen(abbrev
);
1935 static char hex
[41];
1936 if (len
< abblen
&& abblen
<= len
+ 2)
1937 sprintf(hex
, "%s%.*s", abbrev
, len
+3-abblen
, "..");
1939 sprintf(hex
, "%s...", abbrev
);
1942 return sha1_to_hex(sha1
);
1945 static void diff_flush_raw(struct diff_filepair
*p
,
1946 struct diff_options
*options
)
1950 int abbrev
= options
->abbrev
;
1951 const char *path_one
, *path_two
;
1952 int inter_name_termination
= '\t';
1953 int line_termination
= options
->line_termination
;
1955 if (!line_termination
)
1956 inter_name_termination
= 0;
1958 path_one
= p
->one
->path
;
1959 path_two
= p
->two
->path
;
1960 if (line_termination
) {
1961 path_one
= quote_one(path_one
);
1962 path_two
= quote_one(path_two
);
1966 sprintf(status
, "%c%03d", p
->status
,
1967 (int)(0.5 + p
->score
* 100.0/MAX_SCORE
));
1969 status
[0] = p
->status
;
1972 switch (p
->status
) {
1973 case DIFF_STATUS_COPIED
:
1974 case DIFF_STATUS_RENAMED
:
1977 case DIFF_STATUS_ADDED
:
1978 case DIFF_STATUS_DELETED
:
1985 if (!(options
->output_format
& DIFF_FORMAT_NAME_STATUS
)) {
1986 printf(":%06o %06o %s ",
1987 p
->one
->mode
, p
->two
->mode
,
1988 diff_unique_abbrev(p
->one
->sha1
, abbrev
));
1990 diff_unique_abbrev(p
->two
->sha1
, abbrev
));
1992 printf("%s%c%s", status
, inter_name_termination
, path_one
);
1994 printf("%c%s", inter_name_termination
, path_two
);
1995 putchar(line_termination
);
1996 if (path_one
!= p
->one
->path
)
1997 free((void*)path_one
);
1998 if (path_two
!= p
->two
->path
)
1999 free((void*)path_two
);
2002 static void diff_flush_name(struct diff_filepair
*p
, int line_termination
)
2004 char *path
= p
->two
->path
;
2006 if (line_termination
)
2007 path
= quote_one(p
->two
->path
);
2008 printf("%s%c", path
, line_termination
);
2009 if (p
->two
->path
!= path
)
2013 int diff_unmodified_pair(struct diff_filepair
*p
)
2015 /* This function is written stricter than necessary to support
2016 * the currently implemented transformers, but the idea is to
2017 * let transformers to produce diff_filepairs any way they want,
2018 * and filter and clean them up here before producing the output.
2020 struct diff_filespec
*one
, *two
;
2022 if (DIFF_PAIR_UNMERGED(p
))
2023 return 0; /* unmerged is interesting */
2028 /* deletion, addition, mode or type change
2029 * and rename are all interesting.
2031 if (DIFF_FILE_VALID(one
) != DIFF_FILE_VALID(two
) ||
2032 DIFF_PAIR_MODE_CHANGED(p
) ||
2033 strcmp(one
->path
, two
->path
))
2036 /* both are valid and point at the same path. that is, we are
2037 * dealing with a change.
2039 if (one
->sha1_valid
&& two
->sha1_valid
&&
2040 !hashcmp(one
->sha1
, two
->sha1
))
2041 return 1; /* no change */
2042 if (!one
->sha1_valid
&& !two
->sha1_valid
)
2043 return 1; /* both look at the same file on the filesystem. */
2047 static void diff_flush_patch(struct diff_filepair
*p
, struct diff_options
*o
)
2049 if (diff_unmodified_pair(p
))
2052 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
2053 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
2054 return; /* no tree diffs in patch format */
2059 static void diff_flush_stat(struct diff_filepair
*p
, struct diff_options
*o
,
2060 struct diffstat_t
*diffstat
)
2062 if (diff_unmodified_pair(p
))
2065 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
2066 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
2067 return; /* no tree diffs in patch format */
2069 run_diffstat(p
, o
, diffstat
);
2072 static void diff_flush_checkdiff(struct diff_filepair
*p
,
2073 struct diff_options
*o
)
2075 if (diff_unmodified_pair(p
))
2078 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
2079 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
2080 return; /* no tree diffs in patch format */
2082 run_checkdiff(p
, o
);
2085 int diff_queue_is_empty(void)
2087 struct diff_queue_struct
*q
= &diff_queued_diff
;
2089 for (i
= 0; i
< q
->nr
; i
++)
2090 if (!diff_unmodified_pair(q
->queue
[i
]))
2096 void diff_debug_filespec(struct diff_filespec
*s
, int x
, const char *one
)
2098 fprintf(stderr
, "queue[%d] %s (%s) %s %06o %s\n",
2101 DIFF_FILE_VALID(s
) ? "valid" : "invalid",
2103 s
->sha1_valid
? sha1_to_hex(s
->sha1
) : "");
2104 fprintf(stderr
, "queue[%d] %s size %lu flags %d\n",
2106 s
->size
, s
->xfrm_flags
);
2109 void diff_debug_filepair(const struct diff_filepair
*p
, int i
)
2111 diff_debug_filespec(p
->one
, i
, "one");
2112 diff_debug_filespec(p
->two
, i
, "two");
2113 fprintf(stderr
, "score %d, status %c stays %d broken %d\n",
2114 p
->score
, p
->status
? p
->status
: '?',
2115 p
->source_stays
, p
->broken_pair
);
2118 void diff_debug_queue(const char *msg
, struct diff_queue_struct
*q
)
2122 fprintf(stderr
, "%s\n", msg
);
2123 fprintf(stderr
, "q->nr = %d\n", q
->nr
);
2124 for (i
= 0; i
< q
->nr
; i
++) {
2125 struct diff_filepair
*p
= q
->queue
[i
];
2126 diff_debug_filepair(p
, i
);
2131 static void diff_resolve_rename_copy(void)
2134 struct diff_filepair
*p
, *pp
;
2135 struct diff_queue_struct
*q
= &diff_queued_diff
;
2137 diff_debug_queue("resolve-rename-copy", q
);
2139 for (i
= 0; i
< q
->nr
; i
++) {
2141 p
->status
= 0; /* undecided */
2142 if (DIFF_PAIR_UNMERGED(p
))
2143 p
->status
= DIFF_STATUS_UNMERGED
;
2144 else if (!DIFF_FILE_VALID(p
->one
))
2145 p
->status
= DIFF_STATUS_ADDED
;
2146 else if (!DIFF_FILE_VALID(p
->two
))
2147 p
->status
= DIFF_STATUS_DELETED
;
2148 else if (DIFF_PAIR_TYPE_CHANGED(p
))
2149 p
->status
= DIFF_STATUS_TYPE_CHANGED
;
2151 /* from this point on, we are dealing with a pair
2152 * whose both sides are valid and of the same type, i.e.
2153 * either in-place edit or rename/copy edit.
2155 else if (DIFF_PAIR_RENAME(p
)) {
2156 if (p
->source_stays
) {
2157 p
->status
= DIFF_STATUS_COPIED
;
2160 /* See if there is some other filepair that
2161 * copies from the same source as us. If so
2162 * we are a copy. Otherwise we are either a
2163 * copy if the path stays, or a rename if it
2164 * does not, but we already handled "stays" case.
2166 for (j
= i
+ 1; j
< q
->nr
; j
++) {
2168 if (strcmp(pp
->one
->path
, p
->one
->path
))
2169 continue; /* not us */
2170 if (!DIFF_PAIR_RENAME(pp
))
2171 continue; /* not a rename/copy */
2172 /* pp is a rename/copy from the same source */
2173 p
->status
= DIFF_STATUS_COPIED
;
2177 p
->status
= DIFF_STATUS_RENAMED
;
2179 else if (hashcmp(p
->one
->sha1
, p
->two
->sha1
) ||
2180 p
->one
->mode
!= p
->two
->mode
)
2181 p
->status
= DIFF_STATUS_MODIFIED
;
2183 /* This is a "no-change" entry and should not
2184 * happen anymore, but prepare for broken callers.
2186 error("feeding unmodified %s to diffcore",
2188 p
->status
= DIFF_STATUS_UNKNOWN
;
2191 diff_debug_queue("resolve-rename-copy done", q
);
2194 static int check_pair_status(struct diff_filepair
*p
)
2196 switch (p
->status
) {
2197 case DIFF_STATUS_UNKNOWN
:
2200 die("internal error in diff-resolve-rename-copy");
2206 static void flush_one_pair(struct diff_filepair
*p
, struct diff_options
*opt
)
2208 int fmt
= opt
->output_format
;
2210 if (fmt
& DIFF_FORMAT_CHECKDIFF
)
2211 diff_flush_checkdiff(p
, opt
);
2212 else if (fmt
& (DIFF_FORMAT_RAW
| DIFF_FORMAT_NAME_STATUS
))
2213 diff_flush_raw(p
, opt
);
2214 else if (fmt
& DIFF_FORMAT_NAME
)
2215 diff_flush_name(p
, opt
->line_termination
);
2218 static void show_file_mode_name(const char *newdelete
, struct diff_filespec
*fs
)
2221 printf(" %s mode %06o %s\n", newdelete
, fs
->mode
, fs
->path
);
2223 printf(" %s %s\n", newdelete
, fs
->path
);
2227 static void show_mode_change(struct diff_filepair
*p
, int show_name
)
2229 if (p
->one
->mode
&& p
->two
->mode
&& p
->one
->mode
!= p
->two
->mode
) {
2231 printf(" mode change %06o => %06o %s\n",
2232 p
->one
->mode
, p
->two
->mode
, p
->two
->path
);
2234 printf(" mode change %06o => %06o\n",
2235 p
->one
->mode
, p
->two
->mode
);
2239 static void show_rename_copy(const char *renamecopy
, struct diff_filepair
*p
)
2241 const char *old
, *new;
2243 /* Find common prefix */
2247 const char *slash_old
, *slash_new
;
2248 slash_old
= strchr(old
, '/');
2249 slash_new
= strchr(new, '/');
2252 slash_old
- old
!= slash_new
- new ||
2253 memcmp(old
, new, slash_new
- new))
2255 old
= slash_old
+ 1;
2256 new = slash_new
+ 1;
2258 /* p->one->path thru old is the common prefix, and old and new
2259 * through the end of names are renames
2261 if (old
!= p
->one
->path
)
2262 printf(" %s %.*s{%s => %s} (%d%%)\n", renamecopy
,
2263 (int)(old
- p
->one
->path
), p
->one
->path
,
2264 old
, new, (int)(0.5 + p
->score
* 100.0/MAX_SCORE
));
2266 printf(" %s %s => %s (%d%%)\n", renamecopy
,
2267 p
->one
->path
, p
->two
->path
,
2268 (int)(0.5 + p
->score
* 100.0/MAX_SCORE
));
2269 show_mode_change(p
, 0);
2272 static void diff_summary(struct diff_filepair
*p
)
2275 case DIFF_STATUS_DELETED
:
2276 show_file_mode_name("delete", p
->one
);
2278 case DIFF_STATUS_ADDED
:
2279 show_file_mode_name("create", p
->two
);
2281 case DIFF_STATUS_COPIED
:
2282 show_rename_copy("copy", p
);
2284 case DIFF_STATUS_RENAMED
:
2285 show_rename_copy("rename", p
);
2289 printf(" rewrite %s (%d%%)\n", p
->two
->path
,
2290 (int)(0.5 + p
->score
* 100.0/MAX_SCORE
));
2291 show_mode_change(p
, 0);
2292 } else show_mode_change(p
, 1);
2298 struct xdiff_emit_state xm
;
2303 static int remove_space(char *line
, int len
)
2309 for (i
= 0; i
< len
; i
++)
2310 if (!isspace((c
= line
[i
])))
2316 static void patch_id_consume(void *priv
, char *line
, unsigned long len
)
2318 struct patch_id_t
*data
= priv
;
2321 /* Ignore line numbers when computing the SHA1 of the patch */
2322 if (!strncmp(line
, "@@ -", 4))
2325 new_len
= remove_space(line
, len
);
2327 SHA1_Update(data
->ctx
, line
, new_len
);
2328 data
->patchlen
+= new_len
;
2331 /* returns 0 upon success, and writes result into sha1 */
2332 static int diff_get_patch_id(struct diff_options
*options
, unsigned char *sha1
)
2334 struct diff_queue_struct
*q
= &diff_queued_diff
;
2337 struct patch_id_t data
;
2338 char buffer
[PATH_MAX
* 4 + 20];
2341 memset(&data
, 0, sizeof(struct patch_id_t
));
2343 data
.xm
.consume
= patch_id_consume
;
2345 for (i
= 0; i
< q
->nr
; i
++) {
2350 struct diff_filepair
*p
= q
->queue
[i
];
2354 return error("internal diff status error");
2355 if (p
->status
== DIFF_STATUS_UNKNOWN
)
2357 if (diff_unmodified_pair(p
))
2359 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
2360 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
2362 if (DIFF_PAIR_UNMERGED(p
))
2365 diff_fill_sha1_info(p
->one
);
2366 diff_fill_sha1_info(p
->two
);
2367 if (fill_mmfile(&mf1
, p
->one
) < 0 ||
2368 fill_mmfile(&mf2
, p
->two
) < 0)
2369 return error("unable to read files to diff");
2371 /* Maybe hash p->two? into the patch id? */
2372 if (mmfile_is_binary(&mf2
))
2375 len1
= remove_space(p
->one
->path
, strlen(p
->one
->path
));
2376 len2
= remove_space(p
->two
->path
, strlen(p
->two
->path
));
2377 if (p
->one
->mode
== 0)
2378 len1
= snprintf(buffer
, sizeof(buffer
),
2379 "diff--gita/%.*sb/%.*s"
2386 len2
, p
->two
->path
);
2387 else if (p
->two
->mode
== 0)
2388 len1
= snprintf(buffer
, sizeof(buffer
),
2389 "diff--gita/%.*sb/%.*s"
2390 "deletedfilemode%06o"
2396 len1
, p
->one
->path
);
2398 len1
= snprintf(buffer
, sizeof(buffer
),
2399 "diff--gita/%.*sb/%.*s"
2405 len2
, p
->two
->path
);
2406 SHA1_Update(&ctx
, buffer
, len1
);
2408 xpp
.flags
= XDF_NEED_MINIMAL
;
2410 xecfg
.flags
= XDL_EMIT_FUNCNAMES
;
2411 ecb
.outf
= xdiff_outf
;
2413 xdl_diff(&mf1
, &mf2
, &xpp
, &xecfg
, &ecb
);
2416 SHA1_Final(sha1
, &ctx
);
2420 int diff_flush_patch_id(struct diff_options
*options
, unsigned char *sha1
)
2422 struct diff_queue_struct
*q
= &diff_queued_diff
;
2424 int result
= diff_get_patch_id(options
, sha1
);
2426 for (i
= 0; i
< q
->nr
; i
++)
2427 diff_free_filepair(q
->queue
[i
]);
2431 q
->nr
= q
->alloc
= 0;
2436 static int is_summary_empty(const struct diff_queue_struct
*q
)
2440 for (i
= 0; i
< q
->nr
; i
++) {
2441 const struct diff_filepair
*p
= q
->queue
[i
];
2443 switch (p
->status
) {
2444 case DIFF_STATUS_DELETED
:
2445 case DIFF_STATUS_ADDED
:
2446 case DIFF_STATUS_COPIED
:
2447 case DIFF_STATUS_RENAMED
:
2452 if (p
->one
->mode
&& p
->two
->mode
&&
2453 p
->one
->mode
!= p
->two
->mode
)
2461 void diff_flush(struct diff_options
*options
)
2463 struct diff_queue_struct
*q
= &diff_queued_diff
;
2464 int i
, output_format
= options
->output_format
;
2468 * Order: raw, stat, summary, patch
2469 * or: name/name-status/checkdiff (other bits clear)
2474 if (output_format
& (DIFF_FORMAT_RAW
|
2476 DIFF_FORMAT_NAME_STATUS
|
2477 DIFF_FORMAT_CHECKDIFF
)) {
2478 for (i
= 0; i
< q
->nr
; i
++) {
2479 struct diff_filepair
*p
= q
->queue
[i
];
2480 if (check_pair_status(p
))
2481 flush_one_pair(p
, options
);
2486 if (output_format
& DIFF_FORMAT_DIFFSTAT
) {
2487 struct diffstat_t diffstat
;
2489 memset(&diffstat
, 0, sizeof(struct diffstat_t
));
2490 diffstat
.xm
.consume
= diffstat_consume
;
2491 for (i
= 0; i
< q
->nr
; i
++) {
2492 struct diff_filepair
*p
= q
->queue
[i
];
2493 if (check_pair_status(p
))
2494 diff_flush_stat(p
, options
, &diffstat
);
2496 show_stats(&diffstat
, options
);
2500 if (output_format
& DIFF_FORMAT_SUMMARY
&& !is_summary_empty(q
)) {
2501 for (i
= 0; i
< q
->nr
; i
++)
2502 diff_summary(q
->queue
[i
]);
2506 if (output_format
& DIFF_FORMAT_PATCH
) {
2508 if (options
->stat_sep
) {
2509 /* attach patch instead of inline */
2510 fputs(options
->stat_sep
, stdout
);
2512 putchar(options
->line_termination
);
2516 for (i
= 0; i
< q
->nr
; i
++) {
2517 struct diff_filepair
*p
= q
->queue
[i
];
2518 if (check_pair_status(p
))
2519 diff_flush_patch(p
, options
);
2523 if (output_format
& DIFF_FORMAT_CALLBACK
)
2524 options
->format_callback(q
, options
, options
->format_callback_data
);
2526 for (i
= 0; i
< q
->nr
; i
++)
2527 diff_free_filepair(q
->queue
[i
]);
2531 q
->nr
= q
->alloc
= 0;
2534 static void diffcore_apply_filter(const char *filter
)
2537 struct diff_queue_struct
*q
= &diff_queued_diff
;
2538 struct diff_queue_struct outq
;
2540 outq
.nr
= outq
.alloc
= 0;
2545 if (strchr(filter
, DIFF_STATUS_FILTER_AON
)) {
2547 for (i
= found
= 0; !found
&& i
< q
->nr
; i
++) {
2548 struct diff_filepair
*p
= q
->queue
[i
];
2549 if (((p
->status
== DIFF_STATUS_MODIFIED
) &&
2551 strchr(filter
, DIFF_STATUS_FILTER_BROKEN
)) ||
2553 strchr(filter
, DIFF_STATUS_MODIFIED
)))) ||
2554 ((p
->status
!= DIFF_STATUS_MODIFIED
) &&
2555 strchr(filter
, p
->status
)))
2561 /* otherwise we will clear the whole queue
2562 * by copying the empty outq at the end of this
2563 * function, but first clear the current entries
2566 for (i
= 0; i
< q
->nr
; i
++)
2567 diff_free_filepair(q
->queue
[i
]);
2570 /* Only the matching ones */
2571 for (i
= 0; i
< q
->nr
; i
++) {
2572 struct diff_filepair
*p
= q
->queue
[i
];
2574 if (((p
->status
== DIFF_STATUS_MODIFIED
) &&
2576 strchr(filter
, DIFF_STATUS_FILTER_BROKEN
)) ||
2578 strchr(filter
, DIFF_STATUS_MODIFIED
)))) ||
2579 ((p
->status
!= DIFF_STATUS_MODIFIED
) &&
2580 strchr(filter
, p
->status
)))
2583 diff_free_filepair(p
);
2590 void diffcore_std(struct diff_options
*options
)
2592 if (options
->break_opt
!= -1)
2593 diffcore_break(options
->break_opt
);
2594 if (options
->detect_rename
)
2595 diffcore_rename(options
);
2596 if (options
->break_opt
!= -1)
2597 diffcore_merge_broken();
2598 if (options
->pickaxe
)
2599 diffcore_pickaxe(options
->pickaxe
, options
->pickaxe_opts
);
2600 if (options
->orderfile
)
2601 diffcore_order(options
->orderfile
);
2602 diff_resolve_rename_copy();
2603 diffcore_apply_filter(options
->filter
);
2607 void diffcore_std_no_resolve(struct diff_options
*options
)
2609 if (options
->pickaxe
)
2610 diffcore_pickaxe(options
->pickaxe
, options
->pickaxe_opts
);
2611 if (options
->orderfile
)
2612 diffcore_order(options
->orderfile
);
2613 diffcore_apply_filter(options
->filter
);
2616 void diff_addremove(struct diff_options
*options
,
2617 int addremove
, unsigned mode
,
2618 const unsigned char *sha1
,
2619 const char *base
, const char *path
)
2621 char concatpath
[PATH_MAX
];
2622 struct diff_filespec
*one
, *two
;
2624 /* This may look odd, but it is a preparation for
2625 * feeding "there are unchanged files which should
2626 * not produce diffs, but when you are doing copy
2627 * detection you would need them, so here they are"
2628 * entries to the diff-core. They will be prefixed
2629 * with something like '=' or '*' (I haven't decided
2630 * which but should not make any difference).
2631 * Feeding the same new and old to diff_change()
2632 * also has the same effect.
2633 * Before the final output happens, they are pruned after
2634 * merged into rename/copy pairs as appropriate.
2636 if (options
->reverse_diff
)
2637 addremove
= (addremove
== '+' ? '-' :
2638 addremove
== '-' ? '+' : addremove
);
2640 if (!path
) path
= "";
2641 sprintf(concatpath
, "%s%s", base
, path
);
2642 one
= alloc_filespec(concatpath
);
2643 two
= alloc_filespec(concatpath
);
2645 if (addremove
!= '+')
2646 fill_filespec(one
, sha1
, mode
);
2647 if (addremove
!= '-')
2648 fill_filespec(two
, sha1
, mode
);
2650 diff_queue(&diff_queued_diff
, one
, two
);
2653 void diff_change(struct diff_options
*options
,
2654 unsigned old_mode
, unsigned new_mode
,
2655 const unsigned char *old_sha1
,
2656 const unsigned char *new_sha1
,
2657 const char *base
, const char *path
)
2659 char concatpath
[PATH_MAX
];
2660 struct diff_filespec
*one
, *two
;
2662 if (options
->reverse_diff
) {
2664 const unsigned char *tmp_c
;
2665 tmp
= old_mode
; old_mode
= new_mode
; new_mode
= tmp
;
2666 tmp_c
= old_sha1
; old_sha1
= new_sha1
; new_sha1
= tmp_c
;
2668 if (!path
) path
= "";
2669 sprintf(concatpath
, "%s%s", base
, path
);
2670 one
= alloc_filespec(concatpath
);
2671 two
= alloc_filespec(concatpath
);
2672 fill_filespec(one
, old_sha1
, old_mode
);
2673 fill_filespec(two
, new_sha1
, new_mode
);
2675 diff_queue(&diff_queued_diff
, one
, two
);
2678 void diff_unmerge(struct diff_options
*options
,
2681 struct diff_filespec
*one
, *two
;
2682 one
= alloc_filespec(path
);
2683 two
= alloc_filespec(path
);
2684 diff_queue(&diff_queued_diff
, one
, two
);