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 return git_diff_basic_config(var, value, cb);
99 int git_diff_basic_config(const char *var, const char *value, void *cb)
101 if (!strcmp(var, "diff.renamelimit")) {
102 diff_rename_limit_default = git_config_int(var, value);
106 switch (userdiff_config(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 return git_color_default_config(var, value, cb);
129 static char *quote_two(const char *one, const char *two)
131 int need_one = quote_c_style(one, NULL, NULL, 1);
132 int need_two = quote_c_style(two, NULL, NULL, 1);
133 struct strbuf res = STRBUF_INIT;
135 if (need_one + need_two) {
136 strbuf_addch(&res, '"');
137 quote_c_style(one, &res, NULL, 1);
138 quote_c_style(two, &res, NULL, 1);
139 strbuf_addch(&res, '"');
141 strbuf_addstr(&res, one);
142 strbuf_addstr(&res, two);
144 return strbuf_detach(&res, NULL);
147 static const char *external_diff(void)
149 static const char *external_diff_cmd = NULL;
150 static int done_preparing = 0;
153 return external_diff_cmd;
154 external_diff_cmd = getenv("GIT_EXTERNAL_DIFF");
155 if (!external_diff_cmd)
156 external_diff_cmd = external_diff_cmd_cfg;
158 return external_diff_cmd;
161 static struct diff_tempfile {
162 const char *name; /* filename external diff should read from */
165 char tmp_path[PATH_MAX];
168 static int count_lines(const char *data, int size)
170 int count, ch, completely_empty = 1, nl_just_seen = 0;
177 completely_empty = 0;
181 completely_empty = 0;
184 if (completely_empty)
187 count++; /* no trailing newline */
191 static void print_line_count(FILE *file, int count)
195 fprintf(file, "0,0");
201 fprintf(file, "1,%d", count);
206 static void copy_file_with_prefix(FILE *file,
207 int prefix, const char *data, int size,
208 const char *set, const char *reset)
210 int ch, nl_just_seen = 1;
225 fprintf(file, "%s\n\\ No newline at end of file\n", reset);
228 static void emit_rewrite_diff(const char *name_a,
230 struct diff_filespec *one,
231 struct diff_filespec *two,
232 const char *textconv_one,
233 const char *textconv_two,
234 struct diff_options *o)
237 int color_diff = DIFF_OPT_TST(o, COLOR_DIFF);
238 const char *name_a_tab, *name_b_tab;
239 const char *metainfo = diff_get_color(color_diff, DIFF_METAINFO);
240 const char *fraginfo = diff_get_color(color_diff, DIFF_FRAGINFO);
241 const char *old = diff_get_color(color_diff, DIFF_FILE_OLD);
242 const char *new = diff_get_color(color_diff, DIFF_FILE_NEW);
243 const char *reset = diff_get_color(color_diff, DIFF_RESET);
244 static struct strbuf a_name = STRBUF_INIT, b_name = STRBUF_INIT;
245 const char *a_prefix, *b_prefix;
246 const char *data_one, *data_two;
247 size_t size_one, size_two;
249 if (diff_mnemonic_prefix && DIFF_OPT_TST(o, REVERSE_DIFF)) {
250 a_prefix = o->b_prefix;
251 b_prefix = o->a_prefix;
253 a_prefix = o->a_prefix;
254 b_prefix = o->b_prefix;
257 name_a += (*name_a == '/');
258 name_b += (*name_b == '/');
259 name_a_tab = strchr(name_a, ' ') ? "\t" : "";
260 name_b_tab = strchr(name_b, ' ') ? "\t" : "";
262 strbuf_reset(&a_name);
263 strbuf_reset(&b_name);
264 quote_two_c_style(&a_name, a_prefix, name_a, 0);
265 quote_two_c_style(&b_name, b_prefix, name_b, 0);
267 diff_populate_filespec(one, 0);
268 diff_populate_filespec(two, 0);
270 data_one = run_textconv(textconv_one, one, &size_one);
272 die("unable to read files to diff");
275 data_one = one->data;
276 size_one = one->size;
279 data_two = run_textconv(textconv_two, two, &size_two);
281 die("unable to read files to diff");
284 data_two = two->data;
285 size_two = two->size;
288 lc_a = count_lines(data_one, size_one);
289 lc_b = count_lines(data_two, size_two);
291 "%s--- %s%s%s\n%s+++ %s%s%s\n%s@@ -",
292 metainfo, a_name.buf, name_a_tab, reset,
293 metainfo, b_name.buf, name_b_tab, reset, fraginfo);
294 print_line_count(o->file, lc_a);
295 fprintf(o->file, " +");
296 print_line_count(o->file, lc_b);
297 fprintf(o->file, " @@%s\n", reset);
299 copy_file_with_prefix(o->file, '-', data_one, size_one, old, reset);
301 copy_file_with_prefix(o->file, '+', data_two, size_two, new, reset);
304 static int fill_mmfile(mmfile_t *mf, struct diff_filespec *one)
306 if (!DIFF_FILE_VALID(one)) {
307 mf->ptr = (char *)""; /* does not matter */
311 else if (diff_populate_filespec(one, 0))
315 mf->size = one->size;
319 struct diff_words_buffer {
322 struct diff_words_orig {
323 const char *begin, *end;
325 int orig_nr, orig_alloc;
328 static void diff_words_append(char *line, unsigned long len,
329 struct diff_words_buffer *buffer)
331 ALLOC_GROW(buffer->text.ptr, buffer->text.size + len, buffer->alloc);
334 memcpy(buffer->text.ptr + buffer->text.size, line, len);
335 buffer->text.size += len;
338 struct diff_words_data {
339 struct diff_words_buffer minus, plus;
340 const char *current_plus;
344 static void fn_out_diff_words_aux(void *priv, char *line, unsigned long len)
346 struct diff_words_data *diff_words = priv;
347 int minus_first, minus_len, plus_first, plus_len;
348 const char *minus_begin, *minus_end, *plus_begin, *plus_end;
350 if (line[0] != '@' || parse_hunk_header(line, len,
351 &minus_first, &minus_len, &plus_first, &plus_len))
354 /* POSIX requires that first be decremented by one if len == 0... */
356 minus_begin = diff_words->minus.orig[minus_first].begin;
358 diff_words->minus.orig[minus_first + minus_len - 1].end;
360 minus_begin = minus_end =
361 diff_words->minus.orig[minus_first].end;
364 plus_begin = diff_words->plus.orig[plus_first].begin;
365 plus_end = diff_words->plus.orig[plus_first + plus_len - 1].end;
367 plus_begin = plus_end = diff_words->plus.orig[plus_first].end;
369 if (diff_words->current_plus != plus_begin)
370 fwrite(diff_words->current_plus,
371 plus_begin - diff_words->current_plus, 1,
373 if (minus_begin != minus_end)
374 color_fwrite_lines(diff_words->file,
375 diff_get_color(1, DIFF_FILE_OLD),
376 minus_end - minus_begin, minus_begin);
377 if (plus_begin != plus_end)
378 color_fwrite_lines(diff_words->file,
379 diff_get_color(1, DIFF_FILE_NEW),
380 plus_end - plus_begin, plus_begin);
382 diff_words->current_plus = plus_end;
386 * This function splits the words in buffer->text, stores the list with
387 * newline separator into out, and saves the offsets of the original words
390 static void diff_words_fill(struct diff_words_buffer *buffer, mmfile_t *out)
395 out->ptr = xmalloc(buffer->text.size);
397 /* fake an empty "0th" word */
398 ALLOC_GROW(buffer->orig, 1, buffer->orig_alloc);
399 buffer->orig[0].begin = buffer->orig[0].end = buffer->text.ptr;
402 for (i = 0; i < buffer->text.size; i++) {
403 if (isspace(buffer->text.ptr[i]))
405 for (j = i + 1; j < buffer->text.size &&
406 !isspace(buffer->text.ptr[j]); j++)
407 ; /* find the end of the word */
409 /* store original boundaries */
410 ALLOC_GROW(buffer->orig, buffer->orig_nr + 1,
412 buffer->orig[buffer->orig_nr].begin = buffer->text.ptr + i;
413 buffer->orig[buffer->orig_nr].end = buffer->text.ptr + j;
417 memcpy(out->ptr + out->size, buffer->text.ptr + i, j - i);
418 out->ptr[out->size + j - i] = '\n';
419 out->size += j - i + 1;
425 /* this executes the word diff on the accumulated buffers */
426 static void diff_words_show(struct diff_words_data *diff_words)
431 mmfile_t minus, plus;
433 /* special case: only removal */
434 if (!diff_words->plus.text.size) {
435 color_fwrite_lines(diff_words->file,
436 diff_get_color(1, DIFF_FILE_OLD),
437 diff_words->minus.text.size, diff_words->minus.text.ptr);
438 diff_words->minus.text.size = 0;
442 diff_words->current_plus = diff_words->plus.text.ptr;
444 memset(&xpp, 0, sizeof(xpp));
445 memset(&xecfg, 0, sizeof(xecfg));
446 diff_words_fill(&diff_words->minus, &minus);
447 diff_words_fill(&diff_words->plus, &plus);
448 xpp.flags = XDF_NEED_MINIMAL;
450 xdi_diff_outf(&minus, &plus, fn_out_diff_words_aux, diff_words,
454 if (diff_words->current_plus != diff_words->plus.text.ptr +
455 diff_words->plus.text.size)
456 fwrite(diff_words->current_plus,
457 diff_words->plus.text.ptr + diff_words->plus.text.size
458 - diff_words->current_plus, 1,
460 diff_words->minus.text.size = diff_words->plus.text.size = 0;
463 typedef unsigned long (*sane_truncate_fn)(char *line, unsigned long len);
465 struct emit_callback {
466 int nparents, color_diff;
468 sane_truncate_fn truncate;
469 const char **label_path;
470 struct diff_words_data *diff_words;
475 static void free_diff_words_data(struct emit_callback *ecbdata)
477 if (ecbdata->diff_words) {
479 if (ecbdata->diff_words->minus.text.size ||
480 ecbdata->diff_words->plus.text.size)
481 diff_words_show(ecbdata->diff_words);
483 free (ecbdata->diff_words->minus.text.ptr);
484 free (ecbdata->diff_words->minus.orig);
485 free (ecbdata->diff_words->plus.text.ptr);
486 free (ecbdata->diff_words->plus.orig);
487 free(ecbdata->diff_words);
488 ecbdata->diff_words = NULL;
492 const char *diff_get_color(int diff_use_color, enum color_diff ix)
495 return diff_colors[ix];
499 static void emit_line(FILE *file, const char *set, const char *reset, const char *line, int len)
501 int has_trailing_newline, has_trailing_carriage_return;
503 has_trailing_newline = (len > 0 && line[len-1] == '\n');
504 if (has_trailing_newline)
506 has_trailing_carriage_return = (len > 0 && line[len-1] == '\r');
507 if (has_trailing_carriage_return)
511 fwrite(line, len, 1, file);
513 if (has_trailing_carriage_return)
515 if (has_trailing_newline)
519 static void emit_add_line(const char *reset, struct emit_callback *ecbdata, const char *line, int len)
521 const char *ws = diff_get_color(ecbdata->color_diff, DIFF_WHITESPACE);
522 const char *set = diff_get_color(ecbdata->color_diff, DIFF_FILE_NEW);
525 emit_line(ecbdata->file, set, reset, line, len);
527 /* Emit just the prefix, then the rest. */
528 emit_line(ecbdata->file, set, reset, line, ecbdata->nparents);
529 ws_check_emit(line + ecbdata->nparents,
530 len - ecbdata->nparents, ecbdata->ws_rule,
531 ecbdata->file, set, reset, ws);
535 static unsigned long sane_truncate_line(struct emit_callback *ecb, char *line, unsigned long len)
542 return ecb->truncate(line, len);
546 (void) utf8_width(&cp, &l);
548 break; /* truncated in the middle? */
553 static void fn_out_consume(void *priv, char *line, unsigned long len)
557 struct emit_callback *ecbdata = priv;
558 const char *meta = diff_get_color(ecbdata->color_diff, DIFF_METAINFO);
559 const char *plain = diff_get_color(ecbdata->color_diff, DIFF_PLAIN);
560 const char *reset = diff_get_color(ecbdata->color_diff, DIFF_RESET);
562 *(ecbdata->found_changesp) = 1;
564 if (ecbdata->label_path[0]) {
565 const char *name_a_tab, *name_b_tab;
567 name_a_tab = strchr(ecbdata->label_path[0], ' ') ? "\t" : "";
568 name_b_tab = strchr(ecbdata->label_path[1], ' ') ? "\t" : "";
570 fprintf(ecbdata->file, "%s--- %s%s%s\n",
571 meta, ecbdata->label_path[0], reset, name_a_tab);
572 fprintf(ecbdata->file, "%s+++ %s%s%s\n",
573 meta, ecbdata->label_path[1], reset, name_b_tab);
574 ecbdata->label_path[0] = ecbdata->label_path[1] = NULL;
577 if (diff_suppress_blank_empty
578 && len == 2 && line[0] == ' ' && line[1] == '\n') {
583 /* This is not really necessary for now because
584 * this codepath only deals with two-way diffs.
586 for (i = 0; i < len && line[i] == '@'; i++)
588 if (2 <= i && i < len && line[i] == ' ') {
589 ecbdata->nparents = i - 1;
590 len = sane_truncate_line(ecbdata, line, len);
591 emit_line(ecbdata->file,
592 diff_get_color(ecbdata->color_diff, DIFF_FRAGINFO),
594 if (line[len-1] != '\n')
595 putc('\n', ecbdata->file);
599 if (len < ecbdata->nparents) {
600 emit_line(ecbdata->file, reset, reset, line, len);
605 if (ecbdata->diff_words && ecbdata->nparents != 1)
606 /* fall back to normal diff */
607 free_diff_words_data(ecbdata);
608 if (ecbdata->diff_words) {
609 if (line[0] == '-') {
610 diff_words_append(line, len,
611 &ecbdata->diff_words->minus);
613 } else if (line[0] == '+') {
614 diff_words_append(line, len,
615 &ecbdata->diff_words->plus);
618 if (ecbdata->diff_words->minus.text.size ||
619 ecbdata->diff_words->plus.text.size)
620 diff_words_show(ecbdata->diff_words);
623 emit_line(ecbdata->file, plain, reset, line, len);
626 for (i = 0; i < ecbdata->nparents && len; i++) {
628 color = DIFF_FILE_OLD;
629 else if (line[i] == '+')
630 color = DIFF_FILE_NEW;
633 if (color != DIFF_FILE_NEW) {
634 emit_line(ecbdata->file,
635 diff_get_color(ecbdata->color_diff, color),
639 emit_add_line(reset, ecbdata, line, len);
642 static char *pprint_rename(const char *a, const char *b)
646 struct strbuf name = STRBUF_INIT;
647 int pfx_length, sfx_length;
648 int len_a = strlen(a);
649 int len_b = strlen(b);
650 int a_midlen, b_midlen;
651 int qlen_a = quote_c_style(a, NULL, NULL, 0);
652 int qlen_b = quote_c_style(b, NULL, NULL, 0);
654 if (qlen_a || qlen_b) {
655 quote_c_style(a, &name, NULL, 0);
656 strbuf_addstr(&name, " => ");
657 quote_c_style(b, &name, NULL, 0);
658 return strbuf_detach(&name, NULL);
661 /* Find common prefix */
663 while (*old && *new && *old == *new) {
665 pfx_length = old - a + 1;
670 /* Find common suffix */
674 while (a <= old && b <= new && *old == *new) {
676 sfx_length = len_a - (old - a);
682 * pfx{mid-a => mid-b}sfx
683 * {pfx-a => pfx-b}sfx
684 * pfx{sfx-a => sfx-b}
687 a_midlen = len_a - pfx_length - sfx_length;
688 b_midlen = len_b - pfx_length - sfx_length;
694 strbuf_grow(&name, pfx_length + a_midlen + b_midlen + sfx_length + 7);
695 if (pfx_length + sfx_length) {
696 strbuf_add(&name, a, pfx_length);
697 strbuf_addch(&name, '{');
699 strbuf_add(&name, a + pfx_length, a_midlen);
700 strbuf_addstr(&name, " => ");
701 strbuf_add(&name, b + pfx_length, b_midlen);
702 if (pfx_length + sfx_length) {
703 strbuf_addch(&name, '}');
704 strbuf_add(&name, a + len_a - sfx_length, sfx_length);
706 return strbuf_detach(&name, NULL);
712 struct diffstat_file {
716 unsigned is_unmerged:1;
717 unsigned is_binary:1;
718 unsigned is_renamed:1;
719 unsigned int added, deleted;
723 static struct diffstat_file *diffstat_add(struct diffstat_t *diffstat,
727 struct diffstat_file *x;
728 x = xcalloc(sizeof (*x), 1);
729 if (diffstat->nr == diffstat->alloc) {
730 diffstat->alloc = alloc_nr(diffstat->alloc);
731 diffstat->files = xrealloc(diffstat->files,
732 diffstat->alloc * sizeof(x));
734 diffstat->files[diffstat->nr++] = x;
736 x->from_name = xstrdup(name_a);
737 x->name = xstrdup(name_b);
742 x->name = xstrdup(name_a);
747 static void diffstat_consume(void *priv, char *line, unsigned long len)
749 struct diffstat_t *diffstat = priv;
750 struct diffstat_file *x = diffstat->files[diffstat->nr - 1];
754 else if (line[0] == '-')
758 const char mime_boundary_leader[] = "------------";
760 static int scale_linear(int it, int width, int max_change)
763 * make sure that at least one '-' is printed if there were deletions,
764 * and likewise for '+'.
768 return ((it - 1) * (width - 1) + max_change - 1) / (max_change - 1);
771 static void show_name(FILE *file,
772 const char *prefix, const char *name, int len,
773 const char *reset, const char *set)
775 fprintf(file, " %s%s%-*s%s |", set, prefix, len, name, reset);
778 static void show_graph(FILE *file, char ch, int cnt, const char *set, const char *reset)
782 fprintf(file, "%s", set);
785 fprintf(file, "%s", reset);
788 static void fill_print_name(struct diffstat_file *file)
792 if (file->print_name)
795 if (!file->is_renamed) {
796 struct strbuf buf = STRBUF_INIT;
797 if (quote_c_style(file->name, &buf, NULL, 0)) {
798 pname = strbuf_detach(&buf, NULL);
801 strbuf_release(&buf);
804 pname = pprint_rename(file->from_name, file->name);
806 file->print_name = pname;
809 static void show_stats(struct diffstat_t* data, struct diff_options *options)
811 int i, len, add, del, total, adds = 0, dels = 0;
812 int max_change = 0, max_len = 0;
813 int total_files = data->nr;
814 int width, name_width;
815 const char *reset, *set, *add_c, *del_c;
820 width = options->stat_width ? options->stat_width : 80;
821 name_width = options->stat_name_width ? options->stat_name_width : 50;
823 /* Sanity: give at least 5 columns to the graph,
824 * but leave at least 10 columns for the name.
830 else if (width < name_width + 15)
831 name_width = width - 15;
833 /* Find the longest filename and max number of changes */
834 reset = diff_get_color_opt(options, DIFF_RESET);
835 set = diff_get_color_opt(options, DIFF_PLAIN);
836 add_c = diff_get_color_opt(options, DIFF_FILE_NEW);
837 del_c = diff_get_color_opt(options, DIFF_FILE_OLD);
839 for (i = 0; i < data->nr; i++) {
840 struct diffstat_file *file = data->files[i];
841 int change = file->added + file->deleted;
842 fill_print_name(file);
843 len = strlen(file->print_name);
847 if (file->is_binary || file->is_unmerged)
849 if (max_change < change)
853 /* Compute the width of the graph part;
854 * 10 is for one blank at the beginning of the line plus
855 * " | count " between the name and the graph.
857 * From here on, name_width is the width of the name area,
858 * and width is the width of the graph area.
860 name_width = (name_width < max_len) ? name_width : max_len;
861 if (width < (name_width + 10) + max_change)
862 width = width - (name_width + 10);
866 for (i = 0; i < data->nr; i++) {
867 const char *prefix = "";
868 char *name = data->files[i]->print_name;
869 int added = data->files[i]->added;
870 int deleted = data->files[i]->deleted;
874 * "scale" the filename
877 name_len = strlen(name);
878 if (name_width < name_len) {
882 name += name_len - len;
883 slash = strchr(name, '/');
888 if (data->files[i]->is_binary) {
889 show_name(options->file, prefix, name, len, reset, set);
890 fprintf(options->file, " Bin ");
891 fprintf(options->file, "%s%d%s", del_c, deleted, reset);
892 fprintf(options->file, " -> ");
893 fprintf(options->file, "%s%d%s", add_c, added, reset);
894 fprintf(options->file, " bytes");
895 fprintf(options->file, "\n");
898 else if (data->files[i]->is_unmerged) {
899 show_name(options->file, prefix, name, len, reset, set);
900 fprintf(options->file, " Unmerged\n");
903 else if (!data->files[i]->is_renamed &&
904 (added + deleted == 0)) {
910 * scale the add/delete
918 if (width <= max_change) {
919 add = scale_linear(add, width, max_change);
920 del = scale_linear(del, width, max_change);
923 show_name(options->file, prefix, name, len, reset, set);
924 fprintf(options->file, "%5d%s", added + deleted,
925 added + deleted ? " " : "");
926 show_graph(options->file, '+', add, add_c, reset);
927 show_graph(options->file, '-', del, del_c, reset);
928 fprintf(options->file, "\n");
930 fprintf(options->file,
931 "%s %d files changed, %d insertions(+), %d deletions(-)%s\n",
932 set, total_files, adds, dels, reset);
935 static void show_shortstats(struct diffstat_t* data, struct diff_options *options)
937 int i, adds = 0, dels = 0, total_files = data->nr;
942 for (i = 0; i < data->nr; i++) {
943 if (!data->files[i]->is_binary &&
944 !data->files[i]->is_unmerged) {
945 int added = data->files[i]->added;
946 int deleted= data->files[i]->deleted;
947 if (!data->files[i]->is_renamed &&
948 (added + deleted == 0)) {
956 fprintf(options->file, " %d files changed, %d insertions(+), %d deletions(-)\n",
957 total_files, adds, dels);
960 static void show_numstat(struct diffstat_t* data, struct diff_options *options)
967 for (i = 0; i < data->nr; i++) {
968 struct diffstat_file *file = data->files[i];
971 fprintf(options->file, "-\t-\t");
973 fprintf(options->file,
974 "%d\t%d\t", file->added, file->deleted);
975 if (options->line_termination) {
976 fill_print_name(file);
977 if (!file->is_renamed)
978 write_name_quoted(file->name, options->file,
979 options->line_termination);
981 fputs(file->print_name, options->file);
982 putc(options->line_termination, options->file);
985 if (file->is_renamed) {
986 putc('\0', options->file);
987 write_name_quoted(file->from_name, options->file, '\0');
989 write_name_quoted(file->name, options->file, '\0');
994 struct dirstat_file {
996 unsigned long changed;
1000 struct dirstat_file *files;
1001 int alloc, nr, percent, cumulative;
1004 static long gather_dirstat(FILE *file, struct dirstat_dir *dir, unsigned long changed, const char *base, int baselen)
1006 unsigned long this_dir = 0;
1007 unsigned int sources = 0;
1010 struct dirstat_file *f = dir->files;
1011 int namelen = strlen(f->name);
1015 if (namelen < baselen)
1017 if (memcmp(f->name, base, baselen))
1019 slash = strchr(f->name + baselen, '/');
1021 int newbaselen = slash + 1 - f->name;
1022 this = gather_dirstat(file, dir, changed, f->name, newbaselen);
1034 * We don't report dirstat's for
1036 * - or cases where everything came from a single directory
1037 * under this directory (sources == 1).
1039 if (baselen && sources != 1) {
1040 int permille = this_dir * 1000 / changed;
1042 int percent = permille / 10;
1043 if (percent >= dir->percent) {
1044 fprintf(file, "%4d.%01d%% %.*s\n", percent, permille % 10, baselen, base);
1045 if (!dir->cumulative)
1053 static int dirstat_compare(const void *_a, const void *_b)
1055 const struct dirstat_file *a = _a;
1056 const struct dirstat_file *b = _b;
1057 return strcmp(a->name, b->name);
1060 static void show_dirstat(struct diff_options *options)
1063 unsigned long changed;
1064 struct dirstat_dir dir;
1065 struct diff_queue_struct *q = &diff_queued_diff;
1070 dir.percent = options->dirstat_percent;
1071 dir.cumulative = DIFF_OPT_TST(options, DIRSTAT_CUMULATIVE);
1074 for (i = 0; i < q->nr; i++) {
1075 struct diff_filepair *p = q->queue[i];
1077 unsigned long copied, added, damage;
1079 name = p->one->path ? p->one->path : p->two->path;
1081 if (DIFF_FILE_VALID(p->one) && DIFF_FILE_VALID(p->two)) {
1082 diff_populate_filespec(p->one, 0);
1083 diff_populate_filespec(p->two, 0);
1084 diffcore_count_changes(p->one, p->two, NULL, NULL, 0,
1086 diff_free_filespec_data(p->one);
1087 diff_free_filespec_data(p->two);
1088 } else if (DIFF_FILE_VALID(p->one)) {
1089 diff_populate_filespec(p->one, 1);
1091 diff_free_filespec_data(p->one);
1092 } else if (DIFF_FILE_VALID(p->two)) {
1093 diff_populate_filespec(p->two, 1);
1095 added = p->two->size;
1096 diff_free_filespec_data(p->two);
1101 * Original minus copied is the removed material,
1102 * added is the new material. They are both damages
1103 * made to the preimage. In --dirstat-by-file mode, count
1104 * damaged files, not damaged lines. This is done by
1105 * counting only a single damaged line per file.
1107 damage = (p->one->size - copied) + added;
1108 if (DIFF_OPT_TST(options, DIRSTAT_BY_FILE) && damage > 0)
1111 ALLOC_GROW(dir.files, dir.nr + 1, dir.alloc);
1112 dir.files[dir.nr].name = name;
1113 dir.files[dir.nr].changed = damage;
1118 /* This can happen even with many files, if everything was renames */
1122 /* Show all directories with more than x% of the changes */
1123 qsort(dir.files, dir.nr, sizeof(dir.files[0]), dirstat_compare);
1124 gather_dirstat(options->file, &dir, changed, "", 0);
1127 static void free_diffstat_info(struct diffstat_t *diffstat)
1130 for (i = 0; i < diffstat->nr; i++) {
1131 struct diffstat_file *f = diffstat->files[i];
1132 if (f->name != f->print_name)
1133 free(f->print_name);
1138 free(diffstat->files);
1141 struct checkdiff_t {
1142 const char *filename;
1144 struct diff_options *o;
1147 int trailing_blanks_start;
1150 static int is_conflict_marker(const char *line, unsigned long len)
1157 firstchar = line[0];
1158 switch (firstchar) {
1159 case '=': case '>': case '<':
1164 for (cnt = 1; cnt < 7; cnt++)
1165 if (line[cnt] != firstchar)
1167 /* line[0] thru line[6] are same as firstchar */
1168 if (firstchar == '=') {
1169 /* divider between ours and theirs? */
1170 if (len != 8 || line[7] != '\n')
1172 } else if (len < 8 || !isspace(line[7])) {
1173 /* not divider before ours nor after theirs */
1179 static void checkdiff_consume(void *priv, char *line, unsigned long len)
1181 struct checkdiff_t *data = priv;
1182 int color_diff = DIFF_OPT_TST(data->o, COLOR_DIFF);
1183 const char *ws = diff_get_color(color_diff, DIFF_WHITESPACE);
1184 const char *reset = diff_get_color(color_diff, DIFF_RESET);
1185 const char *set = diff_get_color(color_diff, DIFF_FILE_NEW);
1188 if (line[0] == '+') {
1191 if (!ws_blank_line(line + 1, len - 1, data->ws_rule))
1192 data->trailing_blanks_start = 0;
1193 else if (!data->trailing_blanks_start)
1194 data->trailing_blanks_start = data->lineno;
1195 if (is_conflict_marker(line + 1, len - 1)) {
1197 fprintf(data->o->file,
1198 "%s:%d: leftover conflict marker\n",
1199 data->filename, data->lineno);
1201 bad = ws_check(line + 1, len - 1, data->ws_rule);
1204 data->status |= bad;
1205 err = whitespace_error_string(bad);
1206 fprintf(data->o->file, "%s:%d: %s.\n",
1207 data->filename, data->lineno, err);
1209 emit_line(data->o->file, set, reset, line, 1);
1210 ws_check_emit(line + 1, len - 1, data->ws_rule,
1211 data->o->file, set, reset, ws);
1212 } else if (line[0] == ' ') {
1214 data->trailing_blanks_start = 0;
1215 } else if (line[0] == '@') {
1216 char *plus = strchr(line, '+');
1218 data->lineno = strtol(plus, NULL, 10) - 1;
1220 die("invalid diff");
1221 data->trailing_blanks_start = 0;
1225 static unsigned char *deflate_it(char *data,
1227 unsigned long *result_size)
1230 unsigned char *deflated;
1233 memset(&stream, 0, sizeof(stream));
1234 deflateInit(&stream, zlib_compression_level);
1235 bound = deflateBound(&stream, size);
1236 deflated = xmalloc(bound);
1237 stream.next_out = deflated;
1238 stream.avail_out = bound;
1240 stream.next_in = (unsigned char *)data;
1241 stream.avail_in = size;
1242 while (deflate(&stream, Z_FINISH) == Z_OK)
1244 deflateEnd(&stream);
1245 *result_size = stream.total_out;
1249 static void emit_binary_diff_body(FILE *file, mmfile_t *one, mmfile_t *two)
1255 unsigned long orig_size;
1256 unsigned long delta_size;
1257 unsigned long deflate_size;
1258 unsigned long data_size;
1260 /* We could do deflated delta, or we could do just deflated two,
1261 * whichever is smaller.
1264 deflated = deflate_it(two->ptr, two->size, &deflate_size);
1265 if (one->size && two->size) {
1266 delta = diff_delta(one->ptr, one->size,
1267 two->ptr, two->size,
1268 &delta_size, deflate_size);
1270 void *to_free = delta;
1271 orig_size = delta_size;
1272 delta = deflate_it(delta, delta_size, &delta_size);
1277 if (delta && delta_size < deflate_size) {
1278 fprintf(file, "delta %lu\n", orig_size);
1281 data_size = delta_size;
1284 fprintf(file, "literal %lu\n", two->size);
1287 data_size = deflate_size;
1290 /* emit data encoded in base85 */
1293 int bytes = (52 < data_size) ? 52 : data_size;
1297 line[0] = bytes + 'A' - 1;
1299 line[0] = bytes - 26 + 'a' - 1;
1300 encode_85(line + 1, cp, bytes);
1301 cp = (char *) cp + bytes;
1305 fprintf(file, "\n");
1309 static void emit_binary_diff(FILE *file, mmfile_t *one, mmfile_t *two)
1311 fprintf(file, "GIT binary patch\n");
1312 emit_binary_diff_body(file, one, two);
1313 emit_binary_diff_body(file, two, one);
1316 static void diff_filespec_load_driver(struct diff_filespec *one)
1319 one->driver = userdiff_find_by_path(one->path);
1321 one->driver = userdiff_find_by_name("default");
1324 int diff_filespec_is_binary(struct diff_filespec *one)
1326 if (one->is_binary == -1) {
1327 diff_filespec_load_driver(one);
1328 if (one->driver->binary != -1)
1329 one->is_binary = one->driver->binary;
1331 if (!one->data && DIFF_FILE_VALID(one))
1332 diff_populate_filespec(one, 0);
1334 one->is_binary = buffer_is_binary(one->data,
1336 if (one->is_binary == -1)
1340 return one->is_binary;
1343 static const struct userdiff_funcname *diff_funcname_pattern(struct diff_filespec *one)
1345 diff_filespec_load_driver(one);
1346 return one->driver->funcname.pattern ? &one->driver->funcname : NULL;
1349 void diff_set_mnemonic_prefix(struct diff_options *options, const char *a, const char *b)
1351 if (!options->a_prefix)
1352 options->a_prefix = a;
1353 if (!options->b_prefix)
1354 options->b_prefix = b;
1357 static const char *get_textconv(struct diff_filespec *one)
1359 if (!DIFF_FILE_VALID(one))
1361 if (!S_ISREG(one->mode))
1363 diff_filespec_load_driver(one);
1364 return one->driver->textconv;
1367 static void builtin_diff(const char *name_a,
1369 struct diff_filespec *one,
1370 struct diff_filespec *two,
1371 const char *xfrm_msg,
1372 struct diff_options *o,
1373 int complete_rewrite)
1377 char *a_one, *b_two;
1378 const char *set = diff_get_color_opt(o, DIFF_METAINFO);
1379 const char *reset = diff_get_color_opt(o, DIFF_RESET);
1380 const char *a_prefix, *b_prefix;
1381 const char *textconv_one = NULL, *textconv_two = NULL;
1383 if (DIFF_OPT_TST(o, ALLOW_TEXTCONV)) {
1384 textconv_one = get_textconv(one);
1385 textconv_two = get_textconv(two);
1388 diff_set_mnemonic_prefix(o, "a/", "b/");
1389 if (DIFF_OPT_TST(o, REVERSE_DIFF)) {
1390 a_prefix = o->b_prefix;
1391 b_prefix = o->a_prefix;
1393 a_prefix = o->a_prefix;
1394 b_prefix = o->b_prefix;
1397 /* Never use a non-valid filename anywhere if at all possible */
1398 name_a = DIFF_FILE_VALID(one) ? name_a : name_b;
1399 name_b = DIFF_FILE_VALID(two) ? name_b : name_a;
1401 a_one = quote_two(a_prefix, name_a + (*name_a == '/'));
1402 b_two = quote_two(b_prefix, name_b + (*name_b == '/'));
1403 lbl[0] = DIFF_FILE_VALID(one) ? a_one : "/dev/null";
1404 lbl[1] = DIFF_FILE_VALID(two) ? b_two : "/dev/null";
1405 fprintf(o->file, "%sdiff --git %s %s%s\n", set, a_one, b_two, reset);
1406 if (lbl[0][0] == '/') {
1408 fprintf(o->file, "%snew file mode %06o%s\n", set, two->mode, reset);
1409 if (xfrm_msg && xfrm_msg[0])
1410 fprintf(o->file, "%s%s%s\n", set, xfrm_msg, reset);
1412 else if (lbl[1][0] == '/') {
1413 fprintf(o->file, "%sdeleted file mode %06o%s\n", set, one->mode, reset);
1414 if (xfrm_msg && xfrm_msg[0])
1415 fprintf(o->file, "%s%s%s\n", set, xfrm_msg, reset);
1418 if (one->mode != two->mode) {
1419 fprintf(o->file, "%sold mode %06o%s\n", set, one->mode, reset);
1420 fprintf(o->file, "%snew mode %06o%s\n", set, two->mode, reset);
1422 if (xfrm_msg && xfrm_msg[0])
1423 fprintf(o->file, "%s%s%s\n", set, xfrm_msg, reset);
1425 * we do not run diff between different kind
1428 if ((one->mode ^ two->mode) & S_IFMT)
1429 goto free_ab_and_return;
1430 if (complete_rewrite &&
1431 (textconv_one || !diff_filespec_is_binary(one)) &&
1432 (textconv_two || !diff_filespec_is_binary(two))) {
1433 emit_rewrite_diff(name_a, name_b, one, two,
1434 textconv_one, textconv_two, o);
1435 o->found_changes = 1;
1436 goto free_ab_and_return;
1440 if (fill_mmfile(&mf1, one) < 0 || fill_mmfile(&mf2, two) < 0)
1441 die("unable to read files to diff");
1443 if (!DIFF_OPT_TST(o, TEXT) &&
1444 ( (diff_filespec_is_binary(one) && !textconv_one) ||
1445 (diff_filespec_is_binary(two) && !textconv_two) )) {
1446 /* Quite common confusing case */
1447 if (mf1.size == mf2.size &&
1448 !memcmp(mf1.ptr, mf2.ptr, mf1.size))
1449 goto free_ab_and_return;
1450 if (DIFF_OPT_TST(o, BINARY))
1451 emit_binary_diff(o->file, &mf1, &mf2);
1453 fprintf(o->file, "Binary files %s and %s differ\n",
1455 o->found_changes = 1;
1458 /* Crazy xdl interfaces.. */
1459 const char *diffopts = getenv("GIT_DIFF_OPTS");
1463 struct emit_callback ecbdata;
1464 const struct userdiff_funcname *pe;
1468 mf1.ptr = run_textconv(textconv_one, one, &size);
1470 die("unable to read files to diff");
1475 mf2.ptr = run_textconv(textconv_two, two, &size);
1477 die("unable to read files to diff");
1481 pe = diff_funcname_pattern(one);
1483 pe = diff_funcname_pattern(two);
1485 memset(&xpp, 0, sizeof(xpp));
1486 memset(&xecfg, 0, sizeof(xecfg));
1487 memset(&ecbdata, 0, sizeof(ecbdata));
1488 ecbdata.label_path = lbl;
1489 ecbdata.color_diff = DIFF_OPT_TST(o, COLOR_DIFF);
1490 ecbdata.found_changesp = &o->found_changes;
1491 ecbdata.ws_rule = whitespace_rule(name_b ? name_b : name_a);
1492 ecbdata.file = o->file;
1493 xpp.flags = XDF_NEED_MINIMAL | o->xdl_opts;
1494 xecfg.ctxlen = o->context;
1495 xecfg.interhunkctxlen = o->interhunkcontext;
1496 xecfg.flags = XDL_EMIT_FUNCNAMES;
1498 xdiff_set_find_func(&xecfg, pe->pattern, pe->cflags);
1501 else if (!prefixcmp(diffopts, "--unified="))
1502 xecfg.ctxlen = strtoul(diffopts + 10, NULL, 10);
1503 else if (!prefixcmp(diffopts, "-u"))
1504 xecfg.ctxlen = strtoul(diffopts + 2, NULL, 10);
1505 if (DIFF_OPT_TST(o, COLOR_DIFF_WORDS)) {
1506 ecbdata.diff_words =
1507 xcalloc(1, sizeof(struct diff_words_data));
1508 ecbdata.diff_words->file = o->file;
1510 xdi_diff_outf(&mf1, &mf2, fn_out_consume, &ecbdata,
1511 &xpp, &xecfg, &ecb);
1512 if (DIFF_OPT_TST(o, COLOR_DIFF_WORDS))
1513 free_diff_words_data(&ecbdata);
1521 diff_free_filespec_data(one);
1522 diff_free_filespec_data(two);
1528 static void builtin_diffstat(const char *name_a, const char *name_b,
1529 struct diff_filespec *one,
1530 struct diff_filespec *two,
1531 struct diffstat_t *diffstat,
1532 struct diff_options *o,
1533 int complete_rewrite)
1536 struct diffstat_file *data;
1538 data = diffstat_add(diffstat, name_a, name_b);
1541 data->is_unmerged = 1;
1544 if (complete_rewrite) {
1545 diff_populate_filespec(one, 0);
1546 diff_populate_filespec(two, 0);
1547 data->deleted = count_lines(one->data, one->size);
1548 data->added = count_lines(two->data, two->size);
1549 goto free_and_return;
1551 if (fill_mmfile(&mf1, one) < 0 || fill_mmfile(&mf2, two) < 0)
1552 die("unable to read files to diff");
1554 if (diff_filespec_is_binary(one) || diff_filespec_is_binary(two)) {
1555 data->is_binary = 1;
1556 data->added = mf2.size;
1557 data->deleted = mf1.size;
1559 /* Crazy xdl interfaces.. */
1564 memset(&xpp, 0, sizeof(xpp));
1565 memset(&xecfg, 0, sizeof(xecfg));
1566 xpp.flags = XDF_NEED_MINIMAL | o->xdl_opts;
1567 xdi_diff_outf(&mf1, &mf2, diffstat_consume, diffstat,
1568 &xpp, &xecfg, &ecb);
1572 diff_free_filespec_data(one);
1573 diff_free_filespec_data(two);
1576 static void builtin_checkdiff(const char *name_a, const char *name_b,
1577 const char *attr_path,
1578 struct diff_filespec *one,
1579 struct diff_filespec *two,
1580 struct diff_options *o)
1583 struct checkdiff_t data;
1588 memset(&data, 0, sizeof(data));
1589 data.filename = name_b ? name_b : name_a;
1592 data.ws_rule = whitespace_rule(attr_path);
1594 if (fill_mmfile(&mf1, one) < 0 || fill_mmfile(&mf2, two) < 0)
1595 die("unable to read files to diff");
1598 * All the other codepaths check both sides, but not checking
1599 * the "old" side here is deliberate. We are checking the newly
1600 * introduced changes, and as long as the "new" side is text, we
1601 * can and should check what it introduces.
1603 if (diff_filespec_is_binary(two))
1604 goto free_and_return;
1606 /* Crazy xdl interfaces.. */
1611 memset(&xpp, 0, sizeof(xpp));
1612 memset(&xecfg, 0, sizeof(xecfg));
1613 xecfg.ctxlen = 1; /* at least one context line */
1614 xpp.flags = XDF_NEED_MINIMAL;
1615 xdi_diff_outf(&mf1, &mf2, checkdiff_consume, &data,
1616 &xpp, &xecfg, &ecb);
1618 if ((data.ws_rule & WS_TRAILING_SPACE) &&
1619 data.trailing_blanks_start) {
1620 fprintf(o->file, "%s:%d: ends with blank lines.\n",
1621 data.filename, data.trailing_blanks_start);
1622 data.status = 1; /* report errors */
1626 diff_free_filespec_data(one);
1627 diff_free_filespec_data(two);
1629 DIFF_OPT_SET(o, CHECK_FAILED);
1632 struct diff_filespec *alloc_filespec(const char *path)
1634 int namelen = strlen(path);
1635 struct diff_filespec *spec = xmalloc(sizeof(*spec) + namelen + 1);
1637 memset(spec, 0, sizeof(*spec));
1638 spec->path = (char *)(spec + 1);
1639 memcpy(spec->path, path, namelen+1);
1641 spec->is_binary = -1;
1645 void free_filespec(struct diff_filespec *spec)
1647 if (!--spec->count) {
1648 diff_free_filespec_data(spec);
1653 void fill_filespec(struct diff_filespec *spec, const unsigned char *sha1,
1654 unsigned short mode)
1657 spec->mode = canon_mode(mode);
1658 hashcpy(spec->sha1, sha1);
1659 spec->sha1_valid = !is_null_sha1(sha1);
1664 * Given a name and sha1 pair, if the index tells us the file in
1665 * the work tree has that object contents, return true, so that
1666 * prepare_temp_file() does not have to inflate and extract.
1668 static int reuse_worktree_file(const char *name, const unsigned char *sha1, int want_file)
1670 struct cache_entry *ce;
1674 /* We do not read the cache ourselves here, because the
1675 * benchmark with my previous version that always reads cache
1676 * shows that it makes things worse for diff-tree comparing
1677 * two linux-2.6 kernel trees in an already checked out work
1678 * tree. This is because most diff-tree comparisons deal with
1679 * only a small number of files, while reading the cache is
1680 * expensive for a large project, and its cost outweighs the
1681 * savings we get by not inflating the object to a temporary
1682 * file. Practically, this code only helps when we are used
1683 * by diff-cache --cached, which does read the cache before
1689 /* We want to avoid the working directory if our caller
1690 * doesn't need the data in a normal file, this system
1691 * is rather slow with its stat/open/mmap/close syscalls,
1692 * and the object is contained in a pack file. The pack
1693 * is probably already open and will be faster to obtain
1694 * the data through than the working directory. Loose
1695 * objects however would tend to be slower as they need
1696 * to be individually opened and inflated.
1698 if (!FAST_WORKING_DIRECTORY && !want_file && has_sha1_pack(sha1, NULL))
1702 pos = cache_name_pos(name, len);
1705 ce = active_cache[pos];
1708 * This is not the sha1 we are looking for, or
1709 * unreusable because it is not a regular file.
1711 if (hashcmp(sha1, ce->sha1) || !S_ISREG(ce->ce_mode))
1715 * If ce matches the file in the work tree, we can reuse it.
1717 if (ce_uptodate(ce) ||
1718 (!lstat(name, &st) && !ce_match_stat(ce, &st, 0)))
1724 static int populate_from_stdin(struct diff_filespec *s)
1726 struct strbuf buf = STRBUF_INIT;
1729 if (strbuf_read(&buf, 0, 0) < 0)
1730 return error("error while reading from stdin %s",
1733 s->should_munmap = 0;
1734 s->data = strbuf_detach(&buf, &size);
1740 static int diff_populate_gitlink(struct diff_filespec *s, int size_only)
1743 char *data = xmalloc(100);
1744 len = snprintf(data, 100,
1745 "Subproject commit %s\n", sha1_to_hex(s->sha1));
1757 * While doing rename detection and pickaxe operation, we may need to
1758 * grab the data for the blob (or file) for our own in-core comparison.
1759 * diff_filespec has data and size fields for this purpose.
1761 int diff_populate_filespec(struct diff_filespec *s, int size_only)
1764 if (!DIFF_FILE_VALID(s))
1765 die("internal error: asking to populate invalid file.");
1766 if (S_ISDIR(s->mode))
1772 if (size_only && 0 < s->size)
1775 if (S_ISGITLINK(s->mode))
1776 return diff_populate_gitlink(s, size_only);
1778 if (!s->sha1_valid ||
1779 reuse_worktree_file(s->path, s->sha1, 0)) {
1780 struct strbuf buf = STRBUF_INIT;
1784 if (!strcmp(s->path, "-"))
1785 return populate_from_stdin(s);
1787 if (lstat(s->path, &st) < 0) {
1788 if (errno == ENOENT) {
1792 s->data = (char *)"";
1797 s->size = xsize_t(st.st_size);
1800 if (S_ISLNK(st.st_mode)) {
1801 struct strbuf sb = STRBUF_INIT;
1803 if (strbuf_readlink(&sb, s->path, s->size))
1806 s->data = strbuf_detach(&sb, NULL);
1812 fd = open(s->path, O_RDONLY);
1815 s->data = xmmap(NULL, s->size, PROT_READ, MAP_PRIVATE, fd, 0);
1817 s->should_munmap = 1;
1820 * Convert from working tree format to canonical git format
1822 if (convert_to_git(s->path, s->data, s->size, &buf, safe_crlf)) {
1824 munmap(s->data, s->size);
1825 s->should_munmap = 0;
1826 s->data = strbuf_detach(&buf, &size);
1832 enum object_type type;
1834 type = sha1_object_info(s->sha1, &s->size);
1836 s->data = read_sha1_file(s->sha1, &type, &s->size);
1843 void diff_free_filespec_blob(struct diff_filespec *s)
1847 else if (s->should_munmap)
1848 munmap(s->data, s->size);
1850 if (s->should_free || s->should_munmap) {
1851 s->should_free = s->should_munmap = 0;
1856 void diff_free_filespec_data(struct diff_filespec *s)
1858 diff_free_filespec_blob(s);
1863 static void prep_temp_blob(struct diff_tempfile *temp,
1866 const unsigned char *sha1,
1871 fd = git_mkstemp(temp->tmp_path, PATH_MAX, ".diff_XXXXXX");
1873 die("unable to create temp-file: %s", strerror(errno));
1874 if (write_in_full(fd, blob, size) != size)
1875 die("unable to write temp-file");
1877 temp->name = temp->tmp_path;
1878 strcpy(temp->hex, sha1_to_hex(sha1));
1880 sprintf(temp->mode, "%06o", mode);
1883 static void prepare_temp_file(const char *name,
1884 struct diff_tempfile *temp,
1885 struct diff_filespec *one)
1887 if (!DIFF_FILE_VALID(one)) {
1889 /* A '-' entry produces this for file-2, and
1890 * a '+' entry produces this for file-1.
1892 temp->name = "/dev/null";
1893 strcpy(temp->hex, ".");
1894 strcpy(temp->mode, ".");
1898 if (!one->sha1_valid ||
1899 reuse_worktree_file(name, one->sha1, 1)) {
1901 if (lstat(name, &st) < 0) {
1902 if (errno == ENOENT)
1903 goto not_a_valid_file;
1904 die("stat(%s): %s", name, strerror(errno));
1906 if (S_ISLNK(st.st_mode)) {
1908 char buf[PATH_MAX + 1]; /* ought to be SYMLINK_MAX */
1909 ret = readlink(name, buf, sizeof(buf));
1911 die("readlink(%s)", name);
1912 if (ret == sizeof(buf))
1913 die("symlink too long: %s", name);
1914 prep_temp_blob(temp, buf, ret,
1916 one->sha1 : null_sha1),
1918 one->mode : S_IFLNK));
1921 /* we can borrow from the file in the work tree */
1923 if (!one->sha1_valid)
1924 strcpy(temp->hex, sha1_to_hex(null_sha1));
1926 strcpy(temp->hex, sha1_to_hex(one->sha1));
1927 /* Even though we may sometimes borrow the
1928 * contents from the work tree, we always want
1929 * one->mode. mode is trustworthy even when
1930 * !(one->sha1_valid), as long as
1931 * DIFF_FILE_VALID(one).
1933 sprintf(temp->mode, "%06o", one->mode);
1938 if (diff_populate_filespec(one, 0))
1939 die("cannot read data blob for %s", one->path);
1940 prep_temp_blob(temp, one->data, one->size,
1941 one->sha1, one->mode);
1945 static void remove_tempfile(void)
1949 for (i = 0; i < 2; i++)
1950 if (diff_temp[i].name == diff_temp[i].tmp_path) {
1951 unlink(diff_temp[i].name);
1952 diff_temp[i].name = NULL;
1956 static void remove_tempfile_on_signal(int signo)
1959 signal(SIGINT, SIG_DFL);
1963 /* An external diff command takes:
1965 * diff-cmd name infile1 infile1-sha1 infile1-mode \
1966 * infile2 infile2-sha1 infile2-mode [ rename-to ]
1969 static void run_external_diff(const char *pgm,
1972 struct diff_filespec *one,
1973 struct diff_filespec *two,
1974 const char *xfrm_msg,
1975 int complete_rewrite)
1977 const char *spawn_arg[10];
1978 struct diff_tempfile *temp = diff_temp;
1980 static int atexit_asked = 0;
1981 const char *othername;
1982 const char **arg = &spawn_arg[0];
1984 othername = (other? other : name);
1986 prepare_temp_file(name, &temp[0], one);
1987 prepare_temp_file(othername, &temp[1], two);
1988 if (! atexit_asked &&
1989 (temp[0].name == temp[0].tmp_path ||
1990 temp[1].name == temp[1].tmp_path)) {
1992 atexit(remove_tempfile);
1994 signal(SIGINT, remove_tempfile_on_signal);
2000 *arg++ = temp[0].name;
2001 *arg++ = temp[0].hex;
2002 *arg++ = temp[0].mode;
2003 *arg++ = temp[1].name;
2004 *arg++ = temp[1].hex;
2005 *arg++ = temp[1].mode;
2016 retval = run_command_v_opt(spawn_arg, 0);
2019 fprintf(stderr, "external diff died, stopping at %s.\n", name);
2024 static void run_diff_cmd(const char *pgm,
2027 const char *attr_path,
2028 struct diff_filespec *one,
2029 struct diff_filespec *two,
2030 const char *xfrm_msg,
2031 struct diff_options *o,
2032 int complete_rewrite)
2034 if (!DIFF_OPT_TST(o, ALLOW_EXTERNAL))
2037 struct userdiff_driver *drv = userdiff_find_by_path(attr_path);
2038 if (drv && drv->external)
2039 pgm = drv->external;
2043 run_external_diff(pgm, name, other, one, two, xfrm_msg,
2048 builtin_diff(name, other ? other : name,
2049 one, two, xfrm_msg, o, complete_rewrite);
2051 fprintf(o->file, "* Unmerged path %s\n", name);
2054 static void diff_fill_sha1_info(struct diff_filespec *one)
2056 if (DIFF_FILE_VALID(one)) {
2057 if (!one->sha1_valid) {
2059 if (!strcmp(one->path, "-")) {
2060 hashcpy(one->sha1, null_sha1);
2063 if (lstat(one->path, &st) < 0)
2064 die("stat %s", one->path);
2065 if (index_path(one->sha1, one->path, &st, 0))
2066 die("cannot hash %s", one->path);
2073 static int similarity_index(struct diff_filepair *p)
2075 return p->score * 100 / MAX_SCORE;
2078 static void strip_prefix(int prefix_length, const char **namep, const char **otherp)
2080 /* Strip the prefix but do not molest /dev/null and absolute paths */
2081 if (*namep && **namep != '/')
2082 *namep += prefix_length;
2083 if (*otherp && **otherp != '/')
2084 *otherp += prefix_length;
2087 static void run_diff(struct diff_filepair *p, struct diff_options *o)
2089 const char *pgm = external_diff();
2092 struct diff_filespec *one = p->one;
2093 struct diff_filespec *two = p->two;
2096 const char *attr_path;
2097 int complete_rewrite = 0;
2099 name = p->one->path;
2100 other = (strcmp(name, p->two->path) ? p->two->path : NULL);
2102 if (o->prefix_length)
2103 strip_prefix(o->prefix_length, &name, &other);
2105 if (DIFF_PAIR_UNMERGED(p)) {
2106 run_diff_cmd(pgm, name, NULL, attr_path,
2107 NULL, NULL, NULL, o, 0);
2111 diff_fill_sha1_info(one);
2112 diff_fill_sha1_info(two);
2114 strbuf_init(&msg, PATH_MAX * 2 + 300);
2115 switch (p->status) {
2116 case DIFF_STATUS_COPIED:
2117 strbuf_addf(&msg, "similarity index %d%%", similarity_index(p));
2118 strbuf_addstr(&msg, "\ncopy from ");
2119 quote_c_style(name, &msg, NULL, 0);
2120 strbuf_addstr(&msg, "\ncopy to ");
2121 quote_c_style(other, &msg, NULL, 0);
2122 strbuf_addch(&msg, '\n');
2124 case DIFF_STATUS_RENAMED:
2125 strbuf_addf(&msg, "similarity index %d%%", similarity_index(p));
2126 strbuf_addstr(&msg, "\nrename from ");
2127 quote_c_style(name, &msg, NULL, 0);
2128 strbuf_addstr(&msg, "\nrename to ");
2129 quote_c_style(other, &msg, NULL, 0);
2130 strbuf_addch(&msg, '\n');
2132 case DIFF_STATUS_MODIFIED:
2134 strbuf_addf(&msg, "dissimilarity index %d%%\n",
2135 similarity_index(p));
2136 complete_rewrite = 1;
2145 if (hashcmp(one->sha1, two->sha1)) {
2146 int abbrev = DIFF_OPT_TST(o, FULL_INDEX) ? 40 : DEFAULT_ABBREV;
2148 if (DIFF_OPT_TST(o, BINARY)) {
2150 if ((!fill_mmfile(&mf, one) && diff_filespec_is_binary(one)) ||
2151 (!fill_mmfile(&mf, two) && diff_filespec_is_binary(two)))
2154 strbuf_addf(&msg, "index %.*s..%.*s",
2155 abbrev, sha1_to_hex(one->sha1),
2156 abbrev, sha1_to_hex(two->sha1));
2157 if (one->mode == two->mode)
2158 strbuf_addf(&msg, " %06o", one->mode);
2159 strbuf_addch(&msg, '\n');
2163 strbuf_setlen(&msg, msg.len - 1);
2164 xfrm_msg = msg.len ? msg.buf : NULL;
2167 DIFF_FILE_VALID(one) && DIFF_FILE_VALID(two) &&
2168 (S_IFMT & one->mode) != (S_IFMT & two->mode)) {
2169 /* a filepair that changes between file and symlink
2170 * needs to be split into deletion and creation.
2172 struct diff_filespec *null = alloc_filespec(two->path);
2173 run_diff_cmd(NULL, name, other, attr_path,
2174 one, null, xfrm_msg, o, 0);
2176 null = alloc_filespec(one->path);
2177 run_diff_cmd(NULL, name, other, attr_path,
2178 null, two, xfrm_msg, o, 0);
2182 run_diff_cmd(pgm, name, other, attr_path,
2183 one, two, xfrm_msg, o, complete_rewrite);
2185 strbuf_release(&msg);
2188 static void run_diffstat(struct diff_filepair *p, struct diff_options *o,
2189 struct diffstat_t *diffstat)
2193 int complete_rewrite = 0;
2195 if (DIFF_PAIR_UNMERGED(p)) {
2197 builtin_diffstat(p->one->path, NULL, NULL, NULL, diffstat, o, 0);
2201 name = p->one->path;
2202 other = (strcmp(name, p->two->path) ? p->two->path : NULL);
2204 if (o->prefix_length)
2205 strip_prefix(o->prefix_length, &name, &other);
2207 diff_fill_sha1_info(p->one);
2208 diff_fill_sha1_info(p->two);
2210 if (p->status == DIFF_STATUS_MODIFIED && p->score)
2211 complete_rewrite = 1;
2212 builtin_diffstat(name, other, p->one, p->two, diffstat, o, complete_rewrite);
2215 static void run_checkdiff(struct diff_filepair *p, struct diff_options *o)
2219 const char *attr_path;
2221 if (DIFF_PAIR_UNMERGED(p)) {
2226 name = p->one->path;
2227 other = (strcmp(name, p->two->path) ? p->two->path : NULL);
2228 attr_path = other ? other : name;
2230 if (o->prefix_length)
2231 strip_prefix(o->prefix_length, &name, &other);
2233 diff_fill_sha1_info(p->one);
2234 diff_fill_sha1_info(p->two);
2236 builtin_checkdiff(name, other, attr_path, p->one, p->two, o);
2239 void diff_setup(struct diff_options *options)
2241 memset(options, 0, sizeof(*options));
2243 options->file = stdout;
2245 options->line_termination = '\n';
2246 options->break_opt = -1;
2247 options->rename_limit = -1;
2248 options->dirstat_percent = 3;
2249 DIFF_OPT_CLR(options, DIRSTAT_CUMULATIVE);
2250 options->context = 3;
2252 options->change = diff_change;
2253 options->add_remove = diff_addremove;
2254 if (diff_use_color_default > 0)
2255 DIFF_OPT_SET(options, COLOR_DIFF);
2257 DIFF_OPT_CLR(options, COLOR_DIFF);
2258 options->detect_rename = diff_detect_rename_default;
2260 if (!diff_mnemonic_prefix) {
2261 options->a_prefix = "a/";
2262 options->b_prefix = "b/";
2266 int diff_setup_done(struct diff_options *options)
2270 if (options->output_format & DIFF_FORMAT_NAME)
2272 if (options->output_format & DIFF_FORMAT_NAME_STATUS)
2274 if (options->output_format & DIFF_FORMAT_CHECKDIFF)
2276 if (options->output_format & DIFF_FORMAT_NO_OUTPUT)
2279 die("--name-only, --name-status, --check and -s are mutually exclusive");
2281 if (DIFF_OPT_TST(options, FIND_COPIES_HARDER))
2282 options->detect_rename = DIFF_DETECT_COPY;
2284 if (!DIFF_OPT_TST(options, RELATIVE_NAME))
2285 options->prefix = NULL;
2286 if (options->prefix)
2287 options->prefix_length = strlen(options->prefix);
2289 options->prefix_length = 0;
2291 if (options->output_format & (DIFF_FORMAT_NAME |
2292 DIFF_FORMAT_NAME_STATUS |
2293 DIFF_FORMAT_CHECKDIFF |
2294 DIFF_FORMAT_NO_OUTPUT))
2295 options->output_format &= ~(DIFF_FORMAT_RAW |
2296 DIFF_FORMAT_NUMSTAT |
2297 DIFF_FORMAT_DIFFSTAT |
2298 DIFF_FORMAT_SHORTSTAT |
2299 DIFF_FORMAT_DIRSTAT |
2300 DIFF_FORMAT_SUMMARY |
2304 * These cases always need recursive; we do not drop caller-supplied
2305 * recursive bits for other formats here.
2307 if (options->output_format & (DIFF_FORMAT_PATCH |
2308 DIFF_FORMAT_NUMSTAT |
2309 DIFF_FORMAT_DIFFSTAT |
2310 DIFF_FORMAT_SHORTSTAT |
2311 DIFF_FORMAT_DIRSTAT |
2312 DIFF_FORMAT_SUMMARY |
2313 DIFF_FORMAT_CHECKDIFF))
2314 DIFF_OPT_SET(options, RECURSIVE);
2316 * Also pickaxe would not work very well if you do not say recursive
2318 if (options->pickaxe)
2319 DIFF_OPT_SET(options, RECURSIVE);
2321 if (options->detect_rename && options->rename_limit < 0)
2322 options->rename_limit = diff_rename_limit_default;
2323 if (options->setup & DIFF_SETUP_USE_CACHE) {
2325 /* read-cache does not die even when it fails
2326 * so it is safe for us to do this here. Also
2327 * it does not smudge active_cache or active_nr
2328 * when it fails, so we do not have to worry about
2329 * cleaning it up ourselves either.
2333 if (options->abbrev <= 0 || 40 < options->abbrev)
2334 options->abbrev = 40; /* full */
2337 * It does not make sense to show the first hit we happened
2338 * to have found. It does not make sense not to return with
2339 * exit code in such a case either.
2341 if (DIFF_OPT_TST(options, QUIET)) {
2342 options->output_format = DIFF_FORMAT_NO_OUTPUT;
2343 DIFF_OPT_SET(options, EXIT_WITH_STATUS);
2349 static int opt_arg(const char *arg, int arg_short, const char *arg_long, int *val)
2359 if (c == arg_short) {
2363 if (val && isdigit(c)) {
2365 int n = strtoul(arg, &end, 10);
2376 eq = strchr(arg, '=');
2381 if (!len || strncmp(arg, arg_long, len))
2386 if (!isdigit(*++eq))
2388 n = strtoul(eq, &end, 10);
2396 static int diff_scoreopt_parse(const char *opt);
2398 int diff_opt_parse(struct diff_options *options, const char **av, int ac)
2400 const char *arg = av[0];
2402 /* Output format options */
2403 if (!strcmp(arg, "-p") || !strcmp(arg, "-u"))
2404 options->output_format |= DIFF_FORMAT_PATCH;
2405 else if (opt_arg(arg, 'U', "unified", &options->context))
2406 options->output_format |= DIFF_FORMAT_PATCH;
2407 else if (!strcmp(arg, "--raw"))
2408 options->output_format |= DIFF_FORMAT_RAW;
2409 else if (!strcmp(arg, "--patch-with-raw"))
2410 options->output_format |= DIFF_FORMAT_PATCH | DIFF_FORMAT_RAW;
2411 else if (!strcmp(arg, "--numstat"))
2412 options->output_format |= DIFF_FORMAT_NUMSTAT;
2413 else if (!strcmp(arg, "--shortstat"))
2414 options->output_format |= DIFF_FORMAT_SHORTSTAT;
2415 else if (opt_arg(arg, 'X', "dirstat", &options->dirstat_percent))
2416 options->output_format |= DIFF_FORMAT_DIRSTAT;
2417 else if (!strcmp(arg, "--cumulative")) {
2418 options->output_format |= DIFF_FORMAT_DIRSTAT;
2419 DIFF_OPT_SET(options, DIRSTAT_CUMULATIVE);
2420 } else if (opt_arg(arg, 0, "dirstat-by-file",
2421 &options->dirstat_percent)) {
2422 options->output_format |= DIFF_FORMAT_DIRSTAT;
2423 DIFF_OPT_SET(options, DIRSTAT_BY_FILE);
2425 else if (!strcmp(arg, "--check"))
2426 options->output_format |= DIFF_FORMAT_CHECKDIFF;
2427 else if (!strcmp(arg, "--summary"))
2428 options->output_format |= DIFF_FORMAT_SUMMARY;
2429 else if (!strcmp(arg, "--patch-with-stat"))
2430 options->output_format |= DIFF_FORMAT_PATCH | DIFF_FORMAT_DIFFSTAT;
2431 else if (!strcmp(arg, "--name-only"))
2432 options->output_format |= DIFF_FORMAT_NAME;
2433 else if (!strcmp(arg, "--name-status"))
2434 options->output_format |= DIFF_FORMAT_NAME_STATUS;
2435 else if (!strcmp(arg, "-s"))
2436 options->output_format |= DIFF_FORMAT_NO_OUTPUT;
2437 else if (!prefixcmp(arg, "--stat")) {
2439 int width = options->stat_width;
2440 int name_width = options->stat_name_width;
2446 if (!prefixcmp(arg, "-width="))
2447 width = strtoul(arg + 7, &end, 10);
2448 else if (!prefixcmp(arg, "-name-width="))
2449 name_width = strtoul(arg + 12, &end, 10);
2452 width = strtoul(arg+1, &end, 10);
2454 name_width = strtoul(end+1, &end, 10);
2457 /* Important! This checks all the error cases! */
2460 options->output_format |= DIFF_FORMAT_DIFFSTAT;
2461 options->stat_name_width = name_width;
2462 options->stat_width = width;
2465 /* renames options */
2466 else if (!prefixcmp(arg, "-B")) {
2467 if ((options->break_opt = diff_scoreopt_parse(arg)) == -1)
2470 else if (!prefixcmp(arg, "-M")) {
2471 if ((options->rename_score = diff_scoreopt_parse(arg)) == -1)
2473 options->detect_rename = DIFF_DETECT_RENAME;
2475 else if (!prefixcmp(arg, "-C")) {
2476 if (options->detect_rename == DIFF_DETECT_COPY)
2477 DIFF_OPT_SET(options, FIND_COPIES_HARDER);
2478 if ((options->rename_score = diff_scoreopt_parse(arg)) == -1)
2480 options->detect_rename = DIFF_DETECT_COPY;
2482 else if (!strcmp(arg, "--no-renames"))
2483 options->detect_rename = 0;
2484 else if (!strcmp(arg, "--relative"))
2485 DIFF_OPT_SET(options, RELATIVE_NAME);
2486 else if (!prefixcmp(arg, "--relative=")) {
2487 DIFF_OPT_SET(options, RELATIVE_NAME);
2488 options->prefix = arg + 11;
2492 else if (!strcmp(arg, "-w") || !strcmp(arg, "--ignore-all-space"))
2493 options->xdl_opts |= XDF_IGNORE_WHITESPACE;
2494 else if (!strcmp(arg, "-b") || !strcmp(arg, "--ignore-space-change"))
2495 options->xdl_opts |= XDF_IGNORE_WHITESPACE_CHANGE;
2496 else if (!strcmp(arg, "--ignore-space-at-eol"))
2497 options->xdl_opts |= XDF_IGNORE_WHITESPACE_AT_EOL;
2500 else if (!strcmp(arg, "--binary")) {
2501 options->output_format |= DIFF_FORMAT_PATCH;
2502 DIFF_OPT_SET(options, BINARY);
2504 else if (!strcmp(arg, "--full-index"))
2505 DIFF_OPT_SET(options, FULL_INDEX);
2506 else if (!strcmp(arg, "-a") || !strcmp(arg, "--text"))
2507 DIFF_OPT_SET(options, TEXT);
2508 else if (!strcmp(arg, "-R"))
2509 DIFF_OPT_SET(options, REVERSE_DIFF);
2510 else if (!strcmp(arg, "--find-copies-harder"))
2511 DIFF_OPT_SET(options, FIND_COPIES_HARDER);
2512 else if (!strcmp(arg, "--follow"))
2513 DIFF_OPT_SET(options, FOLLOW_RENAMES);
2514 else if (!strcmp(arg, "--color"))
2515 DIFF_OPT_SET(options, COLOR_DIFF);
2516 else if (!strcmp(arg, "--no-color"))
2517 DIFF_OPT_CLR(options, COLOR_DIFF);
2518 else if (!strcmp(arg, "--color-words"))
2519 options->flags |= DIFF_OPT_COLOR_DIFF | DIFF_OPT_COLOR_DIFF_WORDS;
2520 else if (!strcmp(arg, "--exit-code"))
2521 DIFF_OPT_SET(options, EXIT_WITH_STATUS);
2522 else if (!strcmp(arg, "--quiet"))
2523 DIFF_OPT_SET(options, QUIET);
2524 else if (!strcmp(arg, "--ext-diff"))
2525 DIFF_OPT_SET(options, ALLOW_EXTERNAL);
2526 else if (!strcmp(arg, "--no-ext-diff"))
2527 DIFF_OPT_CLR(options, ALLOW_EXTERNAL);
2528 else if (!strcmp(arg, "--textconv"))
2529 DIFF_OPT_SET(options, ALLOW_TEXTCONV);
2530 else if (!strcmp(arg, "--no-textconv"))
2531 DIFF_OPT_CLR(options, ALLOW_TEXTCONV);
2532 else if (!strcmp(arg, "--ignore-submodules"))
2533 DIFF_OPT_SET(options, IGNORE_SUBMODULES);
2536 else if (!strcmp(arg, "-z"))
2537 options->line_termination = 0;
2538 else if (!prefixcmp(arg, "-l"))
2539 options->rename_limit = strtoul(arg+2, NULL, 10);
2540 else if (!prefixcmp(arg, "-S"))
2541 options->pickaxe = arg + 2;
2542 else if (!strcmp(arg, "--pickaxe-all"))
2543 options->pickaxe_opts = DIFF_PICKAXE_ALL;
2544 else if (!strcmp(arg, "--pickaxe-regex"))
2545 options->pickaxe_opts = DIFF_PICKAXE_REGEX;
2546 else if (!prefixcmp(arg, "-O"))
2547 options->orderfile = arg + 2;
2548 else if (!prefixcmp(arg, "--diff-filter="))
2549 options->filter = arg + 14;
2550 else if (!strcmp(arg, "--abbrev"))
2551 options->abbrev = DEFAULT_ABBREV;
2552 else if (!prefixcmp(arg, "--abbrev=")) {
2553 options->abbrev = strtoul(arg + 9, NULL, 10);
2554 if (options->abbrev < MINIMUM_ABBREV)
2555 options->abbrev = MINIMUM_ABBREV;
2556 else if (40 < options->abbrev)
2557 options->abbrev = 40;
2559 else if (!prefixcmp(arg, "--src-prefix="))
2560 options->a_prefix = arg + 13;
2561 else if (!prefixcmp(arg, "--dst-prefix="))
2562 options->b_prefix = arg + 13;
2563 else if (!strcmp(arg, "--no-prefix"))
2564 options->a_prefix = options->b_prefix = "";
2565 else if (opt_arg(arg, '\0', "inter-hunk-context",
2566 &options->interhunkcontext))
2568 else if (!prefixcmp(arg, "--output=")) {
2569 options->file = fopen(arg + strlen("--output="), "w");
2570 options->close_file = 1;
2576 static int parse_num(const char **cp_p)
2578 unsigned long num, scale;
2580 const char *cp = *cp_p;
2587 if ( !dot && ch == '.' ) {
2590 } else if ( ch == '%' ) {
2591 scale = dot ? scale*100 : 100;
2592 cp++; /* % is always at the end */
2594 } else if ( ch >= '0' && ch <= '9' ) {
2595 if ( scale < 100000 ) {
2597 num = (num*10) + (ch-'0');
2606 /* user says num divided by scale and we say internally that
2607 * is MAX_SCORE * num / scale.
2609 return (int)((num >= scale) ? MAX_SCORE : (MAX_SCORE * num / scale));
2612 static int diff_scoreopt_parse(const char *opt)
2614 int opt1, opt2, cmd;
2619 if (cmd != 'M' && cmd != 'C' && cmd != 'B')
2620 return -1; /* that is not a -M, -C nor -B option */
2622 opt1 = parse_num(&opt);
2628 else if (*opt != '/')
2629 return -1; /* we expect -B80/99 or -B80 */
2632 opt2 = parse_num(&opt);
2637 return opt1 | (opt2 << 16);
2640 struct diff_queue_struct diff_queued_diff;
2642 void diff_q(struct diff_queue_struct *queue, struct diff_filepair *dp)
2644 if (queue->alloc <= queue->nr) {
2645 queue->alloc = alloc_nr(queue->alloc);
2646 queue->queue = xrealloc(queue->queue,
2647 sizeof(dp) * queue->alloc);
2649 queue->queue[queue->nr++] = dp;
2652 struct diff_filepair *diff_queue(struct diff_queue_struct *queue,
2653 struct diff_filespec *one,
2654 struct diff_filespec *two)
2656 struct diff_filepair *dp = xcalloc(1, sizeof(*dp));
2664 void diff_free_filepair(struct diff_filepair *p)
2666 free_filespec(p->one);
2667 free_filespec(p->two);
2671 /* This is different from find_unique_abbrev() in that
2672 * it stuffs the result with dots for alignment.
2674 const char *diff_unique_abbrev(const unsigned char *sha1, int len)
2679 return sha1_to_hex(sha1);
2681 abbrev = find_unique_abbrev(sha1, len);
2682 abblen = strlen(abbrev);
2684 static char hex[41];
2685 if (len < abblen && abblen <= len + 2)
2686 sprintf(hex, "%s%.*s", abbrev, len+3-abblen, "..");
2688 sprintf(hex, "%s...", abbrev);
2691 return sha1_to_hex(sha1);
2694 static void diff_flush_raw(struct diff_filepair *p, struct diff_options *opt)
2696 int line_termination = opt->line_termination;
2697 int inter_name_termination = line_termination ? '\t' : '\0';
2699 if (!(opt->output_format & DIFF_FORMAT_NAME_STATUS)) {
2700 fprintf(opt->file, ":%06o %06o %s ", p->one->mode, p->two->mode,
2701 diff_unique_abbrev(p->one->sha1, opt->abbrev));
2702 fprintf(opt->file, "%s ", diff_unique_abbrev(p->two->sha1, opt->abbrev));
2705 fprintf(opt->file, "%c%03d%c", p->status, similarity_index(p),
2706 inter_name_termination);
2708 fprintf(opt->file, "%c%c", p->status, inter_name_termination);
2711 if (p->status == DIFF_STATUS_COPIED ||
2712 p->status == DIFF_STATUS_RENAMED) {
2713 const char *name_a, *name_b;
2714 name_a = p->one->path;
2715 name_b = p->two->path;
2716 strip_prefix(opt->prefix_length, &name_a, &name_b);
2717 write_name_quoted(name_a, opt->file, inter_name_termination);
2718 write_name_quoted(name_b, opt->file, line_termination);
2720 const char *name_a, *name_b;
2721 name_a = p->one->mode ? p->one->path : p->two->path;
2723 strip_prefix(opt->prefix_length, &name_a, &name_b);
2724 write_name_quoted(name_a, opt->file, line_termination);
2728 int diff_unmodified_pair(struct diff_filepair *p)
2730 /* This function is written stricter than necessary to support
2731 * the currently implemented transformers, but the idea is to
2732 * let transformers to produce diff_filepairs any way they want,
2733 * and filter and clean them up here before producing the output.
2735 struct diff_filespec *one = p->one, *two = p->two;
2737 if (DIFF_PAIR_UNMERGED(p))
2738 return 0; /* unmerged is interesting */
2740 /* deletion, addition, mode or type change
2741 * and rename are all interesting.
2743 if (DIFF_FILE_VALID(one) != DIFF_FILE_VALID(two) ||
2744 DIFF_PAIR_MODE_CHANGED(p) ||
2745 strcmp(one->path, two->path))
2748 /* both are valid and point at the same path. that is, we are
2749 * dealing with a change.
2751 if (one->sha1_valid && two->sha1_valid &&
2752 !hashcmp(one->sha1, two->sha1))
2753 return 1; /* no change */
2754 if (!one->sha1_valid && !two->sha1_valid)
2755 return 1; /* both look at the same file on the filesystem. */
2759 static void diff_flush_patch(struct diff_filepair *p, struct diff_options *o)
2761 if (diff_unmodified_pair(p))
2764 if ((DIFF_FILE_VALID(p->one) && S_ISDIR(p->one->mode)) ||
2765 (DIFF_FILE_VALID(p->two) && S_ISDIR(p->two->mode)))
2766 return; /* no tree diffs in patch format */
2771 static void diff_flush_stat(struct diff_filepair *p, struct diff_options *o,
2772 struct diffstat_t *diffstat)
2774 if (diff_unmodified_pair(p))
2777 if ((DIFF_FILE_VALID(p->one) && S_ISDIR(p->one->mode)) ||
2778 (DIFF_FILE_VALID(p->two) && S_ISDIR(p->two->mode)))
2779 return; /* no tree diffs in patch format */
2781 run_diffstat(p, o, diffstat);
2784 static void diff_flush_checkdiff(struct diff_filepair *p,
2785 struct diff_options *o)
2787 if (diff_unmodified_pair(p))
2790 if ((DIFF_FILE_VALID(p->one) && S_ISDIR(p->one->mode)) ||
2791 (DIFF_FILE_VALID(p->two) && S_ISDIR(p->two->mode)))
2792 return; /* no tree diffs in patch format */
2794 run_checkdiff(p, o);
2797 int diff_queue_is_empty(void)
2799 struct diff_queue_struct *q = &diff_queued_diff;
2801 for (i = 0; i < q->nr; i++)
2802 if (!diff_unmodified_pair(q->queue[i]))
2808 void diff_debug_filespec(struct diff_filespec *s, int x, const char *one)
2810 fprintf(stderr, "queue[%d] %s (%s) %s %06o %s\n",
2813 DIFF_FILE_VALID(s) ? "valid" : "invalid",
2815 s->sha1_valid ? sha1_to_hex(s->sha1) : "");
2816 fprintf(stderr, "queue[%d] %s size %lu flags %d\n",
2818 s->size, s->xfrm_flags);
2821 void diff_debug_filepair(const struct diff_filepair *p, int i)
2823 diff_debug_filespec(p->one, i, "one");
2824 diff_debug_filespec(p->two, i, "two");
2825 fprintf(stderr, "score %d, status %c rename_used %d broken %d\n",
2826 p->score, p->status ? p->status : '?',
2827 p->one->rename_used, p->broken_pair);
2830 void diff_debug_queue(const char *msg, struct diff_queue_struct *q)
2834 fprintf(stderr, "%s\n", msg);
2835 fprintf(stderr, "q->nr = %d\n", q->nr);
2836 for (i = 0; i < q->nr; i++) {
2837 struct diff_filepair *p = q->queue[i];
2838 diff_debug_filepair(p, i);
2843 static void diff_resolve_rename_copy(void)
2846 struct diff_filepair *p;
2847 struct diff_queue_struct *q = &diff_queued_diff;
2849 diff_debug_queue("resolve-rename-copy", q);
2851 for (i = 0; i < q->nr; i++) {
2853 p->status = 0; /* undecided */
2854 if (DIFF_PAIR_UNMERGED(p))
2855 p->status = DIFF_STATUS_UNMERGED;
2856 else if (!DIFF_FILE_VALID(p->one))
2857 p->status = DIFF_STATUS_ADDED;
2858 else if (!DIFF_FILE_VALID(p->two))
2859 p->status = DIFF_STATUS_DELETED;
2860 else if (DIFF_PAIR_TYPE_CHANGED(p))
2861 p->status = DIFF_STATUS_TYPE_CHANGED;
2863 /* from this point on, we are dealing with a pair
2864 * whose both sides are valid and of the same type, i.e.
2865 * either in-place edit or rename/copy edit.
2867 else if (DIFF_PAIR_RENAME(p)) {
2869 * A rename might have re-connected a broken
2870 * pair up, causing the pathnames to be the
2871 * same again. If so, that's not a rename at
2872 * all, just a modification..
2874 * Otherwise, see if this source was used for
2875 * multiple renames, in which case we decrement
2876 * the count, and call it a copy.
2878 if (!strcmp(p->one->path, p->two->path))
2879 p->status = DIFF_STATUS_MODIFIED;
2880 else if (--p->one->rename_used > 0)
2881 p->status = DIFF_STATUS_COPIED;
2883 p->status = DIFF_STATUS_RENAMED;
2885 else if (hashcmp(p->one->sha1, p->two->sha1) ||
2886 p->one->mode != p->two->mode ||
2887 is_null_sha1(p->one->sha1))
2888 p->status = DIFF_STATUS_MODIFIED;
2890 /* This is a "no-change" entry and should not
2891 * happen anymore, but prepare for broken callers.
2893 error("feeding unmodified %s to diffcore",
2895 p->status = DIFF_STATUS_UNKNOWN;
2898 diff_debug_queue("resolve-rename-copy done", q);
2901 static int check_pair_status(struct diff_filepair *p)
2903 switch (p->status) {
2904 case DIFF_STATUS_UNKNOWN:
2907 die("internal error in diff-resolve-rename-copy");
2913 static void flush_one_pair(struct diff_filepair *p, struct diff_options *opt)
2915 int fmt = opt->output_format;
2917 if (fmt & DIFF_FORMAT_CHECKDIFF)
2918 diff_flush_checkdiff(p, opt);
2919 else if (fmt & (DIFF_FORMAT_RAW | DIFF_FORMAT_NAME_STATUS))
2920 diff_flush_raw(p, opt);
2921 else if (fmt & DIFF_FORMAT_NAME) {
2922 const char *name_a, *name_b;
2923 name_a = p->two->path;
2925 strip_prefix(opt->prefix_length, &name_a, &name_b);
2926 write_name_quoted(name_a, opt->file, opt->line_termination);
2930 static void show_file_mode_name(FILE *file, const char *newdelete, struct diff_filespec *fs)
2933 fprintf(file, " %s mode %06o ", newdelete, fs->mode);
2935 fprintf(file, " %s ", newdelete);
2936 write_name_quoted(fs->path, file, '\n');
2940 static void show_mode_change(FILE *file, struct diff_filepair *p, int show_name)
2942 if (p->one->mode && p->two->mode && p->one->mode != p->two->mode) {
2943 fprintf(file, " mode change %06o => %06o%c", p->one->mode, p->two->mode,
2944 show_name ? ' ' : '\n');
2946 write_name_quoted(p->two->path, file, '\n');
2951 static void show_rename_copy(FILE *file, const char *renamecopy, struct diff_filepair *p)
2953 char *names = pprint_rename(p->one->path, p->two->path);
2955 fprintf(file, " %s %s (%d%%)\n", renamecopy, names, similarity_index(p));
2957 show_mode_change(file, p, 0);
2960 static void diff_summary(FILE *file, struct diff_filepair *p)
2963 case DIFF_STATUS_DELETED:
2964 show_file_mode_name(file, "delete", p->one);
2966 case DIFF_STATUS_ADDED:
2967 show_file_mode_name(file, "create", p->two);
2969 case DIFF_STATUS_COPIED:
2970 show_rename_copy(file, "copy", p);
2972 case DIFF_STATUS_RENAMED:
2973 show_rename_copy(file, "rename", p);
2977 fputs(" rewrite ", file);
2978 write_name_quoted(p->two->path, file, ' ');
2979 fprintf(file, "(%d%%)\n", similarity_index(p));
2981 show_mode_change(file, p, !p->score);
2991 static int remove_space(char *line, int len)
2997 for (i = 0; i < len; i++)
2998 if (!isspace((c = line[i])))
3004 static void patch_id_consume(void *priv, char *line, unsigned long len)
3006 struct patch_id_t *data = priv;
3009 /* Ignore line numbers when computing the SHA1 of the patch */
3010 if (!prefixcmp(line, "@@ -"))
3013 new_len = remove_space(line, len);
3015 git_SHA1_Update(data->ctx, line, new_len);
3016 data->patchlen += new_len;
3019 /* returns 0 upon success, and writes result into sha1 */
3020 static int diff_get_patch_id(struct diff_options *options, unsigned char *sha1)
3022 struct diff_queue_struct *q = &diff_queued_diff;
3025 struct patch_id_t data;
3026 char buffer[PATH_MAX * 4 + 20];
3028 git_SHA1_Init(&ctx);
3029 memset(&data, 0, sizeof(struct patch_id_t));
3032 for (i = 0; i < q->nr; i++) {
3037 struct diff_filepair *p = q->queue[i];
3040 memset(&xpp, 0, sizeof(xpp));
3041 memset(&xecfg, 0, sizeof(xecfg));
3043 return error("internal diff status error");
3044 if (p->status == DIFF_STATUS_UNKNOWN)
3046 if (diff_unmodified_pair(p))
3048 if ((DIFF_FILE_VALID(p->one) && S_ISDIR(p->one->mode)) ||
3049 (DIFF_FILE_VALID(p->two) && S_ISDIR(p->two->mode)))
3051 if (DIFF_PAIR_UNMERGED(p))
3054 diff_fill_sha1_info(p->one);
3055 diff_fill_sha1_info(p->two);
3056 if (fill_mmfile(&mf1, p->one) < 0 ||
3057 fill_mmfile(&mf2, p->two) < 0)
3058 return error("unable to read files to diff");
3060 len1 = remove_space(p->one->path, strlen(p->one->path));
3061 len2 = remove_space(p->two->path, strlen(p->two->path));
3062 if (p->one->mode == 0)
3063 len1 = snprintf(buffer, sizeof(buffer),
3064 "diff--gita/%.*sb/%.*s"
3071 len2, p->two->path);
3072 else if (p->two->mode == 0)
3073 len1 = snprintf(buffer, sizeof(buffer),
3074 "diff--gita/%.*sb/%.*s"
3075 "deletedfilemode%06o"
3081 len1, p->one->path);
3083 len1 = snprintf(buffer, sizeof(buffer),
3084 "diff--gita/%.*sb/%.*s"
3090 len2, p->two->path);
3091 git_SHA1_Update(&ctx, buffer, len1);
3093 xpp.flags = XDF_NEED_MINIMAL;
3095 xecfg.flags = XDL_EMIT_FUNCNAMES;
3096 xdi_diff_outf(&mf1, &mf2, patch_id_consume, &data,
3097 &xpp, &xecfg, &ecb);
3100 git_SHA1_Final(sha1, &ctx);
3104 int diff_flush_patch_id(struct diff_options *options, unsigned char *sha1)
3106 struct diff_queue_struct *q = &diff_queued_diff;
3108 int result = diff_get_patch_id(options, sha1);
3110 for (i = 0; i < q->nr; i++)
3111 diff_free_filepair(q->queue[i]);
3115 q->nr = q->alloc = 0;
3120 static int is_summary_empty(const struct diff_queue_struct *q)
3124 for (i = 0; i < q->nr; i++) {
3125 const struct diff_filepair *p = q->queue[i];
3127 switch (p->status) {
3128 case DIFF_STATUS_DELETED:
3129 case DIFF_STATUS_ADDED:
3130 case DIFF_STATUS_COPIED:
3131 case DIFF_STATUS_RENAMED:
3136 if (p->one->mode && p->two->mode &&
3137 p->one->mode != p->two->mode)
3145 void diff_flush(struct diff_options *options)
3147 struct diff_queue_struct *q = &diff_queued_diff;
3148 int i, output_format = options->output_format;
3152 * Order: raw, stat, summary, patch
3153 * or: name/name-status/checkdiff (other bits clear)
3158 if (output_format & (DIFF_FORMAT_RAW |
3160 DIFF_FORMAT_NAME_STATUS |
3161 DIFF_FORMAT_CHECKDIFF)) {
3162 for (i = 0; i < q->nr; i++) {
3163 struct diff_filepair *p = q->queue[i];
3164 if (check_pair_status(p))
3165 flush_one_pair(p, options);
3170 if (output_format & (DIFF_FORMAT_DIFFSTAT|DIFF_FORMAT_SHORTSTAT|DIFF_FORMAT_NUMSTAT)) {
3171 struct diffstat_t diffstat;
3173 memset(&diffstat, 0, sizeof(struct diffstat_t));
3174 for (i = 0; i < q->nr; i++) {
3175 struct diff_filepair *p = q->queue[i];
3176 if (check_pair_status(p))
3177 diff_flush_stat(p, options, &diffstat);
3179 if (output_format & DIFF_FORMAT_NUMSTAT)
3180 show_numstat(&diffstat, options);
3181 if (output_format & DIFF_FORMAT_DIFFSTAT)
3182 show_stats(&diffstat, options);
3183 if (output_format & DIFF_FORMAT_SHORTSTAT)
3184 show_shortstats(&diffstat, options);
3185 free_diffstat_info(&diffstat);
3188 if (output_format & DIFF_FORMAT_DIRSTAT)
3189 show_dirstat(options);
3191 if (output_format & DIFF_FORMAT_SUMMARY && !is_summary_empty(q)) {
3192 for (i = 0; i < q->nr; i++)
3193 diff_summary(options->file, q->queue[i]);
3197 if (output_format & DIFF_FORMAT_PATCH) {
3199 putc(options->line_termination, options->file);
3200 if (options->stat_sep) {
3201 /* attach patch instead of inline */
3202 fputs(options->stat_sep, options->file);
3206 for (i = 0; i < q->nr; i++) {
3207 struct diff_filepair *p = q->queue[i];
3208 if (check_pair_status(p))
3209 diff_flush_patch(p, options);
3213 if (output_format & DIFF_FORMAT_CALLBACK)
3214 options->format_callback(q, options, options->format_callback_data);
3216 for (i = 0; i < q->nr; i++)
3217 diff_free_filepair(q->queue[i]);
3221 q->nr = q->alloc = 0;
3222 if (options->close_file)
3223 fclose(options->file);
3226 static void diffcore_apply_filter(const char *filter)
3229 struct diff_queue_struct *q = &diff_queued_diff;
3230 struct diff_queue_struct outq;
3232 outq.nr = outq.alloc = 0;
3237 if (strchr(filter, DIFF_STATUS_FILTER_AON)) {
3239 for (i = found = 0; !found && i < q->nr; i++) {
3240 struct diff_filepair *p = q->queue[i];
3241 if (((p->status == DIFF_STATUS_MODIFIED) &&
3243 strchr(filter, DIFF_STATUS_FILTER_BROKEN)) ||
3245 strchr(filter, DIFF_STATUS_MODIFIED)))) ||
3246 ((p->status != DIFF_STATUS_MODIFIED) &&
3247 strchr(filter, p->status)))
3253 /* otherwise we will clear the whole queue
3254 * by copying the empty outq at the end of this
3255 * function, but first clear the current entries
3258 for (i = 0; i < q->nr; i++)
3259 diff_free_filepair(q->queue[i]);
3262 /* Only the matching ones */
3263 for (i = 0; i < q->nr; i++) {
3264 struct diff_filepair *p = q->queue[i];
3266 if (((p->status == DIFF_STATUS_MODIFIED) &&
3268 strchr(filter, DIFF_STATUS_FILTER_BROKEN)) ||
3270 strchr(filter, DIFF_STATUS_MODIFIED)))) ||
3271 ((p->status != DIFF_STATUS_MODIFIED) &&
3272 strchr(filter, p->status)))
3275 diff_free_filepair(p);
3282 /* Check whether two filespecs with the same mode and size are identical */
3283 static int diff_filespec_is_identical(struct diff_filespec *one,
3284 struct diff_filespec *two)
3286 if (S_ISGITLINK(one->mode))
3288 if (diff_populate_filespec(one, 0))
3290 if (diff_populate_filespec(two, 0))
3292 return !memcmp(one->data, two->data, one->size);
3295 static void diffcore_skip_stat_unmatch(struct diff_options *diffopt)
3298 struct diff_queue_struct *q = &diff_queued_diff;
3299 struct diff_queue_struct outq;
3301 outq.nr = outq.alloc = 0;
3303 for (i = 0; i < q->nr; i++) {
3304 struct diff_filepair *p = q->queue[i];
3307 * 1. Entries that come from stat info dirtyness
3308 * always have both sides (iow, not create/delete),
3309 * one side of the object name is unknown, with
3310 * the same mode and size. Keep the ones that
3311 * do not match these criteria. They have real
3314 * 2. At this point, the file is known to be modified,
3315 * with the same mode and size, and the object
3316 * name of one side is unknown. Need to inspect
3317 * the identical contents.
3319 if (!DIFF_FILE_VALID(p->one) || /* (1) */
3320 !DIFF_FILE_VALID(p->two) ||
3321 (p->one->sha1_valid && p->two->sha1_valid) ||
3322 (p->one->mode != p->two->mode) ||
3323 diff_populate_filespec(p->one, 1) ||
3324 diff_populate_filespec(p->two, 1) ||
3325 (p->one->size != p->two->size) ||
3326 !diff_filespec_is_identical(p->one, p->two)) /* (2) */
3330 * The caller can subtract 1 from skip_stat_unmatch
3331 * to determine how many paths were dirty only
3332 * due to stat info mismatch.
3334 if (!DIFF_OPT_TST(diffopt, NO_INDEX))
3335 diffopt->skip_stat_unmatch++;
3336 diff_free_filepair(p);
3343 void diffcore_std(struct diff_options *options)
3345 if (options->skip_stat_unmatch)
3346 diffcore_skip_stat_unmatch(options);
3347 if (options->break_opt != -1)
3348 diffcore_break(options->break_opt);
3349 if (options->detect_rename)
3350 diffcore_rename(options);
3351 if (options->break_opt != -1)
3352 diffcore_merge_broken();
3353 if (options->pickaxe)
3354 diffcore_pickaxe(options->pickaxe, options->pickaxe_opts);
3355 if (options->orderfile)
3356 diffcore_order(options->orderfile);
3357 diff_resolve_rename_copy();
3358 diffcore_apply_filter(options->filter);
3360 if (diff_queued_diff.nr)
3361 DIFF_OPT_SET(options, HAS_CHANGES);
3363 DIFF_OPT_CLR(options, HAS_CHANGES);
3366 int diff_result_code(struct diff_options *opt, int status)
3369 if (!DIFF_OPT_TST(opt, EXIT_WITH_STATUS) &&
3370 !(opt->output_format & DIFF_FORMAT_CHECKDIFF))
3372 if (DIFF_OPT_TST(opt, EXIT_WITH_STATUS) &&
3373 DIFF_OPT_TST(opt, HAS_CHANGES))
3375 if ((opt->output_format & DIFF_FORMAT_CHECKDIFF) &&
3376 DIFF_OPT_TST(opt, CHECK_FAILED))
3381 void diff_addremove(struct diff_options *options,
3382 int addremove, unsigned mode,
3383 const unsigned char *sha1,
3384 const char *concatpath)
3386 struct diff_filespec *one, *two;
3388 if (DIFF_OPT_TST(options, IGNORE_SUBMODULES) && S_ISGITLINK(mode))
3391 /* This may look odd, but it is a preparation for
3392 * feeding "there are unchanged files which should
3393 * not produce diffs, but when you are doing copy
3394 * detection you would need them, so here they are"
3395 * entries to the diff-core. They will be prefixed
3396 * with something like '=' or '*' (I haven't decided
3397 * which but should not make any difference).
3398 * Feeding the same new and old to diff_change()
3399 * also has the same effect.
3400 * Before the final output happens, they are pruned after
3401 * merged into rename/copy pairs as appropriate.
3403 if (DIFF_OPT_TST(options, REVERSE_DIFF))
3404 addremove = (addremove == '+' ? '-' :
3405 addremove == '-' ? '+' : addremove);
3407 if (options->prefix &&
3408 strncmp(concatpath, options->prefix, options->prefix_length))
3411 one = alloc_filespec(concatpath);
3412 two = alloc_filespec(concatpath);
3414 if (addremove != '+')
3415 fill_filespec(one, sha1, mode);
3416 if (addremove != '-')
3417 fill_filespec(two, sha1, mode);
3419 diff_queue(&diff_queued_diff, one, two);
3420 DIFF_OPT_SET(options, HAS_CHANGES);
3423 void diff_change(struct diff_options *options,
3424 unsigned old_mode, unsigned new_mode,
3425 const unsigned char *old_sha1,
3426 const unsigned char *new_sha1,
3427 const char *concatpath)
3429 struct diff_filespec *one, *two;
3431 if (DIFF_OPT_TST(options, IGNORE_SUBMODULES) && S_ISGITLINK(old_mode)
3432 && S_ISGITLINK(new_mode))
3435 if (DIFF_OPT_TST(options, REVERSE_DIFF)) {
3437 const unsigned char *tmp_c;
3438 tmp = old_mode; old_mode = new_mode; new_mode = tmp;
3439 tmp_c = old_sha1; old_sha1 = new_sha1; new_sha1 = tmp_c;
3442 if (options->prefix &&
3443 strncmp(concatpath, options->prefix, options->prefix_length))
3446 one = alloc_filespec(concatpath);
3447 two = alloc_filespec(concatpath);
3448 fill_filespec(one, old_sha1, old_mode);
3449 fill_filespec(two, new_sha1, new_mode);
3451 diff_queue(&diff_queued_diff, one, two);
3452 DIFF_OPT_SET(options, HAS_CHANGES);
3455 void diff_unmerge(struct diff_options *options,
3457 unsigned mode, const unsigned char *sha1)
3459 struct diff_filespec *one, *two;
3461 if (options->prefix &&
3462 strncmp(path, options->prefix, options->prefix_length))
3465 one = alloc_filespec(path);
3466 two = alloc_filespec(path);
3467 fill_filespec(one, sha1, mode);
3468 diff_queue(&diff_queued_diff, one, two)->is_unmerged = 1;
3471 static char *run_textconv(const char *pgm, struct diff_filespec *spec,
3474 struct diff_tempfile temp;
3475 const char *argv[3];
3476 const char **arg = argv;
3477 struct child_process child;
3478 struct strbuf buf = STRBUF_INIT;
3480 prepare_temp_file(spec->path, &temp, spec);
3485 memset(&child, 0, sizeof(child));
3488 if (start_command(&child) != 0 ||
3489 strbuf_read(&buf, child.out, 0) < 0 ||
3490 finish_command(&child) != 0) {
3491 if (temp.name == temp.tmp_path)
3493 error("error running textconv command '%s'", pgm);
3496 if (temp.name == temp.tmp_path)
3499 return strbuf_detach(&buf, outsize);