builtin-mv: readability patch
[git/haiku.git] / diff.c
blobda7cca1952e7e245730bd81419e1b23ce57ef173
1 /*
2 * Copyright (C) 2005 Junio C Hamano
3 */
4 #include <sys/types.h>
5 #include <sys/wait.h>
6 #include <signal.h>
7 #include "cache.h"
8 #include "quote.h"
9 #include "diff.h"
10 #include "diffcore.h"
11 #include "delta.h"
12 #include "xdiff-interface.h"
14 static int use_size_cache;
16 static int diff_detect_rename_default;
17 static int diff_rename_limit_default = -1;
18 static int diff_use_color_default;
20 /* "\033[1;38;5;2xx;48;5;2xxm\0" is 23 bytes */
21 static char diff_colors[][24] = {
22 "\033[m", /* reset */
23 "", /* normal */
24 "\033[1m", /* bold */
25 "\033[36m", /* cyan */
26 "\033[31m", /* red */
27 "\033[32m", /* green */
28 "\033[33m" /* yellow */
31 static int parse_diff_color_slot(const char *var, int ofs)
33 if (!strcasecmp(var+ofs, "plain"))
34 return DIFF_PLAIN;
35 if (!strcasecmp(var+ofs, "meta"))
36 return DIFF_METAINFO;
37 if (!strcasecmp(var+ofs, "frag"))
38 return DIFF_FRAGINFO;
39 if (!strcasecmp(var+ofs, "old"))
40 return DIFF_FILE_OLD;
41 if (!strcasecmp(var+ofs, "new"))
42 return DIFF_FILE_NEW;
43 if (!strcasecmp(var+ofs, "commit"))
44 return DIFF_COMMIT;
45 die("bad config variable '%s'", var);
48 static int parse_color(const char *name, int len)
50 static const char * const color_names[] = {
51 "normal", "black", "red", "green", "yellow",
52 "blue", "magenta", "cyan", "white"
54 char *end;
55 int i;
56 for (i = 0; i < ARRAY_SIZE(color_names); i++) {
57 const char *str = color_names[i];
58 if (!strncasecmp(name, str, len) && !str[len])
59 return i - 1;
61 i = strtol(name, &end, 10);
62 if (*name && !*end && i >= -1 && i <= 255)
63 return i;
64 return -2;
67 static int parse_attr(const char *name, int len)
69 static const int attr_values[] = { 1, 2, 4, 5, 7 };
70 static const char * const attr_names[] = {
71 "bold", "dim", "ul", "blink", "reverse"
73 int i;
74 for (i = 0; i < ARRAY_SIZE(attr_names); i++) {
75 const char *str = attr_names[i];
76 if (!strncasecmp(name, str, len) && !str[len])
77 return attr_values[i];
79 return -1;
82 static void parse_diff_color_value(const char *value, const char *var, char *dst)
84 const char *ptr = value;
85 int attr = -1;
86 int fg = -2;
87 int bg = -2;
89 if (!strcasecmp(value, "reset")) {
90 strcpy(dst, "\033[m");
91 return;
94 /* [fg [bg]] [attr] */
95 while (*ptr) {
96 const char *word = ptr;
97 int val, len = 0;
99 while (word[len] && !isspace(word[len]))
100 len++;
102 ptr = word + len;
103 while (*ptr && isspace(*ptr))
104 ptr++;
106 val = parse_color(word, len);
107 if (val >= -1) {
108 if (fg == -2) {
109 fg = val;
110 continue;
112 if (bg == -2) {
113 bg = val;
114 continue;
116 goto bad;
118 val = parse_attr(word, len);
119 if (val < 0 || attr != -1)
120 goto bad;
121 attr = val;
124 if (attr >= 0 || fg >= 0 || bg >= 0) {
125 int sep = 0;
127 *dst++ = '\033';
128 *dst++ = '[';
129 if (attr >= 0) {
130 *dst++ = '0' + attr;
131 sep++;
133 if (fg >= 0) {
134 if (sep++)
135 *dst++ = ';';
136 if (fg < 8) {
137 *dst++ = '3';
138 *dst++ = '0' + fg;
139 } else {
140 dst += sprintf(dst, "38;5;%d", fg);
143 if (bg >= 0) {
144 if (sep++)
145 *dst++ = ';';
146 if (bg < 8) {
147 *dst++ = '4';
148 *dst++ = '0' + bg;
149 } else {
150 dst += sprintf(dst, "48;5;%d", bg);
153 *dst++ = 'm';
155 *dst = 0;
156 return;
157 bad:
158 die("bad config value '%s' for variable '%s'", value, var);
162 * These are to give UI layer defaults.
163 * The core-level commands such as git-diff-files should
164 * never be affected by the setting of diff.renames
165 * the user happens to have in the configuration file.
167 int git_diff_ui_config(const char *var, const char *value)
169 if (!strcmp(var, "diff.renamelimit")) {
170 diff_rename_limit_default = git_config_int(var, value);
171 return 0;
173 if (!strcmp(var, "diff.color")) {
174 if (!value)
175 diff_use_color_default = 1; /* bool */
176 else if (!strcasecmp(value, "auto")) {
177 diff_use_color_default = 0;
178 if (isatty(1) || (pager_in_use && pager_use_color)) {
179 char *term = getenv("TERM");
180 if (term && strcmp(term, "dumb"))
181 diff_use_color_default = 1;
184 else if (!strcasecmp(value, "never"))
185 diff_use_color_default = 0;
186 else if (!strcasecmp(value, "always"))
187 diff_use_color_default = 1;
188 else
189 diff_use_color_default = git_config_bool(var, value);
190 return 0;
192 if (!strcmp(var, "diff.renames")) {
193 if (!value)
194 diff_detect_rename_default = DIFF_DETECT_RENAME;
195 else if (!strcasecmp(value, "copies") ||
196 !strcasecmp(value, "copy"))
197 diff_detect_rename_default = DIFF_DETECT_COPY;
198 else if (git_config_bool(var,value))
199 diff_detect_rename_default = DIFF_DETECT_RENAME;
200 return 0;
202 if (!strncmp(var, "diff.color.", 11)) {
203 int slot = parse_diff_color_slot(var, 11);
204 parse_diff_color_value(value, var, diff_colors[slot]);
205 return 0;
207 return git_default_config(var, value);
210 static char *quote_one(const char *str)
212 int needlen;
213 char *xp;
215 if (!str)
216 return NULL;
217 needlen = quote_c_style(str, NULL, NULL, 0);
218 if (!needlen)
219 return strdup(str);
220 xp = xmalloc(needlen + 1);
221 quote_c_style(str, xp, NULL, 0);
222 return xp;
225 static char *quote_two(const char *one, const char *two)
227 int need_one = quote_c_style(one, NULL, NULL, 1);
228 int need_two = quote_c_style(two, NULL, NULL, 1);
229 char *xp;
231 if (need_one + need_two) {
232 if (!need_one) need_one = strlen(one);
233 if (!need_two) need_one = strlen(two);
235 xp = xmalloc(need_one + need_two + 3);
236 xp[0] = '"';
237 quote_c_style(one, xp + 1, NULL, 1);
238 quote_c_style(two, xp + need_one + 1, NULL, 1);
239 strcpy(xp + need_one + need_two + 1, "\"");
240 return xp;
242 need_one = strlen(one);
243 need_two = strlen(two);
244 xp = xmalloc(need_one + need_two + 1);
245 strcpy(xp, one);
246 strcpy(xp + need_one, two);
247 return xp;
250 static const char *external_diff(void)
252 static const char *external_diff_cmd = NULL;
253 static int done_preparing = 0;
255 if (done_preparing)
256 return external_diff_cmd;
257 external_diff_cmd = getenv("GIT_EXTERNAL_DIFF");
258 done_preparing = 1;
259 return external_diff_cmd;
262 #define TEMPFILE_PATH_LEN 50
264 static struct diff_tempfile {
265 const char *name; /* filename external diff should read from */
266 char hex[41];
267 char mode[10];
268 char tmp_path[TEMPFILE_PATH_LEN];
269 } diff_temp[2];
271 static int count_lines(const char *data, int size)
273 int count, ch, completely_empty = 1, nl_just_seen = 0;
274 count = 0;
275 while (0 < size--) {
276 ch = *data++;
277 if (ch == '\n') {
278 count++;
279 nl_just_seen = 1;
280 completely_empty = 0;
282 else {
283 nl_just_seen = 0;
284 completely_empty = 0;
287 if (completely_empty)
288 return 0;
289 if (!nl_just_seen)
290 count++; /* no trailing newline */
291 return count;
294 static void print_line_count(int count)
296 switch (count) {
297 case 0:
298 printf("0,0");
299 break;
300 case 1:
301 printf("1");
302 break;
303 default:
304 printf("1,%d", count);
305 break;
309 static void copy_file(int prefix, const char *data, int size)
311 int ch, nl_just_seen = 1;
312 while (0 < size--) {
313 ch = *data++;
314 if (nl_just_seen)
315 putchar(prefix);
316 putchar(ch);
317 if (ch == '\n')
318 nl_just_seen = 1;
319 else
320 nl_just_seen = 0;
322 if (!nl_just_seen)
323 printf("\n\\ No newline at end of file\n");
326 static void emit_rewrite_diff(const char *name_a,
327 const char *name_b,
328 struct diff_filespec *one,
329 struct diff_filespec *two)
331 int lc_a, lc_b;
332 diff_populate_filespec(one, 0);
333 diff_populate_filespec(two, 0);
334 lc_a = count_lines(one->data, one->size);
335 lc_b = count_lines(two->data, two->size);
336 printf("--- %s\n+++ %s\n@@ -", name_a, name_b);
337 print_line_count(lc_a);
338 printf(" +");
339 print_line_count(lc_b);
340 printf(" @@\n");
341 if (lc_a)
342 copy_file('-', one->data, one->size);
343 if (lc_b)
344 copy_file('+', two->data, two->size);
347 static int fill_mmfile(mmfile_t *mf, struct diff_filespec *one)
349 if (!DIFF_FILE_VALID(one)) {
350 mf->ptr = (char *)""; /* does not matter */
351 mf->size = 0;
352 return 0;
354 else if (diff_populate_filespec(one, 0))
355 return -1;
356 mf->ptr = one->data;
357 mf->size = one->size;
358 return 0;
361 struct diff_words_buffer {
362 mmfile_t text;
363 long alloc;
364 long current; /* output pointer */
365 int suppressed_newline;
368 static void diff_words_append(char *line, unsigned long len,
369 struct diff_words_buffer *buffer)
371 if (buffer->text.size + len > buffer->alloc) {
372 buffer->alloc = (buffer->text.size + len) * 3 / 2;
373 buffer->text.ptr = xrealloc(buffer->text.ptr, buffer->alloc);
375 line++;
376 len--;
377 memcpy(buffer->text.ptr + buffer->text.size, line, len);
378 buffer->text.size += len;
381 struct diff_words_data {
382 struct xdiff_emit_state xm;
383 struct diff_words_buffer minus, plus;
386 static void print_word(struct diff_words_buffer *buffer, int len, int color,
387 int suppress_newline)
389 const char *ptr;
390 int eol = 0;
392 if (len == 0)
393 return;
395 ptr = buffer->text.ptr + buffer->current;
396 buffer->current += len;
398 if (ptr[len - 1] == '\n') {
399 eol = 1;
400 len--;
403 fputs(diff_get_color(1, color), stdout);
404 fwrite(ptr, len, 1, stdout);
405 fputs(diff_get_color(1, DIFF_RESET), stdout);
407 if (eol) {
408 if (suppress_newline)
409 buffer->suppressed_newline = 1;
410 else
411 putchar('\n');
415 static void fn_out_diff_words_aux(void *priv, char *line, unsigned long len)
417 struct diff_words_data *diff_words = priv;
419 if (diff_words->minus.suppressed_newline) {
420 if (line[0] != '+')
421 putchar('\n');
422 diff_words->minus.suppressed_newline = 0;
425 len--;
426 switch (line[0]) {
427 case '-':
428 print_word(&diff_words->minus, len, DIFF_FILE_OLD, 1);
429 break;
430 case '+':
431 print_word(&diff_words->plus, len, DIFF_FILE_NEW, 0);
432 break;
433 case ' ':
434 print_word(&diff_words->plus, len, DIFF_PLAIN, 0);
435 diff_words->minus.current += len;
436 break;
440 /* this executes the word diff on the accumulated buffers */
441 static void diff_words_show(struct diff_words_data *diff_words)
443 xpparam_t xpp;
444 xdemitconf_t xecfg;
445 xdemitcb_t ecb;
446 mmfile_t minus, plus;
447 int i;
449 minus.size = diff_words->minus.text.size;
450 minus.ptr = xmalloc(minus.size);
451 memcpy(minus.ptr, diff_words->minus.text.ptr, minus.size);
452 for (i = 0; i < minus.size; i++)
453 if (isspace(minus.ptr[i]))
454 minus.ptr[i] = '\n';
455 diff_words->minus.current = 0;
457 plus.size = diff_words->plus.text.size;
458 plus.ptr = xmalloc(plus.size);
459 memcpy(plus.ptr, diff_words->plus.text.ptr, plus.size);
460 for (i = 0; i < plus.size; i++)
461 if (isspace(plus.ptr[i]))
462 plus.ptr[i] = '\n';
463 diff_words->plus.current = 0;
465 xpp.flags = XDF_NEED_MINIMAL;
466 xecfg.ctxlen = diff_words->minus.alloc + diff_words->plus.alloc;
467 xecfg.flags = 0;
468 ecb.outf = xdiff_outf;
469 ecb.priv = diff_words;
470 diff_words->xm.consume = fn_out_diff_words_aux;
471 xdl_diff(&minus, &plus, &xpp, &xecfg, &ecb);
473 free(minus.ptr);
474 free(plus.ptr);
475 diff_words->minus.text.size = diff_words->plus.text.size = 0;
477 if (diff_words->minus.suppressed_newline) {
478 putchar('\n');
479 diff_words->minus.suppressed_newline = 0;
483 struct emit_callback {
484 struct xdiff_emit_state xm;
485 int nparents, color_diff;
486 const char **label_path;
487 struct diff_words_data *diff_words;
490 static void free_diff_words_data(struct emit_callback *ecbdata)
492 if (ecbdata->diff_words) {
493 /* flush buffers */
494 if (ecbdata->diff_words->minus.text.size ||
495 ecbdata->diff_words->plus.text.size)
496 diff_words_show(ecbdata->diff_words);
498 if (ecbdata->diff_words->minus.text.ptr)
499 free (ecbdata->diff_words->minus.text.ptr);
500 if (ecbdata->diff_words->plus.text.ptr)
501 free (ecbdata->diff_words->plus.text.ptr);
502 free(ecbdata->diff_words);
503 ecbdata->diff_words = NULL;
507 const char *diff_get_color(int diff_use_color, enum color_diff ix)
509 if (diff_use_color)
510 return diff_colors[ix];
511 return "";
514 static void fn_out_consume(void *priv, char *line, unsigned long len)
516 int i;
517 struct emit_callback *ecbdata = priv;
518 const char *set = diff_get_color(ecbdata->color_diff, DIFF_METAINFO);
519 const char *reset = diff_get_color(ecbdata->color_diff, DIFF_RESET);
521 if (ecbdata->label_path[0]) {
522 printf("%s--- %s%s\n", set, ecbdata->label_path[0], reset);
523 printf("%s+++ %s%s\n", set, ecbdata->label_path[1], reset);
524 ecbdata->label_path[0] = ecbdata->label_path[1] = NULL;
527 /* This is not really necessary for now because
528 * this codepath only deals with two-way diffs.
530 for (i = 0; i < len && line[i] == '@'; i++)
532 if (2 <= i && i < len && line[i] == ' ') {
533 ecbdata->nparents = i - 1;
534 set = diff_get_color(ecbdata->color_diff, DIFF_FRAGINFO);
536 else if (len < ecbdata->nparents)
537 set = reset;
538 else {
539 int nparents = ecbdata->nparents;
540 int color = DIFF_PLAIN;
541 if (ecbdata->diff_words && nparents != 1)
542 /* fall back to normal diff */
543 free_diff_words_data(ecbdata);
544 if (ecbdata->diff_words) {
545 if (line[0] == '-') {
546 diff_words_append(line, len,
547 &ecbdata->diff_words->minus);
548 return;
549 } else if (line[0] == '+') {
550 diff_words_append(line, len,
551 &ecbdata->diff_words->plus);
552 return;
554 if (ecbdata->diff_words->minus.text.size ||
555 ecbdata->diff_words->plus.text.size)
556 diff_words_show(ecbdata->diff_words);
557 line++;
558 len--;
559 } else
560 for (i = 0; i < nparents && len; i++) {
561 if (line[i] == '-')
562 color = DIFF_FILE_OLD;
563 else if (line[i] == '+')
564 color = DIFF_FILE_NEW;
566 set = diff_get_color(ecbdata->color_diff, color);
568 if (len > 0 && line[len-1] == '\n')
569 len--;
570 fputs (set, stdout);
571 fwrite (line, len, 1, stdout);
572 puts (reset);
575 static char *pprint_rename(const char *a, const char *b)
577 const char *old = a;
578 const char *new = b;
579 char *name = NULL;
580 int pfx_length, sfx_length;
581 int len_a = strlen(a);
582 int len_b = strlen(b);
584 /* Find common prefix */
585 pfx_length = 0;
586 while (*old && *new && *old == *new) {
587 if (*old == '/')
588 pfx_length = old - a + 1;
589 old++;
590 new++;
593 /* Find common suffix */
594 old = a + len_a;
595 new = b + len_b;
596 sfx_length = 0;
597 while (a <= old && b <= new && *old == *new) {
598 if (*old == '/')
599 sfx_length = len_a - (old - a);
600 old--;
601 new--;
605 * pfx{mid-a => mid-b}sfx
606 * {pfx-a => pfx-b}sfx
607 * pfx{sfx-a => sfx-b}
608 * name-a => name-b
610 if (pfx_length + sfx_length) {
611 int a_midlen = len_a - pfx_length - sfx_length;
612 int b_midlen = len_b - pfx_length - sfx_length;
613 if (a_midlen < 0) a_midlen = 0;
614 if (b_midlen < 0) b_midlen = 0;
616 name = xmalloc(pfx_length + a_midlen + b_midlen + sfx_length + 7);
617 sprintf(name, "%.*s{%.*s => %.*s}%s",
618 pfx_length, a,
619 a_midlen, a + pfx_length,
620 b_midlen, b + pfx_length,
621 a + len_a - sfx_length);
623 else {
624 name = xmalloc(len_a + len_b + 5);
625 sprintf(name, "%s => %s", a, b);
627 return name;
630 struct diffstat_t {
631 struct xdiff_emit_state xm;
633 int nr;
634 int alloc;
635 struct diffstat_file {
636 char *name;
637 unsigned is_unmerged:1;
638 unsigned is_binary:1;
639 unsigned is_renamed:1;
640 unsigned int added, deleted;
641 } **files;
644 static struct diffstat_file *diffstat_add(struct diffstat_t *diffstat,
645 const char *name_a,
646 const char *name_b)
648 struct diffstat_file *x;
649 x = xcalloc(sizeof (*x), 1);
650 if (diffstat->nr == diffstat->alloc) {
651 diffstat->alloc = alloc_nr(diffstat->alloc);
652 diffstat->files = xrealloc(diffstat->files,
653 diffstat->alloc * sizeof(x));
655 diffstat->files[diffstat->nr++] = x;
656 if (name_b) {
657 x->name = pprint_rename(name_a, name_b);
658 x->is_renamed = 1;
660 else
661 x->name = strdup(name_a);
662 return x;
665 static void diffstat_consume(void *priv, char *line, unsigned long len)
667 struct diffstat_t *diffstat = priv;
668 struct diffstat_file *x = diffstat->files[diffstat->nr - 1];
670 if (line[0] == '+')
671 x->added++;
672 else if (line[0] == '-')
673 x->deleted++;
676 static const char pluses[] = "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++";
677 static const char minuses[]= "----------------------------------------------------------------------";
678 const char mime_boundary_leader[] = "------------";
680 static void show_stats(struct diffstat_t* data)
682 int i, len, add, del, total, adds = 0, dels = 0;
683 int max, max_change = 0, max_len = 0;
684 int total_files = data->nr;
686 if (data->nr == 0)
687 return;
689 for (i = 0; i < data->nr; i++) {
690 struct diffstat_file *file = data->files[i];
692 len = strlen(file->name);
693 if (max_len < len)
694 max_len = len;
696 if (file->is_binary || file->is_unmerged)
697 continue;
698 if (max_change < file->added + file->deleted)
699 max_change = file->added + file->deleted;
702 for (i = 0; i < data->nr; i++) {
703 const char *prefix = "";
704 char *name = data->files[i]->name;
705 int added = data->files[i]->added;
706 int deleted = data->files[i]->deleted;
708 if (0 < (len = quote_c_style(name, NULL, NULL, 0))) {
709 char *qname = xmalloc(len + 1);
710 quote_c_style(name, qname, NULL, 0);
711 free(name);
712 data->files[i]->name = name = qname;
716 * "scale" the filename
718 len = strlen(name);
719 max = max_len;
720 if (max > 50)
721 max = 50;
722 if (len > max) {
723 char *slash;
724 prefix = "...";
725 max -= 3;
726 name += len - max;
727 slash = strchr(name, '/');
728 if (slash)
729 name = slash;
731 len = max;
734 * scale the add/delete
736 max = max_change;
737 if (max + len > 70)
738 max = 70 - len;
740 if (data->files[i]->is_binary) {
741 printf(" %s%-*s | Bin\n", prefix, len, name);
742 goto free_diffstat_file;
744 else if (data->files[i]->is_unmerged) {
745 printf(" %s%-*s | Unmerged\n", prefix, len, name);
746 goto free_diffstat_file;
748 else if (!data->files[i]->is_renamed &&
749 (added + deleted == 0)) {
750 total_files--;
751 goto free_diffstat_file;
754 add = added;
755 del = deleted;
756 total = add + del;
757 adds += add;
758 dels += del;
760 if (max_change > 0) {
761 total = (total * max + max_change / 2) / max_change;
762 add = (add * max + max_change / 2) / max_change;
763 del = total - add;
765 printf(" %s%-*s |%5d %.*s%.*s\n", prefix,
766 len, name, added + deleted,
767 add, pluses, del, minuses);
768 free_diffstat_file:
769 free(data->files[i]->name);
770 free(data->files[i]);
772 free(data->files);
773 printf(" %d files changed, %d insertions(+), %d deletions(-)\n",
774 total_files, adds, dels);
777 struct checkdiff_t {
778 struct xdiff_emit_state xm;
779 const char *filename;
780 int lineno;
783 static void checkdiff_consume(void *priv, char *line, unsigned long len)
785 struct checkdiff_t *data = priv;
787 if (line[0] == '+') {
788 int i, spaces = 0;
790 data->lineno++;
792 /* check space before tab */
793 for (i = 1; i < len && (line[i] == ' ' || line[i] == '\t'); i++)
794 if (line[i] == ' ')
795 spaces++;
796 if (line[i - 1] == '\t' && spaces)
797 printf("%s:%d: space before tab:%.*s\n",
798 data->filename, data->lineno, (int)len, line);
800 /* check white space at line end */
801 if (line[len - 1] == '\n')
802 len--;
803 if (isspace(line[len - 1]))
804 printf("%s:%d: white space at end: %.*s\n",
805 data->filename, data->lineno, (int)len, line);
806 } else if (line[0] == ' ')
807 data->lineno++;
808 else if (line[0] == '@') {
809 char *plus = strchr(line, '+');
810 if (plus)
811 data->lineno = strtol(plus, NULL, 10);
812 else
813 die("invalid diff");
817 static unsigned char *deflate_it(char *data,
818 unsigned long size,
819 unsigned long *result_size)
821 int bound;
822 unsigned char *deflated;
823 z_stream stream;
825 memset(&stream, 0, sizeof(stream));
826 deflateInit(&stream, zlib_compression_level);
827 bound = deflateBound(&stream, size);
828 deflated = xmalloc(bound);
829 stream.next_out = deflated;
830 stream.avail_out = bound;
832 stream.next_in = (unsigned char *)data;
833 stream.avail_in = size;
834 while (deflate(&stream, Z_FINISH) == Z_OK)
835 ; /* nothing */
836 deflateEnd(&stream);
837 *result_size = stream.total_out;
838 return deflated;
841 static void emit_binary_diff(mmfile_t *one, mmfile_t *two)
843 void *cp;
844 void *delta;
845 void *deflated;
846 void *data;
847 unsigned long orig_size;
848 unsigned long delta_size;
849 unsigned long deflate_size;
850 unsigned long data_size;
852 printf("GIT binary patch\n");
853 /* We could do deflated delta, or we could do just deflated two,
854 * whichever is smaller.
856 delta = NULL;
857 deflated = deflate_it(two->ptr, two->size, &deflate_size);
858 if (one->size && two->size) {
859 delta = diff_delta(one->ptr, one->size,
860 two->ptr, two->size,
861 &delta_size, deflate_size);
862 if (delta) {
863 void *to_free = delta;
864 orig_size = delta_size;
865 delta = deflate_it(delta, delta_size, &delta_size);
866 free(to_free);
870 if (delta && delta_size < deflate_size) {
871 printf("delta %lu\n", orig_size);
872 free(deflated);
873 data = delta;
874 data_size = delta_size;
876 else {
877 printf("literal %lu\n", two->size);
878 free(delta);
879 data = deflated;
880 data_size = deflate_size;
883 /* emit data encoded in base85 */
884 cp = data;
885 while (data_size) {
886 int bytes = (52 < data_size) ? 52 : data_size;
887 char line[70];
888 data_size -= bytes;
889 if (bytes <= 26)
890 line[0] = bytes + 'A' - 1;
891 else
892 line[0] = bytes - 26 + 'a' - 1;
893 encode_85(line + 1, cp, bytes);
894 cp = (char *) cp + bytes;
895 puts(line);
897 printf("\n");
898 free(data);
901 #define FIRST_FEW_BYTES 8000
902 static int mmfile_is_binary(mmfile_t *mf)
904 long sz = mf->size;
905 if (FIRST_FEW_BYTES < sz)
906 sz = FIRST_FEW_BYTES;
907 return !!memchr(mf->ptr, 0, sz);
910 static void builtin_diff(const char *name_a,
911 const char *name_b,
912 struct diff_filespec *one,
913 struct diff_filespec *two,
914 const char *xfrm_msg,
915 struct diff_options *o,
916 int complete_rewrite)
918 mmfile_t mf1, mf2;
919 const char *lbl[2];
920 char *a_one, *b_two;
921 const char *set = diff_get_color(o->color_diff, DIFF_METAINFO);
922 const char *reset = diff_get_color(o->color_diff, DIFF_RESET);
924 a_one = quote_two("a/", name_a);
925 b_two = quote_two("b/", name_b);
926 lbl[0] = DIFF_FILE_VALID(one) ? a_one : "/dev/null";
927 lbl[1] = DIFF_FILE_VALID(two) ? b_two : "/dev/null";
928 printf("%sdiff --git %s %s%s\n", set, a_one, b_two, reset);
929 if (lbl[0][0] == '/') {
930 /* /dev/null */
931 printf("%snew file mode %06o%s\n", set, two->mode, reset);
932 if (xfrm_msg && xfrm_msg[0])
933 printf("%s%s%s\n", set, xfrm_msg, reset);
935 else if (lbl[1][0] == '/') {
936 printf("%sdeleted file mode %06o%s\n", set, one->mode, reset);
937 if (xfrm_msg && xfrm_msg[0])
938 printf("%s%s%s\n", set, xfrm_msg, reset);
940 else {
941 if (one->mode != two->mode) {
942 printf("%sold mode %06o%s\n", set, one->mode, reset);
943 printf("%snew mode %06o%s\n", set, two->mode, reset);
945 if (xfrm_msg && xfrm_msg[0])
946 printf("%s%s%s\n", set, xfrm_msg, reset);
948 * we do not run diff between different kind
949 * of objects.
951 if ((one->mode ^ two->mode) & S_IFMT)
952 goto free_ab_and_return;
953 if (complete_rewrite) {
954 emit_rewrite_diff(name_a, name_b, one, two);
955 goto free_ab_and_return;
959 if (fill_mmfile(&mf1, one) < 0 || fill_mmfile(&mf2, two) < 0)
960 die("unable to read files to diff");
962 if (!o->text && (mmfile_is_binary(&mf1) || mmfile_is_binary(&mf2))) {
963 /* Quite common confusing case */
964 if (mf1.size == mf2.size &&
965 !memcmp(mf1.ptr, mf2.ptr, mf1.size))
966 goto free_ab_and_return;
967 if (o->binary)
968 emit_binary_diff(&mf1, &mf2);
969 else
970 printf("Binary files %s and %s differ\n",
971 lbl[0], lbl[1]);
973 else {
974 /* Crazy xdl interfaces.. */
975 const char *diffopts = getenv("GIT_DIFF_OPTS");
976 xpparam_t xpp;
977 xdemitconf_t xecfg;
978 xdemitcb_t ecb;
979 struct emit_callback ecbdata;
981 memset(&ecbdata, 0, sizeof(ecbdata));
982 ecbdata.label_path = lbl;
983 ecbdata.color_diff = o->color_diff;
984 xpp.flags = XDF_NEED_MINIMAL | o->xdl_opts;
985 xecfg.ctxlen = o->context;
986 xecfg.flags = XDL_EMIT_FUNCNAMES;
987 if (!diffopts)
989 else if (!strncmp(diffopts, "--unified=", 10))
990 xecfg.ctxlen = strtoul(diffopts + 10, NULL, 10);
991 else if (!strncmp(diffopts, "-u", 2))
992 xecfg.ctxlen = strtoul(diffopts + 2, NULL, 10);
993 ecb.outf = xdiff_outf;
994 ecb.priv = &ecbdata;
995 ecbdata.xm.consume = fn_out_consume;
996 if (o->color_diff_words)
997 ecbdata.diff_words =
998 xcalloc(1, sizeof(struct diff_words_data));
999 xdl_diff(&mf1, &mf2, &xpp, &xecfg, &ecb);
1000 if (o->color_diff_words)
1001 free_diff_words_data(&ecbdata);
1004 free_ab_and_return:
1005 free(a_one);
1006 free(b_two);
1007 return;
1010 static void builtin_diffstat(const char *name_a, const char *name_b,
1011 struct diff_filespec *one,
1012 struct diff_filespec *two,
1013 struct diffstat_t *diffstat,
1014 struct diff_options *o,
1015 int complete_rewrite)
1017 mmfile_t mf1, mf2;
1018 struct diffstat_file *data;
1020 data = diffstat_add(diffstat, name_a, name_b);
1022 if (!one || !two) {
1023 data->is_unmerged = 1;
1024 return;
1026 if (complete_rewrite) {
1027 diff_populate_filespec(one, 0);
1028 diff_populate_filespec(two, 0);
1029 data->deleted = count_lines(one->data, one->size);
1030 data->added = count_lines(two->data, two->size);
1031 return;
1033 if (fill_mmfile(&mf1, one) < 0 || fill_mmfile(&mf2, two) < 0)
1034 die("unable to read files to diff");
1036 if (mmfile_is_binary(&mf1) || mmfile_is_binary(&mf2))
1037 data->is_binary = 1;
1038 else {
1039 /* Crazy xdl interfaces.. */
1040 xpparam_t xpp;
1041 xdemitconf_t xecfg;
1042 xdemitcb_t ecb;
1044 xpp.flags = XDF_NEED_MINIMAL | o->xdl_opts;
1045 xecfg.ctxlen = 0;
1046 xecfg.flags = 0;
1047 ecb.outf = xdiff_outf;
1048 ecb.priv = diffstat;
1049 xdl_diff(&mf1, &mf2, &xpp, &xecfg, &ecb);
1053 static void builtin_checkdiff(const char *name_a, const char *name_b,
1054 struct diff_filespec *one,
1055 struct diff_filespec *two)
1057 mmfile_t mf1, mf2;
1058 struct checkdiff_t data;
1060 if (!two)
1061 return;
1063 memset(&data, 0, sizeof(data));
1064 data.xm.consume = checkdiff_consume;
1065 data.filename = name_b ? name_b : name_a;
1066 data.lineno = 0;
1068 if (fill_mmfile(&mf1, one) < 0 || fill_mmfile(&mf2, two) < 0)
1069 die("unable to read files to diff");
1071 if (mmfile_is_binary(&mf2))
1072 return;
1073 else {
1074 /* Crazy xdl interfaces.. */
1075 xpparam_t xpp;
1076 xdemitconf_t xecfg;
1077 xdemitcb_t ecb;
1079 xpp.flags = XDF_NEED_MINIMAL;
1080 xecfg.ctxlen = 0;
1081 xecfg.flags = 0;
1082 ecb.outf = xdiff_outf;
1083 ecb.priv = &data;
1084 xdl_diff(&mf1, &mf2, &xpp, &xecfg, &ecb);
1088 struct diff_filespec *alloc_filespec(const char *path)
1090 int namelen = strlen(path);
1091 struct diff_filespec *spec = xmalloc(sizeof(*spec) + namelen + 1);
1093 memset(spec, 0, sizeof(*spec));
1094 spec->path = (char *)(spec + 1);
1095 memcpy(spec->path, path, namelen+1);
1096 return spec;
1099 void fill_filespec(struct diff_filespec *spec, const unsigned char *sha1,
1100 unsigned short mode)
1102 if (mode) {
1103 spec->mode = canon_mode(mode);
1104 memcpy(spec->sha1, sha1, 20);
1105 spec->sha1_valid = !is_null_sha1(sha1);
1110 * Given a name and sha1 pair, if the dircache tells us the file in
1111 * the work tree has that object contents, return true, so that
1112 * prepare_temp_file() does not have to inflate and extract.
1114 static int work_tree_matches(const char *name, const unsigned char *sha1)
1116 struct cache_entry *ce;
1117 struct stat st;
1118 int pos, len;
1120 /* We do not read the cache ourselves here, because the
1121 * benchmark with my previous version that always reads cache
1122 * shows that it makes things worse for diff-tree comparing
1123 * two linux-2.6 kernel trees in an already checked out work
1124 * tree. This is because most diff-tree comparisons deal with
1125 * only a small number of files, while reading the cache is
1126 * expensive for a large project, and its cost outweighs the
1127 * savings we get by not inflating the object to a temporary
1128 * file. Practically, this code only helps when we are used
1129 * by diff-cache --cached, which does read the cache before
1130 * calling us.
1132 if (!active_cache)
1133 return 0;
1135 len = strlen(name);
1136 pos = cache_name_pos(name, len);
1137 if (pos < 0)
1138 return 0;
1139 ce = active_cache[pos];
1140 if ((lstat(name, &st) < 0) ||
1141 !S_ISREG(st.st_mode) || /* careful! */
1142 ce_match_stat(ce, &st, 0) ||
1143 hashcmp(sha1, ce->sha1))
1144 return 0;
1145 /* we return 1 only when we can stat, it is a regular file,
1146 * stat information matches, and sha1 recorded in the cache
1147 * matches. I.e. we know the file in the work tree really is
1148 * the same as the <name, sha1> pair.
1150 return 1;
1153 static struct sha1_size_cache {
1154 unsigned char sha1[20];
1155 unsigned long size;
1156 } **sha1_size_cache;
1157 static int sha1_size_cache_nr, sha1_size_cache_alloc;
1159 static struct sha1_size_cache *locate_size_cache(unsigned char *sha1,
1160 int find_only,
1161 unsigned long size)
1163 int first, last;
1164 struct sha1_size_cache *e;
1166 first = 0;
1167 last = sha1_size_cache_nr;
1168 while (last > first) {
1169 int cmp, next = (last + first) >> 1;
1170 e = sha1_size_cache[next];
1171 cmp = hashcmp(e->sha1, sha1);
1172 if (!cmp)
1173 return e;
1174 if (cmp < 0) {
1175 last = next;
1176 continue;
1178 first = next+1;
1180 /* not found */
1181 if (find_only)
1182 return NULL;
1183 /* insert to make it at "first" */
1184 if (sha1_size_cache_alloc <= sha1_size_cache_nr) {
1185 sha1_size_cache_alloc = alloc_nr(sha1_size_cache_alloc);
1186 sha1_size_cache = xrealloc(sha1_size_cache,
1187 sha1_size_cache_alloc *
1188 sizeof(*sha1_size_cache));
1190 sha1_size_cache_nr++;
1191 if (first < sha1_size_cache_nr)
1192 memmove(sha1_size_cache + first + 1, sha1_size_cache + first,
1193 (sha1_size_cache_nr - first - 1) *
1194 sizeof(*sha1_size_cache));
1195 e = xmalloc(sizeof(struct sha1_size_cache));
1196 sha1_size_cache[first] = e;
1197 memcpy(e->sha1, sha1, 20);
1198 e->size = size;
1199 return e;
1203 * While doing rename detection and pickaxe operation, we may need to
1204 * grab the data for the blob (or file) for our own in-core comparison.
1205 * diff_filespec has data and size fields for this purpose.
1207 int diff_populate_filespec(struct diff_filespec *s, int size_only)
1209 int err = 0;
1210 if (!DIFF_FILE_VALID(s))
1211 die("internal error: asking to populate invalid file.");
1212 if (S_ISDIR(s->mode))
1213 return -1;
1215 if (!use_size_cache)
1216 size_only = 0;
1218 if (s->data)
1219 return err;
1220 if (!s->sha1_valid ||
1221 work_tree_matches(s->path, s->sha1)) {
1222 struct stat st;
1223 int fd;
1224 if (lstat(s->path, &st) < 0) {
1225 if (errno == ENOENT) {
1226 err_empty:
1227 err = -1;
1228 empty:
1229 s->data = (char *)"";
1230 s->size = 0;
1231 return err;
1234 s->size = st.st_size;
1235 if (!s->size)
1236 goto empty;
1237 if (size_only)
1238 return 0;
1239 if (S_ISLNK(st.st_mode)) {
1240 int ret;
1241 s->data = xmalloc(s->size);
1242 s->should_free = 1;
1243 ret = readlink(s->path, s->data, s->size);
1244 if (ret < 0) {
1245 free(s->data);
1246 goto err_empty;
1248 return 0;
1250 fd = open(s->path, O_RDONLY);
1251 if (fd < 0)
1252 goto err_empty;
1253 s->data = mmap(NULL, s->size, PROT_READ, MAP_PRIVATE, fd, 0);
1254 close(fd);
1255 if (s->data == MAP_FAILED)
1256 goto err_empty;
1257 s->should_munmap = 1;
1259 else {
1260 char type[20];
1261 struct sha1_size_cache *e;
1263 if (size_only) {
1264 e = locate_size_cache(s->sha1, 1, 0);
1265 if (e) {
1266 s->size = e->size;
1267 return 0;
1269 if (!sha1_object_info(s->sha1, type, &s->size))
1270 locate_size_cache(s->sha1, 0, s->size);
1272 else {
1273 s->data = read_sha1_file(s->sha1, type, &s->size);
1274 s->should_free = 1;
1277 return 0;
1280 void diff_free_filespec_data(struct diff_filespec *s)
1282 if (s->should_free)
1283 free(s->data);
1284 else if (s->should_munmap)
1285 munmap(s->data, s->size);
1286 s->should_free = s->should_munmap = 0;
1287 s->data = NULL;
1288 free(s->cnt_data);
1289 s->cnt_data = NULL;
1292 static void prep_temp_blob(struct diff_tempfile *temp,
1293 void *blob,
1294 unsigned long size,
1295 const unsigned char *sha1,
1296 int mode)
1298 int fd;
1300 fd = git_mkstemp(temp->tmp_path, TEMPFILE_PATH_LEN, ".diff_XXXXXX");
1301 if (fd < 0)
1302 die("unable to create temp-file");
1303 if (write(fd, blob, size) != size)
1304 die("unable to write temp-file");
1305 close(fd);
1306 temp->name = temp->tmp_path;
1307 strcpy(temp->hex, sha1_to_hex(sha1));
1308 temp->hex[40] = 0;
1309 sprintf(temp->mode, "%06o", mode);
1312 static void prepare_temp_file(const char *name,
1313 struct diff_tempfile *temp,
1314 struct diff_filespec *one)
1316 if (!DIFF_FILE_VALID(one)) {
1317 not_a_valid_file:
1318 /* A '-' entry produces this for file-2, and
1319 * a '+' entry produces this for file-1.
1321 temp->name = "/dev/null";
1322 strcpy(temp->hex, ".");
1323 strcpy(temp->mode, ".");
1324 return;
1327 if (!one->sha1_valid ||
1328 work_tree_matches(name, one->sha1)) {
1329 struct stat st;
1330 if (lstat(name, &st) < 0) {
1331 if (errno == ENOENT)
1332 goto not_a_valid_file;
1333 die("stat(%s): %s", name, strerror(errno));
1335 if (S_ISLNK(st.st_mode)) {
1336 int ret;
1337 char buf[PATH_MAX + 1]; /* ought to be SYMLINK_MAX */
1338 if (sizeof(buf) <= st.st_size)
1339 die("symlink too long: %s", name);
1340 ret = readlink(name, buf, st.st_size);
1341 if (ret < 0)
1342 die("readlink(%s)", name);
1343 prep_temp_blob(temp, buf, st.st_size,
1344 (one->sha1_valid ?
1345 one->sha1 : null_sha1),
1346 (one->sha1_valid ?
1347 one->mode : S_IFLNK));
1349 else {
1350 /* we can borrow from the file in the work tree */
1351 temp->name = name;
1352 if (!one->sha1_valid)
1353 strcpy(temp->hex, sha1_to_hex(null_sha1));
1354 else
1355 strcpy(temp->hex, sha1_to_hex(one->sha1));
1356 /* Even though we may sometimes borrow the
1357 * contents from the work tree, we always want
1358 * one->mode. mode is trustworthy even when
1359 * !(one->sha1_valid), as long as
1360 * DIFF_FILE_VALID(one).
1362 sprintf(temp->mode, "%06o", one->mode);
1364 return;
1366 else {
1367 if (diff_populate_filespec(one, 0))
1368 die("cannot read data blob for %s", one->path);
1369 prep_temp_blob(temp, one->data, one->size,
1370 one->sha1, one->mode);
1374 static void remove_tempfile(void)
1376 int i;
1378 for (i = 0; i < 2; i++)
1379 if (diff_temp[i].name == diff_temp[i].tmp_path) {
1380 unlink(diff_temp[i].name);
1381 diff_temp[i].name = NULL;
1385 static void remove_tempfile_on_signal(int signo)
1387 remove_tempfile();
1388 signal(SIGINT, SIG_DFL);
1389 raise(signo);
1392 static int spawn_prog(const char *pgm, const char **arg)
1394 pid_t pid;
1395 int status;
1397 fflush(NULL);
1398 pid = fork();
1399 if (pid < 0)
1400 die("unable to fork");
1401 if (!pid) {
1402 execvp(pgm, (char *const*) arg);
1403 exit(255);
1406 while (waitpid(pid, &status, 0) < 0) {
1407 if (errno == EINTR)
1408 continue;
1409 return -1;
1412 /* Earlier we did not check the exit status because
1413 * diff exits non-zero if files are different, and
1414 * we are not interested in knowing that. It was a
1415 * mistake which made it harder to quit a diff-*
1416 * session that uses the git-apply-patch-script as
1417 * the GIT_EXTERNAL_DIFF. A custom GIT_EXTERNAL_DIFF
1418 * should also exit non-zero only when it wants to
1419 * abort the entire diff-* session.
1421 if (WIFEXITED(status) && !WEXITSTATUS(status))
1422 return 0;
1423 return -1;
1426 /* An external diff command takes:
1428 * diff-cmd name infile1 infile1-sha1 infile1-mode \
1429 * infile2 infile2-sha1 infile2-mode [ rename-to ]
1432 static void run_external_diff(const char *pgm,
1433 const char *name,
1434 const char *other,
1435 struct diff_filespec *one,
1436 struct diff_filespec *two,
1437 const char *xfrm_msg,
1438 int complete_rewrite)
1440 const char *spawn_arg[10];
1441 struct diff_tempfile *temp = diff_temp;
1442 int retval;
1443 static int atexit_asked = 0;
1444 const char *othername;
1445 const char **arg = &spawn_arg[0];
1447 othername = (other? other : name);
1448 if (one && two) {
1449 prepare_temp_file(name, &temp[0], one);
1450 prepare_temp_file(othername, &temp[1], two);
1451 if (! atexit_asked &&
1452 (temp[0].name == temp[0].tmp_path ||
1453 temp[1].name == temp[1].tmp_path)) {
1454 atexit_asked = 1;
1455 atexit(remove_tempfile);
1457 signal(SIGINT, remove_tempfile_on_signal);
1460 if (one && two) {
1461 *arg++ = pgm;
1462 *arg++ = name;
1463 *arg++ = temp[0].name;
1464 *arg++ = temp[0].hex;
1465 *arg++ = temp[0].mode;
1466 *arg++ = temp[1].name;
1467 *arg++ = temp[1].hex;
1468 *arg++ = temp[1].mode;
1469 if (other) {
1470 *arg++ = other;
1471 *arg++ = xfrm_msg;
1473 } else {
1474 *arg++ = pgm;
1475 *arg++ = name;
1477 *arg = NULL;
1478 retval = spawn_prog(pgm, spawn_arg);
1479 remove_tempfile();
1480 if (retval) {
1481 fprintf(stderr, "external diff died, stopping at %s.\n", name);
1482 exit(1);
1486 static void run_diff_cmd(const char *pgm,
1487 const char *name,
1488 const char *other,
1489 struct diff_filespec *one,
1490 struct diff_filespec *two,
1491 const char *xfrm_msg,
1492 struct diff_options *o,
1493 int complete_rewrite)
1495 if (pgm) {
1496 run_external_diff(pgm, name, other, one, two, xfrm_msg,
1497 complete_rewrite);
1498 return;
1500 if (one && two)
1501 builtin_diff(name, other ? other : name,
1502 one, two, xfrm_msg, o, complete_rewrite);
1503 else
1504 printf("* Unmerged path %s\n", name);
1507 static void diff_fill_sha1_info(struct diff_filespec *one)
1509 if (DIFF_FILE_VALID(one)) {
1510 if (!one->sha1_valid) {
1511 struct stat st;
1512 if (lstat(one->path, &st) < 0)
1513 die("stat %s", one->path);
1514 if (index_path(one->sha1, one->path, &st, 0))
1515 die("cannot hash %s\n", one->path);
1518 else
1519 memset(one->sha1, 0, 20);
1522 static void run_diff(struct diff_filepair *p, struct diff_options *o)
1524 const char *pgm = external_diff();
1525 char msg[PATH_MAX*2+300], *xfrm_msg;
1526 struct diff_filespec *one;
1527 struct diff_filespec *two;
1528 const char *name;
1529 const char *other;
1530 char *name_munged, *other_munged;
1531 int complete_rewrite = 0;
1532 int len;
1534 if (DIFF_PAIR_UNMERGED(p)) {
1535 /* unmerged */
1536 run_diff_cmd(pgm, p->one->path, NULL, NULL, NULL, NULL, o, 0);
1537 return;
1540 name = p->one->path;
1541 other = (strcmp(name, p->two->path) ? p->two->path : NULL);
1542 name_munged = quote_one(name);
1543 other_munged = quote_one(other);
1544 one = p->one; two = p->two;
1546 diff_fill_sha1_info(one);
1547 diff_fill_sha1_info(two);
1549 len = 0;
1550 switch (p->status) {
1551 case DIFF_STATUS_COPIED:
1552 len += snprintf(msg + len, sizeof(msg) - len,
1553 "similarity index %d%%\n"
1554 "copy from %s\n"
1555 "copy to %s\n",
1556 (int)(0.5 + p->score * 100.0/MAX_SCORE),
1557 name_munged, other_munged);
1558 break;
1559 case DIFF_STATUS_RENAMED:
1560 len += snprintf(msg + len, sizeof(msg) - len,
1561 "similarity index %d%%\n"
1562 "rename from %s\n"
1563 "rename to %s\n",
1564 (int)(0.5 + p->score * 100.0/MAX_SCORE),
1565 name_munged, other_munged);
1566 break;
1567 case DIFF_STATUS_MODIFIED:
1568 if (p->score) {
1569 len += snprintf(msg + len, sizeof(msg) - len,
1570 "dissimilarity index %d%%\n",
1571 (int)(0.5 + p->score *
1572 100.0/MAX_SCORE));
1573 complete_rewrite = 1;
1574 break;
1576 /* fallthru */
1577 default:
1578 /* nothing */
1582 if (hashcmp(one->sha1, two->sha1)) {
1583 int abbrev = o->full_index ? 40 : DEFAULT_ABBREV;
1585 len += snprintf(msg + len, sizeof(msg) - len,
1586 "index %.*s..%.*s",
1587 abbrev, sha1_to_hex(one->sha1),
1588 abbrev, sha1_to_hex(two->sha1));
1589 if (one->mode == two->mode)
1590 len += snprintf(msg + len, sizeof(msg) - len,
1591 " %06o", one->mode);
1592 len += snprintf(msg + len, sizeof(msg) - len, "\n");
1595 if (len)
1596 msg[--len] = 0;
1597 xfrm_msg = len ? msg : NULL;
1599 if (!pgm &&
1600 DIFF_FILE_VALID(one) && DIFF_FILE_VALID(two) &&
1601 (S_IFMT & one->mode) != (S_IFMT & two->mode)) {
1602 /* a filepair that changes between file and symlink
1603 * needs to be split into deletion and creation.
1605 struct diff_filespec *null = alloc_filespec(two->path);
1606 run_diff_cmd(NULL, name, other, one, null, xfrm_msg, o, 0);
1607 free(null);
1608 null = alloc_filespec(one->path);
1609 run_diff_cmd(NULL, name, other, null, two, xfrm_msg, o, 0);
1610 free(null);
1612 else
1613 run_diff_cmd(pgm, name, other, one, two, xfrm_msg, o,
1614 complete_rewrite);
1616 free(name_munged);
1617 free(other_munged);
1620 static void run_diffstat(struct diff_filepair *p, struct diff_options *o,
1621 struct diffstat_t *diffstat)
1623 const char *name;
1624 const char *other;
1625 int complete_rewrite = 0;
1627 if (DIFF_PAIR_UNMERGED(p)) {
1628 /* unmerged */
1629 builtin_diffstat(p->one->path, NULL, NULL, NULL, diffstat, o, 0);
1630 return;
1633 name = p->one->path;
1634 other = (strcmp(name, p->two->path) ? p->two->path : NULL);
1636 diff_fill_sha1_info(p->one);
1637 diff_fill_sha1_info(p->two);
1639 if (p->status == DIFF_STATUS_MODIFIED && p->score)
1640 complete_rewrite = 1;
1641 builtin_diffstat(name, other, p->one, p->two, diffstat, o, complete_rewrite);
1644 static void run_checkdiff(struct diff_filepair *p, struct diff_options *o)
1646 const char *name;
1647 const char *other;
1649 if (DIFF_PAIR_UNMERGED(p)) {
1650 /* unmerged */
1651 return;
1654 name = p->one->path;
1655 other = (strcmp(name, p->two->path) ? p->two->path : NULL);
1657 diff_fill_sha1_info(p->one);
1658 diff_fill_sha1_info(p->two);
1660 builtin_checkdiff(name, other, p->one, p->two);
1663 void diff_setup(struct diff_options *options)
1665 memset(options, 0, sizeof(*options));
1666 options->line_termination = '\n';
1667 options->break_opt = -1;
1668 options->rename_limit = -1;
1669 options->context = 3;
1670 options->msg_sep = "";
1672 options->change = diff_change;
1673 options->add_remove = diff_addremove;
1674 options->color_diff = diff_use_color_default;
1675 options->detect_rename = diff_detect_rename_default;
1678 int diff_setup_done(struct diff_options *options)
1680 int count = 0;
1682 if (options->output_format & DIFF_FORMAT_NAME)
1683 count++;
1684 if (options->output_format & DIFF_FORMAT_NAME_STATUS)
1685 count++;
1686 if (options->output_format & DIFF_FORMAT_CHECKDIFF)
1687 count++;
1688 if (options->output_format & DIFF_FORMAT_NO_OUTPUT)
1689 count++;
1690 if (count > 1)
1691 die("--name-only, --name-status, --check and -s are mutually exclusive");
1693 if (options->find_copies_harder)
1694 options->detect_rename = DIFF_DETECT_COPY;
1696 if (options->output_format & (DIFF_FORMAT_NAME |
1697 DIFF_FORMAT_NAME_STATUS |
1698 DIFF_FORMAT_CHECKDIFF |
1699 DIFF_FORMAT_NO_OUTPUT))
1700 options->output_format &= ~(DIFF_FORMAT_RAW |
1701 DIFF_FORMAT_DIFFSTAT |
1702 DIFF_FORMAT_SUMMARY |
1703 DIFF_FORMAT_PATCH);
1706 * These cases always need recursive; we do not drop caller-supplied
1707 * recursive bits for other formats here.
1709 if (options->output_format & (DIFF_FORMAT_PATCH |
1710 DIFF_FORMAT_DIFFSTAT |
1711 DIFF_FORMAT_CHECKDIFF))
1712 options->recursive = 1;
1714 * Also pickaxe would not work very well if you do not say recursive
1716 if (options->pickaxe)
1717 options->recursive = 1;
1719 if (options->detect_rename && options->rename_limit < 0)
1720 options->rename_limit = diff_rename_limit_default;
1721 if (options->setup & DIFF_SETUP_USE_CACHE) {
1722 if (!active_cache)
1723 /* read-cache does not die even when it fails
1724 * so it is safe for us to do this here. Also
1725 * it does not smudge active_cache or active_nr
1726 * when it fails, so we do not have to worry about
1727 * cleaning it up ourselves either.
1729 read_cache();
1731 if (options->setup & DIFF_SETUP_USE_SIZE_CACHE)
1732 use_size_cache = 1;
1733 if (options->abbrev <= 0 || 40 < options->abbrev)
1734 options->abbrev = 40; /* full */
1736 return 0;
1739 static int opt_arg(const char *arg, int arg_short, const char *arg_long, int *val)
1741 char c, *eq;
1742 int len;
1744 if (*arg != '-')
1745 return 0;
1746 c = *++arg;
1747 if (!c)
1748 return 0;
1749 if (c == arg_short) {
1750 c = *++arg;
1751 if (!c)
1752 return 1;
1753 if (val && isdigit(c)) {
1754 char *end;
1755 int n = strtoul(arg, &end, 10);
1756 if (*end)
1757 return 0;
1758 *val = n;
1759 return 1;
1761 return 0;
1763 if (c != '-')
1764 return 0;
1765 arg++;
1766 eq = strchr(arg, '=');
1767 if (eq)
1768 len = eq - arg;
1769 else
1770 len = strlen(arg);
1771 if (!len || strncmp(arg, arg_long, len))
1772 return 0;
1773 if (eq) {
1774 int n;
1775 char *end;
1776 if (!isdigit(*++eq))
1777 return 0;
1778 n = strtoul(eq, &end, 10);
1779 if (*end)
1780 return 0;
1781 *val = n;
1783 return 1;
1786 int diff_opt_parse(struct diff_options *options, const char **av, int ac)
1788 const char *arg = av[0];
1789 if (!strcmp(arg, "-p") || !strcmp(arg, "-u"))
1790 options->output_format |= DIFF_FORMAT_PATCH;
1791 else if (opt_arg(arg, 'U', "unified", &options->context))
1792 options->output_format |= DIFF_FORMAT_PATCH;
1793 else if (!strcmp(arg, "--raw"))
1794 options->output_format |= DIFF_FORMAT_RAW;
1795 else if (!strcmp(arg, "--patch-with-raw")) {
1796 options->output_format |= DIFF_FORMAT_PATCH | DIFF_FORMAT_RAW;
1798 else if (!strcmp(arg, "--stat"))
1799 options->output_format |= DIFF_FORMAT_DIFFSTAT;
1800 else if (!strcmp(arg, "--check"))
1801 options->output_format |= DIFF_FORMAT_CHECKDIFF;
1802 else if (!strcmp(arg, "--summary"))
1803 options->output_format |= DIFF_FORMAT_SUMMARY;
1804 else if (!strcmp(arg, "--patch-with-stat")) {
1805 options->output_format |= DIFF_FORMAT_PATCH | DIFF_FORMAT_DIFFSTAT;
1807 else if (!strcmp(arg, "-z"))
1808 options->line_termination = 0;
1809 else if (!strncmp(arg, "-l", 2))
1810 options->rename_limit = strtoul(arg+2, NULL, 10);
1811 else if (!strcmp(arg, "--full-index"))
1812 options->full_index = 1;
1813 else if (!strcmp(arg, "--binary")) {
1814 options->output_format |= DIFF_FORMAT_PATCH;
1815 options->full_index = options->binary = 1;
1817 else if (!strcmp(arg, "-a") || !strcmp(arg, "--text")) {
1818 options->text = 1;
1820 else if (!strcmp(arg, "--name-only"))
1821 options->output_format |= DIFF_FORMAT_NAME;
1822 else if (!strcmp(arg, "--name-status"))
1823 options->output_format |= DIFF_FORMAT_NAME_STATUS;
1824 else if (!strcmp(arg, "-R"))
1825 options->reverse_diff = 1;
1826 else if (!strncmp(arg, "-S", 2))
1827 options->pickaxe = arg + 2;
1828 else if (!strcmp(arg, "-s")) {
1829 options->output_format |= DIFF_FORMAT_NO_OUTPUT;
1831 else if (!strncmp(arg, "-O", 2))
1832 options->orderfile = arg + 2;
1833 else if (!strncmp(arg, "--diff-filter=", 14))
1834 options->filter = arg + 14;
1835 else if (!strcmp(arg, "--pickaxe-all"))
1836 options->pickaxe_opts = DIFF_PICKAXE_ALL;
1837 else if (!strcmp(arg, "--pickaxe-regex"))
1838 options->pickaxe_opts = DIFF_PICKAXE_REGEX;
1839 else if (!strncmp(arg, "-B", 2)) {
1840 if ((options->break_opt =
1841 diff_scoreopt_parse(arg)) == -1)
1842 return -1;
1844 else if (!strncmp(arg, "-M", 2)) {
1845 if ((options->rename_score =
1846 diff_scoreopt_parse(arg)) == -1)
1847 return -1;
1848 options->detect_rename = DIFF_DETECT_RENAME;
1850 else if (!strncmp(arg, "-C", 2)) {
1851 if ((options->rename_score =
1852 diff_scoreopt_parse(arg)) == -1)
1853 return -1;
1854 options->detect_rename = DIFF_DETECT_COPY;
1856 else if (!strcmp(arg, "--find-copies-harder"))
1857 options->find_copies_harder = 1;
1858 else if (!strcmp(arg, "--abbrev"))
1859 options->abbrev = DEFAULT_ABBREV;
1860 else if (!strncmp(arg, "--abbrev=", 9)) {
1861 options->abbrev = strtoul(arg + 9, NULL, 10);
1862 if (options->abbrev < MINIMUM_ABBREV)
1863 options->abbrev = MINIMUM_ABBREV;
1864 else if (40 < options->abbrev)
1865 options->abbrev = 40;
1867 else if (!strcmp(arg, "--color"))
1868 options->color_diff = 1;
1869 else if (!strcmp(arg, "--no-color"))
1870 options->color_diff = 0;
1871 else if (!strcmp(arg, "-w") || !strcmp(arg, "--ignore-all-space"))
1872 options->xdl_opts |= XDF_IGNORE_WHITESPACE;
1873 else if (!strcmp(arg, "-b") || !strcmp(arg, "--ignore-space-change"))
1874 options->xdl_opts |= XDF_IGNORE_WHITESPACE_CHANGE;
1875 else if (!strcmp(arg, "--color-words"))
1876 options->color_diff = options->color_diff_words = 1;
1877 else if (!strcmp(arg, "--no-renames"))
1878 options->detect_rename = 0;
1879 else
1880 return 0;
1881 return 1;
1884 static int parse_num(const char **cp_p)
1886 unsigned long num, scale;
1887 int ch, dot;
1888 const char *cp = *cp_p;
1890 num = 0;
1891 scale = 1;
1892 dot = 0;
1893 for(;;) {
1894 ch = *cp;
1895 if ( !dot && ch == '.' ) {
1896 scale = 1;
1897 dot = 1;
1898 } else if ( ch == '%' ) {
1899 scale = dot ? scale*100 : 100;
1900 cp++; /* % is always at the end */
1901 break;
1902 } else if ( ch >= '0' && ch <= '9' ) {
1903 if ( scale < 100000 ) {
1904 scale *= 10;
1905 num = (num*10) + (ch-'0');
1907 } else {
1908 break;
1910 cp++;
1912 *cp_p = cp;
1914 /* user says num divided by scale and we say internally that
1915 * is MAX_SCORE * num / scale.
1917 return (num >= scale) ? MAX_SCORE : (MAX_SCORE * num / scale);
1920 int diff_scoreopt_parse(const char *opt)
1922 int opt1, opt2, cmd;
1924 if (*opt++ != '-')
1925 return -1;
1926 cmd = *opt++;
1927 if (cmd != 'M' && cmd != 'C' && cmd != 'B')
1928 return -1; /* that is not a -M, -C nor -B option */
1930 opt1 = parse_num(&opt);
1931 if (cmd != 'B')
1932 opt2 = 0;
1933 else {
1934 if (*opt == 0)
1935 opt2 = 0;
1936 else if (*opt != '/')
1937 return -1; /* we expect -B80/99 or -B80 */
1938 else {
1939 opt++;
1940 opt2 = parse_num(&opt);
1943 if (*opt != 0)
1944 return -1;
1945 return opt1 | (opt2 << 16);
1948 struct diff_queue_struct diff_queued_diff;
1950 void diff_q(struct diff_queue_struct *queue, struct diff_filepair *dp)
1952 if (queue->alloc <= queue->nr) {
1953 queue->alloc = alloc_nr(queue->alloc);
1954 queue->queue = xrealloc(queue->queue,
1955 sizeof(dp) * queue->alloc);
1957 queue->queue[queue->nr++] = dp;
1960 struct diff_filepair *diff_queue(struct diff_queue_struct *queue,
1961 struct diff_filespec *one,
1962 struct diff_filespec *two)
1964 struct diff_filepair *dp = xcalloc(1, sizeof(*dp));
1965 dp->one = one;
1966 dp->two = two;
1967 if (queue)
1968 diff_q(queue, dp);
1969 return dp;
1972 void diff_free_filepair(struct diff_filepair *p)
1974 diff_free_filespec_data(p->one);
1975 diff_free_filespec_data(p->two);
1976 free(p->one);
1977 free(p->two);
1978 free(p);
1981 /* This is different from find_unique_abbrev() in that
1982 * it stuffs the result with dots for alignment.
1984 const char *diff_unique_abbrev(const unsigned char *sha1, int len)
1986 int abblen;
1987 const char *abbrev;
1988 if (len == 40)
1989 return sha1_to_hex(sha1);
1991 abbrev = find_unique_abbrev(sha1, len);
1992 if (!abbrev)
1993 return sha1_to_hex(sha1);
1994 abblen = strlen(abbrev);
1995 if (abblen < 37) {
1996 static char hex[41];
1997 if (len < abblen && abblen <= len + 2)
1998 sprintf(hex, "%s%.*s", abbrev, len+3-abblen, "..");
1999 else
2000 sprintf(hex, "%s...", abbrev);
2001 return hex;
2003 return sha1_to_hex(sha1);
2006 static void diff_flush_raw(struct diff_filepair *p,
2007 struct diff_options *options)
2009 int two_paths;
2010 char status[10];
2011 int abbrev = options->abbrev;
2012 const char *path_one, *path_two;
2013 int inter_name_termination = '\t';
2014 int line_termination = options->line_termination;
2016 if (!line_termination)
2017 inter_name_termination = 0;
2019 path_one = p->one->path;
2020 path_two = p->two->path;
2021 if (line_termination) {
2022 path_one = quote_one(path_one);
2023 path_two = quote_one(path_two);
2026 if (p->score)
2027 sprintf(status, "%c%03d", p->status,
2028 (int)(0.5 + p->score * 100.0/MAX_SCORE));
2029 else {
2030 status[0] = p->status;
2031 status[1] = 0;
2033 switch (p->status) {
2034 case DIFF_STATUS_COPIED:
2035 case DIFF_STATUS_RENAMED:
2036 two_paths = 1;
2037 break;
2038 case DIFF_STATUS_ADDED:
2039 case DIFF_STATUS_DELETED:
2040 two_paths = 0;
2041 break;
2042 default:
2043 two_paths = 0;
2044 break;
2046 if (!(options->output_format & DIFF_FORMAT_NAME_STATUS)) {
2047 printf(":%06o %06o %s ",
2048 p->one->mode, p->two->mode,
2049 diff_unique_abbrev(p->one->sha1, abbrev));
2050 printf("%s ",
2051 diff_unique_abbrev(p->two->sha1, abbrev));
2053 printf("%s%c%s", status, inter_name_termination, path_one);
2054 if (two_paths)
2055 printf("%c%s", inter_name_termination, path_two);
2056 putchar(line_termination);
2057 if (path_one != p->one->path)
2058 free((void*)path_one);
2059 if (path_two != p->two->path)
2060 free((void*)path_two);
2063 static void diff_flush_name(struct diff_filepair *p, int line_termination)
2065 char *path = p->two->path;
2067 if (line_termination)
2068 path = quote_one(p->two->path);
2069 printf("%s%c", path, line_termination);
2070 if (p->two->path != path)
2071 free(path);
2074 int diff_unmodified_pair(struct diff_filepair *p)
2076 /* This function is written stricter than necessary to support
2077 * the currently implemented transformers, but the idea is to
2078 * let transformers to produce diff_filepairs any way they want,
2079 * and filter and clean them up here before producing the output.
2081 struct diff_filespec *one, *two;
2083 if (DIFF_PAIR_UNMERGED(p))
2084 return 0; /* unmerged is interesting */
2086 one = p->one;
2087 two = p->two;
2089 /* deletion, addition, mode or type change
2090 * and rename are all interesting.
2092 if (DIFF_FILE_VALID(one) != DIFF_FILE_VALID(two) ||
2093 DIFF_PAIR_MODE_CHANGED(p) ||
2094 strcmp(one->path, two->path))
2095 return 0;
2097 /* both are valid and point at the same path. that is, we are
2098 * dealing with a change.
2100 if (one->sha1_valid && two->sha1_valid &&
2101 !hashcmp(one->sha1, two->sha1))
2102 return 1; /* no change */
2103 if (!one->sha1_valid && !two->sha1_valid)
2104 return 1; /* both look at the same file on the filesystem. */
2105 return 0;
2108 static void diff_flush_patch(struct diff_filepair *p, struct diff_options *o)
2110 if (diff_unmodified_pair(p))
2111 return;
2113 if ((DIFF_FILE_VALID(p->one) && S_ISDIR(p->one->mode)) ||
2114 (DIFF_FILE_VALID(p->two) && S_ISDIR(p->two->mode)))
2115 return; /* no tree diffs in patch format */
2117 run_diff(p, o);
2120 static void diff_flush_stat(struct diff_filepair *p, struct diff_options *o,
2121 struct diffstat_t *diffstat)
2123 if (diff_unmodified_pair(p))
2124 return;
2126 if ((DIFF_FILE_VALID(p->one) && S_ISDIR(p->one->mode)) ||
2127 (DIFF_FILE_VALID(p->two) && S_ISDIR(p->two->mode)))
2128 return; /* no tree diffs in patch format */
2130 run_diffstat(p, o, diffstat);
2133 static void diff_flush_checkdiff(struct diff_filepair *p,
2134 struct diff_options *o)
2136 if (diff_unmodified_pair(p))
2137 return;
2139 if ((DIFF_FILE_VALID(p->one) && S_ISDIR(p->one->mode)) ||
2140 (DIFF_FILE_VALID(p->two) && S_ISDIR(p->two->mode)))
2141 return; /* no tree diffs in patch format */
2143 run_checkdiff(p, o);
2146 int diff_queue_is_empty(void)
2148 struct diff_queue_struct *q = &diff_queued_diff;
2149 int i;
2150 for (i = 0; i < q->nr; i++)
2151 if (!diff_unmodified_pair(q->queue[i]))
2152 return 0;
2153 return 1;
2156 #if DIFF_DEBUG
2157 void diff_debug_filespec(struct diff_filespec *s, int x, const char *one)
2159 fprintf(stderr, "queue[%d] %s (%s) %s %06o %s\n",
2160 x, one ? one : "",
2161 s->path,
2162 DIFF_FILE_VALID(s) ? "valid" : "invalid",
2163 s->mode,
2164 s->sha1_valid ? sha1_to_hex(s->sha1) : "");
2165 fprintf(stderr, "queue[%d] %s size %lu flags %d\n",
2166 x, one ? one : "",
2167 s->size, s->xfrm_flags);
2170 void diff_debug_filepair(const struct diff_filepair *p, int i)
2172 diff_debug_filespec(p->one, i, "one");
2173 diff_debug_filespec(p->two, i, "two");
2174 fprintf(stderr, "score %d, status %c stays %d broken %d\n",
2175 p->score, p->status ? p->status : '?',
2176 p->source_stays, p->broken_pair);
2179 void diff_debug_queue(const char *msg, struct diff_queue_struct *q)
2181 int i;
2182 if (msg)
2183 fprintf(stderr, "%s\n", msg);
2184 fprintf(stderr, "q->nr = %d\n", q->nr);
2185 for (i = 0; i < q->nr; i++) {
2186 struct diff_filepair *p = q->queue[i];
2187 diff_debug_filepair(p, i);
2190 #endif
2192 static void diff_resolve_rename_copy(void)
2194 int i, j;
2195 struct diff_filepair *p, *pp;
2196 struct diff_queue_struct *q = &diff_queued_diff;
2198 diff_debug_queue("resolve-rename-copy", q);
2200 for (i = 0; i < q->nr; i++) {
2201 p = q->queue[i];
2202 p->status = 0; /* undecided */
2203 if (DIFF_PAIR_UNMERGED(p))
2204 p->status = DIFF_STATUS_UNMERGED;
2205 else if (!DIFF_FILE_VALID(p->one))
2206 p->status = DIFF_STATUS_ADDED;
2207 else if (!DIFF_FILE_VALID(p->two))
2208 p->status = DIFF_STATUS_DELETED;
2209 else if (DIFF_PAIR_TYPE_CHANGED(p))
2210 p->status = DIFF_STATUS_TYPE_CHANGED;
2212 /* from this point on, we are dealing with a pair
2213 * whose both sides are valid and of the same type, i.e.
2214 * either in-place edit or rename/copy edit.
2216 else if (DIFF_PAIR_RENAME(p)) {
2217 if (p->source_stays) {
2218 p->status = DIFF_STATUS_COPIED;
2219 continue;
2221 /* See if there is some other filepair that
2222 * copies from the same source as us. If so
2223 * we are a copy. Otherwise we are either a
2224 * copy if the path stays, or a rename if it
2225 * does not, but we already handled "stays" case.
2227 for (j = i + 1; j < q->nr; j++) {
2228 pp = q->queue[j];
2229 if (strcmp(pp->one->path, p->one->path))
2230 continue; /* not us */
2231 if (!DIFF_PAIR_RENAME(pp))
2232 continue; /* not a rename/copy */
2233 /* pp is a rename/copy from the same source */
2234 p->status = DIFF_STATUS_COPIED;
2235 break;
2237 if (!p->status)
2238 p->status = DIFF_STATUS_RENAMED;
2240 else if (hashcmp(p->one->sha1, p->two->sha1) ||
2241 p->one->mode != p->two->mode)
2242 p->status = DIFF_STATUS_MODIFIED;
2243 else {
2244 /* This is a "no-change" entry and should not
2245 * happen anymore, but prepare for broken callers.
2247 error("feeding unmodified %s to diffcore",
2248 p->one->path);
2249 p->status = DIFF_STATUS_UNKNOWN;
2252 diff_debug_queue("resolve-rename-copy done", q);
2255 static int check_pair_status(struct diff_filepair *p)
2257 switch (p->status) {
2258 case DIFF_STATUS_UNKNOWN:
2259 return 0;
2260 case 0:
2261 die("internal error in diff-resolve-rename-copy");
2262 default:
2263 return 1;
2267 static void flush_one_pair(struct diff_filepair *p, struct diff_options *opt)
2269 int fmt = opt->output_format;
2271 if (fmt & DIFF_FORMAT_CHECKDIFF)
2272 diff_flush_checkdiff(p, opt);
2273 else if (fmt & (DIFF_FORMAT_RAW | DIFF_FORMAT_NAME_STATUS))
2274 diff_flush_raw(p, opt);
2275 else if (fmt & DIFF_FORMAT_NAME)
2276 diff_flush_name(p, opt->line_termination);
2279 static void show_file_mode_name(const char *newdelete, struct diff_filespec *fs)
2281 if (fs->mode)
2282 printf(" %s mode %06o %s\n", newdelete, fs->mode, fs->path);
2283 else
2284 printf(" %s %s\n", newdelete, fs->path);
2288 static void show_mode_change(struct diff_filepair *p, int show_name)
2290 if (p->one->mode && p->two->mode && p->one->mode != p->two->mode) {
2291 if (show_name)
2292 printf(" mode change %06o => %06o %s\n",
2293 p->one->mode, p->two->mode, p->two->path);
2294 else
2295 printf(" mode change %06o => %06o\n",
2296 p->one->mode, p->two->mode);
2300 static void show_rename_copy(const char *renamecopy, struct diff_filepair *p)
2302 const char *old, *new;
2304 /* Find common prefix */
2305 old = p->one->path;
2306 new = p->two->path;
2307 while (1) {
2308 const char *slash_old, *slash_new;
2309 slash_old = strchr(old, '/');
2310 slash_new = strchr(new, '/');
2311 if (!slash_old ||
2312 !slash_new ||
2313 slash_old - old != slash_new - new ||
2314 memcmp(old, new, slash_new - new))
2315 break;
2316 old = slash_old + 1;
2317 new = slash_new + 1;
2319 /* p->one->path thru old is the common prefix, and old and new
2320 * through the end of names are renames
2322 if (old != p->one->path)
2323 printf(" %s %.*s{%s => %s} (%d%%)\n", renamecopy,
2324 (int)(old - p->one->path), p->one->path,
2325 old, new, (int)(0.5 + p->score * 100.0/MAX_SCORE));
2326 else
2327 printf(" %s %s => %s (%d%%)\n", renamecopy,
2328 p->one->path, p->two->path,
2329 (int)(0.5 + p->score * 100.0/MAX_SCORE));
2330 show_mode_change(p, 0);
2333 static void diff_summary(struct diff_filepair *p)
2335 switch(p->status) {
2336 case DIFF_STATUS_DELETED:
2337 show_file_mode_name("delete", p->one);
2338 break;
2339 case DIFF_STATUS_ADDED:
2340 show_file_mode_name("create", p->two);
2341 break;
2342 case DIFF_STATUS_COPIED:
2343 show_rename_copy("copy", p);
2344 break;
2345 case DIFF_STATUS_RENAMED:
2346 show_rename_copy("rename", p);
2347 break;
2348 default:
2349 if (p->score) {
2350 printf(" rewrite %s (%d%%)\n", p->two->path,
2351 (int)(0.5 + p->score * 100.0/MAX_SCORE));
2352 show_mode_change(p, 0);
2353 } else show_mode_change(p, 1);
2354 break;
2358 struct patch_id_t {
2359 struct xdiff_emit_state xm;
2360 SHA_CTX *ctx;
2361 int patchlen;
2364 static int remove_space(char *line, int len)
2366 int i;
2367 char *dst = line;
2368 unsigned char c;
2370 for (i = 0; i < len; i++)
2371 if (!isspace((c = line[i])))
2372 *dst++ = c;
2374 return dst - line;
2377 static void patch_id_consume(void *priv, char *line, unsigned long len)
2379 struct patch_id_t *data = priv;
2380 int new_len;
2382 /* Ignore line numbers when computing the SHA1 of the patch */
2383 if (!strncmp(line, "@@ -", 4))
2384 return;
2386 new_len = remove_space(line, len);
2388 SHA1_Update(data->ctx, line, new_len);
2389 data->patchlen += new_len;
2392 /* returns 0 upon success, and writes result into sha1 */
2393 static int diff_get_patch_id(struct diff_options *options, unsigned char *sha1)
2395 struct diff_queue_struct *q = &diff_queued_diff;
2396 int i;
2397 SHA_CTX ctx;
2398 struct patch_id_t data;
2399 char buffer[PATH_MAX * 4 + 20];
2401 SHA1_Init(&ctx);
2402 memset(&data, 0, sizeof(struct patch_id_t));
2403 data.ctx = &ctx;
2404 data.xm.consume = patch_id_consume;
2406 for (i = 0; i < q->nr; i++) {
2407 xpparam_t xpp;
2408 xdemitconf_t xecfg;
2409 xdemitcb_t ecb;
2410 mmfile_t mf1, mf2;
2411 struct diff_filepair *p = q->queue[i];
2412 int len1, len2;
2414 if (p->status == 0)
2415 return error("internal diff status error");
2416 if (p->status == DIFF_STATUS_UNKNOWN)
2417 continue;
2418 if (diff_unmodified_pair(p))
2419 continue;
2420 if ((DIFF_FILE_VALID(p->one) && S_ISDIR(p->one->mode)) ||
2421 (DIFF_FILE_VALID(p->two) && S_ISDIR(p->two->mode)))
2422 continue;
2423 if (DIFF_PAIR_UNMERGED(p))
2424 continue;
2426 diff_fill_sha1_info(p->one);
2427 diff_fill_sha1_info(p->two);
2428 if (fill_mmfile(&mf1, p->one) < 0 ||
2429 fill_mmfile(&mf2, p->two) < 0)
2430 return error("unable to read files to diff");
2432 /* Maybe hash p->two? into the patch id? */
2433 if (mmfile_is_binary(&mf2))
2434 continue;
2436 len1 = remove_space(p->one->path, strlen(p->one->path));
2437 len2 = remove_space(p->two->path, strlen(p->two->path));
2438 if (p->one->mode == 0)
2439 len1 = snprintf(buffer, sizeof(buffer),
2440 "diff--gita/%.*sb/%.*s"
2441 "newfilemode%06o"
2442 "---/dev/null"
2443 "+++b/%.*s",
2444 len1, p->one->path,
2445 len2, p->two->path,
2446 p->two->mode,
2447 len2, p->two->path);
2448 else if (p->two->mode == 0)
2449 len1 = snprintf(buffer, sizeof(buffer),
2450 "diff--gita/%.*sb/%.*s"
2451 "deletedfilemode%06o"
2452 "---a/%.*s"
2453 "+++/dev/null",
2454 len1, p->one->path,
2455 len2, p->two->path,
2456 p->one->mode,
2457 len1, p->one->path);
2458 else
2459 len1 = snprintf(buffer, sizeof(buffer),
2460 "diff--gita/%.*sb/%.*s"
2461 "---a/%.*s"
2462 "+++b/%.*s",
2463 len1, p->one->path,
2464 len2, p->two->path,
2465 len1, p->one->path,
2466 len2, p->two->path);
2467 SHA1_Update(&ctx, buffer, len1);
2469 xpp.flags = XDF_NEED_MINIMAL;
2470 xecfg.ctxlen = 3;
2471 xecfg.flags = XDL_EMIT_FUNCNAMES;
2472 ecb.outf = xdiff_outf;
2473 ecb.priv = &data;
2474 xdl_diff(&mf1, &mf2, &xpp, &xecfg, &ecb);
2477 SHA1_Final(sha1, &ctx);
2478 return 0;
2481 int diff_flush_patch_id(struct diff_options *options, unsigned char *sha1)
2483 struct diff_queue_struct *q = &diff_queued_diff;
2484 int i;
2485 int result = diff_get_patch_id(options, sha1);
2487 for (i = 0; i < q->nr; i++)
2488 diff_free_filepair(q->queue[i]);
2490 free(q->queue);
2491 q->queue = NULL;
2492 q->nr = q->alloc = 0;
2494 return result;
2497 static int is_summary_empty(const struct diff_queue_struct *q)
2499 int i;
2501 for (i = 0; i < q->nr; i++) {
2502 const struct diff_filepair *p = q->queue[i];
2504 switch (p->status) {
2505 case DIFF_STATUS_DELETED:
2506 case DIFF_STATUS_ADDED:
2507 case DIFF_STATUS_COPIED:
2508 case DIFF_STATUS_RENAMED:
2509 return 0;
2510 default:
2511 if (p->score)
2512 return 0;
2513 if (p->one->mode && p->two->mode &&
2514 p->one->mode != p->two->mode)
2515 return 0;
2516 break;
2519 return 1;
2522 void diff_flush(struct diff_options *options)
2524 struct diff_queue_struct *q = &diff_queued_diff;
2525 int i, output_format = options->output_format;
2526 int separator = 0;
2529 * Order: raw, stat, summary, patch
2530 * or: name/name-status/checkdiff (other bits clear)
2532 if (!q->nr)
2533 goto free_queue;
2535 if (output_format & (DIFF_FORMAT_RAW |
2536 DIFF_FORMAT_NAME |
2537 DIFF_FORMAT_NAME_STATUS |
2538 DIFF_FORMAT_CHECKDIFF)) {
2539 for (i = 0; i < q->nr; i++) {
2540 struct diff_filepair *p = q->queue[i];
2541 if (check_pair_status(p))
2542 flush_one_pair(p, options);
2544 separator++;
2547 if (output_format & DIFF_FORMAT_DIFFSTAT) {
2548 struct diffstat_t diffstat;
2550 memset(&diffstat, 0, sizeof(struct diffstat_t));
2551 diffstat.xm.consume = diffstat_consume;
2552 for (i = 0; i < q->nr; i++) {
2553 struct diff_filepair *p = q->queue[i];
2554 if (check_pair_status(p))
2555 diff_flush_stat(p, options, &diffstat);
2557 show_stats(&diffstat);
2558 separator++;
2561 if (output_format & DIFF_FORMAT_SUMMARY && !is_summary_empty(q)) {
2562 for (i = 0; i < q->nr; i++)
2563 diff_summary(q->queue[i]);
2564 separator++;
2567 if (output_format & DIFF_FORMAT_PATCH) {
2568 if (separator) {
2569 if (options->stat_sep) {
2570 /* attach patch instead of inline */
2571 fputs(options->stat_sep, stdout);
2572 } else {
2573 putchar(options->line_termination);
2577 for (i = 0; i < q->nr; i++) {
2578 struct diff_filepair *p = q->queue[i];
2579 if (check_pair_status(p))
2580 diff_flush_patch(p, options);
2584 for (i = 0; i < q->nr; i++)
2585 diff_free_filepair(q->queue[i]);
2586 free_queue:
2587 free(q->queue);
2588 q->queue = NULL;
2589 q->nr = q->alloc = 0;
2592 static void diffcore_apply_filter(const char *filter)
2594 int i;
2595 struct diff_queue_struct *q = &diff_queued_diff;
2596 struct diff_queue_struct outq;
2597 outq.queue = NULL;
2598 outq.nr = outq.alloc = 0;
2600 if (!filter)
2601 return;
2603 if (strchr(filter, DIFF_STATUS_FILTER_AON)) {
2604 int found;
2605 for (i = found = 0; !found && i < q->nr; i++) {
2606 struct diff_filepair *p = q->queue[i];
2607 if (((p->status == DIFF_STATUS_MODIFIED) &&
2608 ((p->score &&
2609 strchr(filter, DIFF_STATUS_FILTER_BROKEN)) ||
2610 (!p->score &&
2611 strchr(filter, DIFF_STATUS_MODIFIED)))) ||
2612 ((p->status != DIFF_STATUS_MODIFIED) &&
2613 strchr(filter, p->status)))
2614 found++;
2616 if (found)
2617 return;
2619 /* otherwise we will clear the whole queue
2620 * by copying the empty outq at the end of this
2621 * function, but first clear the current entries
2622 * in the queue.
2624 for (i = 0; i < q->nr; i++)
2625 diff_free_filepair(q->queue[i]);
2627 else {
2628 /* Only the matching ones */
2629 for (i = 0; i < q->nr; i++) {
2630 struct diff_filepair *p = q->queue[i];
2632 if (((p->status == DIFF_STATUS_MODIFIED) &&
2633 ((p->score &&
2634 strchr(filter, DIFF_STATUS_FILTER_BROKEN)) ||
2635 (!p->score &&
2636 strchr(filter, DIFF_STATUS_MODIFIED)))) ||
2637 ((p->status != DIFF_STATUS_MODIFIED) &&
2638 strchr(filter, p->status)))
2639 diff_q(&outq, p);
2640 else
2641 diff_free_filepair(p);
2644 free(q->queue);
2645 *q = outq;
2648 void diffcore_std(struct diff_options *options)
2650 if (options->break_opt != -1)
2651 diffcore_break(options->break_opt);
2652 if (options->detect_rename)
2653 diffcore_rename(options);
2654 if (options->break_opt != -1)
2655 diffcore_merge_broken();
2656 if (options->pickaxe)
2657 diffcore_pickaxe(options->pickaxe, options->pickaxe_opts);
2658 if (options->orderfile)
2659 diffcore_order(options->orderfile);
2660 diff_resolve_rename_copy();
2661 diffcore_apply_filter(options->filter);
2665 void diffcore_std_no_resolve(struct diff_options *options)
2667 if (options->pickaxe)
2668 diffcore_pickaxe(options->pickaxe, options->pickaxe_opts);
2669 if (options->orderfile)
2670 diffcore_order(options->orderfile);
2671 diffcore_apply_filter(options->filter);
2674 void diff_addremove(struct diff_options *options,
2675 int addremove, unsigned mode,
2676 const unsigned char *sha1,
2677 const char *base, const char *path)
2679 char concatpath[PATH_MAX];
2680 struct diff_filespec *one, *two;
2682 /* This may look odd, but it is a preparation for
2683 * feeding "there are unchanged files which should
2684 * not produce diffs, but when you are doing copy
2685 * detection you would need them, so here they are"
2686 * entries to the diff-core. They will be prefixed
2687 * with something like '=' or '*' (I haven't decided
2688 * which but should not make any difference).
2689 * Feeding the same new and old to diff_change()
2690 * also has the same effect.
2691 * Before the final output happens, they are pruned after
2692 * merged into rename/copy pairs as appropriate.
2694 if (options->reverse_diff)
2695 addremove = (addremove == '+' ? '-' :
2696 addremove == '-' ? '+' : addremove);
2698 if (!path) path = "";
2699 sprintf(concatpath, "%s%s", base, path);
2700 one = alloc_filespec(concatpath);
2701 two = alloc_filespec(concatpath);
2703 if (addremove != '+')
2704 fill_filespec(one, sha1, mode);
2705 if (addremove != '-')
2706 fill_filespec(two, sha1, mode);
2708 diff_queue(&diff_queued_diff, one, two);
2711 void diff_change(struct diff_options *options,
2712 unsigned old_mode, unsigned new_mode,
2713 const unsigned char *old_sha1,
2714 const unsigned char *new_sha1,
2715 const char *base, const char *path)
2717 char concatpath[PATH_MAX];
2718 struct diff_filespec *one, *two;
2720 if (options->reverse_diff) {
2721 unsigned tmp;
2722 const unsigned char *tmp_c;
2723 tmp = old_mode; old_mode = new_mode; new_mode = tmp;
2724 tmp_c = old_sha1; old_sha1 = new_sha1; new_sha1 = tmp_c;
2726 if (!path) path = "";
2727 sprintf(concatpath, "%s%s", base, path);
2728 one = alloc_filespec(concatpath);
2729 two = alloc_filespec(concatpath);
2730 fill_filespec(one, old_sha1, old_mode);
2731 fill_filespec(two, new_sha1, new_mode);
2733 diff_queue(&diff_queued_diff, one, two);
2736 void diff_unmerge(struct diff_options *options,
2737 const char *path)
2739 struct diff_filespec *one, *two;
2740 one = alloc_filespec(path);
2741 two = alloc_filespec(path);
2742 diff_queue(&diff_queued_diff, one, two);