2 * Copyright (C) 2005 Junio C Hamano
9 #include "xdiff-interface.h"
12 #include "run-command.h"
16 #ifdef NO_FAST_WORKING_DIRECTORY
17 #define FAST_WORKING_DIRECTORY 0
19 #define FAST_WORKING_DIRECTORY 1
22 static int diff_detect_rename_default;
23 static int diff_rename_limit_default = 200;
24 static int diff_suppress_blank_empty;
25 int diff_use_color_default = -1;
26 static const char *external_diff_cmd_cfg;
27 int diff_auto_refresh_index = 1;
28 static int diff_mnemonic_prefix;
30 static char diff_colors[][COLOR_MAXLEN] = {
32 "", /* PLAIN (normal) */
33 "\033[1m", /* METAINFO (bold) */
34 "\033[36m", /* FRAGINFO (cyan) */
35 "\033[31m", /* OLD (red) */
36 "\033[32m", /* NEW (green) */
37 "\033[33m", /* COMMIT (yellow) */
38 "\033[41m", /* WHITESPACE (red background) */
41 static void diff_filespec_load_driver(struct diff_filespec *one);
42 static char *run_textconv(const char *, struct diff_filespec *, size_t *);
44 static int parse_diff_color_slot(const char *var, int ofs)
46 if (!strcasecmp(var+ofs, "plain"))
48 if (!strcasecmp(var+ofs, "meta"))
50 if (!strcasecmp(var+ofs, "frag"))
52 if (!strcasecmp(var+ofs, "old"))
54 if (!strcasecmp(var+ofs, "new"))
56 if (!strcasecmp(var+ofs, "commit"))
58 if (!strcasecmp(var+ofs, "whitespace"))
59 return DIFF_WHITESPACE;
60 die("bad config variable '%s'", var);
64 * These are to give UI layer defaults.
65 * The core-level commands such as git-diff-files should
66 * never be affected by the setting of diff.renames
67 * the user happens to have in the configuration file.
69 int git_diff_ui_config(const char *var, const char *value, void *cb)
71 if (!strcmp(var, "diff.color") || !strcmp(var, "color.diff")) {
72 diff_use_color_default = git_config_colorbool(var, value, -1);
75 if (!strcmp(var, "diff.renames")) {
77 diff_detect_rename_default = DIFF_DETECT_RENAME;
78 else if (!strcasecmp(value, "copies") ||
79 !strcasecmp(value, "copy"))
80 diff_detect_rename_default = DIFF_DETECT_COPY;
81 else if (git_config_bool(var,value))
82 diff_detect_rename_default = DIFF_DETECT_RENAME;
85 if (!strcmp(var, "diff.autorefreshindex")) {
86 diff_auto_refresh_index = git_config_bool(var, value);
89 if (!strcmp(var, "diff.mnemonicprefix")) {
90 diff_mnemonic_prefix = git_config_bool(var, value);
93 if (!strcmp(var, "diff.external"))
94 return git_config_string(&external_diff_cmd_cfg, var, value);
96 switch (userdiff_config_porcelain(var, value)) {
102 return git_diff_basic_config(var, value, cb);
105 int git_diff_basic_config(const char *var, const char *value, void *cb)
107 if (!strcmp(var, "diff.renamelimit")) {
108 diff_rename_limit_default = git_config_int(var, value);
112 if (!prefixcmp(var, "diff.color.") || !prefixcmp(var, "color.diff.")) {
113 int slot = parse_diff_color_slot(var, 11);
115 return config_error_nonbool(var);
116 color_parse(value, var, diff_colors[slot]);
120 /* like GNU diff's --suppress-blank-empty option */
121 if (!strcmp(var, "diff.suppress-blank-empty")) {
122 diff_suppress_blank_empty = git_config_bool(var, value);
126 switch (userdiff_config_basic(var, value)) {
132 return git_color_default_config(var, value, cb);
135 static char *quote_two(const char *one, const char *two)
137 int need_one = quote_c_style(one, NULL, NULL, 1);
138 int need_two = quote_c_style(two, NULL, NULL, 1);
139 struct strbuf res = STRBUF_INIT;
141 if (need_one + need_two) {
142 strbuf_addch(&res, '"');
143 quote_c_style(one, &res, NULL, 1);
144 quote_c_style(two, &res, NULL, 1);
145 strbuf_addch(&res, '"');
147 strbuf_addstr(&res, one);
148 strbuf_addstr(&res, two);
150 return strbuf_detach(&res, NULL);
153 static const char *external_diff(void)
155 static const char *external_diff_cmd = NULL;
156 static int done_preparing = 0;
159 return external_diff_cmd;
160 external_diff_cmd = getenv("GIT_EXTERNAL_DIFF");
161 if (!external_diff_cmd)
162 external_diff_cmd = external_diff_cmd_cfg;
164 return external_diff_cmd;
167 static struct diff_tempfile {
168 const char *name; /* filename external diff should read from */
171 char tmp_path[PATH_MAX];
174 static int count_lines(const char *data, int size)
176 int count, ch, completely_empty = 1, nl_just_seen = 0;
183 completely_empty = 0;
187 completely_empty = 0;
190 if (completely_empty)
193 count++; /* no trailing newline */
197 static void print_line_count(FILE *file, int count)
201 fprintf(file, "0,0");
207 fprintf(file, "1,%d", count);
212 static void copy_file_with_prefix(FILE *file,
213 int prefix, const char *data, int size,
214 const char *set, const char *reset)
216 int ch, nl_just_seen = 1;
231 fprintf(file, "%s\n\\ No newline at end of file\n", reset);
234 static void emit_rewrite_diff(const char *name_a,
236 struct diff_filespec *one,
237 struct diff_filespec *two,
238 struct diff_options *o)
241 int color_diff = DIFF_OPT_TST(o, COLOR_DIFF);
242 const char *name_a_tab, *name_b_tab;
243 const char *metainfo = diff_get_color(color_diff, DIFF_METAINFO);
244 const char *fraginfo = diff_get_color(color_diff, DIFF_FRAGINFO);
245 const char *old = diff_get_color(color_diff, DIFF_FILE_OLD);
246 const char *new = diff_get_color(color_diff, DIFF_FILE_NEW);
247 const char *reset = diff_get_color(color_diff, DIFF_RESET);
248 static struct strbuf a_name = STRBUF_INIT, b_name = STRBUF_INIT;
249 const char *a_prefix, *b_prefix;
251 if (diff_mnemonic_prefix && DIFF_OPT_TST(o, REVERSE_DIFF)) {
252 a_prefix = o->b_prefix;
253 b_prefix = o->a_prefix;
255 a_prefix = o->a_prefix;
256 b_prefix = o->b_prefix;
259 name_a += (*name_a == '/');
260 name_b += (*name_b == '/');
261 name_a_tab = strchr(name_a, ' ') ? "\t" : "";
262 name_b_tab = strchr(name_b, ' ') ? "\t" : "";
264 strbuf_reset(&a_name);
265 strbuf_reset(&b_name);
266 quote_two_c_style(&a_name, a_prefix, name_a, 0);
267 quote_two_c_style(&b_name, b_prefix, name_b, 0);
269 diff_populate_filespec(one, 0);
270 diff_populate_filespec(two, 0);
271 lc_a = count_lines(one->data, one->size);
272 lc_b = count_lines(two->data, two->size);
274 "%s--- %s%s%s\n%s+++ %s%s%s\n%s@@ -",
275 metainfo, a_name.buf, name_a_tab, reset,
276 metainfo, b_name.buf, name_b_tab, reset, fraginfo);
277 print_line_count(o->file, lc_a);
278 fprintf(o->file, " +");
279 print_line_count(o->file, lc_b);
280 fprintf(o->file, " @@%s\n", reset);
282 copy_file_with_prefix(o->file, '-', one->data, one->size, old, reset);
284 copy_file_with_prefix(o->file, '+', two->data, two->size, new, reset);
287 static int fill_mmfile(mmfile_t *mf, struct diff_filespec *one)
289 if (!DIFF_FILE_VALID(one)) {
290 mf->ptr = (char *)""; /* does not matter */
294 else if (diff_populate_filespec(one, 0))
298 mf->size = one->size;
302 struct diff_words_buffer {
305 long current; /* output pointer */
306 int suppressed_newline;
309 static void diff_words_append(char *line, unsigned long len,
310 struct diff_words_buffer *buffer)
312 if (buffer->text.size + len > buffer->alloc) {
313 buffer->alloc = (buffer->text.size + len) * 3 / 2;
314 buffer->text.ptr = xrealloc(buffer->text.ptr, buffer->alloc);
318 memcpy(buffer->text.ptr + buffer->text.size, line, len);
319 buffer->text.size += len;
322 struct diff_words_data {
323 struct diff_words_buffer minus, plus;
327 static void print_word(FILE *file, struct diff_words_buffer *buffer, int len, int color,
328 int suppress_newline)
336 ptr = buffer->text.ptr + buffer->current;
337 buffer->current += len;
339 if (ptr[len - 1] == '\n') {
344 fputs(diff_get_color(1, color), file);
345 fwrite(ptr, len, 1, file);
346 fputs(diff_get_color(1, DIFF_RESET), file);
349 if (suppress_newline)
350 buffer->suppressed_newline = 1;
356 static void fn_out_diff_words_aux(void *priv, char *line, unsigned long len)
358 struct diff_words_data *diff_words = priv;
360 if (diff_words->minus.suppressed_newline) {
362 putc('\n', diff_words->file);
363 diff_words->minus.suppressed_newline = 0;
369 print_word(diff_words->file,
370 &diff_words->minus, len, DIFF_FILE_OLD, 1);
373 print_word(diff_words->file,
374 &diff_words->plus, len, DIFF_FILE_NEW, 0);
377 print_word(diff_words->file,
378 &diff_words->plus, len, DIFF_PLAIN, 0);
379 diff_words->minus.current += len;
384 /* this executes the word diff on the accumulated buffers */
385 static void diff_words_show(struct diff_words_data *diff_words)
390 mmfile_t minus, plus;
393 memset(&xecfg, 0, sizeof(xecfg));
394 minus.size = diff_words->minus.text.size;
395 minus.ptr = xmalloc(minus.size);
396 memcpy(minus.ptr, diff_words->minus.text.ptr, minus.size);
397 for (i = 0; i < minus.size; i++)
398 if (isspace(minus.ptr[i]))
400 diff_words->minus.current = 0;
402 plus.size = diff_words->plus.text.size;
403 plus.ptr = xmalloc(plus.size);
404 memcpy(plus.ptr, diff_words->plus.text.ptr, plus.size);
405 for (i = 0; i < plus.size; i++)
406 if (isspace(plus.ptr[i]))
408 diff_words->plus.current = 0;
410 xpp.flags = XDF_NEED_MINIMAL;
411 xecfg.ctxlen = diff_words->minus.alloc + diff_words->plus.alloc;
412 xdi_diff_outf(&minus, &plus, fn_out_diff_words_aux, diff_words,
416 diff_words->minus.text.size = diff_words->plus.text.size = 0;
418 if (diff_words->minus.suppressed_newline) {
419 putc('\n', diff_words->file);
420 diff_words->minus.suppressed_newline = 0;
424 typedef unsigned long (*sane_truncate_fn)(char *line, unsigned long len);
426 struct emit_callback {
427 int nparents, color_diff;
429 sane_truncate_fn truncate;
430 const char **label_path;
431 struct diff_words_data *diff_words;
436 static void free_diff_words_data(struct emit_callback *ecbdata)
438 if (ecbdata->diff_words) {
440 if (ecbdata->diff_words->minus.text.size ||
441 ecbdata->diff_words->plus.text.size)
442 diff_words_show(ecbdata->diff_words);
444 free (ecbdata->diff_words->minus.text.ptr);
445 free (ecbdata->diff_words->plus.text.ptr);
446 free(ecbdata->diff_words);
447 ecbdata->diff_words = NULL;
451 const char *diff_get_color(int diff_use_color, enum color_diff ix)
454 return diff_colors[ix];
458 static void emit_line(FILE *file, const char *set, const char *reset, const char *line, int len)
460 int has_trailing_newline, has_trailing_carriage_return;
462 has_trailing_newline = (len > 0 && line[len-1] == '\n');
463 if (has_trailing_newline)
465 has_trailing_carriage_return = (len > 0 && line[len-1] == '\r');
466 if (has_trailing_carriage_return)
470 fwrite(line, len, 1, file);
472 if (has_trailing_carriage_return)
474 if (has_trailing_newline)
478 static void emit_add_line(const char *reset, struct emit_callback *ecbdata, const char *line, int len)
480 const char *ws = diff_get_color(ecbdata->color_diff, DIFF_WHITESPACE);
481 const char *set = diff_get_color(ecbdata->color_diff, DIFF_FILE_NEW);
484 emit_line(ecbdata->file, set, reset, line, len);
486 /* Emit just the prefix, then the rest. */
487 emit_line(ecbdata->file, set, reset, line, ecbdata->nparents);
488 ws_check_emit(line + ecbdata->nparents,
489 len - ecbdata->nparents, ecbdata->ws_rule,
490 ecbdata->file, set, reset, ws);
494 static unsigned long sane_truncate_line(struct emit_callback *ecb, char *line, unsigned long len)
501 return ecb->truncate(line, len);
505 (void) utf8_width(&cp, &l);
507 break; /* truncated in the middle? */
512 static void fn_out_consume(void *priv, char *line, unsigned long len)
516 struct emit_callback *ecbdata = priv;
517 const char *meta = diff_get_color(ecbdata->color_diff, DIFF_METAINFO);
518 const char *plain = diff_get_color(ecbdata->color_diff, DIFF_PLAIN);
519 const char *reset = diff_get_color(ecbdata->color_diff, DIFF_RESET);
521 *(ecbdata->found_changesp) = 1;
523 if (ecbdata->label_path[0]) {
524 const char *name_a_tab, *name_b_tab;
526 name_a_tab = strchr(ecbdata->label_path[0], ' ') ? "\t" : "";
527 name_b_tab = strchr(ecbdata->label_path[1], ' ') ? "\t" : "";
529 fprintf(ecbdata->file, "%s--- %s%s%s\n",
530 meta, ecbdata->label_path[0], reset, name_a_tab);
531 fprintf(ecbdata->file, "%s+++ %s%s%s\n",
532 meta, ecbdata->label_path[1], reset, name_b_tab);
533 ecbdata->label_path[0] = ecbdata->label_path[1] = NULL;
536 if (diff_suppress_blank_empty
537 && len == 2 && line[0] == ' ' && line[1] == '\n') {
542 /* This is not really necessary for now because
543 * this codepath only deals with two-way diffs.
545 for (i = 0; i < len && line[i] == '@'; i++)
547 if (2 <= i && i < len && line[i] == ' ') {
548 ecbdata->nparents = i - 1;
549 len = sane_truncate_line(ecbdata, line, len);
550 emit_line(ecbdata->file,
551 diff_get_color(ecbdata->color_diff, DIFF_FRAGINFO),
553 if (line[len-1] != '\n')
554 putc('\n', ecbdata->file);
558 if (len < ecbdata->nparents) {
559 emit_line(ecbdata->file, reset, reset, line, len);
564 if (ecbdata->diff_words && ecbdata->nparents != 1)
565 /* fall back to normal diff */
566 free_diff_words_data(ecbdata);
567 if (ecbdata->diff_words) {
568 if (line[0] == '-') {
569 diff_words_append(line, len,
570 &ecbdata->diff_words->minus);
572 } else if (line[0] == '+') {
573 diff_words_append(line, len,
574 &ecbdata->diff_words->plus);
577 if (ecbdata->diff_words->minus.text.size ||
578 ecbdata->diff_words->plus.text.size)
579 diff_words_show(ecbdata->diff_words);
582 emit_line(ecbdata->file, plain, reset, line, len);
585 for (i = 0; i < ecbdata->nparents && len; i++) {
587 color = DIFF_FILE_OLD;
588 else if (line[i] == '+')
589 color = DIFF_FILE_NEW;
592 if (color != DIFF_FILE_NEW) {
593 emit_line(ecbdata->file,
594 diff_get_color(ecbdata->color_diff, color),
598 emit_add_line(reset, ecbdata, line, len);
601 static char *pprint_rename(const char *a, const char *b)
605 struct strbuf name = STRBUF_INIT;
606 int pfx_length, sfx_length;
607 int len_a = strlen(a);
608 int len_b = strlen(b);
609 int a_midlen, b_midlen;
610 int qlen_a = quote_c_style(a, NULL, NULL, 0);
611 int qlen_b = quote_c_style(b, NULL, NULL, 0);
613 if (qlen_a || qlen_b) {
614 quote_c_style(a, &name, NULL, 0);
615 strbuf_addstr(&name, " => ");
616 quote_c_style(b, &name, NULL, 0);
617 return strbuf_detach(&name, NULL);
620 /* Find common prefix */
622 while (*old && *new && *old == *new) {
624 pfx_length = old - a + 1;
629 /* Find common suffix */
633 while (a <= old && b <= new && *old == *new) {
635 sfx_length = len_a - (old - a);
641 * pfx{mid-a => mid-b}sfx
642 * {pfx-a => pfx-b}sfx
643 * pfx{sfx-a => sfx-b}
646 a_midlen = len_a - pfx_length - sfx_length;
647 b_midlen = len_b - pfx_length - sfx_length;
653 strbuf_grow(&name, pfx_length + a_midlen + b_midlen + sfx_length + 7);
654 if (pfx_length + sfx_length) {
655 strbuf_add(&name, a, pfx_length);
656 strbuf_addch(&name, '{');
658 strbuf_add(&name, a + pfx_length, a_midlen);
659 strbuf_addstr(&name, " => ");
660 strbuf_add(&name, b + pfx_length, b_midlen);
661 if (pfx_length + sfx_length) {
662 strbuf_addch(&name, '}');
663 strbuf_add(&name, a + len_a - sfx_length, sfx_length);
665 return strbuf_detach(&name, NULL);
671 struct diffstat_file {
675 unsigned is_unmerged:1;
676 unsigned is_binary:1;
677 unsigned is_renamed:1;
678 unsigned int added, deleted;
682 static struct diffstat_file *diffstat_add(struct diffstat_t *diffstat,
686 struct diffstat_file *x;
687 x = xcalloc(sizeof (*x), 1);
688 if (diffstat->nr == diffstat->alloc) {
689 diffstat->alloc = alloc_nr(diffstat->alloc);
690 diffstat->files = xrealloc(diffstat->files,
691 diffstat->alloc * sizeof(x));
693 diffstat->files[diffstat->nr++] = x;
695 x->from_name = xstrdup(name_a);
696 x->name = xstrdup(name_b);
701 x->name = xstrdup(name_a);
706 static void diffstat_consume(void *priv, char *line, unsigned long len)
708 struct diffstat_t *diffstat = priv;
709 struct diffstat_file *x = diffstat->files[diffstat->nr - 1];
713 else if (line[0] == '-')
717 const char mime_boundary_leader[] = "------------";
719 static int scale_linear(int it, int width, int max_change)
722 * make sure that at least one '-' is printed if there were deletions,
723 * and likewise for '+'.
727 return ((it - 1) * (width - 1) + max_change - 1) / (max_change - 1);
730 static void show_name(FILE *file,
731 const char *prefix, const char *name, int len,
732 const char *reset, const char *set)
734 fprintf(file, " %s%s%-*s%s |", set, prefix, len, name, reset);
737 static void show_graph(FILE *file, char ch, int cnt, const char *set, const char *reset)
741 fprintf(file, "%s", set);
744 fprintf(file, "%s", reset);
747 static void fill_print_name(struct diffstat_file *file)
751 if (file->print_name)
754 if (!file->is_renamed) {
755 struct strbuf buf = STRBUF_INIT;
756 if (quote_c_style(file->name, &buf, NULL, 0)) {
757 pname = strbuf_detach(&buf, NULL);
760 strbuf_release(&buf);
763 pname = pprint_rename(file->from_name, file->name);
765 file->print_name = pname;
768 static void show_stats(struct diffstat_t* data, struct diff_options *options)
770 int i, len, add, del, total, adds = 0, dels = 0;
771 int max_change = 0, max_len = 0;
772 int total_files = data->nr;
773 int width, name_width;
774 const char *reset, *set, *add_c, *del_c;
779 width = options->stat_width ? options->stat_width : 80;
780 name_width = options->stat_name_width ? options->stat_name_width : 50;
782 /* Sanity: give at least 5 columns to the graph,
783 * but leave at least 10 columns for the name.
789 else if (width < name_width + 15)
790 name_width = width - 15;
792 /* Find the longest filename and max number of changes */
793 reset = diff_get_color_opt(options, DIFF_RESET);
794 set = diff_get_color_opt(options, DIFF_PLAIN);
795 add_c = diff_get_color_opt(options, DIFF_FILE_NEW);
796 del_c = diff_get_color_opt(options, DIFF_FILE_OLD);
798 for (i = 0; i < data->nr; i++) {
799 struct diffstat_file *file = data->files[i];
800 int change = file->added + file->deleted;
801 fill_print_name(file);
802 len = strlen(file->print_name);
806 if (file->is_binary || file->is_unmerged)
808 if (max_change < change)
812 /* Compute the width of the graph part;
813 * 10 is for one blank at the beginning of the line plus
814 * " | count " between the name and the graph.
816 * From here on, name_width is the width of the name area,
817 * and width is the width of the graph area.
819 name_width = (name_width < max_len) ? name_width : max_len;
820 if (width < (name_width + 10) + max_change)
821 width = width - (name_width + 10);
825 for (i = 0; i < data->nr; i++) {
826 const char *prefix = "";
827 char *name = data->files[i]->print_name;
828 int added = data->files[i]->added;
829 int deleted = data->files[i]->deleted;
833 * "scale" the filename
836 name_len = strlen(name);
837 if (name_width < name_len) {
841 name += name_len - len;
842 slash = strchr(name, '/');
847 if (data->files[i]->is_binary) {
848 show_name(options->file, prefix, name, len, reset, set);
849 fprintf(options->file, " Bin ");
850 fprintf(options->file, "%s%d%s", del_c, deleted, reset);
851 fprintf(options->file, " -> ");
852 fprintf(options->file, "%s%d%s", add_c, added, reset);
853 fprintf(options->file, " bytes");
854 fprintf(options->file, "\n");
857 else if (data->files[i]->is_unmerged) {
858 show_name(options->file, prefix, name, len, reset, set);
859 fprintf(options->file, " Unmerged\n");
862 else if (!data->files[i]->is_renamed &&
863 (added + deleted == 0)) {
869 * scale the add/delete
877 if (width <= max_change) {
878 add = scale_linear(add, width, max_change);
879 del = scale_linear(del, width, max_change);
882 show_name(options->file, prefix, name, len, reset, set);
883 fprintf(options->file, "%5d%s", added + deleted,
884 added + deleted ? " " : "");
885 show_graph(options->file, '+', add, add_c, reset);
886 show_graph(options->file, '-', del, del_c, reset);
887 fprintf(options->file, "\n");
889 fprintf(options->file,
890 "%s %d files changed, %d insertions(+), %d deletions(-)%s\n",
891 set, total_files, adds, dels, reset);
894 static void show_shortstats(struct diffstat_t* data, struct diff_options *options)
896 int i, adds = 0, dels = 0, total_files = data->nr;
901 for (i = 0; i < data->nr; i++) {
902 if (!data->files[i]->is_binary &&
903 !data->files[i]->is_unmerged) {
904 int added = data->files[i]->added;
905 int deleted= data->files[i]->deleted;
906 if (!data->files[i]->is_renamed &&
907 (added + deleted == 0)) {
915 fprintf(options->file, " %d files changed, %d insertions(+), %d deletions(-)\n",
916 total_files, adds, dels);
919 static void show_numstat(struct diffstat_t* data, struct diff_options *options)
926 for (i = 0; i < data->nr; i++) {
927 struct diffstat_file *file = data->files[i];
930 fprintf(options->file, "-\t-\t");
932 fprintf(options->file,
933 "%d\t%d\t", file->added, file->deleted);
934 if (options->line_termination) {
935 fill_print_name(file);
936 if (!file->is_renamed)
937 write_name_quoted(file->name, options->file,
938 options->line_termination);
940 fputs(file->print_name, options->file);
941 putc(options->line_termination, options->file);
944 if (file->is_renamed) {
945 putc('\0', options->file);
946 write_name_quoted(file->from_name, options->file, '\0');
948 write_name_quoted(file->name, options->file, '\0');
953 struct dirstat_file {
955 unsigned long changed;
959 struct dirstat_file *files;
960 int alloc, nr, percent, cumulative;
963 static long gather_dirstat(FILE *file, struct dirstat_dir *dir, unsigned long changed, const char *base, int baselen)
965 unsigned long this_dir = 0;
966 unsigned int sources = 0;
969 struct dirstat_file *f = dir->files;
970 int namelen = strlen(f->name);
974 if (namelen < baselen)
976 if (memcmp(f->name, base, baselen))
978 slash = strchr(f->name + baselen, '/');
980 int newbaselen = slash + 1 - f->name;
981 this = gather_dirstat(file, dir, changed, f->name, newbaselen);
993 * We don't report dirstat's for
995 * - or cases where everything came from a single directory
996 * under this directory (sources == 1).
998 if (baselen && sources != 1) {
999 int permille = this_dir * 1000 / changed;
1001 int percent = permille / 10;
1002 if (percent >= dir->percent) {
1003 fprintf(file, "%4d.%01d%% %.*s\n", percent, permille % 10, baselen, base);
1004 if (!dir->cumulative)
1012 static int dirstat_compare(const void *_a, const void *_b)
1014 const struct dirstat_file *a = _a;
1015 const struct dirstat_file *b = _b;
1016 return strcmp(a->name, b->name);
1019 static void show_dirstat(struct diff_options *options)
1022 unsigned long changed;
1023 struct dirstat_dir dir;
1024 struct diff_queue_struct *q = &diff_queued_diff;
1029 dir.percent = options->dirstat_percent;
1030 dir.cumulative = DIFF_OPT_TST(options, DIRSTAT_CUMULATIVE);
1033 for (i = 0; i < q->nr; i++) {
1034 struct diff_filepair *p = q->queue[i];
1036 unsigned long copied, added, damage;
1038 name = p->one->path ? p->one->path : p->two->path;
1040 if (DIFF_FILE_VALID(p->one) && DIFF_FILE_VALID(p->two)) {
1041 diff_populate_filespec(p->one, 0);
1042 diff_populate_filespec(p->two, 0);
1043 diffcore_count_changes(p->one, p->two, NULL, NULL, 0,
1045 diff_free_filespec_data(p->one);
1046 diff_free_filespec_data(p->two);
1047 } else if (DIFF_FILE_VALID(p->one)) {
1048 diff_populate_filespec(p->one, 1);
1050 diff_free_filespec_data(p->one);
1051 } else if (DIFF_FILE_VALID(p->two)) {
1052 diff_populate_filespec(p->two, 1);
1054 added = p->two->size;
1055 diff_free_filespec_data(p->two);
1060 * Original minus copied is the removed material,
1061 * added is the new material. They are both damages
1062 * made to the preimage. In --dirstat-by-file mode, count
1063 * damaged files, not damaged lines. This is done by
1064 * counting only a single damaged line per file.
1066 damage = (p->one->size - copied) + added;
1067 if (DIFF_OPT_TST(options, DIRSTAT_BY_FILE) && damage > 0)
1070 ALLOC_GROW(dir.files, dir.nr + 1, dir.alloc);
1071 dir.files[dir.nr].name = name;
1072 dir.files[dir.nr].changed = damage;
1077 /* This can happen even with many files, if everything was renames */
1081 /* Show all directories with more than x% of the changes */
1082 qsort(dir.files, dir.nr, sizeof(dir.files[0]), dirstat_compare);
1083 gather_dirstat(options->file, &dir, changed, "", 0);
1086 static void free_diffstat_info(struct diffstat_t *diffstat)
1089 for (i = 0; i < diffstat->nr; i++) {
1090 struct diffstat_file *f = diffstat->files[i];
1091 if (f->name != f->print_name)
1092 free(f->print_name);
1097 free(diffstat->files);
1100 struct checkdiff_t {
1101 const char *filename;
1103 struct diff_options *o;
1106 int trailing_blanks_start;
1109 static int is_conflict_marker(const char *line, unsigned long len)
1116 firstchar = line[0];
1117 switch (firstchar) {
1118 case '=': case '>': case '<':
1123 for (cnt = 1; cnt < 7; cnt++)
1124 if (line[cnt] != firstchar)
1126 /* line[0] thru line[6] are same as firstchar */
1127 if (firstchar == '=') {
1128 /* divider between ours and theirs? */
1129 if (len != 8 || line[7] != '\n')
1131 } else if (len < 8 || !isspace(line[7])) {
1132 /* not divider before ours nor after theirs */
1138 static void checkdiff_consume(void *priv, char *line, unsigned long len)
1140 struct checkdiff_t *data = priv;
1141 int color_diff = DIFF_OPT_TST(data->o, COLOR_DIFF);
1142 const char *ws = diff_get_color(color_diff, DIFF_WHITESPACE);
1143 const char *reset = diff_get_color(color_diff, DIFF_RESET);
1144 const char *set = diff_get_color(color_diff, DIFF_FILE_NEW);
1147 if (line[0] == '+') {
1150 if (!ws_blank_line(line + 1, len - 1, data->ws_rule))
1151 data->trailing_blanks_start = 0;
1152 else if (!data->trailing_blanks_start)
1153 data->trailing_blanks_start = data->lineno;
1154 if (is_conflict_marker(line + 1, len - 1)) {
1156 fprintf(data->o->file,
1157 "%s:%d: leftover conflict marker\n",
1158 data->filename, data->lineno);
1160 bad = ws_check(line + 1, len - 1, data->ws_rule);
1163 data->status |= bad;
1164 err = whitespace_error_string(bad);
1165 fprintf(data->o->file, "%s:%d: %s.\n",
1166 data->filename, data->lineno, err);
1168 emit_line(data->o->file, set, reset, line, 1);
1169 ws_check_emit(line + 1, len - 1, data->ws_rule,
1170 data->o->file, set, reset, ws);
1171 } else if (line[0] == ' ') {
1173 data->trailing_blanks_start = 0;
1174 } else if (line[0] == '@') {
1175 char *plus = strchr(line, '+');
1177 data->lineno = strtol(plus, NULL, 10) - 1;
1179 die("invalid diff");
1180 data->trailing_blanks_start = 0;
1184 static unsigned char *deflate_it(char *data,
1186 unsigned long *result_size)
1189 unsigned char *deflated;
1192 memset(&stream, 0, sizeof(stream));
1193 deflateInit(&stream, zlib_compression_level);
1194 bound = deflateBound(&stream, size);
1195 deflated = xmalloc(bound);
1196 stream.next_out = deflated;
1197 stream.avail_out = bound;
1199 stream.next_in = (unsigned char *)data;
1200 stream.avail_in = size;
1201 while (deflate(&stream, Z_FINISH) == Z_OK)
1203 deflateEnd(&stream);
1204 *result_size = stream.total_out;
1208 static void emit_binary_diff_body(FILE *file, mmfile_t *one, mmfile_t *two)
1214 unsigned long orig_size;
1215 unsigned long delta_size;
1216 unsigned long deflate_size;
1217 unsigned long data_size;
1219 /* We could do deflated delta, or we could do just deflated two,
1220 * whichever is smaller.
1223 deflated = deflate_it(two->ptr, two->size, &deflate_size);
1224 if (one->size && two->size) {
1225 delta = diff_delta(one->ptr, one->size,
1226 two->ptr, two->size,
1227 &delta_size, deflate_size);
1229 void *to_free = delta;
1230 orig_size = delta_size;
1231 delta = deflate_it(delta, delta_size, &delta_size);
1236 if (delta && delta_size < deflate_size) {
1237 fprintf(file, "delta %lu\n", orig_size);
1240 data_size = delta_size;
1243 fprintf(file, "literal %lu\n", two->size);
1246 data_size = deflate_size;
1249 /* emit data encoded in base85 */
1252 int bytes = (52 < data_size) ? 52 : data_size;
1256 line[0] = bytes + 'A' - 1;
1258 line[0] = bytes - 26 + 'a' - 1;
1259 encode_85(line + 1, cp, bytes);
1260 cp = (char *) cp + bytes;
1264 fprintf(file, "\n");
1268 static void emit_binary_diff(FILE *file, mmfile_t *one, mmfile_t *two)
1270 fprintf(file, "GIT binary patch\n");
1271 emit_binary_diff_body(file, one, two);
1272 emit_binary_diff_body(file, two, one);
1275 static void diff_filespec_load_driver(struct diff_filespec *one)
1278 one->driver = userdiff_find_by_path(one->path);
1280 one->driver = userdiff_find_by_name("default");
1283 int diff_filespec_is_binary(struct diff_filespec *one)
1285 if (one->is_binary == -1) {
1286 diff_filespec_load_driver(one);
1287 if (one->driver->binary != -1)
1288 one->is_binary = one->driver->binary;
1290 if (!one->data && DIFF_FILE_VALID(one))
1291 diff_populate_filespec(one, 0);
1293 one->is_binary = buffer_is_binary(one->data,
1295 if (one->is_binary == -1)
1299 return one->is_binary;
1302 static const struct userdiff_funcname *diff_funcname_pattern(struct diff_filespec *one)
1304 diff_filespec_load_driver(one);
1305 return one->driver->funcname.pattern ? &one->driver->funcname : NULL;
1308 void diff_set_mnemonic_prefix(struct diff_options *options, const char *a, const char *b)
1310 if (!options->a_prefix)
1311 options->a_prefix = a;
1312 if (!options->b_prefix)
1313 options->b_prefix = b;
1316 static const char *get_textconv(struct diff_filespec *one)
1318 if (!DIFF_FILE_VALID(one))
1320 diff_filespec_load_driver(one);
1321 return one->driver->textconv;
1324 static void builtin_diff(const char *name_a,
1326 struct diff_filespec *one,
1327 struct diff_filespec *two,
1328 const char *xfrm_msg,
1329 struct diff_options *o,
1330 int complete_rewrite)
1334 char *a_one, *b_two;
1335 const char *set = diff_get_color_opt(o, DIFF_METAINFO);
1336 const char *reset = diff_get_color_opt(o, DIFF_RESET);
1337 const char *a_prefix, *b_prefix;
1338 const char *textconv_one, *textconv_two;
1340 diff_set_mnemonic_prefix(o, "a/", "b/");
1341 if (DIFF_OPT_TST(o, REVERSE_DIFF)) {
1342 a_prefix = o->b_prefix;
1343 b_prefix = o->a_prefix;
1345 a_prefix = o->a_prefix;
1346 b_prefix = o->b_prefix;
1349 /* Never use a non-valid filename anywhere if at all possible */
1350 name_a = DIFF_FILE_VALID(one) ? name_a : name_b;
1351 name_b = DIFF_FILE_VALID(two) ? name_b : name_a;
1353 a_one = quote_two(a_prefix, name_a + (*name_a == '/'));
1354 b_two = quote_two(b_prefix, name_b + (*name_b == '/'));
1355 lbl[0] = DIFF_FILE_VALID(one) ? a_one : "/dev/null";
1356 lbl[1] = DIFF_FILE_VALID(two) ? b_two : "/dev/null";
1357 fprintf(o->file, "%sdiff --git %s %s%s\n", set, a_one, b_two, reset);
1358 if (lbl[0][0] == '/') {
1360 fprintf(o->file, "%snew file mode %06o%s\n", set, two->mode, reset);
1361 if (xfrm_msg && xfrm_msg[0])
1362 fprintf(o->file, "%s%s%s\n", set, xfrm_msg, reset);
1364 else if (lbl[1][0] == '/') {
1365 fprintf(o->file, "%sdeleted file mode %06o%s\n", set, one->mode, reset);
1366 if (xfrm_msg && xfrm_msg[0])
1367 fprintf(o->file, "%s%s%s\n", set, xfrm_msg, reset);
1370 if (one->mode != two->mode) {
1371 fprintf(o->file, "%sold mode %06o%s\n", set, one->mode, reset);
1372 fprintf(o->file, "%snew mode %06o%s\n", set, two->mode, reset);
1374 if (xfrm_msg && xfrm_msg[0])
1375 fprintf(o->file, "%s%s%s\n", set, xfrm_msg, reset);
1377 * we do not run diff between different kind
1380 if ((one->mode ^ two->mode) & S_IFMT)
1381 goto free_ab_and_return;
1382 if (complete_rewrite) {
1383 emit_rewrite_diff(name_a, name_b, one, two, o);
1384 o->found_changes = 1;
1385 goto free_ab_and_return;
1389 if (fill_mmfile(&mf1, one) < 0 || fill_mmfile(&mf2, two) < 0)
1390 die("unable to read files to diff");
1392 textconv_one = get_textconv(one);
1393 textconv_two = get_textconv(two);
1395 if (!DIFF_OPT_TST(o, TEXT) &&
1396 ( (diff_filespec_is_binary(one) && !textconv_one) ||
1397 (diff_filespec_is_binary(two) && !textconv_two) )) {
1398 /* Quite common confusing case */
1399 if (mf1.size == mf2.size &&
1400 !memcmp(mf1.ptr, mf2.ptr, mf1.size))
1401 goto free_ab_and_return;
1402 if (DIFF_OPT_TST(o, BINARY))
1403 emit_binary_diff(o->file, &mf1, &mf2);
1405 fprintf(o->file, "Binary files %s and %s differ\n",
1407 o->found_changes = 1;
1410 /* Crazy xdl interfaces.. */
1411 const char *diffopts = getenv("GIT_DIFF_OPTS");
1415 struct emit_callback ecbdata;
1416 const struct userdiff_funcname *pe;
1420 mf1.ptr = run_textconv(textconv_one, one, &size);
1422 die("unable to read files to diff");
1427 mf2.ptr = run_textconv(textconv_two, two, &size);
1429 die("unable to read files to diff");
1433 pe = diff_funcname_pattern(one);
1435 pe = diff_funcname_pattern(two);
1437 memset(&xecfg, 0, sizeof(xecfg));
1438 memset(&ecbdata, 0, sizeof(ecbdata));
1439 ecbdata.label_path = lbl;
1440 ecbdata.color_diff = DIFF_OPT_TST(o, COLOR_DIFF);
1441 ecbdata.found_changesp = &o->found_changes;
1442 ecbdata.ws_rule = whitespace_rule(name_b ? name_b : name_a);
1443 ecbdata.file = o->file;
1444 xpp.flags = XDF_NEED_MINIMAL | o->xdl_opts;
1445 xecfg.ctxlen = o->context;
1446 xecfg.flags = XDL_EMIT_FUNCNAMES;
1448 xdiff_set_find_func(&xecfg, pe->pattern, pe->cflags);
1451 else if (!prefixcmp(diffopts, "--unified="))
1452 xecfg.ctxlen = strtoul(diffopts + 10, NULL, 10);
1453 else if (!prefixcmp(diffopts, "-u"))
1454 xecfg.ctxlen = strtoul(diffopts + 2, NULL, 10);
1455 if (DIFF_OPT_TST(o, COLOR_DIFF_WORDS)) {
1456 ecbdata.diff_words =
1457 xcalloc(1, sizeof(struct diff_words_data));
1458 ecbdata.diff_words->file = o->file;
1460 xdi_diff_outf(&mf1, &mf2, fn_out_consume, &ecbdata,
1461 &xpp, &xecfg, &ecb);
1462 if (DIFF_OPT_TST(o, COLOR_DIFF_WORDS))
1463 free_diff_words_data(&ecbdata);
1471 diff_free_filespec_data(one);
1472 diff_free_filespec_data(two);
1478 static void builtin_diffstat(const char *name_a, const char *name_b,
1479 struct diff_filespec *one,
1480 struct diff_filespec *two,
1481 struct diffstat_t *diffstat,
1482 struct diff_options *o,
1483 int complete_rewrite)
1486 struct diffstat_file *data;
1488 data = diffstat_add(diffstat, name_a, name_b);
1491 data->is_unmerged = 1;
1494 if (complete_rewrite) {
1495 diff_populate_filespec(one, 0);
1496 diff_populate_filespec(two, 0);
1497 data->deleted = count_lines(one->data, one->size);
1498 data->added = count_lines(two->data, two->size);
1499 goto free_and_return;
1501 if (fill_mmfile(&mf1, one) < 0 || fill_mmfile(&mf2, two) < 0)
1502 die("unable to read files to diff");
1504 if (diff_filespec_is_binary(one) || diff_filespec_is_binary(two)) {
1505 data->is_binary = 1;
1506 data->added = mf2.size;
1507 data->deleted = mf1.size;
1509 /* Crazy xdl interfaces.. */
1514 memset(&xecfg, 0, sizeof(xecfg));
1515 xpp.flags = XDF_NEED_MINIMAL | o->xdl_opts;
1516 xdi_diff_outf(&mf1, &mf2, diffstat_consume, diffstat,
1517 &xpp, &xecfg, &ecb);
1521 diff_free_filespec_data(one);
1522 diff_free_filespec_data(two);
1525 static void builtin_checkdiff(const char *name_a, const char *name_b,
1526 const char *attr_path,
1527 struct diff_filespec *one,
1528 struct diff_filespec *two,
1529 struct diff_options *o)
1532 struct checkdiff_t data;
1537 memset(&data, 0, sizeof(data));
1538 data.filename = name_b ? name_b : name_a;
1541 data.ws_rule = whitespace_rule(attr_path);
1543 if (fill_mmfile(&mf1, one) < 0 || fill_mmfile(&mf2, two) < 0)
1544 die("unable to read files to diff");
1547 * All the other codepaths check both sides, but not checking
1548 * the "old" side here is deliberate. We are checking the newly
1549 * introduced changes, and as long as the "new" side is text, we
1550 * can and should check what it introduces.
1552 if (diff_filespec_is_binary(two))
1553 goto free_and_return;
1555 /* Crazy xdl interfaces.. */
1560 memset(&xecfg, 0, sizeof(xecfg));
1561 xecfg.ctxlen = 1; /* at least one context line */
1562 xpp.flags = XDF_NEED_MINIMAL;
1563 xdi_diff_outf(&mf1, &mf2, checkdiff_consume, &data,
1564 &xpp, &xecfg, &ecb);
1566 if ((data.ws_rule & WS_TRAILING_SPACE) &&
1567 data.trailing_blanks_start) {
1568 fprintf(o->file, "%s:%d: ends with blank lines.\n",
1569 data.filename, data.trailing_blanks_start);
1570 data.status = 1; /* report errors */
1574 diff_free_filespec_data(one);
1575 diff_free_filespec_data(two);
1577 DIFF_OPT_SET(o, CHECK_FAILED);
1580 struct diff_filespec *alloc_filespec(const char *path)
1582 int namelen = strlen(path);
1583 struct diff_filespec *spec = xmalloc(sizeof(*spec) + namelen + 1);
1585 memset(spec, 0, sizeof(*spec));
1586 spec->path = (char *)(spec + 1);
1587 memcpy(spec->path, path, namelen+1);
1589 spec->is_binary = -1;
1593 void free_filespec(struct diff_filespec *spec)
1595 if (!--spec->count) {
1596 diff_free_filespec_data(spec);
1601 void fill_filespec(struct diff_filespec *spec, const unsigned char *sha1,
1602 unsigned short mode)
1605 spec->mode = canon_mode(mode);
1606 hashcpy(spec->sha1, sha1);
1607 spec->sha1_valid = !is_null_sha1(sha1);
1612 * Given a name and sha1 pair, if the index tells us the file in
1613 * the work tree has that object contents, return true, so that
1614 * prepare_temp_file() does not have to inflate and extract.
1616 static int reuse_worktree_file(const char *name, const unsigned char *sha1, int want_file)
1618 struct cache_entry *ce;
1622 /* We do not read the cache ourselves here, because the
1623 * benchmark with my previous version that always reads cache
1624 * shows that it makes things worse for diff-tree comparing
1625 * two linux-2.6 kernel trees in an already checked out work
1626 * tree. This is because most diff-tree comparisons deal with
1627 * only a small number of files, while reading the cache is
1628 * expensive for a large project, and its cost outweighs the
1629 * savings we get by not inflating the object to a temporary
1630 * file. Practically, this code only helps when we are used
1631 * by diff-cache --cached, which does read the cache before
1637 /* We want to avoid the working directory if our caller
1638 * doesn't need the data in a normal file, this system
1639 * is rather slow with its stat/open/mmap/close syscalls,
1640 * and the object is contained in a pack file. The pack
1641 * is probably already open and will be faster to obtain
1642 * the data through than the working directory. Loose
1643 * objects however would tend to be slower as they need
1644 * to be individually opened and inflated.
1646 if (!FAST_WORKING_DIRECTORY && !want_file && has_sha1_pack(sha1, NULL))
1650 pos = cache_name_pos(name, len);
1653 ce = active_cache[pos];
1656 * This is not the sha1 we are looking for, or
1657 * unreusable because it is not a regular file.
1659 if (hashcmp(sha1, ce->sha1) || !S_ISREG(ce->ce_mode))
1663 * If ce matches the file in the work tree, we can reuse it.
1665 if (ce_uptodate(ce) ||
1666 (!lstat(name, &st) && !ce_match_stat(ce, &st, 0)))
1672 static int populate_from_stdin(struct diff_filespec *s)
1674 struct strbuf buf = STRBUF_INIT;
1677 if (strbuf_read(&buf, 0, 0) < 0)
1678 return error("error while reading from stdin %s",
1681 s->should_munmap = 0;
1682 s->data = strbuf_detach(&buf, &size);
1688 static int diff_populate_gitlink(struct diff_filespec *s, int size_only)
1691 char *data = xmalloc(100);
1692 len = snprintf(data, 100,
1693 "Subproject commit %s\n", sha1_to_hex(s->sha1));
1705 * While doing rename detection and pickaxe operation, we may need to
1706 * grab the data for the blob (or file) for our own in-core comparison.
1707 * diff_filespec has data and size fields for this purpose.
1709 int diff_populate_filespec(struct diff_filespec *s, int size_only)
1712 if (!DIFF_FILE_VALID(s))
1713 die("internal error: asking to populate invalid file.");
1714 if (S_ISDIR(s->mode))
1720 if (size_only && 0 < s->size)
1723 if (S_ISGITLINK(s->mode))
1724 return diff_populate_gitlink(s, size_only);
1726 if (!s->sha1_valid ||
1727 reuse_worktree_file(s->path, s->sha1, 0)) {
1728 struct strbuf buf = STRBUF_INIT;
1732 if (!strcmp(s->path, "-"))
1733 return populate_from_stdin(s);
1735 if (lstat(s->path, &st) < 0) {
1736 if (errno == ENOENT) {
1740 s->data = (char *)"";
1745 s->size = xsize_t(st.st_size);
1750 if (S_ISLNK(st.st_mode)) {
1752 s->data = xmalloc(s->size);
1754 ret = readlink(s->path, s->data, s->size);
1761 fd = open(s->path, O_RDONLY);
1764 s->data = xmmap(NULL, s->size, PROT_READ, MAP_PRIVATE, fd, 0);
1766 s->should_munmap = 1;
1769 * Convert from working tree format to canonical git format
1771 if (convert_to_git(s->path, s->data, s->size, &buf, safe_crlf)) {
1773 munmap(s->data, s->size);
1774 s->should_munmap = 0;
1775 s->data = strbuf_detach(&buf, &size);
1781 enum object_type type;
1783 type = sha1_object_info(s->sha1, &s->size);
1785 s->data = read_sha1_file(s->sha1, &type, &s->size);
1792 void diff_free_filespec_blob(struct diff_filespec *s)
1796 else if (s->should_munmap)
1797 munmap(s->data, s->size);
1799 if (s->should_free || s->should_munmap) {
1800 s->should_free = s->should_munmap = 0;
1805 void diff_free_filespec_data(struct diff_filespec *s)
1807 diff_free_filespec_blob(s);
1812 static void prep_temp_blob(struct diff_tempfile *temp,
1815 const unsigned char *sha1,
1820 fd = git_mkstemp(temp->tmp_path, PATH_MAX, ".diff_XXXXXX");
1822 die("unable to create temp-file: %s", strerror(errno));
1823 if (write_in_full(fd, blob, size) != size)
1824 die("unable to write temp-file");
1826 temp->name = temp->tmp_path;
1827 strcpy(temp->hex, sha1_to_hex(sha1));
1829 sprintf(temp->mode, "%06o", mode);
1832 static void prepare_temp_file(const char *name,
1833 struct diff_tempfile *temp,
1834 struct diff_filespec *one)
1836 if (!DIFF_FILE_VALID(one)) {
1838 /* A '-' entry produces this for file-2, and
1839 * a '+' entry produces this for file-1.
1841 temp->name = "/dev/null";
1842 strcpy(temp->hex, ".");
1843 strcpy(temp->mode, ".");
1847 if (!one->sha1_valid ||
1848 reuse_worktree_file(name, one->sha1, 1)) {
1850 if (lstat(name, &st) < 0) {
1851 if (errno == ENOENT)
1852 goto not_a_valid_file;
1853 die("stat(%s): %s", name, strerror(errno));
1855 if (S_ISLNK(st.st_mode)) {
1857 char buf[PATH_MAX + 1]; /* ought to be SYMLINK_MAX */
1858 size_t sz = xsize_t(st.st_size);
1859 if (sizeof(buf) <= st.st_size)
1860 die("symlink too long: %s", name);
1861 ret = readlink(name, buf, sz);
1863 die("readlink(%s)", name);
1864 prep_temp_blob(temp, buf, sz,
1866 one->sha1 : null_sha1),
1868 one->mode : S_IFLNK));
1871 /* we can borrow from the file in the work tree */
1873 if (!one->sha1_valid)
1874 strcpy(temp->hex, sha1_to_hex(null_sha1));
1876 strcpy(temp->hex, sha1_to_hex(one->sha1));
1877 /* Even though we may sometimes borrow the
1878 * contents from the work tree, we always want
1879 * one->mode. mode is trustworthy even when
1880 * !(one->sha1_valid), as long as
1881 * DIFF_FILE_VALID(one).
1883 sprintf(temp->mode, "%06o", one->mode);
1888 if (diff_populate_filespec(one, 0))
1889 die("cannot read data blob for %s", one->path);
1890 prep_temp_blob(temp, one->data, one->size,
1891 one->sha1, one->mode);
1895 static void remove_tempfile(void)
1899 for (i = 0; i < 2; i++)
1900 if (diff_temp[i].name == diff_temp[i].tmp_path) {
1901 unlink(diff_temp[i].name);
1902 diff_temp[i].name = NULL;
1906 static void remove_tempfile_on_signal(int signo)
1909 signal(SIGINT, SIG_DFL);
1913 /* An external diff command takes:
1915 * diff-cmd name infile1 infile1-sha1 infile1-mode \
1916 * infile2 infile2-sha1 infile2-mode [ rename-to ]
1919 static void run_external_diff(const char *pgm,
1922 struct diff_filespec *one,
1923 struct diff_filespec *two,
1924 const char *xfrm_msg,
1925 int complete_rewrite)
1927 const char *spawn_arg[10];
1928 struct diff_tempfile *temp = diff_temp;
1930 static int atexit_asked = 0;
1931 const char *othername;
1932 const char **arg = &spawn_arg[0];
1934 othername = (other? other : name);
1936 prepare_temp_file(name, &temp[0], one);
1937 prepare_temp_file(othername, &temp[1], two);
1938 if (! atexit_asked &&
1939 (temp[0].name == temp[0].tmp_path ||
1940 temp[1].name == temp[1].tmp_path)) {
1942 atexit(remove_tempfile);
1944 signal(SIGINT, remove_tempfile_on_signal);
1950 *arg++ = temp[0].name;
1951 *arg++ = temp[0].hex;
1952 *arg++ = temp[0].mode;
1953 *arg++ = temp[1].name;
1954 *arg++ = temp[1].hex;
1955 *arg++ = temp[1].mode;
1966 retval = run_command_v_opt(spawn_arg, 0);
1969 fprintf(stderr, "external diff died, stopping at %s.\n", name);
1974 static void run_diff_cmd(const char *pgm,
1977 const char *attr_path,
1978 struct diff_filespec *one,
1979 struct diff_filespec *two,
1980 const char *xfrm_msg,
1981 struct diff_options *o,
1982 int complete_rewrite)
1984 if (!DIFF_OPT_TST(o, ALLOW_EXTERNAL))
1987 struct userdiff_driver *drv = userdiff_find_by_path(attr_path);
1988 if (drv && drv->external)
1989 pgm = drv->external;
1993 run_external_diff(pgm, name, other, one, two, xfrm_msg,
1998 builtin_diff(name, other ? other : name,
1999 one, two, xfrm_msg, o, complete_rewrite);
2001 fprintf(o->file, "* Unmerged path %s\n", name);
2004 static void diff_fill_sha1_info(struct diff_filespec *one)
2006 if (DIFF_FILE_VALID(one)) {
2007 if (!one->sha1_valid) {
2009 if (!strcmp(one->path, "-")) {
2010 hashcpy(one->sha1, null_sha1);
2013 if (lstat(one->path, &st) < 0)
2014 die("stat %s", one->path);
2015 if (index_path(one->sha1, one->path, &st, 0))
2016 die("cannot hash %s\n", one->path);
2023 static int similarity_index(struct diff_filepair *p)
2025 return p->score * 100 / MAX_SCORE;
2028 static void strip_prefix(int prefix_length, const char **namep, const char **otherp)
2030 /* Strip the prefix but do not molest /dev/null and absolute paths */
2031 if (*namep && **namep != '/')
2032 *namep += prefix_length;
2033 if (*otherp && **otherp != '/')
2034 *otherp += prefix_length;
2037 static void run_diff(struct diff_filepair *p, struct diff_options *o)
2039 const char *pgm = external_diff();
2042 struct diff_filespec *one = p->one;
2043 struct diff_filespec *two = p->two;
2046 const char *attr_path;
2047 int complete_rewrite = 0;
2049 name = p->one->path;
2050 other = (strcmp(name, p->two->path) ? p->two->path : NULL);
2052 if (o->prefix_length)
2053 strip_prefix(o->prefix_length, &name, &other);
2055 if (DIFF_PAIR_UNMERGED(p)) {
2056 run_diff_cmd(pgm, name, NULL, attr_path,
2057 NULL, NULL, NULL, o, 0);
2061 diff_fill_sha1_info(one);
2062 diff_fill_sha1_info(two);
2064 strbuf_init(&msg, PATH_MAX * 2 + 300);
2065 switch (p->status) {
2066 case DIFF_STATUS_COPIED:
2067 strbuf_addf(&msg, "similarity index %d%%", similarity_index(p));
2068 strbuf_addstr(&msg, "\ncopy from ");
2069 quote_c_style(name, &msg, NULL, 0);
2070 strbuf_addstr(&msg, "\ncopy to ");
2071 quote_c_style(other, &msg, NULL, 0);
2072 strbuf_addch(&msg, '\n');
2074 case DIFF_STATUS_RENAMED:
2075 strbuf_addf(&msg, "similarity index %d%%", similarity_index(p));
2076 strbuf_addstr(&msg, "\nrename from ");
2077 quote_c_style(name, &msg, NULL, 0);
2078 strbuf_addstr(&msg, "\nrename to ");
2079 quote_c_style(other, &msg, NULL, 0);
2080 strbuf_addch(&msg, '\n');
2082 case DIFF_STATUS_MODIFIED:
2084 strbuf_addf(&msg, "dissimilarity index %d%%\n",
2085 similarity_index(p));
2086 complete_rewrite = 1;
2095 if (hashcmp(one->sha1, two->sha1)) {
2096 int abbrev = DIFF_OPT_TST(o, FULL_INDEX) ? 40 : DEFAULT_ABBREV;
2098 if (DIFF_OPT_TST(o, BINARY)) {
2100 if ((!fill_mmfile(&mf, one) && diff_filespec_is_binary(one)) ||
2101 (!fill_mmfile(&mf, two) && diff_filespec_is_binary(two)))
2104 strbuf_addf(&msg, "index %.*s..%.*s",
2105 abbrev, sha1_to_hex(one->sha1),
2106 abbrev, sha1_to_hex(two->sha1));
2107 if (one->mode == two->mode)
2108 strbuf_addf(&msg, " %06o", one->mode);
2109 strbuf_addch(&msg, '\n');
2113 strbuf_setlen(&msg, msg.len - 1);
2114 xfrm_msg = msg.len ? msg.buf : NULL;
2117 DIFF_FILE_VALID(one) && DIFF_FILE_VALID(two) &&
2118 (S_IFMT & one->mode) != (S_IFMT & two->mode)) {
2119 /* a filepair that changes between file and symlink
2120 * needs to be split into deletion and creation.
2122 struct diff_filespec *null = alloc_filespec(two->path);
2123 run_diff_cmd(NULL, name, other, attr_path,
2124 one, null, xfrm_msg, o, 0);
2126 null = alloc_filespec(one->path);
2127 run_diff_cmd(NULL, name, other, attr_path,
2128 null, two, xfrm_msg, o, 0);
2132 run_diff_cmd(pgm, name, other, attr_path,
2133 one, two, xfrm_msg, o, complete_rewrite);
2135 strbuf_release(&msg);
2138 static void run_diffstat(struct diff_filepair *p, struct diff_options *o,
2139 struct diffstat_t *diffstat)
2143 int complete_rewrite = 0;
2145 if (DIFF_PAIR_UNMERGED(p)) {
2147 builtin_diffstat(p->one->path, NULL, NULL, NULL, diffstat, o, 0);
2151 name = p->one->path;
2152 other = (strcmp(name, p->two->path) ? p->two->path : NULL);
2154 if (o->prefix_length)
2155 strip_prefix(o->prefix_length, &name, &other);
2157 diff_fill_sha1_info(p->one);
2158 diff_fill_sha1_info(p->two);
2160 if (p->status == DIFF_STATUS_MODIFIED && p->score)
2161 complete_rewrite = 1;
2162 builtin_diffstat(name, other, p->one, p->two, diffstat, o, complete_rewrite);
2165 static void run_checkdiff(struct diff_filepair *p, struct diff_options *o)
2169 const char *attr_path;
2171 if (DIFF_PAIR_UNMERGED(p)) {
2176 name = p->one->path;
2177 other = (strcmp(name, p->two->path) ? p->two->path : NULL);
2178 attr_path = other ? other : name;
2180 if (o->prefix_length)
2181 strip_prefix(o->prefix_length, &name, &other);
2183 diff_fill_sha1_info(p->one);
2184 diff_fill_sha1_info(p->two);
2186 builtin_checkdiff(name, other, attr_path, p->one, p->two, o);
2189 void diff_setup(struct diff_options *options)
2191 memset(options, 0, sizeof(*options));
2193 options->file = stdout;
2195 options->line_termination = '\n';
2196 options->break_opt = -1;
2197 options->rename_limit = -1;
2198 options->dirstat_percent = 3;
2199 DIFF_OPT_CLR(options, DIRSTAT_CUMULATIVE);
2200 options->context = 3;
2202 options->change = diff_change;
2203 options->add_remove = diff_addremove;
2204 if (diff_use_color_default > 0)
2205 DIFF_OPT_SET(options, COLOR_DIFF);
2207 DIFF_OPT_CLR(options, COLOR_DIFF);
2208 options->detect_rename = diff_detect_rename_default;
2210 if (!diff_mnemonic_prefix) {
2211 options->a_prefix = "a/";
2212 options->b_prefix = "b/";
2216 int diff_setup_done(struct diff_options *options)
2220 if (options->output_format & DIFF_FORMAT_NAME)
2222 if (options->output_format & DIFF_FORMAT_NAME_STATUS)
2224 if (options->output_format & DIFF_FORMAT_CHECKDIFF)
2226 if (options->output_format & DIFF_FORMAT_NO_OUTPUT)
2229 die("--name-only, --name-status, --check and -s are mutually exclusive");
2231 if (DIFF_OPT_TST(options, FIND_COPIES_HARDER))
2232 options->detect_rename = DIFF_DETECT_COPY;
2234 if (!DIFF_OPT_TST(options, RELATIVE_NAME))
2235 options->prefix = NULL;
2236 if (options->prefix)
2237 options->prefix_length = strlen(options->prefix);
2239 options->prefix_length = 0;
2241 if (options->output_format & (DIFF_FORMAT_NAME |
2242 DIFF_FORMAT_NAME_STATUS |
2243 DIFF_FORMAT_CHECKDIFF |
2244 DIFF_FORMAT_NO_OUTPUT))
2245 options->output_format &= ~(DIFF_FORMAT_RAW |
2246 DIFF_FORMAT_NUMSTAT |
2247 DIFF_FORMAT_DIFFSTAT |
2248 DIFF_FORMAT_SHORTSTAT |
2249 DIFF_FORMAT_DIRSTAT |
2250 DIFF_FORMAT_SUMMARY |
2254 * These cases always need recursive; we do not drop caller-supplied
2255 * recursive bits for other formats here.
2257 if (options->output_format & (DIFF_FORMAT_PATCH |
2258 DIFF_FORMAT_NUMSTAT |
2259 DIFF_FORMAT_DIFFSTAT |
2260 DIFF_FORMAT_SHORTSTAT |
2261 DIFF_FORMAT_DIRSTAT |
2262 DIFF_FORMAT_SUMMARY |
2263 DIFF_FORMAT_CHECKDIFF))
2264 DIFF_OPT_SET(options, RECURSIVE);
2266 * Also pickaxe would not work very well if you do not say recursive
2268 if (options->pickaxe)
2269 DIFF_OPT_SET(options, RECURSIVE);
2271 if (options->detect_rename && options->rename_limit < 0)
2272 options->rename_limit = diff_rename_limit_default;
2273 if (options->setup & DIFF_SETUP_USE_CACHE) {
2275 /* read-cache does not die even when it fails
2276 * so it is safe for us to do this here. Also
2277 * it does not smudge active_cache or active_nr
2278 * when it fails, so we do not have to worry about
2279 * cleaning it up ourselves either.
2283 if (options->abbrev <= 0 || 40 < options->abbrev)
2284 options->abbrev = 40; /* full */
2287 * It does not make sense to show the first hit we happened
2288 * to have found. It does not make sense not to return with
2289 * exit code in such a case either.
2291 if (DIFF_OPT_TST(options, QUIET)) {
2292 options->output_format = DIFF_FORMAT_NO_OUTPUT;
2293 DIFF_OPT_SET(options, EXIT_WITH_STATUS);
2299 static int opt_arg(const char *arg, int arg_short, const char *arg_long, int *val)
2309 if (c == arg_short) {
2313 if (val && isdigit(c)) {
2315 int n = strtoul(arg, &end, 10);
2326 eq = strchr(arg, '=');
2331 if (!len || strncmp(arg, arg_long, len))
2336 if (!isdigit(*++eq))
2338 n = strtoul(eq, &end, 10);
2346 static int diff_scoreopt_parse(const char *opt);
2348 int diff_opt_parse(struct diff_options *options, const char **av, int ac)
2350 const char *arg = av[0];
2352 /* Output format options */
2353 if (!strcmp(arg, "-p") || !strcmp(arg, "-u"))
2354 options->output_format |= DIFF_FORMAT_PATCH;
2355 else if (opt_arg(arg, 'U', "unified", &options->context))
2356 options->output_format |= DIFF_FORMAT_PATCH;
2357 else if (!strcmp(arg, "--raw"))
2358 options->output_format |= DIFF_FORMAT_RAW;
2359 else if (!strcmp(arg, "--patch-with-raw"))
2360 options->output_format |= DIFF_FORMAT_PATCH | DIFF_FORMAT_RAW;
2361 else if (!strcmp(arg, "--numstat"))
2362 options->output_format |= DIFF_FORMAT_NUMSTAT;
2363 else if (!strcmp(arg, "--shortstat"))
2364 options->output_format |= DIFF_FORMAT_SHORTSTAT;
2365 else if (opt_arg(arg, 'X', "dirstat", &options->dirstat_percent))
2366 options->output_format |= DIFF_FORMAT_DIRSTAT;
2367 else if (!strcmp(arg, "--cumulative")) {
2368 options->output_format |= DIFF_FORMAT_DIRSTAT;
2369 DIFF_OPT_SET(options, DIRSTAT_CUMULATIVE);
2370 } else if (opt_arg(arg, 0, "dirstat-by-file",
2371 &options->dirstat_percent)) {
2372 options->output_format |= DIFF_FORMAT_DIRSTAT;
2373 DIFF_OPT_SET(options, DIRSTAT_BY_FILE);
2375 else if (!strcmp(arg, "--check"))
2376 options->output_format |= DIFF_FORMAT_CHECKDIFF;
2377 else if (!strcmp(arg, "--summary"))
2378 options->output_format |= DIFF_FORMAT_SUMMARY;
2379 else if (!strcmp(arg, "--patch-with-stat"))
2380 options->output_format |= DIFF_FORMAT_PATCH | DIFF_FORMAT_DIFFSTAT;
2381 else if (!strcmp(arg, "--name-only"))
2382 options->output_format |= DIFF_FORMAT_NAME;
2383 else if (!strcmp(arg, "--name-status"))
2384 options->output_format |= DIFF_FORMAT_NAME_STATUS;
2385 else if (!strcmp(arg, "-s"))
2386 options->output_format |= DIFF_FORMAT_NO_OUTPUT;
2387 else if (!prefixcmp(arg, "--stat")) {
2389 int width = options->stat_width;
2390 int name_width = options->stat_name_width;
2396 if (!prefixcmp(arg, "-width="))
2397 width = strtoul(arg + 7, &end, 10);
2398 else if (!prefixcmp(arg, "-name-width="))
2399 name_width = strtoul(arg + 12, &end, 10);
2402 width = strtoul(arg+1, &end, 10);
2404 name_width = strtoul(end+1, &end, 10);
2407 /* Important! This checks all the error cases! */
2410 options->output_format |= DIFF_FORMAT_DIFFSTAT;
2411 options->stat_name_width = name_width;
2412 options->stat_width = width;
2415 /* renames options */
2416 else if (!prefixcmp(arg, "-B")) {
2417 if ((options->break_opt = diff_scoreopt_parse(arg)) == -1)
2420 else if (!prefixcmp(arg, "-M")) {
2421 if ((options->rename_score = diff_scoreopt_parse(arg)) == -1)
2423 options->detect_rename = DIFF_DETECT_RENAME;
2425 else if (!prefixcmp(arg, "-C")) {
2426 if (options->detect_rename == DIFF_DETECT_COPY)
2427 DIFF_OPT_SET(options, FIND_COPIES_HARDER);
2428 if ((options->rename_score = diff_scoreopt_parse(arg)) == -1)
2430 options->detect_rename = DIFF_DETECT_COPY;
2432 else if (!strcmp(arg, "--no-renames"))
2433 options->detect_rename = 0;
2434 else if (!strcmp(arg, "--relative"))
2435 DIFF_OPT_SET(options, RELATIVE_NAME);
2436 else if (!prefixcmp(arg, "--relative=")) {
2437 DIFF_OPT_SET(options, RELATIVE_NAME);
2438 options->prefix = arg + 11;
2442 else if (!strcmp(arg, "-w") || !strcmp(arg, "--ignore-all-space"))
2443 options->xdl_opts |= XDF_IGNORE_WHITESPACE;
2444 else if (!strcmp(arg, "-b") || !strcmp(arg, "--ignore-space-change"))
2445 options->xdl_opts |= XDF_IGNORE_WHITESPACE_CHANGE;
2446 else if (!strcmp(arg, "--ignore-space-at-eol"))
2447 options->xdl_opts |= XDF_IGNORE_WHITESPACE_AT_EOL;
2450 else if (!strcmp(arg, "--binary")) {
2451 options->output_format |= DIFF_FORMAT_PATCH;
2452 DIFF_OPT_SET(options, BINARY);
2454 else if (!strcmp(arg, "--full-index"))
2455 DIFF_OPT_SET(options, FULL_INDEX);
2456 else if (!strcmp(arg, "-a") || !strcmp(arg, "--text"))
2457 DIFF_OPT_SET(options, TEXT);
2458 else if (!strcmp(arg, "-R"))
2459 DIFF_OPT_SET(options, REVERSE_DIFF);
2460 else if (!strcmp(arg, "--find-copies-harder"))
2461 DIFF_OPT_SET(options, FIND_COPIES_HARDER);
2462 else if (!strcmp(arg, "--follow"))
2463 DIFF_OPT_SET(options, FOLLOW_RENAMES);
2464 else if (!strcmp(arg, "--color"))
2465 DIFF_OPT_SET(options, COLOR_DIFF);
2466 else if (!strcmp(arg, "--no-color"))
2467 DIFF_OPT_CLR(options, COLOR_DIFF);
2468 else if (!strcmp(arg, "--color-words"))
2469 options->flags |= DIFF_OPT_COLOR_DIFF | DIFF_OPT_COLOR_DIFF_WORDS;
2470 else if (!strcmp(arg, "--exit-code"))
2471 DIFF_OPT_SET(options, EXIT_WITH_STATUS);
2472 else if (!strcmp(arg, "--quiet"))
2473 DIFF_OPT_SET(options, QUIET);
2474 else if (!strcmp(arg, "--ext-diff"))
2475 DIFF_OPT_SET(options, ALLOW_EXTERNAL);
2476 else if (!strcmp(arg, "--no-ext-diff"))
2477 DIFF_OPT_CLR(options, ALLOW_EXTERNAL);
2478 else if (!strcmp(arg, "--ignore-submodules"))
2479 DIFF_OPT_SET(options, IGNORE_SUBMODULES);
2482 else if (!strcmp(arg, "-z"))
2483 options->line_termination = 0;
2484 else if (!prefixcmp(arg, "-l"))
2485 options->rename_limit = strtoul(arg+2, NULL, 10);
2486 else if (!prefixcmp(arg, "-S"))
2487 options->pickaxe = arg + 2;
2488 else if (!strcmp(arg, "--pickaxe-all"))
2489 options->pickaxe_opts = DIFF_PICKAXE_ALL;
2490 else if (!strcmp(arg, "--pickaxe-regex"))
2491 options->pickaxe_opts = DIFF_PICKAXE_REGEX;
2492 else if (!prefixcmp(arg, "-O"))
2493 options->orderfile = arg + 2;
2494 else if (!prefixcmp(arg, "--diff-filter="))
2495 options->filter = arg + 14;
2496 else if (!strcmp(arg, "--abbrev"))
2497 options->abbrev = DEFAULT_ABBREV;
2498 else if (!prefixcmp(arg, "--abbrev=")) {
2499 options->abbrev = strtoul(arg + 9, NULL, 10);
2500 if (options->abbrev < MINIMUM_ABBREV)
2501 options->abbrev = MINIMUM_ABBREV;
2502 else if (40 < options->abbrev)
2503 options->abbrev = 40;
2505 else if (!prefixcmp(arg, "--src-prefix="))
2506 options->a_prefix = arg + 13;
2507 else if (!prefixcmp(arg, "--dst-prefix="))
2508 options->b_prefix = arg + 13;
2509 else if (!strcmp(arg, "--no-prefix"))
2510 options->a_prefix = options->b_prefix = "";
2511 else if (!prefixcmp(arg, "--output=")) {
2512 options->file = fopen(arg + strlen("--output="), "w");
2513 options->close_file = 1;
2519 static int parse_num(const char **cp_p)
2521 unsigned long num, scale;
2523 const char *cp = *cp_p;
2530 if ( !dot && ch == '.' ) {
2533 } else if ( ch == '%' ) {
2534 scale = dot ? scale*100 : 100;
2535 cp++; /* % is always at the end */
2537 } else if ( ch >= '0' && ch <= '9' ) {
2538 if ( scale < 100000 ) {
2540 num = (num*10) + (ch-'0');
2549 /* user says num divided by scale and we say internally that
2550 * is MAX_SCORE * num / scale.
2552 return (int)((num >= scale) ? MAX_SCORE : (MAX_SCORE * num / scale));
2555 static int diff_scoreopt_parse(const char *opt)
2557 int opt1, opt2, cmd;
2562 if (cmd != 'M' && cmd != 'C' && cmd != 'B')
2563 return -1; /* that is not a -M, -C nor -B option */
2565 opt1 = parse_num(&opt);
2571 else if (*opt != '/')
2572 return -1; /* we expect -B80/99 or -B80 */
2575 opt2 = parse_num(&opt);
2580 return opt1 | (opt2 << 16);
2583 struct diff_queue_struct diff_queued_diff;
2585 void diff_q(struct diff_queue_struct *queue, struct diff_filepair *dp)
2587 if (queue->alloc <= queue->nr) {
2588 queue->alloc = alloc_nr(queue->alloc);
2589 queue->queue = xrealloc(queue->queue,
2590 sizeof(dp) * queue->alloc);
2592 queue->queue[queue->nr++] = dp;
2595 struct diff_filepair *diff_queue(struct diff_queue_struct *queue,
2596 struct diff_filespec *one,
2597 struct diff_filespec *two)
2599 struct diff_filepair *dp = xcalloc(1, sizeof(*dp));
2607 void diff_free_filepair(struct diff_filepair *p)
2609 free_filespec(p->one);
2610 free_filespec(p->two);
2614 /* This is different from find_unique_abbrev() in that
2615 * it stuffs the result with dots for alignment.
2617 const char *diff_unique_abbrev(const unsigned char *sha1, int len)
2622 return sha1_to_hex(sha1);
2624 abbrev = find_unique_abbrev(sha1, len);
2625 abblen = strlen(abbrev);
2627 static char hex[41];
2628 if (len < abblen && abblen <= len + 2)
2629 sprintf(hex, "%s%.*s", abbrev, len+3-abblen, "..");
2631 sprintf(hex, "%s...", abbrev);
2634 return sha1_to_hex(sha1);
2637 static void diff_flush_raw(struct diff_filepair *p, struct diff_options *opt)
2639 int line_termination = opt->line_termination;
2640 int inter_name_termination = line_termination ? '\t' : '\0';
2642 if (!(opt->output_format & DIFF_FORMAT_NAME_STATUS)) {
2643 fprintf(opt->file, ":%06o %06o %s ", p->one->mode, p->two->mode,
2644 diff_unique_abbrev(p->one->sha1, opt->abbrev));
2645 fprintf(opt->file, "%s ", diff_unique_abbrev(p->two->sha1, opt->abbrev));
2648 fprintf(opt->file, "%c%03d%c", p->status, similarity_index(p),
2649 inter_name_termination);
2651 fprintf(opt->file, "%c%c", p->status, inter_name_termination);
2654 if (p->status == DIFF_STATUS_COPIED ||
2655 p->status == DIFF_STATUS_RENAMED) {
2656 const char *name_a, *name_b;
2657 name_a = p->one->path;
2658 name_b = p->two->path;
2659 strip_prefix(opt->prefix_length, &name_a, &name_b);
2660 write_name_quoted(name_a, opt->file, inter_name_termination);
2661 write_name_quoted(name_b, opt->file, line_termination);
2663 const char *name_a, *name_b;
2664 name_a = p->one->mode ? p->one->path : p->two->path;
2666 strip_prefix(opt->prefix_length, &name_a, &name_b);
2667 write_name_quoted(name_a, opt->file, line_termination);
2671 int diff_unmodified_pair(struct diff_filepair *p)
2673 /* This function is written stricter than necessary to support
2674 * the currently implemented transformers, but the idea is to
2675 * let transformers to produce diff_filepairs any way they want,
2676 * and filter and clean them up here before producing the output.
2678 struct diff_filespec *one = p->one, *two = p->two;
2680 if (DIFF_PAIR_UNMERGED(p))
2681 return 0; /* unmerged is interesting */
2683 /* deletion, addition, mode or type change
2684 * and rename are all interesting.
2686 if (DIFF_FILE_VALID(one) != DIFF_FILE_VALID(two) ||
2687 DIFF_PAIR_MODE_CHANGED(p) ||
2688 strcmp(one->path, two->path))
2691 /* both are valid and point at the same path. that is, we are
2692 * dealing with a change.
2694 if (one->sha1_valid && two->sha1_valid &&
2695 !hashcmp(one->sha1, two->sha1))
2696 return 1; /* no change */
2697 if (!one->sha1_valid && !two->sha1_valid)
2698 return 1; /* both look at the same file on the filesystem. */
2702 static void diff_flush_patch(struct diff_filepair *p, struct diff_options *o)
2704 if (diff_unmodified_pair(p))
2707 if ((DIFF_FILE_VALID(p->one) && S_ISDIR(p->one->mode)) ||
2708 (DIFF_FILE_VALID(p->two) && S_ISDIR(p->two->mode)))
2709 return; /* no tree diffs in patch format */
2714 static void diff_flush_stat(struct diff_filepair *p, struct diff_options *o,
2715 struct diffstat_t *diffstat)
2717 if (diff_unmodified_pair(p))
2720 if ((DIFF_FILE_VALID(p->one) && S_ISDIR(p->one->mode)) ||
2721 (DIFF_FILE_VALID(p->two) && S_ISDIR(p->two->mode)))
2722 return; /* no tree diffs in patch format */
2724 run_diffstat(p, o, diffstat);
2727 static void diff_flush_checkdiff(struct diff_filepair *p,
2728 struct diff_options *o)
2730 if (diff_unmodified_pair(p))
2733 if ((DIFF_FILE_VALID(p->one) && S_ISDIR(p->one->mode)) ||
2734 (DIFF_FILE_VALID(p->two) && S_ISDIR(p->two->mode)))
2735 return; /* no tree diffs in patch format */
2737 run_checkdiff(p, o);
2740 int diff_queue_is_empty(void)
2742 struct diff_queue_struct *q = &diff_queued_diff;
2744 for (i = 0; i < q->nr; i++)
2745 if (!diff_unmodified_pair(q->queue[i]))
2751 void diff_debug_filespec(struct diff_filespec *s, int x, const char *one)
2753 fprintf(stderr, "queue[%d] %s (%s) %s %06o %s\n",
2756 DIFF_FILE_VALID(s) ? "valid" : "invalid",
2758 s->sha1_valid ? sha1_to_hex(s->sha1) : "");
2759 fprintf(stderr, "queue[%d] %s size %lu flags %d\n",
2761 s->size, s->xfrm_flags);
2764 void diff_debug_filepair(const struct diff_filepair *p, int i)
2766 diff_debug_filespec(p->one, i, "one");
2767 diff_debug_filespec(p->two, i, "two");
2768 fprintf(stderr, "score %d, status %c rename_used %d broken %d\n",
2769 p->score, p->status ? p->status : '?',
2770 p->one->rename_used, p->broken_pair);
2773 void diff_debug_queue(const char *msg, struct diff_queue_struct *q)
2777 fprintf(stderr, "%s\n", msg);
2778 fprintf(stderr, "q->nr = %d\n", q->nr);
2779 for (i = 0; i < q->nr; i++) {
2780 struct diff_filepair *p = q->queue[i];
2781 diff_debug_filepair(p, i);
2786 static void diff_resolve_rename_copy(void)
2789 struct diff_filepair *p;
2790 struct diff_queue_struct *q = &diff_queued_diff;
2792 diff_debug_queue("resolve-rename-copy", q);
2794 for (i = 0; i < q->nr; i++) {
2796 p->status = 0; /* undecided */
2797 if (DIFF_PAIR_UNMERGED(p))
2798 p->status = DIFF_STATUS_UNMERGED;
2799 else if (!DIFF_FILE_VALID(p->one))
2800 p->status = DIFF_STATUS_ADDED;
2801 else if (!DIFF_FILE_VALID(p->two))
2802 p->status = DIFF_STATUS_DELETED;
2803 else if (DIFF_PAIR_TYPE_CHANGED(p))
2804 p->status = DIFF_STATUS_TYPE_CHANGED;
2806 /* from this point on, we are dealing with a pair
2807 * whose both sides are valid and of the same type, i.e.
2808 * either in-place edit or rename/copy edit.
2810 else if (DIFF_PAIR_RENAME(p)) {
2812 * A rename might have re-connected a broken
2813 * pair up, causing the pathnames to be the
2814 * same again. If so, that's not a rename at
2815 * all, just a modification..
2817 * Otherwise, see if this source was used for
2818 * multiple renames, in which case we decrement
2819 * the count, and call it a copy.
2821 if (!strcmp(p->one->path, p->two->path))
2822 p->status = DIFF_STATUS_MODIFIED;
2823 else if (--p->one->rename_used > 0)
2824 p->status = DIFF_STATUS_COPIED;
2826 p->status = DIFF_STATUS_RENAMED;
2828 else if (hashcmp(p->one->sha1, p->two->sha1) ||
2829 p->one->mode != p->two->mode ||
2830 is_null_sha1(p->one->sha1))
2831 p->status = DIFF_STATUS_MODIFIED;
2833 /* This is a "no-change" entry and should not
2834 * happen anymore, but prepare for broken callers.
2836 error("feeding unmodified %s to diffcore",
2838 p->status = DIFF_STATUS_UNKNOWN;
2841 diff_debug_queue("resolve-rename-copy done", q);
2844 static int check_pair_status(struct diff_filepair *p)
2846 switch (p->status) {
2847 case DIFF_STATUS_UNKNOWN:
2850 die("internal error in diff-resolve-rename-copy");
2856 static void flush_one_pair(struct diff_filepair *p, struct diff_options *opt)
2858 int fmt = opt->output_format;
2860 if (fmt & DIFF_FORMAT_CHECKDIFF)
2861 diff_flush_checkdiff(p, opt);
2862 else if (fmt & (DIFF_FORMAT_RAW | DIFF_FORMAT_NAME_STATUS))
2863 diff_flush_raw(p, opt);
2864 else if (fmt & DIFF_FORMAT_NAME) {
2865 const char *name_a, *name_b;
2866 name_a = p->two->path;
2868 strip_prefix(opt->prefix_length, &name_a, &name_b);
2869 write_name_quoted(name_a, opt->file, opt->line_termination);
2873 static void show_file_mode_name(FILE *file, const char *newdelete, struct diff_filespec *fs)
2876 fprintf(file, " %s mode %06o ", newdelete, fs->mode);
2878 fprintf(file, " %s ", newdelete);
2879 write_name_quoted(fs->path, file, '\n');
2883 static void show_mode_change(FILE *file, struct diff_filepair *p, int show_name)
2885 if (p->one->mode && p->two->mode && p->one->mode != p->two->mode) {
2886 fprintf(file, " mode change %06o => %06o%c", p->one->mode, p->two->mode,
2887 show_name ? ' ' : '\n');
2889 write_name_quoted(p->two->path, file, '\n');
2894 static void show_rename_copy(FILE *file, const char *renamecopy, struct diff_filepair *p)
2896 char *names = pprint_rename(p->one->path, p->two->path);
2898 fprintf(file, " %s %s (%d%%)\n", renamecopy, names, similarity_index(p));
2900 show_mode_change(file, p, 0);
2903 static void diff_summary(FILE *file, struct diff_filepair *p)
2906 case DIFF_STATUS_DELETED:
2907 show_file_mode_name(file, "delete", p->one);
2909 case DIFF_STATUS_ADDED:
2910 show_file_mode_name(file, "create", p->two);
2912 case DIFF_STATUS_COPIED:
2913 show_rename_copy(file, "copy", p);
2915 case DIFF_STATUS_RENAMED:
2916 show_rename_copy(file, "rename", p);
2920 fputs(" rewrite ", file);
2921 write_name_quoted(p->two->path, file, ' ');
2922 fprintf(file, "(%d%%)\n", similarity_index(p));
2924 show_mode_change(file, p, !p->score);
2934 static int remove_space(char *line, int len)
2940 for (i = 0; i < len; i++)
2941 if (!isspace((c = line[i])))
2947 static void patch_id_consume(void *priv, char *line, unsigned long len)
2949 struct patch_id_t *data = priv;
2952 /* Ignore line numbers when computing the SHA1 of the patch */
2953 if (!prefixcmp(line, "@@ -"))
2956 new_len = remove_space(line, len);
2958 git_SHA1_Update(data->ctx, line, new_len);
2959 data->patchlen += new_len;
2962 /* returns 0 upon success, and writes result into sha1 */
2963 static int diff_get_patch_id(struct diff_options *options, unsigned char *sha1)
2965 struct diff_queue_struct *q = &diff_queued_diff;
2968 struct patch_id_t data;
2969 char buffer[PATH_MAX * 4 + 20];
2971 git_SHA1_Init(&ctx);
2972 memset(&data, 0, sizeof(struct patch_id_t));
2975 for (i = 0; i < q->nr; i++) {
2980 struct diff_filepair *p = q->queue[i];
2983 memset(&xecfg, 0, sizeof(xecfg));
2985 return error("internal diff status error");
2986 if (p->status == DIFF_STATUS_UNKNOWN)
2988 if (diff_unmodified_pair(p))
2990 if ((DIFF_FILE_VALID(p->one) && S_ISDIR(p->one->mode)) ||
2991 (DIFF_FILE_VALID(p->two) && S_ISDIR(p->two->mode)))
2993 if (DIFF_PAIR_UNMERGED(p))
2996 diff_fill_sha1_info(p->one);
2997 diff_fill_sha1_info(p->two);
2998 if (fill_mmfile(&mf1, p->one) < 0 ||
2999 fill_mmfile(&mf2, p->two) < 0)
3000 return error("unable to read files to diff");
3002 len1 = remove_space(p->one->path, strlen(p->one->path));
3003 len2 = remove_space(p->two->path, strlen(p->two->path));
3004 if (p->one->mode == 0)
3005 len1 = snprintf(buffer, sizeof(buffer),
3006 "diff--gita/%.*sb/%.*s"
3013 len2, p->two->path);
3014 else if (p->two->mode == 0)
3015 len1 = snprintf(buffer, sizeof(buffer),
3016 "diff--gita/%.*sb/%.*s"
3017 "deletedfilemode%06o"
3023 len1, p->one->path);
3025 len1 = snprintf(buffer, sizeof(buffer),
3026 "diff--gita/%.*sb/%.*s"
3032 len2, p->two->path);
3033 git_SHA1_Update(&ctx, buffer, len1);
3035 xpp.flags = XDF_NEED_MINIMAL;
3037 xecfg.flags = XDL_EMIT_FUNCNAMES;
3038 xdi_diff_outf(&mf1, &mf2, patch_id_consume, &data,
3039 &xpp, &xecfg, &ecb);
3042 git_SHA1_Final(sha1, &ctx);
3046 int diff_flush_patch_id(struct diff_options *options, unsigned char *sha1)
3048 struct diff_queue_struct *q = &diff_queued_diff;
3050 int result = diff_get_patch_id(options, sha1);
3052 for (i = 0; i < q->nr; i++)
3053 diff_free_filepair(q->queue[i]);
3057 q->nr = q->alloc = 0;
3062 static int is_summary_empty(const struct diff_queue_struct *q)
3066 for (i = 0; i < q->nr; i++) {
3067 const struct diff_filepair *p = q->queue[i];
3069 switch (p->status) {
3070 case DIFF_STATUS_DELETED:
3071 case DIFF_STATUS_ADDED:
3072 case DIFF_STATUS_COPIED:
3073 case DIFF_STATUS_RENAMED:
3078 if (p->one->mode && p->two->mode &&
3079 p->one->mode != p->two->mode)
3087 void diff_flush(struct diff_options *options)
3089 struct diff_queue_struct *q = &diff_queued_diff;
3090 int i, output_format = options->output_format;
3094 * Order: raw, stat, summary, patch
3095 * or: name/name-status/checkdiff (other bits clear)
3100 if (output_format & (DIFF_FORMAT_RAW |
3102 DIFF_FORMAT_NAME_STATUS |
3103 DIFF_FORMAT_CHECKDIFF)) {
3104 for (i = 0; i < q->nr; i++) {
3105 struct diff_filepair *p = q->queue[i];
3106 if (check_pair_status(p))
3107 flush_one_pair(p, options);
3112 if (output_format & (DIFF_FORMAT_DIFFSTAT|DIFF_FORMAT_SHORTSTAT|DIFF_FORMAT_NUMSTAT)) {
3113 struct diffstat_t diffstat;
3115 memset(&diffstat, 0, sizeof(struct diffstat_t));
3116 for (i = 0; i < q->nr; i++) {
3117 struct diff_filepair *p = q->queue[i];
3118 if (check_pair_status(p))
3119 diff_flush_stat(p, options, &diffstat);
3121 if (output_format & DIFF_FORMAT_NUMSTAT)
3122 show_numstat(&diffstat, options);
3123 if (output_format & DIFF_FORMAT_DIFFSTAT)
3124 show_stats(&diffstat, options);
3125 if (output_format & DIFF_FORMAT_SHORTSTAT)
3126 show_shortstats(&diffstat, options);
3127 free_diffstat_info(&diffstat);
3130 if (output_format & DIFF_FORMAT_DIRSTAT)
3131 show_dirstat(options);
3133 if (output_format & DIFF_FORMAT_SUMMARY && !is_summary_empty(q)) {
3134 for (i = 0; i < q->nr; i++)
3135 diff_summary(options->file, q->queue[i]);
3139 if (output_format & DIFF_FORMAT_PATCH) {
3141 putc(options->line_termination, options->file);
3142 if (options->stat_sep) {
3143 /* attach patch instead of inline */
3144 fputs(options->stat_sep, options->file);
3148 for (i = 0; i < q->nr; i++) {
3149 struct diff_filepair *p = q->queue[i];
3150 if (check_pair_status(p))
3151 diff_flush_patch(p, options);
3155 if (output_format & DIFF_FORMAT_CALLBACK)
3156 options->format_callback(q, options, options->format_callback_data);
3158 for (i = 0; i < q->nr; i++)
3159 diff_free_filepair(q->queue[i]);
3163 q->nr = q->alloc = 0;
3164 if (options->close_file)
3165 fclose(options->file);
3168 static void diffcore_apply_filter(const char *filter)
3171 struct diff_queue_struct *q = &diff_queued_diff;
3172 struct diff_queue_struct outq;
3174 outq.nr = outq.alloc = 0;
3179 if (strchr(filter, DIFF_STATUS_FILTER_AON)) {
3181 for (i = found = 0; !found && i < q->nr; i++) {
3182 struct diff_filepair *p = q->queue[i];
3183 if (((p->status == DIFF_STATUS_MODIFIED) &&
3185 strchr(filter, DIFF_STATUS_FILTER_BROKEN)) ||
3187 strchr(filter, DIFF_STATUS_MODIFIED)))) ||
3188 ((p->status != DIFF_STATUS_MODIFIED) &&
3189 strchr(filter, p->status)))
3195 /* otherwise we will clear the whole queue
3196 * by copying the empty outq at the end of this
3197 * function, but first clear the current entries
3200 for (i = 0; i < q->nr; i++)
3201 diff_free_filepair(q->queue[i]);
3204 /* Only the matching ones */
3205 for (i = 0; i < q->nr; i++) {
3206 struct diff_filepair *p = q->queue[i];
3208 if (((p->status == DIFF_STATUS_MODIFIED) &&
3210 strchr(filter, DIFF_STATUS_FILTER_BROKEN)) ||
3212 strchr(filter, DIFF_STATUS_MODIFIED)))) ||
3213 ((p->status != DIFF_STATUS_MODIFIED) &&
3214 strchr(filter, p->status)))
3217 diff_free_filepair(p);
3224 /* Check whether two filespecs with the same mode and size are identical */
3225 static int diff_filespec_is_identical(struct diff_filespec *one,
3226 struct diff_filespec *two)
3228 if (S_ISGITLINK(one->mode))
3230 if (diff_populate_filespec(one, 0))
3232 if (diff_populate_filespec(two, 0))
3234 return !memcmp(one->data, two->data, one->size);
3237 static void diffcore_skip_stat_unmatch(struct diff_options *diffopt)
3240 struct diff_queue_struct *q = &diff_queued_diff;
3241 struct diff_queue_struct outq;
3243 outq.nr = outq.alloc = 0;
3245 for (i = 0; i < q->nr; i++) {
3246 struct diff_filepair *p = q->queue[i];
3249 * 1. Entries that come from stat info dirtyness
3250 * always have both sides (iow, not create/delete),
3251 * one side of the object name is unknown, with
3252 * the same mode and size. Keep the ones that
3253 * do not match these criteria. They have real
3256 * 2. At this point, the file is known to be modified,
3257 * with the same mode and size, and the object
3258 * name of one side is unknown. Need to inspect
3259 * the identical contents.
3261 if (!DIFF_FILE_VALID(p->one) || /* (1) */
3262 !DIFF_FILE_VALID(p->two) ||
3263 (p->one->sha1_valid && p->two->sha1_valid) ||
3264 (p->one->mode != p->two->mode) ||
3265 diff_populate_filespec(p->one, 1) ||
3266 diff_populate_filespec(p->two, 1) ||
3267 (p->one->size != p->two->size) ||
3268 !diff_filespec_is_identical(p->one, p->two)) /* (2) */
3272 * The caller can subtract 1 from skip_stat_unmatch
3273 * to determine how many paths were dirty only
3274 * due to stat info mismatch.
3276 if (!DIFF_OPT_TST(diffopt, NO_INDEX))
3277 diffopt->skip_stat_unmatch++;
3278 diff_free_filepair(p);
3285 void diffcore_std(struct diff_options *options)
3287 if (options->skip_stat_unmatch)
3288 diffcore_skip_stat_unmatch(options);
3289 if (options->break_opt != -1)
3290 diffcore_break(options->break_opt);
3291 if (options->detect_rename)
3292 diffcore_rename(options);
3293 if (options->break_opt != -1)
3294 diffcore_merge_broken();
3295 if (options->pickaxe)
3296 diffcore_pickaxe(options->pickaxe, options->pickaxe_opts);
3297 if (options->orderfile)
3298 diffcore_order(options->orderfile);
3299 diff_resolve_rename_copy();
3300 diffcore_apply_filter(options->filter);
3302 if (diff_queued_diff.nr)
3303 DIFF_OPT_SET(options, HAS_CHANGES);
3305 DIFF_OPT_CLR(options, HAS_CHANGES);
3308 int diff_result_code(struct diff_options *opt, int status)
3311 if (!DIFF_OPT_TST(opt, EXIT_WITH_STATUS) &&
3312 !(opt->output_format & DIFF_FORMAT_CHECKDIFF))
3314 if (DIFF_OPT_TST(opt, EXIT_WITH_STATUS) &&
3315 DIFF_OPT_TST(opt, HAS_CHANGES))
3317 if ((opt->output_format & DIFF_FORMAT_CHECKDIFF) &&
3318 DIFF_OPT_TST(opt, CHECK_FAILED))
3323 void diff_addremove(struct diff_options *options,
3324 int addremove, unsigned mode,
3325 const unsigned char *sha1,
3326 const char *concatpath)
3328 struct diff_filespec *one, *two;
3330 if (DIFF_OPT_TST(options, IGNORE_SUBMODULES) && S_ISGITLINK(mode))
3333 /* This may look odd, but it is a preparation for
3334 * feeding "there are unchanged files which should
3335 * not produce diffs, but when you are doing copy
3336 * detection you would need them, so here they are"
3337 * entries to the diff-core. They will be prefixed
3338 * with something like '=' or '*' (I haven't decided
3339 * which but should not make any difference).
3340 * Feeding the same new and old to diff_change()
3341 * also has the same effect.
3342 * Before the final output happens, they are pruned after
3343 * merged into rename/copy pairs as appropriate.
3345 if (DIFF_OPT_TST(options, REVERSE_DIFF))
3346 addremove = (addremove == '+' ? '-' :
3347 addremove == '-' ? '+' : addremove);
3349 if (options->prefix &&
3350 strncmp(concatpath, options->prefix, options->prefix_length))
3353 one = alloc_filespec(concatpath);
3354 two = alloc_filespec(concatpath);
3356 if (addremove != '+')
3357 fill_filespec(one, sha1, mode);
3358 if (addremove != '-')
3359 fill_filespec(two, sha1, mode);
3361 diff_queue(&diff_queued_diff, one, two);
3362 DIFF_OPT_SET(options, HAS_CHANGES);
3365 void diff_change(struct diff_options *options,
3366 unsigned old_mode, unsigned new_mode,
3367 const unsigned char *old_sha1,
3368 const unsigned char *new_sha1,
3369 const char *concatpath)
3371 struct diff_filespec *one, *two;
3373 if (DIFF_OPT_TST(options, IGNORE_SUBMODULES) && S_ISGITLINK(old_mode)
3374 && S_ISGITLINK(new_mode))
3377 if (DIFF_OPT_TST(options, REVERSE_DIFF)) {
3379 const unsigned char *tmp_c;
3380 tmp = old_mode; old_mode = new_mode; new_mode = tmp;
3381 tmp_c = old_sha1; old_sha1 = new_sha1; new_sha1 = tmp_c;
3384 if (options->prefix &&
3385 strncmp(concatpath, options->prefix, options->prefix_length))
3388 one = alloc_filespec(concatpath);
3389 two = alloc_filespec(concatpath);
3390 fill_filespec(one, old_sha1, old_mode);
3391 fill_filespec(two, new_sha1, new_mode);
3393 diff_queue(&diff_queued_diff, one, two);
3394 DIFF_OPT_SET(options, HAS_CHANGES);
3397 void diff_unmerge(struct diff_options *options,
3399 unsigned mode, const unsigned char *sha1)
3401 struct diff_filespec *one, *two;
3403 if (options->prefix &&
3404 strncmp(path, options->prefix, options->prefix_length))
3407 one = alloc_filespec(path);
3408 two = alloc_filespec(path);
3409 fill_filespec(one, sha1, mode);
3410 diff_queue(&diff_queued_diff, one, two)->is_unmerged = 1;
3413 static char *run_textconv(const char *pgm, struct diff_filespec *spec,
3416 struct diff_tempfile temp;
3417 const char *argv[3];
3418 const char **arg = argv;
3419 struct child_process child;
3420 struct strbuf buf = STRBUF_INIT;
3422 prepare_temp_file(spec->path, &temp, spec);
3427 memset(&child, 0, sizeof(child));
3430 if (start_command(&child) != 0 ||
3431 strbuf_read(&buf, child.out, 0) < 0 ||
3432 finish_command(&child) != 0) {
3433 if (temp.name == temp.tmp_path)
3435 error("error running textconv command '%s'", pgm);
3438 if (temp.name == temp.tmp_path)
3441 return strbuf_detach(&buf, outsize);