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 * make sure that at least one '-' is printed if there were deletions,
554 * and likewise for '+'.
558 return ((it
- 1) * (width
- 1) + max_change
- 1) / (max_change
- 1);
561 static void show_name(const char *prefix
, const char *name
, int len
,
562 const char *reset
, const char *set
)
564 printf(" %s%s%-*s%s |", set
, prefix
, len
, name
, reset
);
567 static void show_graph(char ch
, int cnt
, const char *set
, const char *reset
)
577 static void show_stats(struct diffstat_t
* data
, struct diff_options
*options
)
579 int i
, len
, add
, del
, total
, adds
= 0, dels
= 0;
580 int max_change
= 0, max_len
= 0;
581 int total_files
= data
->nr
;
582 int width
, name_width
;
583 const char *reset
, *set
, *add_c
, *del_c
;
588 width
= options
->stat_width
? options
->stat_width
: 80;
589 name_width
= options
->stat_name_width
? options
->stat_name_width
: 50;
591 /* Sanity: give at least 5 columns to the graph,
592 * but leave at least 10 columns for the name.
594 if (width
< name_width
+ 15) {
595 if (name_width
<= 25)
596 width
= name_width
+ 15;
598 name_width
= width
- 15;
601 /* Find the longest filename and max number of changes */
602 reset
= diff_get_color(options
->color_diff
, DIFF_RESET
);
603 set
= diff_get_color(options
->color_diff
, DIFF_PLAIN
);
604 add_c
= diff_get_color(options
->color_diff
, DIFF_FILE_NEW
);
605 del_c
= diff_get_color(options
->color_diff
, DIFF_FILE_OLD
);
607 for (i
= 0; i
< data
->nr
; i
++) {
608 struct diffstat_file
*file
= data
->files
[i
];
609 int change
= file
->added
+ file
->deleted
;
611 len
= quote_c_style(file
->name
, NULL
, NULL
, 0);
613 char *qname
= xmalloc(len
+ 1);
614 quote_c_style(file
->name
, qname
, NULL
, 0);
619 len
= strlen(file
->name
);
623 if (file
->is_binary
|| file
->is_unmerged
)
625 if (max_change
< change
)
629 /* Compute the width of the graph part;
630 * 10 is for one blank at the beginning of the line plus
631 * " | count " between the name and the graph.
633 * From here on, name_width is the width of the name area,
634 * and width is the width of the graph area.
636 name_width
= (name_width
< max_len
) ? name_width
: max_len
;
637 if (width
< (name_width
+ 10) + max_change
)
638 width
= width
- (name_width
+ 10);
642 for (i
= 0; i
< data
->nr
; i
++) {
643 const char *prefix
= "";
644 char *name
= data
->files
[i
]->name
;
645 int added
= data
->files
[i
]->added
;
646 int deleted
= data
->files
[i
]->deleted
;
650 * "scale" the filename
653 name_len
= strlen(name
);
654 if (name_width
< name_len
) {
658 name
+= name_len
- len
;
659 slash
= strchr(name
, '/');
664 if (data
->files
[i
]->is_binary
) {
665 show_name(prefix
, name
, len
, reset
, set
);
667 goto free_diffstat_file
;
669 else if (data
->files
[i
]->is_unmerged
) {
670 show_name(prefix
, name
, len
, reset
, set
);
671 printf(" Unmerged\n");
672 goto free_diffstat_file
;
674 else if (!data
->files
[i
]->is_renamed
&&
675 (added
+ deleted
== 0)) {
677 goto free_diffstat_file
;
681 * scale the add/delete
689 if (width
<= max_change
) {
690 add
= scale_linear(add
, width
, max_change
);
691 del
= scale_linear(del
, width
, max_change
);
694 show_name(prefix
, name
, len
, reset
, set
);
695 printf("%5d ", added
+ deleted
);
696 show_graph('+', add
, add_c
, reset
);
697 show_graph('-', del
, del_c
, reset
);
700 free(data
->files
[i
]->name
);
701 free(data
->files
[i
]);
704 printf("%s %d files changed, %d insertions(+), %d deletions(-)%s\n",
705 set
, total_files
, adds
, dels
, reset
);
709 struct xdiff_emit_state xm
;
710 const char *filename
;
714 static void checkdiff_consume(void *priv
, char *line
, unsigned long len
)
716 struct checkdiff_t
*data
= priv
;
718 if (line
[0] == '+') {
723 /* check space before tab */
724 for (i
= 1; i
< len
&& (line
[i
] == ' ' || line
[i
] == '\t'); i
++)
727 if (line
[i
- 1] == '\t' && spaces
)
728 printf("%s:%d: space before tab:%.*s\n",
729 data
->filename
, data
->lineno
, (int)len
, line
);
731 /* check white space at line end */
732 if (line
[len
- 1] == '\n')
734 if (isspace(line
[len
- 1]))
735 printf("%s:%d: white space at end: %.*s\n",
736 data
->filename
, data
->lineno
, (int)len
, line
);
737 } else if (line
[0] == ' ')
739 else if (line
[0] == '@') {
740 char *plus
= strchr(line
, '+');
742 data
->lineno
= strtol(plus
, NULL
, 10);
748 static unsigned char *deflate_it(char *data
,
750 unsigned long *result_size
)
753 unsigned char *deflated
;
756 memset(&stream
, 0, sizeof(stream
));
757 deflateInit(&stream
, zlib_compression_level
);
758 bound
= deflateBound(&stream
, size
);
759 deflated
= xmalloc(bound
);
760 stream
.next_out
= deflated
;
761 stream
.avail_out
= bound
;
763 stream
.next_in
= (unsigned char *)data
;
764 stream
.avail_in
= size
;
765 while (deflate(&stream
, Z_FINISH
) == Z_OK
)
768 *result_size
= stream
.total_out
;
772 static void emit_binary_diff_body(mmfile_t
*one
, mmfile_t
*two
)
778 unsigned long orig_size
;
779 unsigned long delta_size
;
780 unsigned long deflate_size
;
781 unsigned long data_size
;
783 /* We could do deflated delta, or we could do just deflated two,
784 * whichever is smaller.
787 deflated
= deflate_it(two
->ptr
, two
->size
, &deflate_size
);
788 if (one
->size
&& two
->size
) {
789 delta
= diff_delta(one
->ptr
, one
->size
,
791 &delta_size
, deflate_size
);
793 void *to_free
= delta
;
794 orig_size
= delta_size
;
795 delta
= deflate_it(delta
, delta_size
, &delta_size
);
800 if (delta
&& delta_size
< deflate_size
) {
801 printf("delta %lu\n", orig_size
);
804 data_size
= delta_size
;
807 printf("literal %lu\n", two
->size
);
810 data_size
= deflate_size
;
813 /* emit data encoded in base85 */
816 int bytes
= (52 < data_size
) ? 52 : data_size
;
820 line
[0] = bytes
+ 'A' - 1;
822 line
[0] = bytes
- 26 + 'a' - 1;
823 encode_85(line
+ 1, cp
, bytes
);
824 cp
= (char *) cp
+ bytes
;
831 static void emit_binary_diff(mmfile_t
*one
, mmfile_t
*two
)
833 printf("GIT binary patch\n");
834 emit_binary_diff_body(one
, two
);
835 emit_binary_diff_body(two
, one
);
838 #define FIRST_FEW_BYTES 8000
839 static int mmfile_is_binary(mmfile_t
*mf
)
842 if (FIRST_FEW_BYTES
< sz
)
843 sz
= FIRST_FEW_BYTES
;
844 return !!memchr(mf
->ptr
, 0, sz
);
847 static void builtin_diff(const char *name_a
,
849 struct diff_filespec
*one
,
850 struct diff_filespec
*two
,
851 const char *xfrm_msg
,
852 struct diff_options
*o
,
853 int complete_rewrite
)
858 const char *set
= diff_get_color(o
->color_diff
, DIFF_METAINFO
);
859 const char *reset
= diff_get_color(o
->color_diff
, DIFF_RESET
);
861 a_one
= quote_two("a/", name_a
);
862 b_two
= quote_two("b/", name_b
);
863 lbl
[0] = DIFF_FILE_VALID(one
) ? a_one
: "/dev/null";
864 lbl
[1] = DIFF_FILE_VALID(two
) ? b_two
: "/dev/null";
865 printf("%sdiff --git %s %s%s\n", set
, a_one
, b_two
, reset
);
866 if (lbl
[0][0] == '/') {
868 printf("%snew file mode %06o%s\n", set
, two
->mode
, reset
);
869 if (xfrm_msg
&& xfrm_msg
[0])
870 printf("%s%s%s\n", set
, xfrm_msg
, reset
);
872 else if (lbl
[1][0] == '/') {
873 printf("%sdeleted file mode %06o%s\n", set
, one
->mode
, reset
);
874 if (xfrm_msg
&& xfrm_msg
[0])
875 printf("%s%s%s\n", set
, xfrm_msg
, reset
);
878 if (one
->mode
!= two
->mode
) {
879 printf("%sold mode %06o%s\n", set
, one
->mode
, reset
);
880 printf("%snew mode %06o%s\n", set
, two
->mode
, reset
);
882 if (xfrm_msg
&& xfrm_msg
[0])
883 printf("%s%s%s\n", set
, xfrm_msg
, reset
);
885 * we do not run diff between different kind
888 if ((one
->mode
^ two
->mode
) & S_IFMT
)
889 goto free_ab_and_return
;
890 if (complete_rewrite
) {
891 emit_rewrite_diff(name_a
, name_b
, one
, two
);
892 goto free_ab_and_return
;
896 if (fill_mmfile(&mf1
, one
) < 0 || fill_mmfile(&mf2
, two
) < 0)
897 die("unable to read files to diff");
899 if (!o
->text
&& (mmfile_is_binary(&mf1
) || mmfile_is_binary(&mf2
))) {
900 /* Quite common confusing case */
901 if (mf1
.size
== mf2
.size
&&
902 !memcmp(mf1
.ptr
, mf2
.ptr
, mf1
.size
))
903 goto free_ab_and_return
;
905 emit_binary_diff(&mf1
, &mf2
);
907 printf("Binary files %s and %s differ\n",
911 /* Crazy xdl interfaces.. */
912 const char *diffopts
= getenv("GIT_DIFF_OPTS");
916 struct emit_callback ecbdata
;
918 memset(&ecbdata
, 0, sizeof(ecbdata
));
919 ecbdata
.label_path
= lbl
;
920 ecbdata
.color_diff
= o
->color_diff
;
921 xpp
.flags
= XDF_NEED_MINIMAL
| o
->xdl_opts
;
922 xecfg
.ctxlen
= o
->context
;
923 xecfg
.flags
= XDL_EMIT_FUNCNAMES
;
926 else if (!strncmp(diffopts
, "--unified=", 10))
927 xecfg
.ctxlen
= strtoul(diffopts
+ 10, NULL
, 10);
928 else if (!strncmp(diffopts
, "-u", 2))
929 xecfg
.ctxlen
= strtoul(diffopts
+ 2, NULL
, 10);
930 ecb
.outf
= xdiff_outf
;
932 ecbdata
.xm
.consume
= fn_out_consume
;
933 if (o
->color_diff_words
)
935 xcalloc(1, sizeof(struct diff_words_data
));
936 xdl_diff(&mf1
, &mf2
, &xpp
, &xecfg
, &ecb
);
937 if (o
->color_diff_words
)
938 free_diff_words_data(&ecbdata
);
947 static void builtin_diffstat(const char *name_a
, const char *name_b
,
948 struct diff_filespec
*one
,
949 struct diff_filespec
*two
,
950 struct diffstat_t
*diffstat
,
951 struct diff_options
*o
,
952 int complete_rewrite
)
955 struct diffstat_file
*data
;
957 data
= diffstat_add(diffstat
, name_a
, name_b
);
960 data
->is_unmerged
= 1;
963 if (complete_rewrite
) {
964 diff_populate_filespec(one
, 0);
965 diff_populate_filespec(two
, 0);
966 data
->deleted
= count_lines(one
->data
, one
->size
);
967 data
->added
= count_lines(two
->data
, two
->size
);
970 if (fill_mmfile(&mf1
, one
) < 0 || fill_mmfile(&mf2
, two
) < 0)
971 die("unable to read files to diff");
973 if (mmfile_is_binary(&mf1
) || mmfile_is_binary(&mf2
))
976 /* Crazy xdl interfaces.. */
981 xpp
.flags
= XDF_NEED_MINIMAL
| o
->xdl_opts
;
984 ecb
.outf
= xdiff_outf
;
986 xdl_diff(&mf1
, &mf2
, &xpp
, &xecfg
, &ecb
);
990 static void builtin_checkdiff(const char *name_a
, const char *name_b
,
991 struct diff_filespec
*one
,
992 struct diff_filespec
*two
)
995 struct checkdiff_t data
;
1000 memset(&data
, 0, sizeof(data
));
1001 data
.xm
.consume
= checkdiff_consume
;
1002 data
.filename
= name_b
? name_b
: name_a
;
1005 if (fill_mmfile(&mf1
, one
) < 0 || fill_mmfile(&mf2
, two
) < 0)
1006 die("unable to read files to diff");
1008 if (mmfile_is_binary(&mf2
))
1011 /* Crazy xdl interfaces.. */
1016 xpp
.flags
= XDF_NEED_MINIMAL
;
1019 ecb
.outf
= xdiff_outf
;
1021 xdl_diff(&mf1
, &mf2
, &xpp
, &xecfg
, &ecb
);
1025 struct diff_filespec
*alloc_filespec(const char *path
)
1027 int namelen
= strlen(path
);
1028 struct diff_filespec
*spec
= xmalloc(sizeof(*spec
) + namelen
+ 1);
1030 memset(spec
, 0, sizeof(*spec
));
1031 spec
->path
= (char *)(spec
+ 1);
1032 memcpy(spec
->path
, path
, namelen
+1);
1036 void fill_filespec(struct diff_filespec
*spec
, const unsigned char *sha1
,
1037 unsigned short mode
)
1040 spec
->mode
= canon_mode(mode
);
1041 hashcpy(spec
->sha1
, sha1
);
1042 spec
->sha1_valid
= !is_null_sha1(sha1
);
1047 * Given a name and sha1 pair, if the dircache tells us the file in
1048 * the work tree has that object contents, return true, so that
1049 * prepare_temp_file() does not have to inflate and extract.
1051 static int work_tree_matches(const char *name
, const unsigned char *sha1
)
1053 struct cache_entry
*ce
;
1057 /* We do not read the cache ourselves here, because the
1058 * benchmark with my previous version that always reads cache
1059 * shows that it makes things worse for diff-tree comparing
1060 * two linux-2.6 kernel trees in an already checked out work
1061 * tree. This is because most diff-tree comparisons deal with
1062 * only a small number of files, while reading the cache is
1063 * expensive for a large project, and its cost outweighs the
1064 * savings we get by not inflating the object to a temporary
1065 * file. Practically, this code only helps when we are used
1066 * by diff-cache --cached, which does read the cache before
1073 pos
= cache_name_pos(name
, len
);
1076 ce
= active_cache
[pos
];
1077 if ((lstat(name
, &st
) < 0) ||
1078 !S_ISREG(st
.st_mode
) || /* careful! */
1079 ce_match_stat(ce
, &st
, 0) ||
1080 hashcmp(sha1
, ce
->sha1
))
1082 /* we return 1 only when we can stat, it is a regular file,
1083 * stat information matches, and sha1 recorded in the cache
1084 * matches. I.e. we know the file in the work tree really is
1085 * the same as the <name, sha1> pair.
1090 static struct sha1_size_cache
{
1091 unsigned char sha1
[20];
1093 } **sha1_size_cache
;
1094 static int sha1_size_cache_nr
, sha1_size_cache_alloc
;
1096 static struct sha1_size_cache
*locate_size_cache(unsigned char *sha1
,
1101 struct sha1_size_cache
*e
;
1104 last
= sha1_size_cache_nr
;
1105 while (last
> first
) {
1106 int cmp
, next
= (last
+ first
) >> 1;
1107 e
= sha1_size_cache
[next
];
1108 cmp
= hashcmp(e
->sha1
, sha1
);
1120 /* insert to make it at "first" */
1121 if (sha1_size_cache_alloc
<= sha1_size_cache_nr
) {
1122 sha1_size_cache_alloc
= alloc_nr(sha1_size_cache_alloc
);
1123 sha1_size_cache
= xrealloc(sha1_size_cache
,
1124 sha1_size_cache_alloc
*
1125 sizeof(*sha1_size_cache
));
1127 sha1_size_cache_nr
++;
1128 if (first
< sha1_size_cache_nr
)
1129 memmove(sha1_size_cache
+ first
+ 1, sha1_size_cache
+ first
,
1130 (sha1_size_cache_nr
- first
- 1) *
1131 sizeof(*sha1_size_cache
));
1132 e
= xmalloc(sizeof(struct sha1_size_cache
));
1133 sha1_size_cache
[first
] = e
;
1134 hashcpy(e
->sha1
, sha1
);
1140 * While doing rename detection and pickaxe operation, we may need to
1141 * grab the data for the blob (or file) for our own in-core comparison.
1142 * diff_filespec has data and size fields for this purpose.
1144 int diff_populate_filespec(struct diff_filespec
*s
, int size_only
)
1147 if (!DIFF_FILE_VALID(s
))
1148 die("internal error: asking to populate invalid file.");
1149 if (S_ISDIR(s
->mode
))
1152 if (!use_size_cache
)
1157 if (!s
->sha1_valid
||
1158 work_tree_matches(s
->path
, s
->sha1
)) {
1161 if (lstat(s
->path
, &st
) < 0) {
1162 if (errno
== ENOENT
) {
1166 s
->data
= (char *)"";
1171 s
->size
= st
.st_size
;
1176 if (S_ISLNK(st
.st_mode
)) {
1178 s
->data
= xmalloc(s
->size
);
1180 ret
= readlink(s
->path
, s
->data
, s
->size
);
1187 fd
= open(s
->path
, O_RDONLY
);
1190 s
->data
= mmap(NULL
, s
->size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
1192 if (s
->data
== MAP_FAILED
)
1194 s
->should_munmap
= 1;
1198 struct sha1_size_cache
*e
;
1201 e
= locate_size_cache(s
->sha1
, 1, 0);
1206 if (!sha1_object_info(s
->sha1
, type
, &s
->size
))
1207 locate_size_cache(s
->sha1
, 0, s
->size
);
1210 s
->data
= read_sha1_file(s
->sha1
, type
, &s
->size
);
1217 void diff_free_filespec_data(struct diff_filespec
*s
)
1221 else if (s
->should_munmap
)
1222 munmap(s
->data
, s
->size
);
1223 s
->should_free
= s
->should_munmap
= 0;
1229 static void prep_temp_blob(struct diff_tempfile
*temp
,
1232 const unsigned char *sha1
,
1237 fd
= git_mkstemp(temp
->tmp_path
, TEMPFILE_PATH_LEN
, ".diff_XXXXXX");
1239 die("unable to create temp-file");
1240 if (write(fd
, blob
, size
) != size
)
1241 die("unable to write temp-file");
1243 temp
->name
= temp
->tmp_path
;
1244 strcpy(temp
->hex
, sha1_to_hex(sha1
));
1246 sprintf(temp
->mode
, "%06o", mode
);
1249 static void prepare_temp_file(const char *name
,
1250 struct diff_tempfile
*temp
,
1251 struct diff_filespec
*one
)
1253 if (!DIFF_FILE_VALID(one
)) {
1255 /* A '-' entry produces this for file-2, and
1256 * a '+' entry produces this for file-1.
1258 temp
->name
= "/dev/null";
1259 strcpy(temp
->hex
, ".");
1260 strcpy(temp
->mode
, ".");
1264 if (!one
->sha1_valid
||
1265 work_tree_matches(name
, one
->sha1
)) {
1267 if (lstat(name
, &st
) < 0) {
1268 if (errno
== ENOENT
)
1269 goto not_a_valid_file
;
1270 die("stat(%s): %s", name
, strerror(errno
));
1272 if (S_ISLNK(st
.st_mode
)) {
1274 char buf
[PATH_MAX
+ 1]; /* ought to be SYMLINK_MAX */
1275 if (sizeof(buf
) <= st
.st_size
)
1276 die("symlink too long: %s", name
);
1277 ret
= readlink(name
, buf
, st
.st_size
);
1279 die("readlink(%s)", name
);
1280 prep_temp_blob(temp
, buf
, st
.st_size
,
1282 one
->sha1
: null_sha1
),
1284 one
->mode
: S_IFLNK
));
1287 /* we can borrow from the file in the work tree */
1289 if (!one
->sha1_valid
)
1290 strcpy(temp
->hex
, sha1_to_hex(null_sha1
));
1292 strcpy(temp
->hex
, sha1_to_hex(one
->sha1
));
1293 /* Even though we may sometimes borrow the
1294 * contents from the work tree, we always want
1295 * one->mode. mode is trustworthy even when
1296 * !(one->sha1_valid), as long as
1297 * DIFF_FILE_VALID(one).
1299 sprintf(temp
->mode
, "%06o", one
->mode
);
1304 if (diff_populate_filespec(one
, 0))
1305 die("cannot read data blob for %s", one
->path
);
1306 prep_temp_blob(temp
, one
->data
, one
->size
,
1307 one
->sha1
, one
->mode
);
1311 static void remove_tempfile(void)
1315 for (i
= 0; i
< 2; i
++)
1316 if (diff_temp
[i
].name
== diff_temp
[i
].tmp_path
) {
1317 unlink(diff_temp
[i
].name
);
1318 diff_temp
[i
].name
= NULL
;
1322 static void remove_tempfile_on_signal(int signo
)
1325 signal(SIGINT
, SIG_DFL
);
1329 static int spawn_prog(const char *pgm
, const char **arg
)
1337 die("unable to fork");
1339 execvp(pgm
, (char *const*) arg
);
1343 while (waitpid(pid
, &status
, 0) < 0) {
1349 /* Earlier we did not check the exit status because
1350 * diff exits non-zero if files are different, and
1351 * we are not interested in knowing that. It was a
1352 * mistake which made it harder to quit a diff-*
1353 * session that uses the git-apply-patch-script as
1354 * the GIT_EXTERNAL_DIFF. A custom GIT_EXTERNAL_DIFF
1355 * should also exit non-zero only when it wants to
1356 * abort the entire diff-* session.
1358 if (WIFEXITED(status
) && !WEXITSTATUS(status
))
1363 /* An external diff command takes:
1365 * diff-cmd name infile1 infile1-sha1 infile1-mode \
1366 * infile2 infile2-sha1 infile2-mode [ rename-to ]
1369 static void run_external_diff(const char *pgm
,
1372 struct diff_filespec
*one
,
1373 struct diff_filespec
*two
,
1374 const char *xfrm_msg
,
1375 int complete_rewrite
)
1377 const char *spawn_arg
[10];
1378 struct diff_tempfile
*temp
= diff_temp
;
1380 static int atexit_asked
= 0;
1381 const char *othername
;
1382 const char **arg
= &spawn_arg
[0];
1384 othername
= (other
? other
: name
);
1386 prepare_temp_file(name
, &temp
[0], one
);
1387 prepare_temp_file(othername
, &temp
[1], two
);
1388 if (! atexit_asked
&&
1389 (temp
[0].name
== temp
[0].tmp_path
||
1390 temp
[1].name
== temp
[1].tmp_path
)) {
1392 atexit(remove_tempfile
);
1394 signal(SIGINT
, remove_tempfile_on_signal
);
1400 *arg
++ = temp
[0].name
;
1401 *arg
++ = temp
[0].hex
;
1402 *arg
++ = temp
[0].mode
;
1403 *arg
++ = temp
[1].name
;
1404 *arg
++ = temp
[1].hex
;
1405 *arg
++ = temp
[1].mode
;
1415 retval
= spawn_prog(pgm
, spawn_arg
);
1418 fprintf(stderr
, "external diff died, stopping at %s.\n", name
);
1423 static void run_diff_cmd(const char *pgm
,
1426 struct diff_filespec
*one
,
1427 struct diff_filespec
*two
,
1428 const char *xfrm_msg
,
1429 struct diff_options
*o
,
1430 int complete_rewrite
)
1433 run_external_diff(pgm
, name
, other
, one
, two
, xfrm_msg
,
1438 builtin_diff(name
, other
? other
: name
,
1439 one
, two
, xfrm_msg
, o
, complete_rewrite
);
1441 printf("* Unmerged path %s\n", name
);
1444 static void diff_fill_sha1_info(struct diff_filespec
*one
)
1446 if (DIFF_FILE_VALID(one
)) {
1447 if (!one
->sha1_valid
) {
1449 if (lstat(one
->path
, &st
) < 0)
1450 die("stat %s", one
->path
);
1451 if (index_path(one
->sha1
, one
->path
, &st
, 0))
1452 die("cannot hash %s\n", one
->path
);
1459 static void run_diff(struct diff_filepair
*p
, struct diff_options
*o
)
1461 const char *pgm
= external_diff();
1462 char msg
[PATH_MAX
*2+300], *xfrm_msg
;
1463 struct diff_filespec
*one
;
1464 struct diff_filespec
*two
;
1467 char *name_munged
, *other_munged
;
1468 int complete_rewrite
= 0;
1471 if (DIFF_PAIR_UNMERGED(p
)) {
1473 run_diff_cmd(pgm
, p
->one
->path
, NULL
, NULL
, NULL
, NULL
, o
, 0);
1477 name
= p
->one
->path
;
1478 other
= (strcmp(name
, p
->two
->path
) ? p
->two
->path
: NULL
);
1479 name_munged
= quote_one(name
);
1480 other_munged
= quote_one(other
);
1481 one
= p
->one
; two
= p
->two
;
1483 diff_fill_sha1_info(one
);
1484 diff_fill_sha1_info(two
);
1487 switch (p
->status
) {
1488 case DIFF_STATUS_COPIED
:
1489 len
+= snprintf(msg
+ len
, sizeof(msg
) - len
,
1490 "similarity index %d%%\n"
1493 (int)(0.5 + p
->score
* 100.0/MAX_SCORE
),
1494 name_munged
, other_munged
);
1496 case DIFF_STATUS_RENAMED
:
1497 len
+= snprintf(msg
+ len
, sizeof(msg
) - len
,
1498 "similarity index %d%%\n"
1501 (int)(0.5 + p
->score
* 100.0/MAX_SCORE
),
1502 name_munged
, other_munged
);
1504 case DIFF_STATUS_MODIFIED
:
1506 len
+= snprintf(msg
+ len
, sizeof(msg
) - len
,
1507 "dissimilarity index %d%%\n",
1508 (int)(0.5 + p
->score
*
1510 complete_rewrite
= 1;
1519 if (hashcmp(one
->sha1
, two
->sha1
)) {
1520 int abbrev
= o
->full_index
? 40 : DEFAULT_ABBREV
;
1524 if ((!fill_mmfile(&mf
, one
) && mmfile_is_binary(&mf
)) ||
1525 (!fill_mmfile(&mf
, two
) && mmfile_is_binary(&mf
)))
1528 len
+= snprintf(msg
+ len
, sizeof(msg
) - len
,
1530 abbrev
, sha1_to_hex(one
->sha1
),
1531 abbrev
, sha1_to_hex(two
->sha1
));
1532 if (one
->mode
== two
->mode
)
1533 len
+= snprintf(msg
+ len
, sizeof(msg
) - len
,
1534 " %06o", one
->mode
);
1535 len
+= snprintf(msg
+ len
, sizeof(msg
) - len
, "\n");
1540 xfrm_msg
= len
? msg
: NULL
;
1543 DIFF_FILE_VALID(one
) && DIFF_FILE_VALID(two
) &&
1544 (S_IFMT
& one
->mode
) != (S_IFMT
& two
->mode
)) {
1545 /* a filepair that changes between file and symlink
1546 * needs to be split into deletion and creation.
1548 struct diff_filespec
*null
= alloc_filespec(two
->path
);
1549 run_diff_cmd(NULL
, name
, other
, one
, null
, xfrm_msg
, o
, 0);
1551 null
= alloc_filespec(one
->path
);
1552 run_diff_cmd(NULL
, name
, other
, null
, two
, xfrm_msg
, o
, 0);
1556 run_diff_cmd(pgm
, name
, other
, one
, two
, xfrm_msg
, o
,
1563 static void run_diffstat(struct diff_filepair
*p
, struct diff_options
*o
,
1564 struct diffstat_t
*diffstat
)
1568 int complete_rewrite
= 0;
1570 if (DIFF_PAIR_UNMERGED(p
)) {
1572 builtin_diffstat(p
->one
->path
, NULL
, NULL
, NULL
, diffstat
, o
, 0);
1576 name
= p
->one
->path
;
1577 other
= (strcmp(name
, p
->two
->path
) ? p
->two
->path
: NULL
);
1579 diff_fill_sha1_info(p
->one
);
1580 diff_fill_sha1_info(p
->two
);
1582 if (p
->status
== DIFF_STATUS_MODIFIED
&& p
->score
)
1583 complete_rewrite
= 1;
1584 builtin_diffstat(name
, other
, p
->one
, p
->two
, diffstat
, o
, complete_rewrite
);
1587 static void run_checkdiff(struct diff_filepair
*p
, struct diff_options
*o
)
1592 if (DIFF_PAIR_UNMERGED(p
)) {
1597 name
= p
->one
->path
;
1598 other
= (strcmp(name
, p
->two
->path
) ? p
->two
->path
: NULL
);
1600 diff_fill_sha1_info(p
->one
);
1601 diff_fill_sha1_info(p
->two
);
1603 builtin_checkdiff(name
, other
, p
->one
, p
->two
);
1606 void diff_setup(struct diff_options
*options
)
1608 memset(options
, 0, sizeof(*options
));
1609 options
->line_termination
= '\n';
1610 options
->break_opt
= -1;
1611 options
->rename_limit
= -1;
1612 options
->context
= 3;
1613 options
->msg_sep
= "";
1615 options
->change
= diff_change
;
1616 options
->add_remove
= diff_addremove
;
1617 options
->color_diff
= diff_use_color_default
;
1618 options
->detect_rename
= diff_detect_rename_default
;
1621 int diff_setup_done(struct diff_options
*options
)
1625 if (options
->output_format
& DIFF_FORMAT_NAME
)
1627 if (options
->output_format
& DIFF_FORMAT_NAME_STATUS
)
1629 if (options
->output_format
& DIFF_FORMAT_CHECKDIFF
)
1631 if (options
->output_format
& DIFF_FORMAT_NO_OUTPUT
)
1634 die("--name-only, --name-status, --check and -s are mutually exclusive");
1636 if (options
->find_copies_harder
)
1637 options
->detect_rename
= DIFF_DETECT_COPY
;
1639 if (options
->output_format
& (DIFF_FORMAT_NAME
|
1640 DIFF_FORMAT_NAME_STATUS
|
1641 DIFF_FORMAT_CHECKDIFF
|
1642 DIFF_FORMAT_NO_OUTPUT
))
1643 options
->output_format
&= ~(DIFF_FORMAT_RAW
|
1644 DIFF_FORMAT_DIFFSTAT
|
1645 DIFF_FORMAT_SUMMARY
|
1649 * These cases always need recursive; we do not drop caller-supplied
1650 * recursive bits for other formats here.
1652 if (options
->output_format
& (DIFF_FORMAT_PATCH
|
1653 DIFF_FORMAT_DIFFSTAT
|
1654 DIFF_FORMAT_CHECKDIFF
))
1655 options
->recursive
= 1;
1657 * Also pickaxe would not work very well if you do not say recursive
1659 if (options
->pickaxe
)
1660 options
->recursive
= 1;
1662 if (options
->detect_rename
&& options
->rename_limit
< 0)
1663 options
->rename_limit
= diff_rename_limit_default
;
1664 if (options
->setup
& DIFF_SETUP_USE_CACHE
) {
1666 /* read-cache does not die even when it fails
1667 * so it is safe for us to do this here. Also
1668 * it does not smudge active_cache or active_nr
1669 * when it fails, so we do not have to worry about
1670 * cleaning it up ourselves either.
1674 if (options
->setup
& DIFF_SETUP_USE_SIZE_CACHE
)
1676 if (options
->abbrev
<= 0 || 40 < options
->abbrev
)
1677 options
->abbrev
= 40; /* full */
1682 static int opt_arg(const char *arg
, int arg_short
, const char *arg_long
, int *val
)
1692 if (c
== arg_short
) {
1696 if (val
&& isdigit(c
)) {
1698 int n
= strtoul(arg
, &end
, 10);
1709 eq
= strchr(arg
, '=');
1714 if (!len
|| strncmp(arg
, arg_long
, len
))
1719 if (!isdigit(*++eq
))
1721 n
= strtoul(eq
, &end
, 10);
1729 int diff_opt_parse(struct diff_options
*options
, const char **av
, int ac
)
1731 const char *arg
= av
[0];
1732 if (!strcmp(arg
, "-p") || !strcmp(arg
, "-u"))
1733 options
->output_format
|= DIFF_FORMAT_PATCH
;
1734 else if (opt_arg(arg
, 'U', "unified", &options
->context
))
1735 options
->output_format
|= DIFF_FORMAT_PATCH
;
1736 else if (!strcmp(arg
, "--raw"))
1737 options
->output_format
|= DIFF_FORMAT_RAW
;
1738 else if (!strcmp(arg
, "--patch-with-raw")) {
1739 options
->output_format
|= DIFF_FORMAT_PATCH
| DIFF_FORMAT_RAW
;
1741 else if (!strncmp(arg
, "--stat", 6)) {
1743 int width
= options
->stat_width
;
1744 int name_width
= options
->stat_name_width
;
1750 if (!strncmp(arg
, "-width=", 7))
1751 width
= strtoul(arg
+ 7, &end
, 10);
1752 else if (!strncmp(arg
, "-name-width=", 12))
1753 name_width
= strtoul(arg
+ 12, &end
, 10);
1756 width
= strtoul(arg
+1, &end
, 10);
1758 name_width
= strtoul(end
+1, &end
, 10);
1761 /* Important! This checks all the error cases! */
1764 options
->output_format
|= DIFF_FORMAT_DIFFSTAT
;
1765 options
->stat_name_width
= name_width
;
1766 options
->stat_width
= width
;
1768 else if (!strcmp(arg
, "--check"))
1769 options
->output_format
|= DIFF_FORMAT_CHECKDIFF
;
1770 else if (!strcmp(arg
, "--summary"))
1771 options
->output_format
|= DIFF_FORMAT_SUMMARY
;
1772 else if (!strcmp(arg
, "--patch-with-stat")) {
1773 options
->output_format
|= DIFF_FORMAT_PATCH
| DIFF_FORMAT_DIFFSTAT
;
1775 else if (!strcmp(arg
, "-z"))
1776 options
->line_termination
= 0;
1777 else if (!strncmp(arg
, "-l", 2))
1778 options
->rename_limit
= strtoul(arg
+2, NULL
, 10);
1779 else if (!strcmp(arg
, "--full-index"))
1780 options
->full_index
= 1;
1781 else if (!strcmp(arg
, "--binary")) {
1782 options
->output_format
|= DIFF_FORMAT_PATCH
;
1783 options
->binary
= 1;
1785 else if (!strcmp(arg
, "-a") || !strcmp(arg
, "--text")) {
1788 else if (!strcmp(arg
, "--name-only"))
1789 options
->output_format
|= DIFF_FORMAT_NAME
;
1790 else if (!strcmp(arg
, "--name-status"))
1791 options
->output_format
|= DIFF_FORMAT_NAME_STATUS
;
1792 else if (!strcmp(arg
, "-R"))
1793 options
->reverse_diff
= 1;
1794 else if (!strncmp(arg
, "-S", 2))
1795 options
->pickaxe
= arg
+ 2;
1796 else if (!strcmp(arg
, "-s")) {
1797 options
->output_format
|= DIFF_FORMAT_NO_OUTPUT
;
1799 else if (!strncmp(arg
, "-O", 2))
1800 options
->orderfile
= arg
+ 2;
1801 else if (!strncmp(arg
, "--diff-filter=", 14))
1802 options
->filter
= arg
+ 14;
1803 else if (!strcmp(arg
, "--pickaxe-all"))
1804 options
->pickaxe_opts
= DIFF_PICKAXE_ALL
;
1805 else if (!strcmp(arg
, "--pickaxe-regex"))
1806 options
->pickaxe_opts
= DIFF_PICKAXE_REGEX
;
1807 else if (!strncmp(arg
, "-B", 2)) {
1808 if ((options
->break_opt
=
1809 diff_scoreopt_parse(arg
)) == -1)
1812 else if (!strncmp(arg
, "-M", 2)) {
1813 if ((options
->rename_score
=
1814 diff_scoreopt_parse(arg
)) == -1)
1816 options
->detect_rename
= DIFF_DETECT_RENAME
;
1818 else if (!strncmp(arg
, "-C", 2)) {
1819 if ((options
->rename_score
=
1820 diff_scoreopt_parse(arg
)) == -1)
1822 options
->detect_rename
= DIFF_DETECT_COPY
;
1824 else if (!strcmp(arg
, "--find-copies-harder"))
1825 options
->find_copies_harder
= 1;
1826 else if (!strcmp(arg
, "--abbrev"))
1827 options
->abbrev
= DEFAULT_ABBREV
;
1828 else if (!strncmp(arg
, "--abbrev=", 9)) {
1829 options
->abbrev
= strtoul(arg
+ 9, NULL
, 10);
1830 if (options
->abbrev
< MINIMUM_ABBREV
)
1831 options
->abbrev
= MINIMUM_ABBREV
;
1832 else if (40 < options
->abbrev
)
1833 options
->abbrev
= 40;
1835 else if (!strcmp(arg
, "--color"))
1836 options
->color_diff
= 1;
1837 else if (!strcmp(arg
, "--no-color"))
1838 options
->color_diff
= 0;
1839 else if (!strcmp(arg
, "-w") || !strcmp(arg
, "--ignore-all-space"))
1840 options
->xdl_opts
|= XDF_IGNORE_WHITESPACE
;
1841 else if (!strcmp(arg
, "-b") || !strcmp(arg
, "--ignore-space-change"))
1842 options
->xdl_opts
|= XDF_IGNORE_WHITESPACE_CHANGE
;
1843 else if (!strcmp(arg
, "--color-words"))
1844 options
->color_diff
= options
->color_diff_words
= 1;
1845 else if (!strcmp(arg
, "--no-renames"))
1846 options
->detect_rename
= 0;
1852 static int parse_num(const char **cp_p
)
1854 unsigned long num
, scale
;
1856 const char *cp
= *cp_p
;
1863 if ( !dot
&& ch
== '.' ) {
1866 } else if ( ch
== '%' ) {
1867 scale
= dot
? scale
*100 : 100;
1868 cp
++; /* % is always at the end */
1870 } else if ( ch
>= '0' && ch
<= '9' ) {
1871 if ( scale
< 100000 ) {
1873 num
= (num
*10) + (ch
-'0');
1882 /* user says num divided by scale and we say internally that
1883 * is MAX_SCORE * num / scale.
1885 return (num
>= scale
) ? MAX_SCORE
: (MAX_SCORE
* num
/ scale
);
1888 int diff_scoreopt_parse(const char *opt
)
1890 int opt1
, opt2
, cmd
;
1895 if (cmd
!= 'M' && cmd
!= 'C' && cmd
!= 'B')
1896 return -1; /* that is not a -M, -C nor -B option */
1898 opt1
= parse_num(&opt
);
1904 else if (*opt
!= '/')
1905 return -1; /* we expect -B80/99 or -B80 */
1908 opt2
= parse_num(&opt
);
1913 return opt1
| (opt2
<< 16);
1916 struct diff_queue_struct diff_queued_diff
;
1918 void diff_q(struct diff_queue_struct
*queue
, struct diff_filepair
*dp
)
1920 if (queue
->alloc
<= queue
->nr
) {
1921 queue
->alloc
= alloc_nr(queue
->alloc
);
1922 queue
->queue
= xrealloc(queue
->queue
,
1923 sizeof(dp
) * queue
->alloc
);
1925 queue
->queue
[queue
->nr
++] = dp
;
1928 struct diff_filepair
*diff_queue(struct diff_queue_struct
*queue
,
1929 struct diff_filespec
*one
,
1930 struct diff_filespec
*two
)
1932 struct diff_filepair
*dp
= xcalloc(1, sizeof(*dp
));
1940 void diff_free_filepair(struct diff_filepair
*p
)
1942 diff_free_filespec_data(p
->one
);
1943 diff_free_filespec_data(p
->two
);
1949 /* This is different from find_unique_abbrev() in that
1950 * it stuffs the result with dots for alignment.
1952 const char *diff_unique_abbrev(const unsigned char *sha1
, int len
)
1957 return sha1_to_hex(sha1
);
1959 abbrev
= find_unique_abbrev(sha1
, len
);
1961 return sha1_to_hex(sha1
);
1962 abblen
= strlen(abbrev
);
1964 static char hex
[41];
1965 if (len
< abblen
&& abblen
<= len
+ 2)
1966 sprintf(hex
, "%s%.*s", abbrev
, len
+3-abblen
, "..");
1968 sprintf(hex
, "%s...", abbrev
);
1971 return sha1_to_hex(sha1
);
1974 static void diff_flush_raw(struct diff_filepair
*p
,
1975 struct diff_options
*options
)
1979 int abbrev
= options
->abbrev
;
1980 const char *path_one
, *path_two
;
1981 int inter_name_termination
= '\t';
1982 int line_termination
= options
->line_termination
;
1984 if (!line_termination
)
1985 inter_name_termination
= 0;
1987 path_one
= p
->one
->path
;
1988 path_two
= p
->two
->path
;
1989 if (line_termination
) {
1990 path_one
= quote_one(path_one
);
1991 path_two
= quote_one(path_two
);
1995 sprintf(status
, "%c%03d", p
->status
,
1996 (int)(0.5 + p
->score
* 100.0/MAX_SCORE
));
1998 status
[0] = p
->status
;
2001 switch (p
->status
) {
2002 case DIFF_STATUS_COPIED
:
2003 case DIFF_STATUS_RENAMED
:
2006 case DIFF_STATUS_ADDED
:
2007 case DIFF_STATUS_DELETED
:
2014 if (!(options
->output_format
& DIFF_FORMAT_NAME_STATUS
)) {
2015 printf(":%06o %06o %s ",
2016 p
->one
->mode
, p
->two
->mode
,
2017 diff_unique_abbrev(p
->one
->sha1
, abbrev
));
2019 diff_unique_abbrev(p
->two
->sha1
, abbrev
));
2021 printf("%s%c%s", status
, inter_name_termination
, path_one
);
2023 printf("%c%s", inter_name_termination
, path_two
);
2024 putchar(line_termination
);
2025 if (path_one
!= p
->one
->path
)
2026 free((void*)path_one
);
2027 if (path_two
!= p
->two
->path
)
2028 free((void*)path_two
);
2031 static void diff_flush_name(struct diff_filepair
*p
, int line_termination
)
2033 char *path
= p
->two
->path
;
2035 if (line_termination
)
2036 path
= quote_one(p
->two
->path
);
2037 printf("%s%c", path
, line_termination
);
2038 if (p
->two
->path
!= path
)
2042 int diff_unmodified_pair(struct diff_filepair
*p
)
2044 /* This function is written stricter than necessary to support
2045 * the currently implemented transformers, but the idea is to
2046 * let transformers to produce diff_filepairs any way they want,
2047 * and filter and clean them up here before producing the output.
2049 struct diff_filespec
*one
, *two
;
2051 if (DIFF_PAIR_UNMERGED(p
))
2052 return 0; /* unmerged is interesting */
2057 /* deletion, addition, mode or type change
2058 * and rename are all interesting.
2060 if (DIFF_FILE_VALID(one
) != DIFF_FILE_VALID(two
) ||
2061 DIFF_PAIR_MODE_CHANGED(p
) ||
2062 strcmp(one
->path
, two
->path
))
2065 /* both are valid and point at the same path. that is, we are
2066 * dealing with a change.
2068 if (one
->sha1_valid
&& two
->sha1_valid
&&
2069 !hashcmp(one
->sha1
, two
->sha1
))
2070 return 1; /* no change */
2071 if (!one
->sha1_valid
&& !two
->sha1_valid
)
2072 return 1; /* both look at the same file on the filesystem. */
2076 static void diff_flush_patch(struct diff_filepair
*p
, struct diff_options
*o
)
2078 if (diff_unmodified_pair(p
))
2081 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
2082 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
2083 return; /* no tree diffs in patch format */
2088 static void diff_flush_stat(struct diff_filepair
*p
, struct diff_options
*o
,
2089 struct diffstat_t
*diffstat
)
2091 if (diff_unmodified_pair(p
))
2094 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
2095 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
2096 return; /* no tree diffs in patch format */
2098 run_diffstat(p
, o
, diffstat
);
2101 static void diff_flush_checkdiff(struct diff_filepair
*p
,
2102 struct diff_options
*o
)
2104 if (diff_unmodified_pair(p
))
2107 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
2108 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
2109 return; /* no tree diffs in patch format */
2111 run_checkdiff(p
, o
);
2114 int diff_queue_is_empty(void)
2116 struct diff_queue_struct
*q
= &diff_queued_diff
;
2118 for (i
= 0; i
< q
->nr
; i
++)
2119 if (!diff_unmodified_pair(q
->queue
[i
]))
2125 void diff_debug_filespec(struct diff_filespec
*s
, int x
, const char *one
)
2127 fprintf(stderr
, "queue[%d] %s (%s) %s %06o %s\n",
2130 DIFF_FILE_VALID(s
) ? "valid" : "invalid",
2132 s
->sha1_valid
? sha1_to_hex(s
->sha1
) : "");
2133 fprintf(stderr
, "queue[%d] %s size %lu flags %d\n",
2135 s
->size
, s
->xfrm_flags
);
2138 void diff_debug_filepair(const struct diff_filepair
*p
, int i
)
2140 diff_debug_filespec(p
->one
, i
, "one");
2141 diff_debug_filespec(p
->two
, i
, "two");
2142 fprintf(stderr
, "score %d, status %c stays %d broken %d\n",
2143 p
->score
, p
->status
? p
->status
: '?',
2144 p
->source_stays
, p
->broken_pair
);
2147 void diff_debug_queue(const char *msg
, struct diff_queue_struct
*q
)
2151 fprintf(stderr
, "%s\n", msg
);
2152 fprintf(stderr
, "q->nr = %d\n", q
->nr
);
2153 for (i
= 0; i
< q
->nr
; i
++) {
2154 struct diff_filepair
*p
= q
->queue
[i
];
2155 diff_debug_filepair(p
, i
);
2160 static void diff_resolve_rename_copy(void)
2163 struct diff_filepair
*p
, *pp
;
2164 struct diff_queue_struct
*q
= &diff_queued_diff
;
2166 diff_debug_queue("resolve-rename-copy", q
);
2168 for (i
= 0; i
< q
->nr
; i
++) {
2170 p
->status
= 0; /* undecided */
2171 if (DIFF_PAIR_UNMERGED(p
))
2172 p
->status
= DIFF_STATUS_UNMERGED
;
2173 else if (!DIFF_FILE_VALID(p
->one
))
2174 p
->status
= DIFF_STATUS_ADDED
;
2175 else if (!DIFF_FILE_VALID(p
->two
))
2176 p
->status
= DIFF_STATUS_DELETED
;
2177 else if (DIFF_PAIR_TYPE_CHANGED(p
))
2178 p
->status
= DIFF_STATUS_TYPE_CHANGED
;
2180 /* from this point on, we are dealing with a pair
2181 * whose both sides are valid and of the same type, i.e.
2182 * either in-place edit or rename/copy edit.
2184 else if (DIFF_PAIR_RENAME(p
)) {
2185 if (p
->source_stays
) {
2186 p
->status
= DIFF_STATUS_COPIED
;
2189 /* See if there is some other filepair that
2190 * copies from the same source as us. If so
2191 * we are a copy. Otherwise we are either a
2192 * copy if the path stays, or a rename if it
2193 * does not, but we already handled "stays" case.
2195 for (j
= i
+ 1; j
< q
->nr
; j
++) {
2197 if (strcmp(pp
->one
->path
, p
->one
->path
))
2198 continue; /* not us */
2199 if (!DIFF_PAIR_RENAME(pp
))
2200 continue; /* not a rename/copy */
2201 /* pp is a rename/copy from the same source */
2202 p
->status
= DIFF_STATUS_COPIED
;
2206 p
->status
= DIFF_STATUS_RENAMED
;
2208 else if (hashcmp(p
->one
->sha1
, p
->two
->sha1
) ||
2209 p
->one
->mode
!= p
->two
->mode
)
2210 p
->status
= DIFF_STATUS_MODIFIED
;
2212 /* This is a "no-change" entry and should not
2213 * happen anymore, but prepare for broken callers.
2215 error("feeding unmodified %s to diffcore",
2217 p
->status
= DIFF_STATUS_UNKNOWN
;
2220 diff_debug_queue("resolve-rename-copy done", q
);
2223 static int check_pair_status(struct diff_filepair
*p
)
2225 switch (p
->status
) {
2226 case DIFF_STATUS_UNKNOWN
:
2229 die("internal error in diff-resolve-rename-copy");
2235 static void flush_one_pair(struct diff_filepair
*p
, struct diff_options
*opt
)
2237 int fmt
= opt
->output_format
;
2239 if (fmt
& DIFF_FORMAT_CHECKDIFF
)
2240 diff_flush_checkdiff(p
, opt
);
2241 else if (fmt
& (DIFF_FORMAT_RAW
| DIFF_FORMAT_NAME_STATUS
))
2242 diff_flush_raw(p
, opt
);
2243 else if (fmt
& DIFF_FORMAT_NAME
)
2244 diff_flush_name(p
, opt
->line_termination
);
2247 static void show_file_mode_name(const char *newdelete
, struct diff_filespec
*fs
)
2250 printf(" %s mode %06o %s\n", newdelete
, fs
->mode
, fs
->path
);
2252 printf(" %s %s\n", newdelete
, fs
->path
);
2256 static void show_mode_change(struct diff_filepair
*p
, int show_name
)
2258 if (p
->one
->mode
&& p
->two
->mode
&& p
->one
->mode
!= p
->two
->mode
) {
2260 printf(" mode change %06o => %06o %s\n",
2261 p
->one
->mode
, p
->two
->mode
, p
->two
->path
);
2263 printf(" mode change %06o => %06o\n",
2264 p
->one
->mode
, p
->two
->mode
);
2268 static void show_rename_copy(const char *renamecopy
, struct diff_filepair
*p
)
2270 const char *old
, *new;
2272 /* Find common prefix */
2276 const char *slash_old
, *slash_new
;
2277 slash_old
= strchr(old
, '/');
2278 slash_new
= strchr(new, '/');
2281 slash_old
- old
!= slash_new
- new ||
2282 memcmp(old
, new, slash_new
- new))
2284 old
= slash_old
+ 1;
2285 new = slash_new
+ 1;
2287 /* p->one->path thru old is the common prefix, and old and new
2288 * through the end of names are renames
2290 if (old
!= p
->one
->path
)
2291 printf(" %s %.*s{%s => %s} (%d%%)\n", renamecopy
,
2292 (int)(old
- p
->one
->path
), p
->one
->path
,
2293 old
, new, (int)(0.5 + p
->score
* 100.0/MAX_SCORE
));
2295 printf(" %s %s => %s (%d%%)\n", renamecopy
,
2296 p
->one
->path
, p
->two
->path
,
2297 (int)(0.5 + p
->score
* 100.0/MAX_SCORE
));
2298 show_mode_change(p
, 0);
2301 static void diff_summary(struct diff_filepair
*p
)
2304 case DIFF_STATUS_DELETED
:
2305 show_file_mode_name("delete", p
->one
);
2307 case DIFF_STATUS_ADDED
:
2308 show_file_mode_name("create", p
->two
);
2310 case DIFF_STATUS_COPIED
:
2311 show_rename_copy("copy", p
);
2313 case DIFF_STATUS_RENAMED
:
2314 show_rename_copy("rename", p
);
2318 printf(" rewrite %s (%d%%)\n", p
->two
->path
,
2319 (int)(0.5 + p
->score
* 100.0/MAX_SCORE
));
2320 show_mode_change(p
, 0);
2321 } else show_mode_change(p
, 1);
2327 struct xdiff_emit_state xm
;
2332 static int remove_space(char *line
, int len
)
2338 for (i
= 0; i
< len
; i
++)
2339 if (!isspace((c
= line
[i
])))
2345 static void patch_id_consume(void *priv
, char *line
, unsigned long len
)
2347 struct patch_id_t
*data
= priv
;
2350 /* Ignore line numbers when computing the SHA1 of the patch */
2351 if (!strncmp(line
, "@@ -", 4))
2354 new_len
= remove_space(line
, len
);
2356 SHA1_Update(data
->ctx
, line
, new_len
);
2357 data
->patchlen
+= new_len
;
2360 /* returns 0 upon success, and writes result into sha1 */
2361 static int diff_get_patch_id(struct diff_options
*options
, unsigned char *sha1
)
2363 struct diff_queue_struct
*q
= &diff_queued_diff
;
2366 struct patch_id_t data
;
2367 char buffer
[PATH_MAX
* 4 + 20];
2370 memset(&data
, 0, sizeof(struct patch_id_t
));
2372 data
.xm
.consume
= patch_id_consume
;
2374 for (i
= 0; i
< q
->nr
; i
++) {
2379 struct diff_filepair
*p
= q
->queue
[i
];
2383 return error("internal diff status error");
2384 if (p
->status
== DIFF_STATUS_UNKNOWN
)
2386 if (diff_unmodified_pair(p
))
2388 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
2389 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
2391 if (DIFF_PAIR_UNMERGED(p
))
2394 diff_fill_sha1_info(p
->one
);
2395 diff_fill_sha1_info(p
->two
);
2396 if (fill_mmfile(&mf1
, p
->one
) < 0 ||
2397 fill_mmfile(&mf2
, p
->two
) < 0)
2398 return error("unable to read files to diff");
2400 /* Maybe hash p->two? into the patch id? */
2401 if (mmfile_is_binary(&mf2
))
2404 len1
= remove_space(p
->one
->path
, strlen(p
->one
->path
));
2405 len2
= remove_space(p
->two
->path
, strlen(p
->two
->path
));
2406 if (p
->one
->mode
== 0)
2407 len1
= snprintf(buffer
, sizeof(buffer
),
2408 "diff--gita/%.*sb/%.*s"
2415 len2
, p
->two
->path
);
2416 else if (p
->two
->mode
== 0)
2417 len1
= snprintf(buffer
, sizeof(buffer
),
2418 "diff--gita/%.*sb/%.*s"
2419 "deletedfilemode%06o"
2425 len1
, p
->one
->path
);
2427 len1
= snprintf(buffer
, sizeof(buffer
),
2428 "diff--gita/%.*sb/%.*s"
2434 len2
, p
->two
->path
);
2435 SHA1_Update(&ctx
, buffer
, len1
);
2437 xpp
.flags
= XDF_NEED_MINIMAL
;
2439 xecfg
.flags
= XDL_EMIT_FUNCNAMES
;
2440 ecb
.outf
= xdiff_outf
;
2442 xdl_diff(&mf1
, &mf2
, &xpp
, &xecfg
, &ecb
);
2445 SHA1_Final(sha1
, &ctx
);
2449 int diff_flush_patch_id(struct diff_options
*options
, unsigned char *sha1
)
2451 struct diff_queue_struct
*q
= &diff_queued_diff
;
2453 int result
= diff_get_patch_id(options
, sha1
);
2455 for (i
= 0; i
< q
->nr
; i
++)
2456 diff_free_filepair(q
->queue
[i
]);
2460 q
->nr
= q
->alloc
= 0;
2465 static int is_summary_empty(const struct diff_queue_struct
*q
)
2469 for (i
= 0; i
< q
->nr
; i
++) {
2470 const struct diff_filepair
*p
= q
->queue
[i
];
2472 switch (p
->status
) {
2473 case DIFF_STATUS_DELETED
:
2474 case DIFF_STATUS_ADDED
:
2475 case DIFF_STATUS_COPIED
:
2476 case DIFF_STATUS_RENAMED
:
2481 if (p
->one
->mode
&& p
->two
->mode
&&
2482 p
->one
->mode
!= p
->two
->mode
)
2490 void diff_flush(struct diff_options
*options
)
2492 struct diff_queue_struct
*q
= &diff_queued_diff
;
2493 int i
, output_format
= options
->output_format
;
2497 * Order: raw, stat, summary, patch
2498 * or: name/name-status/checkdiff (other bits clear)
2503 if (output_format
& (DIFF_FORMAT_RAW
|
2505 DIFF_FORMAT_NAME_STATUS
|
2506 DIFF_FORMAT_CHECKDIFF
)) {
2507 for (i
= 0; i
< q
->nr
; i
++) {
2508 struct diff_filepair
*p
= q
->queue
[i
];
2509 if (check_pair_status(p
))
2510 flush_one_pair(p
, options
);
2515 if (output_format
& DIFF_FORMAT_DIFFSTAT
) {
2516 struct diffstat_t diffstat
;
2518 memset(&diffstat
, 0, sizeof(struct diffstat_t
));
2519 diffstat
.xm
.consume
= diffstat_consume
;
2520 for (i
= 0; i
< q
->nr
; i
++) {
2521 struct diff_filepair
*p
= q
->queue
[i
];
2522 if (check_pair_status(p
))
2523 diff_flush_stat(p
, options
, &diffstat
);
2525 show_stats(&diffstat
, options
);
2529 if (output_format
& DIFF_FORMAT_SUMMARY
&& !is_summary_empty(q
)) {
2530 for (i
= 0; i
< q
->nr
; i
++)
2531 diff_summary(q
->queue
[i
]);
2535 if (output_format
& DIFF_FORMAT_PATCH
) {
2537 if (options
->stat_sep
) {
2538 /* attach patch instead of inline */
2539 fputs(options
->stat_sep
, stdout
);
2541 putchar(options
->line_termination
);
2545 for (i
= 0; i
< q
->nr
; i
++) {
2546 struct diff_filepair
*p
= q
->queue
[i
];
2547 if (check_pair_status(p
))
2548 diff_flush_patch(p
, options
);
2552 if (output_format
& DIFF_FORMAT_CALLBACK
)
2553 options
->format_callback(q
, options
, options
->format_callback_data
);
2555 for (i
= 0; i
< q
->nr
; i
++)
2556 diff_free_filepair(q
->queue
[i
]);
2560 q
->nr
= q
->alloc
= 0;
2563 static void diffcore_apply_filter(const char *filter
)
2566 struct diff_queue_struct
*q
= &diff_queued_diff
;
2567 struct diff_queue_struct outq
;
2569 outq
.nr
= outq
.alloc
= 0;
2574 if (strchr(filter
, DIFF_STATUS_FILTER_AON
)) {
2576 for (i
= found
= 0; !found
&& i
< q
->nr
; i
++) {
2577 struct diff_filepair
*p
= q
->queue
[i
];
2578 if (((p
->status
== DIFF_STATUS_MODIFIED
) &&
2580 strchr(filter
, DIFF_STATUS_FILTER_BROKEN
)) ||
2582 strchr(filter
, DIFF_STATUS_MODIFIED
)))) ||
2583 ((p
->status
!= DIFF_STATUS_MODIFIED
) &&
2584 strchr(filter
, p
->status
)))
2590 /* otherwise we will clear the whole queue
2591 * by copying the empty outq at the end of this
2592 * function, but first clear the current entries
2595 for (i
= 0; i
< q
->nr
; i
++)
2596 diff_free_filepair(q
->queue
[i
]);
2599 /* Only the matching ones */
2600 for (i
= 0; i
< q
->nr
; i
++) {
2601 struct diff_filepair
*p
= q
->queue
[i
];
2603 if (((p
->status
== DIFF_STATUS_MODIFIED
) &&
2605 strchr(filter
, DIFF_STATUS_FILTER_BROKEN
)) ||
2607 strchr(filter
, DIFF_STATUS_MODIFIED
)))) ||
2608 ((p
->status
!= DIFF_STATUS_MODIFIED
) &&
2609 strchr(filter
, p
->status
)))
2612 diff_free_filepair(p
);
2619 void diffcore_std(struct diff_options
*options
)
2621 if (options
->break_opt
!= -1)
2622 diffcore_break(options
->break_opt
);
2623 if (options
->detect_rename
)
2624 diffcore_rename(options
);
2625 if (options
->break_opt
!= -1)
2626 diffcore_merge_broken();
2627 if (options
->pickaxe
)
2628 diffcore_pickaxe(options
->pickaxe
, options
->pickaxe_opts
);
2629 if (options
->orderfile
)
2630 diffcore_order(options
->orderfile
);
2631 diff_resolve_rename_copy();
2632 diffcore_apply_filter(options
->filter
);
2636 void diffcore_std_no_resolve(struct diff_options
*options
)
2638 if (options
->pickaxe
)
2639 diffcore_pickaxe(options
->pickaxe
, options
->pickaxe_opts
);
2640 if (options
->orderfile
)
2641 diffcore_order(options
->orderfile
);
2642 diffcore_apply_filter(options
->filter
);
2645 void diff_addremove(struct diff_options
*options
,
2646 int addremove
, unsigned mode
,
2647 const unsigned char *sha1
,
2648 const char *base
, const char *path
)
2650 char concatpath
[PATH_MAX
];
2651 struct diff_filespec
*one
, *two
;
2653 /* This may look odd, but it is a preparation for
2654 * feeding "there are unchanged files which should
2655 * not produce diffs, but when you are doing copy
2656 * detection you would need them, so here they are"
2657 * entries to the diff-core. They will be prefixed
2658 * with something like '=' or '*' (I haven't decided
2659 * which but should not make any difference).
2660 * Feeding the same new and old to diff_change()
2661 * also has the same effect.
2662 * Before the final output happens, they are pruned after
2663 * merged into rename/copy pairs as appropriate.
2665 if (options
->reverse_diff
)
2666 addremove
= (addremove
== '+' ? '-' :
2667 addremove
== '-' ? '+' : addremove
);
2669 if (!path
) path
= "";
2670 sprintf(concatpath
, "%s%s", base
, path
);
2671 one
= alloc_filespec(concatpath
);
2672 two
= alloc_filespec(concatpath
);
2674 if (addremove
!= '+')
2675 fill_filespec(one
, sha1
, mode
);
2676 if (addremove
!= '-')
2677 fill_filespec(two
, sha1
, mode
);
2679 diff_queue(&diff_queued_diff
, one
, two
);
2682 void diff_change(struct diff_options
*options
,
2683 unsigned old_mode
, unsigned new_mode
,
2684 const unsigned char *old_sha1
,
2685 const unsigned char *new_sha1
,
2686 const char *base
, const char *path
)
2688 char concatpath
[PATH_MAX
];
2689 struct diff_filespec
*one
, *two
;
2691 if (options
->reverse_diff
) {
2693 const unsigned char *tmp_c
;
2694 tmp
= old_mode
; old_mode
= new_mode
; new_mode
= tmp
;
2695 tmp_c
= old_sha1
; old_sha1
= new_sha1
; new_sha1
= tmp_c
;
2697 if (!path
) path
= "";
2698 sprintf(concatpath
, "%s%s", base
, path
);
2699 one
= alloc_filespec(concatpath
);
2700 two
= alloc_filespec(concatpath
);
2701 fill_filespec(one
, old_sha1
, old_mode
);
2702 fill_filespec(two
, new_sha1
, new_mode
);
2704 diff_queue(&diff_queued_diff
, one
, two
);
2707 void diff_unmerge(struct diff_options
*options
,
2710 struct diff_filespec
*one
, *two
;
2711 one
= alloc_filespec(path
);
2712 two
= alloc_filespec(path
);
2713 diff_queue(&diff_queued_diff
, one
, two
);