2 * Copyright (C) 2005 Junio C Hamano
9 #include "xdiff-interface.h"
12 #ifdef NO_FAST_WORKING_DIRECTORY
13 #define FAST_WORKING_DIRECTORY 0
15 #define FAST_WORKING_DIRECTORY 1
18 static int use_size_cache;
20 static int diff_detect_rename_default;
21 static int diff_rename_limit_default = -1;
22 static int diff_use_color_default;
24 static char diff_colors[][COLOR_MAXLEN] = {
26 "", /* PLAIN (normal) */
27 "\033[1m", /* METAINFO (bold) */
28 "\033[36m", /* FRAGINFO (cyan) */
29 "\033[31m", /* OLD (red) */
30 "\033[32m", /* NEW (green) */
31 "\033[33m", /* COMMIT (yellow) */
32 "\033[41m", /* WHITESPACE (red background) */
35 static int parse_diff_color_slot(const char *var, int ofs)
37 if (!strcasecmp(var+ofs, "plain"))
39 if (!strcasecmp(var+ofs, "meta"))
41 if (!strcasecmp(var+ofs, "frag"))
43 if (!strcasecmp(var+ofs, "old"))
45 if (!strcasecmp(var+ofs, "new"))
47 if (!strcasecmp(var+ofs, "commit"))
49 if (!strcasecmp(var+ofs, "whitespace"))
50 return DIFF_WHITESPACE;
51 die("bad config variable '%s'", var);
55 * These are to give UI layer defaults.
56 * The core-level commands such as git-diff-files should
57 * never be affected by the setting of diff.renames
58 * the user happens to have in the configuration file.
60 int git_diff_ui_config(const char *var, const char *value)
62 if (!strcmp(var, "diff.renamelimit")) {
63 diff_rename_limit_default = git_config_int(var, value);
66 if (!strcmp(var, "diff.color") || !strcmp(var, "color.diff")) {
67 diff_use_color_default = git_config_colorbool(var, value);
70 if (!strcmp(var, "diff.renames")) {
72 diff_detect_rename_default = DIFF_DETECT_RENAME;
73 else if (!strcasecmp(value, "copies") ||
74 !strcasecmp(value, "copy"))
75 diff_detect_rename_default = DIFF_DETECT_COPY;
76 else if (git_config_bool(var,value))
77 diff_detect_rename_default = DIFF_DETECT_RENAME;
80 if (!strncmp(var, "diff.color.", 11) || !strncmp(var, "color.diff.", 11)) {
81 int slot = parse_diff_color_slot(var, 11);
82 color_parse(value, var, diff_colors[slot]);
85 return git_default_config(var, value);
88 static char *quote_one(const char *str)
95 needlen = quote_c_style(str, NULL, NULL, 0);
98 xp = xmalloc(needlen + 1);
99 quote_c_style(str, xp, NULL, 0);
103 static char *quote_two(const char *one, const char *two)
105 int need_one = quote_c_style(one, NULL, NULL, 1);
106 int need_two = quote_c_style(two, NULL, NULL, 1);
109 if (need_one + need_two) {
110 if (!need_one) need_one = strlen(one);
111 if (!need_two) need_one = strlen(two);
113 xp = xmalloc(need_one + need_two + 3);
115 quote_c_style(one, xp + 1, NULL, 1);
116 quote_c_style(two, xp + need_one + 1, NULL, 1);
117 strcpy(xp + need_one + need_two + 1, "\"");
120 need_one = strlen(one);
121 need_two = strlen(two);
122 xp = xmalloc(need_one + need_two + 1);
124 strcpy(xp + need_one, two);
128 static const char *external_diff(void)
130 static const char *external_diff_cmd = NULL;
131 static int done_preparing = 0;
134 return external_diff_cmd;
135 external_diff_cmd = getenv("GIT_EXTERNAL_DIFF");
137 return external_diff_cmd;
140 #define TEMPFILE_PATH_LEN 50
142 static struct diff_tempfile {
143 const char *name; /* filename external diff should read from */
146 char tmp_path[TEMPFILE_PATH_LEN];
149 static int count_lines(const char *data, int size)
151 int count, ch, completely_empty = 1, nl_just_seen = 0;
158 completely_empty = 0;
162 completely_empty = 0;
165 if (completely_empty)
168 count++; /* no trailing newline */
172 static void print_line_count(int count)
182 printf("1,%d", count);
187 static void copy_file(int prefix, const char *data, int size)
189 int ch, nl_just_seen = 1;
201 printf("\n\\ No newline at end of file\n");
204 static void emit_rewrite_diff(const char *name_a,
206 struct diff_filespec *one,
207 struct diff_filespec *two)
210 diff_populate_filespec(one, 0);
211 diff_populate_filespec(two, 0);
212 lc_a = count_lines(one->data, one->size);
213 lc_b = count_lines(two->data, two->size);
214 name_a += (*name_a == '/');
215 name_b += (*name_b == '/');
216 printf("--- a/%s\n+++ b/%s\n@@ -", name_a, name_b);
217 print_line_count(lc_a);
219 print_line_count(lc_b);
222 copy_file('-', one->data, one->size);
224 copy_file('+', two->data, two->size);
227 static int fill_mmfile(mmfile_t *mf, struct diff_filespec *one)
229 if (!DIFF_FILE_VALID(one)) {
230 mf->ptr = (char *)""; /* does not matter */
234 else if (diff_populate_filespec(one, 0))
237 mf->size = one->size;
241 struct diff_words_buffer {
244 long current; /* output pointer */
245 int suppressed_newline;
248 static void diff_words_append(char *line, unsigned long len,
249 struct diff_words_buffer *buffer)
251 if (buffer->text.size + len > buffer->alloc) {
252 buffer->alloc = (buffer->text.size + len) * 3 / 2;
253 buffer->text.ptr = xrealloc(buffer->text.ptr, buffer->alloc);
257 memcpy(buffer->text.ptr + buffer->text.size, line, len);
258 buffer->text.size += len;
261 struct diff_words_data {
262 struct xdiff_emit_state xm;
263 struct diff_words_buffer minus, plus;
266 static void print_word(struct diff_words_buffer *buffer, int len, int color,
267 int suppress_newline)
275 ptr = buffer->text.ptr + buffer->current;
276 buffer->current += len;
278 if (ptr[len - 1] == '\n') {
283 fputs(diff_get_color(1, color), stdout);
284 fwrite(ptr, len, 1, stdout);
285 fputs(diff_get_color(1, DIFF_RESET), stdout);
288 if (suppress_newline)
289 buffer->suppressed_newline = 1;
295 static void fn_out_diff_words_aux(void *priv, char *line, unsigned long len)
297 struct diff_words_data *diff_words = priv;
299 if (diff_words->minus.suppressed_newline) {
302 diff_words->minus.suppressed_newline = 0;
308 print_word(&diff_words->minus, len, DIFF_FILE_OLD, 1);
311 print_word(&diff_words->plus, len, DIFF_FILE_NEW, 0);
314 print_word(&diff_words->plus, len, DIFF_PLAIN, 0);
315 diff_words->minus.current += len;
320 /* this executes the word diff on the accumulated buffers */
321 static void diff_words_show(struct diff_words_data *diff_words)
326 mmfile_t minus, plus;
329 minus.size = diff_words->minus.text.size;
330 minus.ptr = xmalloc(minus.size);
331 memcpy(minus.ptr, diff_words->minus.text.ptr, minus.size);
332 for (i = 0; i < minus.size; i++)
333 if (isspace(minus.ptr[i]))
335 diff_words->minus.current = 0;
337 plus.size = diff_words->plus.text.size;
338 plus.ptr = xmalloc(plus.size);
339 memcpy(plus.ptr, diff_words->plus.text.ptr, plus.size);
340 for (i = 0; i < plus.size; i++)
341 if (isspace(plus.ptr[i]))
343 diff_words->plus.current = 0;
345 xpp.flags = XDF_NEED_MINIMAL;
346 xecfg.ctxlen = diff_words->minus.alloc + diff_words->plus.alloc;
348 ecb.outf = xdiff_outf;
349 ecb.priv = diff_words;
350 diff_words->xm.consume = fn_out_diff_words_aux;
351 xdl_diff(&minus, &plus, &xpp, &xecfg, &ecb);
355 diff_words->minus.text.size = diff_words->plus.text.size = 0;
357 if (diff_words->minus.suppressed_newline) {
359 diff_words->minus.suppressed_newline = 0;
363 struct emit_callback {
364 struct xdiff_emit_state xm;
365 int nparents, color_diff;
366 const char **label_path;
367 struct diff_words_data *diff_words;
370 static void free_diff_words_data(struct emit_callback *ecbdata)
372 if (ecbdata->diff_words) {
374 if (ecbdata->diff_words->minus.text.size ||
375 ecbdata->diff_words->plus.text.size)
376 diff_words_show(ecbdata->diff_words);
378 if (ecbdata->diff_words->minus.text.ptr)
379 free (ecbdata->diff_words->minus.text.ptr);
380 if (ecbdata->diff_words->plus.text.ptr)
381 free (ecbdata->diff_words->plus.text.ptr);
382 free(ecbdata->diff_words);
383 ecbdata->diff_words = NULL;
387 const char *diff_get_color(int diff_use_color, enum color_diff ix)
390 return diff_colors[ix];
394 static void emit_line(const char *set, const char *reset, const char *line, int len)
396 if (len > 0 && line[len-1] == '\n')
399 fwrite(line, len, 1, stdout);
403 static void emit_add_line(const char *reset, struct emit_callback *ecbdata, const char *line, int len)
405 int col0 = ecbdata->nparents;
406 int last_tab_in_indent = -1;
407 int last_space_in_indent = -1;
410 int need_highlight_leading_space = 0;
411 const char *ws = diff_get_color(ecbdata->color_diff, DIFF_WHITESPACE);
412 const char *set = diff_get_color(ecbdata->color_diff, DIFF_FILE_NEW);
415 emit_line(set, reset, line, len);
419 /* The line is a newly added line. Does it have funny leading
420 * whitespaces? In indent, SP should never precede a TAB.
422 for (i = col0; i < len; i++) {
423 if (line[i] == '\t') {
424 last_tab_in_indent = i;
425 if (0 <= last_space_in_indent)
426 need_highlight_leading_space = 1;
428 else if (line[i] == ' ')
429 last_space_in_indent = i;
434 fwrite(line, col0, 1, stdout);
435 fputs(reset, stdout);
436 if (((i == len) || line[i] == '\n') && i != col0) {
437 /* The whole line was indent */
438 emit_line(ws, reset, line + col0, len - col0);
442 if (need_highlight_leading_space) {
443 while (i < last_tab_in_indent) {
444 if (line[i] == ' ') {
447 fputs(reset, stdout);
455 if (line[tail] == '\n' && i < tail)
458 if (!isspace(line[tail]))
462 if ((i < tail && line[tail + 1] != '\n')) {
463 /* This has whitespace between tail+1..len */
465 fwrite(line + i, tail - i + 1, 1, stdout);
466 fputs(reset, stdout);
467 emit_line(ws, reset, line + tail + 1, len - tail - 1);
470 emit_line(set, reset, line + i, len - i);
473 static void fn_out_consume(void *priv, char *line, unsigned long len)
477 struct emit_callback *ecbdata = priv;
478 const char *set = diff_get_color(ecbdata->color_diff, DIFF_METAINFO);
479 const char *reset = diff_get_color(ecbdata->color_diff, DIFF_RESET);
481 if (ecbdata->label_path[0]) {
482 printf("%s--- %s%s\n", set, ecbdata->label_path[0], reset);
483 printf("%s+++ %s%s\n", set, ecbdata->label_path[1], reset);
484 ecbdata->label_path[0] = ecbdata->label_path[1] = NULL;
487 /* This is not really necessary for now because
488 * this codepath only deals with two-way diffs.
490 for (i = 0; i < len && line[i] == '@'; i++)
492 if (2 <= i && i < len && line[i] == ' ') {
493 ecbdata->nparents = i - 1;
494 emit_line(diff_get_color(ecbdata->color_diff, DIFF_FRAGINFO),
499 if (len < ecbdata->nparents) {
501 emit_line(reset, reset, line, len);
506 if (ecbdata->diff_words && ecbdata->nparents != 1)
507 /* fall back to normal diff */
508 free_diff_words_data(ecbdata);
509 if (ecbdata->diff_words) {
510 if (line[0] == '-') {
511 diff_words_append(line, len,
512 &ecbdata->diff_words->minus);
514 } else if (line[0] == '+') {
515 diff_words_append(line, len,
516 &ecbdata->diff_words->plus);
519 if (ecbdata->diff_words->minus.text.size ||
520 ecbdata->diff_words->plus.text.size)
521 diff_words_show(ecbdata->diff_words);
524 emit_line(set, reset, line, len);
527 for (i = 0; i < ecbdata->nparents && len; i++) {
529 color = DIFF_FILE_OLD;
530 else if (line[i] == '+')
531 color = DIFF_FILE_NEW;
534 if (color != DIFF_FILE_NEW) {
535 emit_line(diff_get_color(ecbdata->color_diff, color),
539 emit_add_line(reset, ecbdata, line, len);
542 static char *pprint_rename(const char *a, const char *b)
547 int pfx_length, sfx_length;
548 int len_a = strlen(a);
549 int len_b = strlen(b);
550 int qlen_a = quote_c_style(a, NULL, NULL, 0);
551 int qlen_b = quote_c_style(b, NULL, NULL, 0);
553 if (qlen_a || qlen_b) {
554 if (qlen_a) len_a = qlen_a;
555 if (qlen_b) len_b = qlen_b;
556 name = xmalloc( len_a + len_b + 5 );
558 quote_c_style(a, name, NULL, 0);
560 memcpy(name, a, len_a);
561 memcpy(name + len_a, " => ", 4);
563 quote_c_style(b, name + len_a + 4, NULL, 0);
565 memcpy(name + len_a + 4, b, len_b + 1);
569 /* Find common prefix */
571 while (*old && *new && *old == *new) {
573 pfx_length = old - a + 1;
578 /* Find common suffix */
582 while (a <= old && b <= new && *old == *new) {
584 sfx_length = len_a - (old - a);
590 * pfx{mid-a => mid-b}sfx
591 * {pfx-a => pfx-b}sfx
592 * pfx{sfx-a => sfx-b}
595 if (pfx_length + sfx_length) {
596 int a_midlen = len_a - pfx_length - sfx_length;
597 int b_midlen = len_b - pfx_length - sfx_length;
598 if (a_midlen < 0) a_midlen = 0;
599 if (b_midlen < 0) b_midlen = 0;
601 name = xmalloc(pfx_length + a_midlen + b_midlen + sfx_length + 7);
602 sprintf(name, "%.*s{%.*s => %.*s}%s",
604 a_midlen, a + pfx_length,
605 b_midlen, b + pfx_length,
606 a + len_a - sfx_length);
609 name = xmalloc(len_a + len_b + 5);
610 sprintf(name, "%s => %s", a, b);
616 struct xdiff_emit_state xm;
620 struct diffstat_file {
622 unsigned is_unmerged:1;
623 unsigned is_binary:1;
624 unsigned is_renamed:1;
625 unsigned int added, deleted;
629 static struct diffstat_file *diffstat_add(struct diffstat_t *diffstat,
633 struct diffstat_file *x;
634 x = xcalloc(sizeof (*x), 1);
635 if (diffstat->nr == diffstat->alloc) {
636 diffstat->alloc = alloc_nr(diffstat->alloc);
637 diffstat->files = xrealloc(diffstat->files,
638 diffstat->alloc * sizeof(x));
640 diffstat->files[diffstat->nr++] = x;
642 x->name = pprint_rename(name_a, name_b);
646 x->name = xstrdup(name_a);
650 static void diffstat_consume(void *priv, char *line, unsigned long len)
652 struct diffstat_t *diffstat = priv;
653 struct diffstat_file *x = diffstat->files[diffstat->nr - 1];
657 else if (line[0] == '-')
661 const char mime_boundary_leader[] = "------------";
663 static int scale_linear(int it, int width, int max_change)
666 * make sure that at least one '-' is printed if there were deletions,
667 * and likewise for '+'.
671 return ((it - 1) * (width - 1) + max_change - 1) / (max_change - 1);
674 static void show_name(const char *prefix, const char *name, int len,
675 const char *reset, const char *set)
677 printf(" %s%s%-*s%s |", set, prefix, len, name, reset);
680 static void show_graph(char ch, int cnt, const char *set, const char *reset)
690 static void show_stats(struct diffstat_t* data, struct diff_options *options)
692 int i, len, add, del, total, adds = 0, dels = 0;
693 int max_change = 0, max_len = 0;
694 int total_files = data->nr;
695 int width, name_width;
696 const char *reset, *set, *add_c, *del_c;
701 width = options->stat_width ? options->stat_width : 80;
702 name_width = options->stat_name_width ? options->stat_name_width : 50;
704 /* Sanity: give at least 5 columns to the graph,
705 * but leave at least 10 columns for the name.
707 if (width < name_width + 15) {
708 if (name_width <= 25)
709 width = name_width + 15;
711 name_width = width - 15;
714 /* Find the longest filename and max number of changes */
715 reset = diff_get_color(options->color_diff, DIFF_RESET);
716 set = diff_get_color(options->color_diff, DIFF_PLAIN);
717 add_c = diff_get_color(options->color_diff, DIFF_FILE_NEW);
718 del_c = diff_get_color(options->color_diff, DIFF_FILE_OLD);
720 for (i = 0; i < data->nr; i++) {
721 struct diffstat_file *file = data->files[i];
722 int change = file->added + file->deleted;
724 if (!file->is_renamed) { /* renames are already quoted by pprint_rename */
725 len = quote_c_style(file->name, NULL, NULL, 0);
727 char *qname = xmalloc(len + 1);
728 quote_c_style(file->name, qname, NULL, 0);
734 len = strlen(file->name);
738 if (file->is_binary || file->is_unmerged)
740 if (max_change < change)
744 /* Compute the width of the graph part;
745 * 10 is for one blank at the beginning of the line plus
746 * " | count " between the name and the graph.
748 * From here on, name_width is the width of the name area,
749 * and width is the width of the graph area.
751 name_width = (name_width < max_len) ? name_width : max_len;
752 if (width < (name_width + 10) + max_change)
753 width = width - (name_width + 10);
757 for (i = 0; i < data->nr; i++) {
758 const char *prefix = "";
759 char *name = data->files[i]->name;
760 int added = data->files[i]->added;
761 int deleted = data->files[i]->deleted;
765 * "scale" the filename
768 name_len = strlen(name);
769 if (name_width < name_len) {
773 name += name_len - len;
774 slash = strchr(name, '/');
779 if (data->files[i]->is_binary) {
780 show_name(prefix, name, len, reset, set);
782 goto free_diffstat_file;
784 else if (data->files[i]->is_unmerged) {
785 show_name(prefix, name, len, reset, set);
786 printf(" Unmerged\n");
787 goto free_diffstat_file;
789 else if (!data->files[i]->is_renamed &&
790 (added + deleted == 0)) {
792 goto free_diffstat_file;
796 * scale the add/delete
804 if (width <= max_change) {
805 add = scale_linear(add, width, max_change);
806 del = scale_linear(del, width, max_change);
809 show_name(prefix, name, len, reset, set);
810 printf("%5d ", added + deleted);
811 show_graph('+', add, add_c, reset);
812 show_graph('-', del, del_c, reset);
815 free(data->files[i]->name);
816 free(data->files[i]);
819 printf("%s %d files changed, %d insertions(+), %d deletions(-)%s\n",
820 set, total_files, adds, dels, reset);
823 static void show_shortstats(struct diffstat_t* data)
825 int i, adds = 0, dels = 0, total_files = data->nr;
830 for (i = 0; i < data->nr; i++) {
831 if (!data->files[i]->is_binary &&
832 !data->files[i]->is_unmerged) {
833 int added = data->files[i]->added;
834 int deleted= data->files[i]->deleted;
835 if (!data->files[i]->is_renamed &&
836 (added + deleted == 0)) {
843 free(data->files[i]->name);
844 free(data->files[i]);
848 printf(" %d files changed, %d insertions(+), %d deletions(-)\n",
849 total_files, adds, dels);
852 static void show_numstat(struct diffstat_t* data, struct diff_options *options)
856 for (i = 0; i < data->nr; i++) {
857 struct diffstat_file *file = data->files[i];
862 printf("%d\t%d\t", file->added, file->deleted);
863 if (options->line_termination && !file->is_renamed &&
864 quote_c_style(file->name, NULL, NULL, 0))
865 quote_c_style(file->name, NULL, stdout, 0);
867 fputs(file->name, stdout);
868 putchar(options->line_termination);
873 struct xdiff_emit_state xm;
874 const char *filename;
878 static void checkdiff_consume(void *priv, char *line, unsigned long len)
880 struct checkdiff_t *data = priv;
882 if (line[0] == '+') {
885 /* check space before tab */
886 for (i = 1; i < len && (line[i] == ' ' || line[i] == '\t'); i++)
889 if (line[i - 1] == '\t' && spaces)
890 printf("%s:%d: space before tab:%.*s\n",
891 data->filename, data->lineno, (int)len, line);
893 /* check white space at line end */
894 if (line[len - 1] == '\n')
896 if (isspace(line[len - 1]))
897 printf("%s:%d: white space at end: %.*s\n",
898 data->filename, data->lineno, (int)len, line);
901 } else if (line[0] == ' ')
903 else if (line[0] == '@') {
904 char *plus = strchr(line, '+');
906 data->lineno = strtol(plus, NULL, 10);
912 static unsigned char *deflate_it(char *data,
914 unsigned long *result_size)
917 unsigned char *deflated;
920 memset(&stream, 0, sizeof(stream));
921 deflateInit(&stream, zlib_compression_level);
922 bound = deflateBound(&stream, size);
923 deflated = xmalloc(bound);
924 stream.next_out = deflated;
925 stream.avail_out = bound;
927 stream.next_in = (unsigned char *)data;
928 stream.avail_in = size;
929 while (deflate(&stream, Z_FINISH) == Z_OK)
932 *result_size = stream.total_out;
936 static void emit_binary_diff_body(mmfile_t *one, mmfile_t *two)
942 unsigned long orig_size;
943 unsigned long delta_size;
944 unsigned long deflate_size;
945 unsigned long data_size;
947 /* We could do deflated delta, or we could do just deflated two,
948 * whichever is smaller.
951 deflated = deflate_it(two->ptr, two->size, &deflate_size);
952 if (one->size && two->size) {
953 delta = diff_delta(one->ptr, one->size,
955 &delta_size, deflate_size);
957 void *to_free = delta;
958 orig_size = delta_size;
959 delta = deflate_it(delta, delta_size, &delta_size);
964 if (delta && delta_size < deflate_size) {
965 printf("delta %lu\n", orig_size);
968 data_size = delta_size;
971 printf("literal %lu\n", two->size);
974 data_size = deflate_size;
977 /* emit data encoded in base85 */
980 int bytes = (52 < data_size) ? 52 : data_size;
984 line[0] = bytes + 'A' - 1;
986 line[0] = bytes - 26 + 'a' - 1;
987 encode_85(line + 1, cp, bytes);
988 cp = (char *) cp + bytes;
995 static void emit_binary_diff(mmfile_t *one, mmfile_t *two)
997 printf("GIT binary patch\n");
998 emit_binary_diff_body(one, two);
999 emit_binary_diff_body(two, one);
1002 #define FIRST_FEW_BYTES 8000
1003 static int mmfile_is_binary(mmfile_t *mf)
1006 if (FIRST_FEW_BYTES < sz)
1007 sz = FIRST_FEW_BYTES;
1008 return !!memchr(mf->ptr, 0, sz);
1011 static void builtin_diff(const char *name_a,
1013 struct diff_filespec *one,
1014 struct diff_filespec *two,
1015 const char *xfrm_msg,
1016 struct diff_options *o,
1017 int complete_rewrite)
1021 char *a_one, *b_two;
1022 const char *set = diff_get_color(o->color_diff, DIFF_METAINFO);
1023 const char *reset = diff_get_color(o->color_diff, DIFF_RESET);
1025 a_one = quote_two("a/", name_a + (*name_a == '/'));
1026 b_two = quote_two("b/", name_b + (*name_b == '/'));
1027 lbl[0] = DIFF_FILE_VALID(one) ? a_one : "/dev/null";
1028 lbl[1] = DIFF_FILE_VALID(two) ? b_two : "/dev/null";
1029 printf("%sdiff --git %s %s%s\n", set, a_one, b_two, reset);
1030 if (lbl[0][0] == '/') {
1032 printf("%snew file mode %06o%s\n", set, two->mode, reset);
1033 if (xfrm_msg && xfrm_msg[0])
1034 printf("%s%s%s\n", set, xfrm_msg, reset);
1036 else if (lbl[1][0] == '/') {
1037 printf("%sdeleted file mode %06o%s\n", set, one->mode, reset);
1038 if (xfrm_msg && xfrm_msg[0])
1039 printf("%s%s%s\n", set, xfrm_msg, reset);
1042 if (one->mode != two->mode) {
1043 printf("%sold mode %06o%s\n", set, one->mode, reset);
1044 printf("%snew mode %06o%s\n", set, two->mode, reset);
1046 if (xfrm_msg && xfrm_msg[0])
1047 printf("%s%s%s\n", set, xfrm_msg, reset);
1049 * we do not run diff between different kind
1052 if ((one->mode ^ two->mode) & S_IFMT)
1053 goto free_ab_and_return;
1054 if (complete_rewrite) {
1055 emit_rewrite_diff(name_a, name_b, one, two);
1056 goto free_ab_and_return;
1060 if (fill_mmfile(&mf1, one) < 0 || fill_mmfile(&mf2, two) < 0)
1061 die("unable to read files to diff");
1063 if (!o->text && (mmfile_is_binary(&mf1) || mmfile_is_binary(&mf2))) {
1064 /* Quite common confusing case */
1065 if (mf1.size == mf2.size &&
1066 !memcmp(mf1.ptr, mf2.ptr, mf1.size))
1067 goto free_ab_and_return;
1069 emit_binary_diff(&mf1, &mf2);
1071 printf("Binary files %s and %s differ\n",
1075 /* Crazy xdl interfaces.. */
1076 const char *diffopts = getenv("GIT_DIFF_OPTS");
1080 struct emit_callback ecbdata;
1082 memset(&ecbdata, 0, sizeof(ecbdata));
1083 ecbdata.label_path = lbl;
1084 ecbdata.color_diff = o->color_diff;
1085 xpp.flags = XDF_NEED_MINIMAL | o->xdl_opts;
1086 xecfg.ctxlen = o->context;
1087 xecfg.flags = XDL_EMIT_FUNCNAMES;
1090 else if (!strncmp(diffopts, "--unified=", 10))
1091 xecfg.ctxlen = strtoul(diffopts + 10, NULL, 10);
1092 else if (!strncmp(diffopts, "-u", 2))
1093 xecfg.ctxlen = strtoul(diffopts + 2, NULL, 10);
1094 ecb.outf = xdiff_outf;
1095 ecb.priv = &ecbdata;
1096 ecbdata.xm.consume = fn_out_consume;
1097 if (o->color_diff_words)
1098 ecbdata.diff_words =
1099 xcalloc(1, sizeof(struct diff_words_data));
1100 xdl_diff(&mf1, &mf2, &xpp, &xecfg, &ecb);
1101 if (o->color_diff_words)
1102 free_diff_words_data(&ecbdata);
1111 static void builtin_diffstat(const char *name_a, const char *name_b,
1112 struct diff_filespec *one,
1113 struct diff_filespec *two,
1114 struct diffstat_t *diffstat,
1115 struct diff_options *o,
1116 int complete_rewrite)
1119 struct diffstat_file *data;
1121 data = diffstat_add(diffstat, name_a, name_b);
1124 data->is_unmerged = 1;
1127 if (complete_rewrite) {
1128 diff_populate_filespec(one, 0);
1129 diff_populate_filespec(two, 0);
1130 data->deleted = count_lines(one->data, one->size);
1131 data->added = count_lines(two->data, two->size);
1134 if (fill_mmfile(&mf1, one) < 0 || fill_mmfile(&mf2, two) < 0)
1135 die("unable to read files to diff");
1137 if (mmfile_is_binary(&mf1) || mmfile_is_binary(&mf2))
1138 data->is_binary = 1;
1140 /* Crazy xdl interfaces.. */
1145 xpp.flags = XDF_NEED_MINIMAL | o->xdl_opts;
1148 ecb.outf = xdiff_outf;
1149 ecb.priv = diffstat;
1150 xdl_diff(&mf1, &mf2, &xpp, &xecfg, &ecb);
1154 static void builtin_checkdiff(const char *name_a, const char *name_b,
1155 struct diff_filespec *one,
1156 struct diff_filespec *two)
1159 struct checkdiff_t data;
1164 memset(&data, 0, sizeof(data));
1165 data.xm.consume = checkdiff_consume;
1166 data.filename = name_b ? name_b : name_a;
1169 if (fill_mmfile(&mf1, one) < 0 || fill_mmfile(&mf2, two) < 0)
1170 die("unable to read files to diff");
1172 if (mmfile_is_binary(&mf2))
1175 /* Crazy xdl interfaces.. */
1180 xpp.flags = XDF_NEED_MINIMAL;
1183 ecb.outf = xdiff_outf;
1185 xdl_diff(&mf1, &mf2, &xpp, &xecfg, &ecb);
1189 struct diff_filespec *alloc_filespec(const char *path)
1191 int namelen = strlen(path);
1192 struct diff_filespec *spec = xmalloc(sizeof(*spec) + namelen + 1);
1194 memset(spec, 0, sizeof(*spec));
1195 spec->path = (char *)(spec + 1);
1196 memcpy(spec->path, path, namelen+1);
1200 void fill_filespec(struct diff_filespec *spec, const unsigned char *sha1,
1201 unsigned short mode)
1204 spec->mode = canon_mode(mode);
1205 hashcpy(spec->sha1, sha1);
1206 spec->sha1_valid = !is_null_sha1(sha1);
1211 * Given a name and sha1 pair, if the dircache tells us the file in
1212 * the work tree has that object contents, return true, so that
1213 * prepare_temp_file() does not have to inflate and extract.
1215 static int reuse_worktree_file(const char *name, const unsigned char *sha1, int want_file)
1217 struct cache_entry *ce;
1221 /* We do not read the cache ourselves here, because the
1222 * benchmark with my previous version that always reads cache
1223 * shows that it makes things worse for diff-tree comparing
1224 * two linux-2.6 kernel trees in an already checked out work
1225 * tree. This is because most diff-tree comparisons deal with
1226 * only a small number of files, while reading the cache is
1227 * expensive for a large project, and its cost outweighs the
1228 * savings we get by not inflating the object to a temporary
1229 * file. Practically, this code only helps when we are used
1230 * by diff-cache --cached, which does read the cache before
1236 /* We want to avoid the working directory if our caller
1237 * doesn't need the data in a normal file, this system
1238 * is rather slow with its stat/open/mmap/close syscalls,
1239 * and the object is contained in a pack file. The pack
1240 * is probably already open and will be faster to obtain
1241 * the data through than the working directory. Loose
1242 * objects however would tend to be slower as they need
1243 * to be individually opened and inflated.
1245 if (!FAST_WORKING_DIRECTORY && !want_file && has_sha1_pack(sha1, NULL))
1249 pos = cache_name_pos(name, len);
1252 ce = active_cache[pos];
1253 if ((lstat(name, &st) < 0) ||
1254 !S_ISREG(st.st_mode) || /* careful! */
1255 ce_match_stat(ce, &st, 0) ||
1256 hashcmp(sha1, ce->sha1))
1258 /* we return 1 only when we can stat, it is a regular file,
1259 * stat information matches, and sha1 recorded in the cache
1260 * matches. I.e. we know the file in the work tree really is
1261 * the same as the <name, sha1> pair.
1266 static struct sha1_size_cache {
1267 unsigned char sha1[20];
1269 } **sha1_size_cache;
1270 static int sha1_size_cache_nr, sha1_size_cache_alloc;
1272 static struct sha1_size_cache *locate_size_cache(unsigned char *sha1,
1277 struct sha1_size_cache *e;
1280 last = sha1_size_cache_nr;
1281 while (last > first) {
1282 int cmp, next = (last + first) >> 1;
1283 e = sha1_size_cache[next];
1284 cmp = hashcmp(e->sha1, sha1);
1296 /* insert to make it at "first" */
1297 if (sha1_size_cache_alloc <= sha1_size_cache_nr) {
1298 sha1_size_cache_alloc = alloc_nr(sha1_size_cache_alloc);
1299 sha1_size_cache = xrealloc(sha1_size_cache,
1300 sha1_size_cache_alloc *
1301 sizeof(*sha1_size_cache));
1303 sha1_size_cache_nr++;
1304 if (first < sha1_size_cache_nr)
1305 memmove(sha1_size_cache + first + 1, sha1_size_cache + first,
1306 (sha1_size_cache_nr - first - 1) *
1307 sizeof(*sha1_size_cache));
1308 e = xmalloc(sizeof(struct sha1_size_cache));
1309 sha1_size_cache[first] = e;
1310 hashcpy(e->sha1, sha1);
1316 * While doing rename detection and pickaxe operation, we may need to
1317 * grab the data for the blob (or file) for our own in-core comparison.
1318 * diff_filespec has data and size fields for this purpose.
1320 int diff_populate_filespec(struct diff_filespec *s, int size_only)
1323 if (!DIFF_FILE_VALID(s))
1324 die("internal error: asking to populate invalid file.");
1325 if (S_ISDIR(s->mode))
1328 if (!use_size_cache)
1333 if (!s->sha1_valid ||
1334 reuse_worktree_file(s->path, s->sha1, 0)) {
1337 if (lstat(s->path, &st) < 0) {
1338 if (errno == ENOENT) {
1342 s->data = (char *)"";
1347 s->size = st.st_size;
1352 if (S_ISLNK(st.st_mode)) {
1354 s->data = xmalloc(s->size);
1356 ret = readlink(s->path, s->data, s->size);
1363 fd = open(s->path, O_RDONLY);
1366 s->data = xmmap(NULL, s->size, PROT_READ, MAP_PRIVATE, fd, 0);
1368 s->should_munmap = 1;
1369 /* FIXME! CRLF -> LF conversion goes here, based on "s->path" */
1373 struct sha1_size_cache *e;
1376 e = locate_size_cache(s->sha1, 1, 0);
1381 if (!sha1_object_info(s->sha1, type, &s->size))
1382 locate_size_cache(s->sha1, 0, s->size);
1385 s->data = read_sha1_file(s->sha1, type, &s->size);
1392 void diff_free_filespec_data(struct diff_filespec *s)
1396 else if (s->should_munmap)
1397 munmap(s->data, s->size);
1398 s->should_free = s->should_munmap = 0;
1404 static void prep_temp_blob(struct diff_tempfile *temp,
1407 const unsigned char *sha1,
1412 fd = git_mkstemp(temp->tmp_path, TEMPFILE_PATH_LEN, ".diff_XXXXXX");
1414 die("unable to create temp-file");
1415 if (write_in_full(fd, blob, size) != size)
1416 die("unable to write temp-file");
1418 temp->name = temp->tmp_path;
1419 strcpy(temp->hex, sha1_to_hex(sha1));
1421 sprintf(temp->mode, "%06o", mode);
1424 static void prepare_temp_file(const char *name,
1425 struct diff_tempfile *temp,
1426 struct diff_filespec *one)
1428 if (!DIFF_FILE_VALID(one)) {
1430 /* A '-' entry produces this for file-2, and
1431 * a '+' entry produces this for file-1.
1433 temp->name = "/dev/null";
1434 strcpy(temp->hex, ".");
1435 strcpy(temp->mode, ".");
1439 if (!one->sha1_valid ||
1440 reuse_worktree_file(name, one->sha1, 1)) {
1442 if (lstat(name, &st) < 0) {
1443 if (errno == ENOENT)
1444 goto not_a_valid_file;
1445 die("stat(%s): %s", name, strerror(errno));
1447 if (S_ISLNK(st.st_mode)) {
1449 char buf[PATH_MAX + 1]; /* ought to be SYMLINK_MAX */
1450 if (sizeof(buf) <= st.st_size)
1451 die("symlink too long: %s", name);
1452 ret = readlink(name, buf, st.st_size);
1454 die("readlink(%s)", name);
1455 prep_temp_blob(temp, buf, st.st_size,
1457 one->sha1 : null_sha1),
1459 one->mode : S_IFLNK));
1462 /* we can borrow from the file in the work tree */
1464 if (!one->sha1_valid)
1465 strcpy(temp->hex, sha1_to_hex(null_sha1));
1467 strcpy(temp->hex, sha1_to_hex(one->sha1));
1468 /* Even though we may sometimes borrow the
1469 * contents from the work tree, we always want
1470 * one->mode. mode is trustworthy even when
1471 * !(one->sha1_valid), as long as
1472 * DIFF_FILE_VALID(one).
1474 sprintf(temp->mode, "%06o", one->mode);
1479 if (diff_populate_filespec(one, 0))
1480 die("cannot read data blob for %s", one->path);
1481 prep_temp_blob(temp, one->data, one->size,
1482 one->sha1, one->mode);
1486 static void remove_tempfile(void)
1490 for (i = 0; i < 2; i++)
1491 if (diff_temp[i].name == diff_temp[i].tmp_path) {
1492 unlink(diff_temp[i].name);
1493 diff_temp[i].name = NULL;
1497 static void remove_tempfile_on_signal(int signo)
1500 signal(SIGINT, SIG_DFL);
1504 static int spawn_prog(const char *pgm, const char **arg)
1512 die("unable to fork");
1514 execvp(pgm, (char *const*) arg);
1518 while (waitpid(pid, &status, 0) < 0) {
1524 /* Earlier we did not check the exit status because
1525 * diff exits non-zero if files are different, and
1526 * we are not interested in knowing that. It was a
1527 * mistake which made it harder to quit a diff-*
1528 * session that uses the git-apply-patch-script as
1529 * the GIT_EXTERNAL_DIFF. A custom GIT_EXTERNAL_DIFF
1530 * should also exit non-zero only when it wants to
1531 * abort the entire diff-* session.
1533 if (WIFEXITED(status) && !WEXITSTATUS(status))
1538 /* An external diff command takes:
1540 * diff-cmd name infile1 infile1-sha1 infile1-mode \
1541 * infile2 infile2-sha1 infile2-mode [ rename-to ]
1544 static void run_external_diff(const char *pgm,
1547 struct diff_filespec *one,
1548 struct diff_filespec *two,
1549 const char *xfrm_msg,
1550 int complete_rewrite)
1552 const char *spawn_arg[10];
1553 struct diff_tempfile *temp = diff_temp;
1555 static int atexit_asked = 0;
1556 const char *othername;
1557 const char **arg = &spawn_arg[0];
1559 othername = (other? other : name);
1561 prepare_temp_file(name, &temp[0], one);
1562 prepare_temp_file(othername, &temp[1], two);
1563 if (! atexit_asked &&
1564 (temp[0].name == temp[0].tmp_path ||
1565 temp[1].name == temp[1].tmp_path)) {
1567 atexit(remove_tempfile);
1569 signal(SIGINT, remove_tempfile_on_signal);
1575 *arg++ = temp[0].name;
1576 *arg++ = temp[0].hex;
1577 *arg++ = temp[0].mode;
1578 *arg++ = temp[1].name;
1579 *arg++ = temp[1].hex;
1580 *arg++ = temp[1].mode;
1590 retval = spawn_prog(pgm, spawn_arg);
1593 fprintf(stderr, "external diff died, stopping at %s.\n", name);
1598 static void run_diff_cmd(const char *pgm,
1601 struct diff_filespec *one,
1602 struct diff_filespec *two,
1603 const char *xfrm_msg,
1604 struct diff_options *o,
1605 int complete_rewrite)
1608 run_external_diff(pgm, name, other, one, two, xfrm_msg,
1613 builtin_diff(name, other ? other : name,
1614 one, two, xfrm_msg, o, complete_rewrite);
1616 printf("* Unmerged path %s\n", name);
1619 static void diff_fill_sha1_info(struct diff_filespec *one)
1621 if (DIFF_FILE_VALID(one)) {
1622 if (!one->sha1_valid) {
1624 if (lstat(one->path, &st) < 0)
1625 die("stat %s", one->path);
1626 if (index_path(one->sha1, one->path, &st, 0))
1627 die("cannot hash %s\n", one->path);
1634 static void run_diff(struct diff_filepair *p, struct diff_options *o)
1636 const char *pgm = external_diff();
1637 char msg[PATH_MAX*2+300], *xfrm_msg;
1638 struct diff_filespec *one;
1639 struct diff_filespec *two;
1642 char *name_munged, *other_munged;
1643 int complete_rewrite = 0;
1646 if (DIFF_PAIR_UNMERGED(p)) {
1648 run_diff_cmd(pgm, p->one->path, NULL, NULL, NULL, NULL, o, 0);
1652 name = p->one->path;
1653 other = (strcmp(name, p->two->path) ? p->two->path : NULL);
1654 name_munged = quote_one(name);
1655 other_munged = quote_one(other);
1656 one = p->one; two = p->two;
1658 diff_fill_sha1_info(one);
1659 diff_fill_sha1_info(two);
1662 switch (p->status) {
1663 case DIFF_STATUS_COPIED:
1664 len += snprintf(msg + len, sizeof(msg) - len,
1665 "similarity index %d%%\n"
1668 (int)(0.5 + p->score * 100.0/MAX_SCORE),
1669 name_munged, other_munged);
1671 case DIFF_STATUS_RENAMED:
1672 len += snprintf(msg + len, sizeof(msg) - len,
1673 "similarity index %d%%\n"
1676 (int)(0.5 + p->score * 100.0/MAX_SCORE),
1677 name_munged, other_munged);
1679 case DIFF_STATUS_MODIFIED:
1681 len += snprintf(msg + len, sizeof(msg) - len,
1682 "dissimilarity index %d%%\n",
1683 (int)(0.5 + p->score *
1685 complete_rewrite = 1;
1694 if (hashcmp(one->sha1, two->sha1)) {
1695 int abbrev = o->full_index ? 40 : DEFAULT_ABBREV;
1699 if ((!fill_mmfile(&mf, one) && mmfile_is_binary(&mf)) ||
1700 (!fill_mmfile(&mf, two) && mmfile_is_binary(&mf)))
1703 len += snprintf(msg + len, sizeof(msg) - len,
1705 abbrev, sha1_to_hex(one->sha1),
1706 abbrev, sha1_to_hex(two->sha1));
1707 if (one->mode == two->mode)
1708 len += snprintf(msg + len, sizeof(msg) - len,
1709 " %06o", one->mode);
1710 len += snprintf(msg + len, sizeof(msg) - len, "\n");
1715 xfrm_msg = len ? msg : NULL;
1718 DIFF_FILE_VALID(one) && DIFF_FILE_VALID(two) &&
1719 (S_IFMT & one->mode) != (S_IFMT & two->mode)) {
1720 /* a filepair that changes between file and symlink
1721 * needs to be split into deletion and creation.
1723 struct diff_filespec *null = alloc_filespec(two->path);
1724 run_diff_cmd(NULL, name, other, one, null, xfrm_msg, o, 0);
1726 null = alloc_filespec(one->path);
1727 run_diff_cmd(NULL, name, other, null, two, xfrm_msg, o, 0);
1731 run_diff_cmd(pgm, name, other, one, two, xfrm_msg, o,
1738 static void run_diffstat(struct diff_filepair *p, struct diff_options *o,
1739 struct diffstat_t *diffstat)
1743 int complete_rewrite = 0;
1745 if (DIFF_PAIR_UNMERGED(p)) {
1747 builtin_diffstat(p->one->path, NULL, NULL, NULL, diffstat, o, 0);
1751 name = p->one->path;
1752 other = (strcmp(name, p->two->path) ? p->two->path : NULL);
1754 diff_fill_sha1_info(p->one);
1755 diff_fill_sha1_info(p->two);
1757 if (p->status == DIFF_STATUS_MODIFIED && p->score)
1758 complete_rewrite = 1;
1759 builtin_diffstat(name, other, p->one, p->two, diffstat, o, complete_rewrite);
1762 static void run_checkdiff(struct diff_filepair *p, struct diff_options *o)
1767 if (DIFF_PAIR_UNMERGED(p)) {
1772 name = p->one->path;
1773 other = (strcmp(name, p->two->path) ? p->two->path : NULL);
1775 diff_fill_sha1_info(p->one);
1776 diff_fill_sha1_info(p->two);
1778 builtin_checkdiff(name, other, p->one, p->two);
1781 void diff_setup(struct diff_options *options)
1783 memset(options, 0, sizeof(*options));
1784 options->line_termination = '\n';
1785 options->break_opt = -1;
1786 options->rename_limit = -1;
1787 options->context = 3;
1788 options->msg_sep = "";
1790 options->change = diff_change;
1791 options->add_remove = diff_addremove;
1792 options->color_diff = diff_use_color_default;
1793 options->detect_rename = diff_detect_rename_default;
1796 int diff_setup_done(struct diff_options *options)
1800 if (options->output_format & DIFF_FORMAT_NAME)
1802 if (options->output_format & DIFF_FORMAT_NAME_STATUS)
1804 if (options->output_format & DIFF_FORMAT_CHECKDIFF)
1806 if (options->output_format & DIFF_FORMAT_NO_OUTPUT)
1809 die("--name-only, --name-status, --check and -s are mutually exclusive");
1811 if (options->find_copies_harder)
1812 options->detect_rename = DIFF_DETECT_COPY;
1814 if (options->output_format & (DIFF_FORMAT_NAME |
1815 DIFF_FORMAT_NAME_STATUS |
1816 DIFF_FORMAT_CHECKDIFF |
1817 DIFF_FORMAT_NO_OUTPUT))
1818 options->output_format &= ~(DIFF_FORMAT_RAW |
1819 DIFF_FORMAT_NUMSTAT |
1820 DIFF_FORMAT_DIFFSTAT |
1821 DIFF_FORMAT_SHORTSTAT |
1822 DIFF_FORMAT_SUMMARY |
1826 * These cases always need recursive; we do not drop caller-supplied
1827 * recursive bits for other formats here.
1829 if (options->output_format & (DIFF_FORMAT_PATCH |
1830 DIFF_FORMAT_NUMSTAT |
1831 DIFF_FORMAT_DIFFSTAT |
1832 DIFF_FORMAT_SHORTSTAT |
1833 DIFF_FORMAT_SUMMARY |
1834 DIFF_FORMAT_CHECKDIFF))
1835 options->recursive = 1;
1837 * Also pickaxe would not work very well if you do not say recursive
1839 if (options->pickaxe)
1840 options->recursive = 1;
1842 if (options->detect_rename && options->rename_limit < 0)
1843 options->rename_limit = diff_rename_limit_default;
1844 if (options->setup & DIFF_SETUP_USE_CACHE) {
1846 /* read-cache does not die even when it fails
1847 * so it is safe for us to do this here. Also
1848 * it does not smudge active_cache or active_nr
1849 * when it fails, so we do not have to worry about
1850 * cleaning it up ourselves either.
1854 if (options->setup & DIFF_SETUP_USE_SIZE_CACHE)
1856 if (options->abbrev <= 0 || 40 < options->abbrev)
1857 options->abbrev = 40; /* full */
1862 static int opt_arg(const char *arg, int arg_short, const char *arg_long, int *val)
1872 if (c == arg_short) {
1876 if (val && isdigit(c)) {
1878 int n = strtoul(arg, &end, 10);
1889 eq = strchr(arg, '=');
1894 if (!len || strncmp(arg, arg_long, len))
1899 if (!isdigit(*++eq))
1901 n = strtoul(eq, &end, 10);
1909 int diff_opt_parse(struct diff_options *options, const char **av, int ac)
1911 const char *arg = av[0];
1912 if (!strcmp(arg, "-p") || !strcmp(arg, "-u"))
1913 options->output_format |= DIFF_FORMAT_PATCH;
1914 else if (opt_arg(arg, 'U', "unified", &options->context))
1915 options->output_format |= DIFF_FORMAT_PATCH;
1916 else if (!strcmp(arg, "--raw"))
1917 options->output_format |= DIFF_FORMAT_RAW;
1918 else if (!strcmp(arg, "--patch-with-raw")) {
1919 options->output_format |= DIFF_FORMAT_PATCH | DIFF_FORMAT_RAW;
1921 else if (!strcmp(arg, "--numstat")) {
1922 options->output_format |= DIFF_FORMAT_NUMSTAT;
1924 else if (!strcmp(arg, "--shortstat")) {
1925 options->output_format |= DIFF_FORMAT_SHORTSTAT;
1927 else if (!strncmp(arg, "--stat", 6)) {
1929 int width = options->stat_width;
1930 int name_width = options->stat_name_width;
1936 if (!strncmp(arg, "-width=", 7))
1937 width = strtoul(arg + 7, &end, 10);
1938 else if (!strncmp(arg, "-name-width=", 12))
1939 name_width = strtoul(arg + 12, &end, 10);
1942 width = strtoul(arg+1, &end, 10);
1944 name_width = strtoul(end+1, &end, 10);
1947 /* Important! This checks all the error cases! */
1950 options->output_format |= DIFF_FORMAT_DIFFSTAT;
1951 options->stat_name_width = name_width;
1952 options->stat_width = width;
1954 else if (!strcmp(arg, "--check"))
1955 options->output_format |= DIFF_FORMAT_CHECKDIFF;
1956 else if (!strcmp(arg, "--summary"))
1957 options->output_format |= DIFF_FORMAT_SUMMARY;
1958 else if (!strcmp(arg, "--patch-with-stat")) {
1959 options->output_format |= DIFF_FORMAT_PATCH | DIFF_FORMAT_DIFFSTAT;
1961 else if (!strcmp(arg, "-z"))
1962 options->line_termination = 0;
1963 else if (!strncmp(arg, "-l", 2))
1964 options->rename_limit = strtoul(arg+2, NULL, 10);
1965 else if (!strcmp(arg, "--full-index"))
1966 options->full_index = 1;
1967 else if (!strcmp(arg, "--binary")) {
1968 options->output_format |= DIFF_FORMAT_PATCH;
1969 options->binary = 1;
1971 else if (!strcmp(arg, "-a") || !strcmp(arg, "--text")) {
1974 else if (!strcmp(arg, "--name-only"))
1975 options->output_format |= DIFF_FORMAT_NAME;
1976 else if (!strcmp(arg, "--name-status"))
1977 options->output_format |= DIFF_FORMAT_NAME_STATUS;
1978 else if (!strcmp(arg, "-R"))
1979 options->reverse_diff = 1;
1980 else if (!strncmp(arg, "-S", 2))
1981 options->pickaxe = arg + 2;
1982 else if (!strcmp(arg, "-s")) {
1983 options->output_format |= DIFF_FORMAT_NO_OUTPUT;
1985 else if (!strncmp(arg, "-O", 2))
1986 options->orderfile = arg + 2;
1987 else if (!strncmp(arg, "--diff-filter=", 14))
1988 options->filter = arg + 14;
1989 else if (!strcmp(arg, "--pickaxe-all"))
1990 options->pickaxe_opts = DIFF_PICKAXE_ALL;
1991 else if (!strcmp(arg, "--pickaxe-regex"))
1992 options->pickaxe_opts = DIFF_PICKAXE_REGEX;
1993 else if (!strncmp(arg, "-B", 2)) {
1994 if ((options->break_opt =
1995 diff_scoreopt_parse(arg)) == -1)
1998 else if (!strncmp(arg, "-M", 2)) {
1999 if ((options->rename_score =
2000 diff_scoreopt_parse(arg)) == -1)
2002 options->detect_rename = DIFF_DETECT_RENAME;
2004 else if (!strncmp(arg, "-C", 2)) {
2005 if ((options->rename_score =
2006 diff_scoreopt_parse(arg)) == -1)
2008 options->detect_rename = DIFF_DETECT_COPY;
2010 else if (!strcmp(arg, "--find-copies-harder"))
2011 options->find_copies_harder = 1;
2012 else if (!strcmp(arg, "--abbrev"))
2013 options->abbrev = DEFAULT_ABBREV;
2014 else if (!strncmp(arg, "--abbrev=", 9)) {
2015 options->abbrev = strtoul(arg + 9, NULL, 10);
2016 if (options->abbrev < MINIMUM_ABBREV)
2017 options->abbrev = MINIMUM_ABBREV;
2018 else if (40 < options->abbrev)
2019 options->abbrev = 40;
2021 else if (!strcmp(arg, "--color"))
2022 options->color_diff = 1;
2023 else if (!strcmp(arg, "--no-color"))
2024 options->color_diff = 0;
2025 else if (!strcmp(arg, "-w") || !strcmp(arg, "--ignore-all-space"))
2026 options->xdl_opts |= XDF_IGNORE_WHITESPACE;
2027 else if (!strcmp(arg, "-b") || !strcmp(arg, "--ignore-space-change"))
2028 options->xdl_opts |= XDF_IGNORE_WHITESPACE_CHANGE;
2029 else if (!strcmp(arg, "--color-words"))
2030 options->color_diff = options->color_diff_words = 1;
2031 else if (!strcmp(arg, "--no-renames"))
2032 options->detect_rename = 0;
2038 static int parse_num(const char **cp_p)
2040 unsigned long num, scale;
2042 const char *cp = *cp_p;
2049 if ( !dot && ch == '.' ) {
2052 } else if ( ch == '%' ) {
2053 scale = dot ? scale*100 : 100;
2054 cp++; /* % is always at the end */
2056 } else if ( ch >= '0' && ch <= '9' ) {
2057 if ( scale < 100000 ) {
2059 num = (num*10) + (ch-'0');
2068 /* user says num divided by scale and we say internally that
2069 * is MAX_SCORE * num / scale.
2071 return (num >= scale) ? MAX_SCORE : (MAX_SCORE * num / scale);
2074 int diff_scoreopt_parse(const char *opt)
2076 int opt1, opt2, cmd;
2081 if (cmd != 'M' && cmd != 'C' && cmd != 'B')
2082 return -1; /* that is not a -M, -C nor -B option */
2084 opt1 = parse_num(&opt);
2090 else if (*opt != '/')
2091 return -1; /* we expect -B80/99 or -B80 */
2094 opt2 = parse_num(&opt);
2099 return opt1 | (opt2 << 16);
2102 struct diff_queue_struct diff_queued_diff;
2104 void diff_q(struct diff_queue_struct *queue, struct diff_filepair *dp)
2106 if (queue->alloc <= queue->nr) {
2107 queue->alloc = alloc_nr(queue->alloc);
2108 queue->queue = xrealloc(queue->queue,
2109 sizeof(dp) * queue->alloc);
2111 queue->queue[queue->nr++] = dp;
2114 struct diff_filepair *diff_queue(struct diff_queue_struct *queue,
2115 struct diff_filespec *one,
2116 struct diff_filespec *two)
2118 struct diff_filepair *dp = xcalloc(1, sizeof(*dp));
2126 void diff_free_filepair(struct diff_filepair *p)
2128 diff_free_filespec_data(p->one);
2129 diff_free_filespec_data(p->two);
2135 /* This is different from find_unique_abbrev() in that
2136 * it stuffs the result with dots for alignment.
2138 const char *diff_unique_abbrev(const unsigned char *sha1, int len)
2143 return sha1_to_hex(sha1);
2145 abbrev = find_unique_abbrev(sha1, len);
2147 return sha1_to_hex(sha1);
2148 abblen = strlen(abbrev);
2150 static char hex[41];
2151 if (len < abblen && abblen <= len + 2)
2152 sprintf(hex, "%s%.*s", abbrev, len+3-abblen, "..");
2154 sprintf(hex, "%s...", abbrev);
2157 return sha1_to_hex(sha1);
2160 static void diff_flush_raw(struct diff_filepair *p,
2161 struct diff_options *options)
2165 int abbrev = options->abbrev;
2166 const char *path_one, *path_two;
2167 int inter_name_termination = '\t';
2168 int line_termination = options->line_termination;
2170 if (!line_termination)
2171 inter_name_termination = 0;
2173 path_one = p->one->path;
2174 path_two = p->two->path;
2175 if (line_termination) {
2176 path_one = quote_one(path_one);
2177 path_two = quote_one(path_two);
2181 sprintf(status, "%c%03d", p->status,
2182 (int)(0.5 + p->score * 100.0/MAX_SCORE));
2184 status[0] = p->status;
2187 switch (p->status) {
2188 case DIFF_STATUS_COPIED:
2189 case DIFF_STATUS_RENAMED:
2192 case DIFF_STATUS_ADDED:
2193 case DIFF_STATUS_DELETED:
2200 if (!(options->output_format & DIFF_FORMAT_NAME_STATUS)) {
2201 printf(":%06o %06o %s ",
2202 p->one->mode, p->two->mode,
2203 diff_unique_abbrev(p->one->sha1, abbrev));
2205 diff_unique_abbrev(p->two->sha1, abbrev));
2207 printf("%s%c%s", status, inter_name_termination, path_one);
2209 printf("%c%s", inter_name_termination, path_two);
2210 putchar(line_termination);
2211 if (path_one != p->one->path)
2212 free((void*)path_one);
2213 if (path_two != p->two->path)
2214 free((void*)path_two);
2217 static void diff_flush_name(struct diff_filepair *p, struct diff_options *opt)
2219 char *path = p->two->path;
2221 if (opt->line_termination)
2222 path = quote_one(p->two->path);
2223 printf("%s%c", path, opt->line_termination);
2224 if (p->two->path != path)
2228 int diff_unmodified_pair(struct diff_filepair *p)
2230 /* This function is written stricter than necessary to support
2231 * the currently implemented transformers, but the idea is to
2232 * let transformers to produce diff_filepairs any way they want,
2233 * and filter and clean them up here before producing the output.
2235 struct diff_filespec *one, *two;
2237 if (DIFF_PAIR_UNMERGED(p))
2238 return 0; /* unmerged is interesting */
2243 /* deletion, addition, mode or type change
2244 * and rename are all interesting.
2246 if (DIFF_FILE_VALID(one) != DIFF_FILE_VALID(two) ||
2247 DIFF_PAIR_MODE_CHANGED(p) ||
2248 strcmp(one->path, two->path))
2251 /* both are valid and point at the same path. that is, we are
2252 * dealing with a change.
2254 if (one->sha1_valid && two->sha1_valid &&
2255 !hashcmp(one->sha1, two->sha1))
2256 return 1; /* no change */
2257 if (!one->sha1_valid && !two->sha1_valid)
2258 return 1; /* both look at the same file on the filesystem. */
2262 static void diff_flush_patch(struct diff_filepair *p, struct diff_options *o)
2264 if (diff_unmodified_pair(p))
2267 if ((DIFF_FILE_VALID(p->one) && S_ISDIR(p->one->mode)) ||
2268 (DIFF_FILE_VALID(p->two) && S_ISDIR(p->two->mode)))
2269 return; /* no tree diffs in patch format */
2274 static void diff_flush_stat(struct diff_filepair *p, struct diff_options *o,
2275 struct diffstat_t *diffstat)
2277 if (diff_unmodified_pair(p))
2280 if ((DIFF_FILE_VALID(p->one) && S_ISDIR(p->one->mode)) ||
2281 (DIFF_FILE_VALID(p->two) && S_ISDIR(p->two->mode)))
2282 return; /* no tree diffs in patch format */
2284 run_diffstat(p, o, diffstat);
2287 static void diff_flush_checkdiff(struct diff_filepair *p,
2288 struct diff_options *o)
2290 if (diff_unmodified_pair(p))
2293 if ((DIFF_FILE_VALID(p->one) && S_ISDIR(p->one->mode)) ||
2294 (DIFF_FILE_VALID(p->two) && S_ISDIR(p->two->mode)))
2295 return; /* no tree diffs in patch format */
2297 run_checkdiff(p, o);
2300 int diff_queue_is_empty(void)
2302 struct diff_queue_struct *q = &diff_queued_diff;
2304 for (i = 0; i < q->nr; i++)
2305 if (!diff_unmodified_pair(q->queue[i]))
2311 void diff_debug_filespec(struct diff_filespec *s, int x, const char *one)
2313 fprintf(stderr, "queue[%d] %s (%s) %s %06o %s\n",
2316 DIFF_FILE_VALID(s) ? "valid" : "invalid",
2318 s->sha1_valid ? sha1_to_hex(s->sha1) : "");
2319 fprintf(stderr, "queue[%d] %s size %lu flags %d\n",
2321 s->size, s->xfrm_flags);
2324 void diff_debug_filepair(const struct diff_filepair *p, int i)
2326 diff_debug_filespec(p->one, i, "one");
2327 diff_debug_filespec(p->two, i, "two");
2328 fprintf(stderr, "score %d, status %c stays %d broken %d\n",
2329 p->score, p->status ? p->status : '?',
2330 p->source_stays, p->broken_pair);
2333 void diff_debug_queue(const char *msg, struct diff_queue_struct *q)
2337 fprintf(stderr, "%s\n", msg);
2338 fprintf(stderr, "q->nr = %d\n", q->nr);
2339 for (i = 0; i < q->nr; i++) {
2340 struct diff_filepair *p = q->queue[i];
2341 diff_debug_filepair(p, i);
2346 static void diff_resolve_rename_copy(void)
2349 struct diff_filepair *p, *pp;
2350 struct diff_queue_struct *q = &diff_queued_diff;
2352 diff_debug_queue("resolve-rename-copy", q);
2354 for (i = 0; i < q->nr; i++) {
2356 p->status = 0; /* undecided */
2357 if (DIFF_PAIR_UNMERGED(p))
2358 p->status = DIFF_STATUS_UNMERGED;
2359 else if (!DIFF_FILE_VALID(p->one))
2360 p->status = DIFF_STATUS_ADDED;
2361 else if (!DIFF_FILE_VALID(p->two))
2362 p->status = DIFF_STATUS_DELETED;
2363 else if (DIFF_PAIR_TYPE_CHANGED(p))
2364 p->status = DIFF_STATUS_TYPE_CHANGED;
2366 /* from this point on, we are dealing with a pair
2367 * whose both sides are valid and of the same type, i.e.
2368 * either in-place edit or rename/copy edit.
2370 else if (DIFF_PAIR_RENAME(p)) {
2371 if (p->source_stays) {
2372 p->status = DIFF_STATUS_COPIED;
2375 /* See if there is some other filepair that
2376 * copies from the same source as us. If so
2377 * we are a copy. Otherwise we are either a
2378 * copy if the path stays, or a rename if it
2379 * does not, but we already handled "stays" case.
2381 for (j = i + 1; j < q->nr; j++) {
2383 if (strcmp(pp->one->path, p->one->path))
2384 continue; /* not us */
2385 if (!DIFF_PAIR_RENAME(pp))
2386 continue; /* not a rename/copy */
2387 /* pp is a rename/copy from the same source */
2388 p->status = DIFF_STATUS_COPIED;
2392 p->status = DIFF_STATUS_RENAMED;
2394 else if (hashcmp(p->one->sha1, p->two->sha1) ||
2395 p->one->mode != p->two->mode)
2396 p->status = DIFF_STATUS_MODIFIED;
2398 /* This is a "no-change" entry and should not
2399 * happen anymore, but prepare for broken callers.
2401 error("feeding unmodified %s to diffcore",
2403 p->status = DIFF_STATUS_UNKNOWN;
2406 diff_debug_queue("resolve-rename-copy done", q);
2409 static int check_pair_status(struct diff_filepair *p)
2411 switch (p->status) {
2412 case DIFF_STATUS_UNKNOWN:
2415 die("internal error in diff-resolve-rename-copy");
2421 static void flush_one_pair(struct diff_filepair *p, struct diff_options *opt)
2423 int fmt = opt->output_format;
2425 if (fmt & DIFF_FORMAT_CHECKDIFF)
2426 diff_flush_checkdiff(p, opt);
2427 else if (fmt & (DIFF_FORMAT_RAW | DIFF_FORMAT_NAME_STATUS))
2428 diff_flush_raw(p, opt);
2429 else if (fmt & DIFF_FORMAT_NAME)
2430 diff_flush_name(p, opt);
2433 static void show_file_mode_name(const char *newdelete, struct diff_filespec *fs)
2435 char *name = quote_one(fs->path);
2437 printf(" %s mode %06o %s\n", newdelete, fs->mode, name);
2439 printf(" %s %s\n", newdelete, name);
2444 static void show_mode_change(struct diff_filepair *p, int show_name)
2446 if (p->one->mode && p->two->mode && p->one->mode != p->two->mode) {
2448 char *name = quote_one(p->two->path);
2449 printf(" mode change %06o => %06o %s\n",
2450 p->one->mode, p->two->mode, name);
2454 printf(" mode change %06o => %06o\n",
2455 p->one->mode, p->two->mode);
2459 static void show_rename_copy(const char *renamecopy, struct diff_filepair *p)
2461 char *names = pprint_rename(p->one->path, p->two->path);
2463 printf(" %s %s (%d%%)\n", renamecopy, names,
2464 (int)(0.5 + p->score * 100.0/MAX_SCORE));
2466 show_mode_change(p, 0);
2469 static void diff_summary(struct diff_filepair *p)
2472 case DIFF_STATUS_DELETED:
2473 show_file_mode_name("delete", p->one);
2475 case DIFF_STATUS_ADDED:
2476 show_file_mode_name("create", p->two);
2478 case DIFF_STATUS_COPIED:
2479 show_rename_copy("copy", p);
2481 case DIFF_STATUS_RENAMED:
2482 show_rename_copy("rename", p);
2486 char *name = quote_one(p->two->path);
2487 printf(" rewrite %s (%d%%)\n", name,
2488 (int)(0.5 + p->score * 100.0/MAX_SCORE));
2490 show_mode_change(p, 0);
2491 } else show_mode_change(p, 1);
2497 struct xdiff_emit_state xm;
2502 static int remove_space(char *line, int len)
2508 for (i = 0; i < len; i++)
2509 if (!isspace((c = line[i])))
2515 static void patch_id_consume(void *priv, char *line, unsigned long len)
2517 struct patch_id_t *data = priv;
2520 /* Ignore line numbers when computing the SHA1 of the patch */
2521 if (!strncmp(line, "@@ -", 4))
2524 new_len = remove_space(line, len);
2526 SHA1_Update(data->ctx, line, new_len);
2527 data->patchlen += new_len;
2530 /* returns 0 upon success, and writes result into sha1 */
2531 static int diff_get_patch_id(struct diff_options *options, unsigned char *sha1)
2533 struct diff_queue_struct *q = &diff_queued_diff;
2536 struct patch_id_t data;
2537 char buffer[PATH_MAX * 4 + 20];
2540 memset(&data, 0, sizeof(struct patch_id_t));
2542 data.xm.consume = patch_id_consume;
2544 for (i = 0; i < q->nr; i++) {
2549 struct diff_filepair *p = q->queue[i];
2553 return error("internal diff status error");
2554 if (p->status == DIFF_STATUS_UNKNOWN)
2556 if (diff_unmodified_pair(p))
2558 if ((DIFF_FILE_VALID(p->one) && S_ISDIR(p->one->mode)) ||
2559 (DIFF_FILE_VALID(p->two) && S_ISDIR(p->two->mode)))
2561 if (DIFF_PAIR_UNMERGED(p))
2564 diff_fill_sha1_info(p->one);
2565 diff_fill_sha1_info(p->two);
2566 if (fill_mmfile(&mf1, p->one) < 0 ||
2567 fill_mmfile(&mf2, p->two) < 0)
2568 return error("unable to read files to diff");
2570 /* Maybe hash p->two? into the patch id? */
2571 if (mmfile_is_binary(&mf2))
2574 len1 = remove_space(p->one->path, strlen(p->one->path));
2575 len2 = remove_space(p->two->path, strlen(p->two->path));
2576 if (p->one->mode == 0)
2577 len1 = snprintf(buffer, sizeof(buffer),
2578 "diff--gita/%.*sb/%.*s"
2585 len2, p->two->path);
2586 else if (p->two->mode == 0)
2587 len1 = snprintf(buffer, sizeof(buffer),
2588 "diff--gita/%.*sb/%.*s"
2589 "deletedfilemode%06o"
2595 len1, p->one->path);
2597 len1 = snprintf(buffer, sizeof(buffer),
2598 "diff--gita/%.*sb/%.*s"
2604 len2, p->two->path);
2605 SHA1_Update(&ctx, buffer, len1);
2607 xpp.flags = XDF_NEED_MINIMAL;
2609 xecfg.flags = XDL_EMIT_FUNCNAMES;
2610 ecb.outf = xdiff_outf;
2612 xdl_diff(&mf1, &mf2, &xpp, &xecfg, &ecb);
2615 SHA1_Final(sha1, &ctx);
2619 int diff_flush_patch_id(struct diff_options *options, unsigned char *sha1)
2621 struct diff_queue_struct *q = &diff_queued_diff;
2623 int result = diff_get_patch_id(options, sha1);
2625 for (i = 0; i < q->nr; i++)
2626 diff_free_filepair(q->queue[i]);
2630 q->nr = q->alloc = 0;
2635 static int is_summary_empty(const struct diff_queue_struct *q)
2639 for (i = 0; i < q->nr; i++) {
2640 const struct diff_filepair *p = q->queue[i];
2642 switch (p->status) {
2643 case DIFF_STATUS_DELETED:
2644 case DIFF_STATUS_ADDED:
2645 case DIFF_STATUS_COPIED:
2646 case DIFF_STATUS_RENAMED:
2651 if (p->one->mode && p->two->mode &&
2652 p->one->mode != p->two->mode)
2660 void diff_flush(struct diff_options *options)
2662 struct diff_queue_struct *q = &diff_queued_diff;
2663 int i, output_format = options->output_format;
2667 * Order: raw, stat, summary, patch
2668 * or: name/name-status/checkdiff (other bits clear)
2673 if (output_format & (DIFF_FORMAT_RAW |
2675 DIFF_FORMAT_NAME_STATUS |
2676 DIFF_FORMAT_CHECKDIFF)) {
2677 for (i = 0; i < q->nr; i++) {
2678 struct diff_filepair *p = q->queue[i];
2679 if (check_pair_status(p))
2680 flush_one_pair(p, options);
2685 if (output_format & (DIFF_FORMAT_DIFFSTAT|DIFF_FORMAT_SHORTSTAT|DIFF_FORMAT_NUMSTAT)) {
2686 struct diffstat_t diffstat;
2688 memset(&diffstat, 0, sizeof(struct diffstat_t));
2689 diffstat.xm.consume = diffstat_consume;
2690 for (i = 0; i < q->nr; i++) {
2691 struct diff_filepair *p = q->queue[i];
2692 if (check_pair_status(p))
2693 diff_flush_stat(p, options, &diffstat);
2695 if (output_format & DIFF_FORMAT_NUMSTAT)
2696 show_numstat(&diffstat, options);
2697 if (output_format & DIFF_FORMAT_DIFFSTAT)
2698 show_stats(&diffstat, options);
2699 else if (output_format & DIFF_FORMAT_SHORTSTAT)
2700 show_shortstats(&diffstat);
2704 if (output_format & DIFF_FORMAT_SUMMARY && !is_summary_empty(q)) {
2705 for (i = 0; i < q->nr; i++)
2706 diff_summary(q->queue[i]);
2710 if (output_format & DIFF_FORMAT_PATCH) {
2712 if (options->stat_sep) {
2713 /* attach patch instead of inline */
2714 fputs(options->stat_sep, stdout);
2716 putchar(options->line_termination);
2720 for (i = 0; i < q->nr; i++) {
2721 struct diff_filepair *p = q->queue[i];
2722 if (check_pair_status(p))
2723 diff_flush_patch(p, options);
2727 if (output_format & DIFF_FORMAT_CALLBACK)
2728 options->format_callback(q, options, options->format_callback_data);
2730 for (i = 0; i < q->nr; i++)
2731 diff_free_filepair(q->queue[i]);
2735 q->nr = q->alloc = 0;
2738 static void diffcore_apply_filter(const char *filter)
2741 struct diff_queue_struct *q = &diff_queued_diff;
2742 struct diff_queue_struct outq;
2744 outq.nr = outq.alloc = 0;
2749 if (strchr(filter, DIFF_STATUS_FILTER_AON)) {
2751 for (i = found = 0; !found && i < q->nr; i++) {
2752 struct diff_filepair *p = q->queue[i];
2753 if (((p->status == DIFF_STATUS_MODIFIED) &&
2755 strchr(filter, DIFF_STATUS_FILTER_BROKEN)) ||
2757 strchr(filter, DIFF_STATUS_MODIFIED)))) ||
2758 ((p->status != DIFF_STATUS_MODIFIED) &&
2759 strchr(filter, p->status)))
2765 /* otherwise we will clear the whole queue
2766 * by copying the empty outq at the end of this
2767 * function, but first clear the current entries
2770 for (i = 0; i < q->nr; i++)
2771 diff_free_filepair(q->queue[i]);
2774 /* Only the matching ones */
2775 for (i = 0; i < q->nr; i++) {
2776 struct diff_filepair *p = q->queue[i];
2778 if (((p->status == DIFF_STATUS_MODIFIED) &&
2780 strchr(filter, DIFF_STATUS_FILTER_BROKEN)) ||
2782 strchr(filter, DIFF_STATUS_MODIFIED)))) ||
2783 ((p->status != DIFF_STATUS_MODIFIED) &&
2784 strchr(filter, p->status)))
2787 diff_free_filepair(p);
2794 void diffcore_std(struct diff_options *options)
2796 if (options->break_opt != -1)
2797 diffcore_break(options->break_opt);
2798 if (options->detect_rename)
2799 diffcore_rename(options);
2800 if (options->break_opt != -1)
2801 diffcore_merge_broken();
2802 if (options->pickaxe)
2803 diffcore_pickaxe(options->pickaxe, options->pickaxe_opts);
2804 if (options->orderfile)
2805 diffcore_order(options->orderfile);
2806 diff_resolve_rename_copy();
2807 diffcore_apply_filter(options->filter);
2811 void diffcore_std_no_resolve(struct diff_options *options)
2813 if (options->pickaxe)
2814 diffcore_pickaxe(options->pickaxe, options->pickaxe_opts);
2815 if (options->orderfile)
2816 diffcore_order(options->orderfile);
2817 diffcore_apply_filter(options->filter);
2820 void diff_addremove(struct diff_options *options,
2821 int addremove, unsigned mode,
2822 const unsigned char *sha1,
2823 const char *base, const char *path)
2825 char concatpath[PATH_MAX];
2826 struct diff_filespec *one, *two;
2828 /* This may look odd, but it is a preparation for
2829 * feeding "there are unchanged files which should
2830 * not produce diffs, but when you are doing copy
2831 * detection you would need them, so here they are"
2832 * entries to the diff-core. They will be prefixed
2833 * with something like '=' or '*' (I haven't decided
2834 * which but should not make any difference).
2835 * Feeding the same new and old to diff_change()
2836 * also has the same effect.
2837 * Before the final output happens, they are pruned after
2838 * merged into rename/copy pairs as appropriate.
2840 if (options->reverse_diff)
2841 addremove = (addremove == '+' ? '-' :
2842 addremove == '-' ? '+' : addremove);
2844 if (!path) path = "";
2845 sprintf(concatpath, "%s%s", base, path);
2846 one = alloc_filespec(concatpath);
2847 two = alloc_filespec(concatpath);
2849 if (addremove != '+')
2850 fill_filespec(one, sha1, mode);
2851 if (addremove != '-')
2852 fill_filespec(two, sha1, mode);
2854 diff_queue(&diff_queued_diff, one, two);
2857 void diff_change(struct diff_options *options,
2858 unsigned old_mode, unsigned new_mode,
2859 const unsigned char *old_sha1,
2860 const unsigned char *new_sha1,
2861 const char *base, const char *path)
2863 char concatpath[PATH_MAX];
2864 struct diff_filespec *one, *two;
2866 if (options->reverse_diff) {
2868 const unsigned char *tmp_c;
2869 tmp = old_mode; old_mode = new_mode; new_mode = tmp;
2870 tmp_c = old_sha1; old_sha1 = new_sha1; new_sha1 = tmp_c;
2872 if (!path) path = "";
2873 sprintf(concatpath, "%s%s", base, path);
2874 one = alloc_filespec(concatpath);
2875 two = alloc_filespec(concatpath);
2876 fill_filespec(one, old_sha1, old_mode);
2877 fill_filespec(two, new_sha1, new_mode);
2879 diff_queue(&diff_queued_diff, one, two);
2882 void diff_unmerge(struct diff_options *options,
2884 unsigned mode, const unsigned char *sha1)
2886 struct diff_filespec *one, *two;
2887 one = alloc_filespec(path);
2888 two = alloc_filespec(path);
2889 fill_filespec(one, sha1, mode);
2890 diff_queue(&diff_queued_diff, one, two)->is_unmerged = 1;