Merge branch 'master' of git://git.kernel.org/pub/scm/git/git
[git/jnareb-git.git] / diff.c
bloba89bca91726af61ddc77d202372792457e5d1c36
1 /*
2 * Copyright (C) 2005 Junio C Hamano
3 */
4 #include "cache.h"
5 #include "quote.h"
6 #include "diff.h"
7 #include "diffcore.h"
8 #include "delta.h"
9 #include "xdiff-interface.h"
10 #include "color.h"
11 #include "attr.h"
13 #ifdef NO_FAST_WORKING_DIRECTORY
14 #define FAST_WORKING_DIRECTORY 0
15 #else
16 #define FAST_WORKING_DIRECTORY 1
17 #endif
19 static int diff_detect_rename_default;
20 static int diff_rename_limit_default = -1;
21 static int diff_use_color_default;
23 static char diff_colors[][COLOR_MAXLEN] = {
24 "\033[m", /* reset */
25 "", /* PLAIN (normal) */
26 "\033[1m", /* METAINFO (bold) */
27 "\033[36m", /* FRAGINFO (cyan) */
28 "\033[31m", /* OLD (red) */
29 "\033[32m", /* NEW (green) */
30 "\033[33m", /* COMMIT (yellow) */
31 "\033[41m", /* WHITESPACE (red background) */
34 static int parse_diff_color_slot(const char *var, int ofs)
36 if (!strcasecmp(var+ofs, "plain"))
37 return DIFF_PLAIN;
38 if (!strcasecmp(var+ofs, "meta"))
39 return DIFF_METAINFO;
40 if (!strcasecmp(var+ofs, "frag"))
41 return DIFF_FRAGINFO;
42 if (!strcasecmp(var+ofs, "old"))
43 return DIFF_FILE_OLD;
44 if (!strcasecmp(var+ofs, "new"))
45 return DIFF_FILE_NEW;
46 if (!strcasecmp(var+ofs, "commit"))
47 return DIFF_COMMIT;
48 if (!strcasecmp(var+ofs, "whitespace"))
49 return DIFF_WHITESPACE;
50 die("bad config variable '%s'", var);
53 static struct ll_diff_driver {
54 const char *name;
55 struct ll_diff_driver *next;
56 char *cmd;
57 } *user_diff, **user_diff_tail;
60 * Currently there is only "diff.<drivername>.command" variable;
61 * because there are "diff.color.<slot>" variables, we are parsing
62 * this in a bit convoluted way to allow low level diff driver
63 * called "color".
65 static int parse_lldiff_command(const char *var, const char *ep, const char *value)
67 const char *name;
68 int namelen;
69 struct ll_diff_driver *drv;
71 name = var + 5;
72 namelen = ep - name;
73 for (drv = user_diff; drv; drv = drv->next)
74 if (!strncmp(drv->name, name, namelen) && !drv->name[namelen])
75 break;
76 if (!drv) {
77 char *namebuf;
78 drv = xcalloc(1, sizeof(struct ll_diff_driver));
79 namebuf = xmalloc(namelen + 1);
80 memcpy(namebuf, name, namelen);
81 namebuf[namelen] = 0;
82 drv->name = namebuf;
83 drv->next = NULL;
84 if (!user_diff_tail)
85 user_diff_tail = &user_diff;
86 *user_diff_tail = drv;
87 user_diff_tail = &(drv->next);
90 if (!value)
91 return error("%s: lacks value", var);
92 drv->cmd = strdup(value);
93 return 0;
97 * These are to give UI layer defaults.
98 * The core-level commands such as git-diff-files should
99 * never be affected by the setting of diff.renames
100 * the user happens to have in the configuration file.
102 int git_diff_ui_config(const char *var, const char *value)
104 if (!strcmp(var, "diff.renamelimit")) {
105 diff_rename_limit_default = git_config_int(var, value);
106 return 0;
108 if (!strcmp(var, "diff.color") || !strcmp(var, "color.diff")) {
109 diff_use_color_default = git_config_colorbool(var, value);
110 return 0;
112 if (!strcmp(var, "diff.renames")) {
113 if (!value)
114 diff_detect_rename_default = DIFF_DETECT_RENAME;
115 else if (!strcasecmp(value, "copies") ||
116 !strcasecmp(value, "copy"))
117 diff_detect_rename_default = DIFF_DETECT_COPY;
118 else if (git_config_bool(var,value))
119 diff_detect_rename_default = DIFF_DETECT_RENAME;
120 return 0;
122 if (!prefixcmp(var, "diff.")) {
123 const char *ep = strrchr(var, '.');
125 if (ep != var + 4 && !strcmp(ep, ".command"))
126 return parse_lldiff_command(var, ep, value);
128 if (!prefixcmp(var, "diff.color.") || !prefixcmp(var, "color.diff.")) {
129 int slot = parse_diff_color_slot(var, 11);
130 color_parse(value, var, diff_colors[slot]);
131 return 0;
134 return git_default_config(var, value);
137 static char *quote_one(const char *str)
139 int needlen;
140 char *xp;
142 if (!str)
143 return NULL;
144 needlen = quote_c_style(str, NULL, NULL, 0);
145 if (!needlen)
146 return xstrdup(str);
147 xp = xmalloc(needlen + 1);
148 quote_c_style(str, xp, NULL, 0);
149 return xp;
152 static char *quote_two(const char *one, const char *two)
154 int need_one = quote_c_style(one, NULL, NULL, 1);
155 int need_two = quote_c_style(two, NULL, NULL, 1);
156 char *xp;
158 if (need_one + need_two) {
159 if (!need_one) need_one = strlen(one);
160 if (!need_two) need_one = strlen(two);
162 xp = xmalloc(need_one + need_two + 3);
163 xp[0] = '"';
164 quote_c_style(one, xp + 1, NULL, 1);
165 quote_c_style(two, xp + need_one + 1, NULL, 1);
166 strcpy(xp + need_one + need_two + 1, "\"");
167 return xp;
169 need_one = strlen(one);
170 need_two = strlen(two);
171 xp = xmalloc(need_one + need_two + 1);
172 strcpy(xp, one);
173 strcpy(xp + need_one, two);
174 return xp;
177 static const char *external_diff(void)
179 static const char *external_diff_cmd = NULL;
180 static int done_preparing = 0;
182 if (done_preparing)
183 return external_diff_cmd;
184 external_diff_cmd = getenv("GIT_EXTERNAL_DIFF");
185 done_preparing = 1;
186 return external_diff_cmd;
189 static struct diff_tempfile {
190 const char *name; /* filename external diff should read from */
191 char hex[41];
192 char mode[10];
193 char tmp_path[PATH_MAX];
194 } diff_temp[2];
196 static int count_lines(const char *data, int size)
198 int count, ch, completely_empty = 1, nl_just_seen = 0;
199 count = 0;
200 while (0 < size--) {
201 ch = *data++;
202 if (ch == '\n') {
203 count++;
204 nl_just_seen = 1;
205 completely_empty = 0;
207 else {
208 nl_just_seen = 0;
209 completely_empty = 0;
212 if (completely_empty)
213 return 0;
214 if (!nl_just_seen)
215 count++; /* no trailing newline */
216 return count;
219 static void print_line_count(int count)
221 switch (count) {
222 case 0:
223 printf("0,0");
224 break;
225 case 1:
226 printf("1");
227 break;
228 default:
229 printf("1,%d", count);
230 break;
234 static void copy_file(int prefix, const char *data, int size,
235 const char *set, const char *reset)
237 int ch, nl_just_seen = 1;
238 while (0 < size--) {
239 ch = *data++;
240 if (nl_just_seen) {
241 fputs(set, stdout);
242 putchar(prefix);
244 if (ch == '\n') {
245 nl_just_seen = 1;
246 fputs(reset, stdout);
247 } else
248 nl_just_seen = 0;
249 putchar(ch);
251 if (!nl_just_seen)
252 printf("%s\n\\ No newline at end of file\n", reset);
255 static void emit_rewrite_diff(const char *name_a,
256 const char *name_b,
257 struct diff_filespec *one,
258 struct diff_filespec *two,
259 int color_diff)
261 int lc_a, lc_b;
262 const char *name_a_tab, *name_b_tab;
263 const char *metainfo = diff_get_color(color_diff, DIFF_METAINFO);
264 const char *fraginfo = diff_get_color(color_diff, DIFF_FRAGINFO);
265 const char *old = diff_get_color(color_diff, DIFF_FILE_OLD);
266 const char *new = diff_get_color(color_diff, DIFF_FILE_NEW);
267 const char *reset = diff_get_color(color_diff, DIFF_RESET);
269 name_a += (*name_a == '/');
270 name_b += (*name_b == '/');
271 name_a_tab = strchr(name_a, ' ') ? "\t" : "";
272 name_b_tab = strchr(name_b, ' ') ? "\t" : "";
274 diff_populate_filespec(one, 0);
275 diff_populate_filespec(two, 0);
276 lc_a = count_lines(one->data, one->size);
277 lc_b = count_lines(two->data, two->size);
278 printf("%s--- a/%s%s%s\n%s+++ b/%s%s%s\n%s@@ -",
279 metainfo, name_a, name_a_tab, reset,
280 metainfo, name_b, name_b_tab, reset, fraginfo);
281 print_line_count(lc_a);
282 printf(" +");
283 print_line_count(lc_b);
284 printf(" @@%s\n", reset);
285 if (lc_a)
286 copy_file('-', one->data, one->size, old, reset);
287 if (lc_b)
288 copy_file('+', two->data, two->size, new, reset);
291 static int fill_mmfile(mmfile_t *mf, struct diff_filespec *one)
293 if (!DIFF_FILE_VALID(one)) {
294 mf->ptr = (char *)""; /* does not matter */
295 mf->size = 0;
296 return 0;
298 else if (diff_populate_filespec(one, 0))
299 return -1;
300 mf->ptr = one->data;
301 mf->size = one->size;
302 return 0;
305 struct diff_words_buffer {
306 mmfile_t text;
307 long alloc;
308 long current; /* output pointer */
309 int suppressed_newline;
312 static void diff_words_append(char *line, unsigned long len,
313 struct diff_words_buffer *buffer)
315 if (buffer->text.size + len > buffer->alloc) {
316 buffer->alloc = (buffer->text.size + len) * 3 / 2;
317 buffer->text.ptr = xrealloc(buffer->text.ptr, buffer->alloc);
319 line++;
320 len--;
321 memcpy(buffer->text.ptr + buffer->text.size, line, len);
322 buffer->text.size += len;
325 struct diff_words_data {
326 struct xdiff_emit_state xm;
327 struct diff_words_buffer minus, plus;
330 static void print_word(struct diff_words_buffer *buffer, int len, int color,
331 int suppress_newline)
333 const char *ptr;
334 int eol = 0;
336 if (len == 0)
337 return;
339 ptr = buffer->text.ptr + buffer->current;
340 buffer->current += len;
342 if (ptr[len - 1] == '\n') {
343 eol = 1;
344 len--;
347 fputs(diff_get_color(1, color), stdout);
348 fwrite(ptr, len, 1, stdout);
349 fputs(diff_get_color(1, DIFF_RESET), stdout);
351 if (eol) {
352 if (suppress_newline)
353 buffer->suppressed_newline = 1;
354 else
355 putchar('\n');
359 static void fn_out_diff_words_aux(void *priv, char *line, unsigned long len)
361 struct diff_words_data *diff_words = priv;
363 if (diff_words->minus.suppressed_newline) {
364 if (line[0] != '+')
365 putchar('\n');
366 diff_words->minus.suppressed_newline = 0;
369 len--;
370 switch (line[0]) {
371 case '-':
372 print_word(&diff_words->minus, len, DIFF_FILE_OLD, 1);
373 break;
374 case '+':
375 print_word(&diff_words->plus, len, DIFF_FILE_NEW, 0);
376 break;
377 case ' ':
378 print_word(&diff_words->plus, len, DIFF_PLAIN, 0);
379 diff_words->minus.current += len;
380 break;
384 /* this executes the word diff on the accumulated buffers */
385 static void diff_words_show(struct diff_words_data *diff_words)
387 xpparam_t xpp;
388 xdemitconf_t xecfg;
389 xdemitcb_t ecb;
390 mmfile_t minus, plus;
391 int i;
393 minus.size = diff_words->minus.text.size;
394 minus.ptr = xmalloc(minus.size);
395 memcpy(minus.ptr, diff_words->minus.text.ptr, minus.size);
396 for (i = 0; i < minus.size; i++)
397 if (isspace(minus.ptr[i]))
398 minus.ptr[i] = '\n';
399 diff_words->minus.current = 0;
401 plus.size = diff_words->plus.text.size;
402 plus.ptr = xmalloc(plus.size);
403 memcpy(plus.ptr, diff_words->plus.text.ptr, plus.size);
404 for (i = 0; i < plus.size; i++)
405 if (isspace(plus.ptr[i]))
406 plus.ptr[i] = '\n';
407 diff_words->plus.current = 0;
409 xpp.flags = XDF_NEED_MINIMAL;
410 xecfg.ctxlen = diff_words->minus.alloc + diff_words->plus.alloc;
411 xecfg.flags = 0;
412 ecb.outf = xdiff_outf;
413 ecb.priv = diff_words;
414 diff_words->xm.consume = fn_out_diff_words_aux;
415 xdl_diff(&minus, &plus, &xpp, &xecfg, &ecb);
417 free(minus.ptr);
418 free(plus.ptr);
419 diff_words->minus.text.size = diff_words->plus.text.size = 0;
421 if (diff_words->minus.suppressed_newline) {
422 putchar('\n');
423 diff_words->minus.suppressed_newline = 0;
427 struct emit_callback {
428 struct xdiff_emit_state xm;
429 int nparents, color_diff;
430 const char **label_path;
431 struct diff_words_data *diff_words;
432 int *found_changesp;
435 static void free_diff_words_data(struct emit_callback *ecbdata)
437 if (ecbdata->diff_words) {
438 /* flush buffers */
439 if (ecbdata->diff_words->minus.text.size ||
440 ecbdata->diff_words->plus.text.size)
441 diff_words_show(ecbdata->diff_words);
443 if (ecbdata->diff_words->minus.text.ptr)
444 free (ecbdata->diff_words->minus.text.ptr);
445 if (ecbdata->diff_words->plus.text.ptr)
446 free (ecbdata->diff_words->plus.text.ptr);
447 free(ecbdata->diff_words);
448 ecbdata->diff_words = NULL;
452 const char *diff_get_color(int diff_use_color, enum color_diff ix)
454 if (diff_use_color)
455 return diff_colors[ix];
456 return "";
459 static void emit_line(const char *set, const char *reset, const char *line, int len)
461 if (len > 0 && line[len-1] == '\n')
462 len--;
463 fputs(set, stdout);
464 fwrite(line, len, 1, stdout);
465 puts(reset);
468 static void emit_line_with_ws(int nparents,
469 const char *set, const char *reset, const char *ws,
470 const char *line, int len)
472 int col0 = nparents;
473 int last_tab_in_indent = -1;
474 int last_space_in_indent = -1;
475 int i;
476 int tail = len;
477 int need_highlight_leading_space = 0;
478 /* The line is a newly added line. Does it have funny leading
479 * whitespaces? In indent, SP should never precede a TAB.
481 for (i = col0; i < len; i++) {
482 if (line[i] == '\t') {
483 last_tab_in_indent = i;
484 if (0 <= last_space_in_indent)
485 need_highlight_leading_space = 1;
487 else if (line[i] == ' ')
488 last_space_in_indent = i;
489 else
490 break;
492 fputs(set, stdout);
493 fwrite(line, col0, 1, stdout);
494 fputs(reset, stdout);
495 if (((i == len) || line[i] == '\n') && i != col0) {
496 /* The whole line was indent */
497 emit_line(ws, reset, line + col0, len - col0);
498 return;
500 i = col0;
501 if (need_highlight_leading_space) {
502 while (i < last_tab_in_indent) {
503 if (line[i] == ' ') {
504 fputs(ws, stdout);
505 putchar(' ');
506 fputs(reset, stdout);
508 else
509 putchar(line[i]);
510 i++;
513 tail = len - 1;
514 if (line[tail] == '\n' && i < tail)
515 tail--;
516 while (i < tail) {
517 if (!isspace(line[tail]))
518 break;
519 tail--;
521 if ((i < tail && line[tail + 1] != '\n')) {
522 /* This has whitespace between tail+1..len */
523 fputs(set, stdout);
524 fwrite(line + i, tail - i + 1, 1, stdout);
525 fputs(reset, stdout);
526 emit_line(ws, reset, line + tail + 1, len - tail - 1);
528 else
529 emit_line(set, reset, line + i, len - i);
532 static void emit_add_line(const char *reset, struct emit_callback *ecbdata, const char *line, int len)
534 const char *ws = diff_get_color(ecbdata->color_diff, DIFF_WHITESPACE);
535 const char *set = diff_get_color(ecbdata->color_diff, DIFF_FILE_NEW);
537 if (!*ws)
538 emit_line(set, reset, line, len);
539 else
540 emit_line_with_ws(ecbdata->nparents, set, reset, ws,
541 line, len);
544 static void fn_out_consume(void *priv, char *line, unsigned long len)
546 int i;
547 int color;
548 struct emit_callback *ecbdata = priv;
549 const char *set = diff_get_color(ecbdata->color_diff, DIFF_METAINFO);
550 const char *reset = diff_get_color(ecbdata->color_diff, DIFF_RESET);
552 *(ecbdata->found_changesp) = 1;
554 if (ecbdata->label_path[0]) {
555 const char *name_a_tab, *name_b_tab;
557 name_a_tab = strchr(ecbdata->label_path[0], ' ') ? "\t" : "";
558 name_b_tab = strchr(ecbdata->label_path[1], ' ') ? "\t" : "";
560 printf("%s--- %s%s%s\n",
561 set, ecbdata->label_path[0], reset, name_a_tab);
562 printf("%s+++ %s%s%s\n",
563 set, ecbdata->label_path[1], reset, name_b_tab);
564 ecbdata->label_path[0] = ecbdata->label_path[1] = NULL;
567 /* This is not really necessary for now because
568 * this codepath only deals with two-way diffs.
570 for (i = 0; i < len && line[i] == '@'; i++)
572 if (2 <= i && i < len && line[i] == ' ') {
573 ecbdata->nparents = i - 1;
574 emit_line(diff_get_color(ecbdata->color_diff, DIFF_FRAGINFO),
575 reset, line, len);
576 return;
579 if (len < ecbdata->nparents) {
580 set = reset;
581 emit_line(reset, reset, line, len);
582 return;
585 color = DIFF_PLAIN;
586 if (ecbdata->diff_words && ecbdata->nparents != 1)
587 /* fall back to normal diff */
588 free_diff_words_data(ecbdata);
589 if (ecbdata->diff_words) {
590 if (line[0] == '-') {
591 diff_words_append(line, len,
592 &ecbdata->diff_words->minus);
593 return;
594 } else if (line[0] == '+') {
595 diff_words_append(line, len,
596 &ecbdata->diff_words->plus);
597 return;
599 if (ecbdata->diff_words->minus.text.size ||
600 ecbdata->diff_words->plus.text.size)
601 diff_words_show(ecbdata->diff_words);
602 line++;
603 len--;
604 emit_line(set, reset, line, len);
605 return;
607 for (i = 0; i < ecbdata->nparents && len; i++) {
608 if (line[i] == '-')
609 color = DIFF_FILE_OLD;
610 else if (line[i] == '+')
611 color = DIFF_FILE_NEW;
614 if (color != DIFF_FILE_NEW) {
615 emit_line(diff_get_color(ecbdata->color_diff, color),
616 reset, line, len);
617 return;
619 emit_add_line(reset, ecbdata, line, len);
622 static char *pprint_rename(const char *a, const char *b)
624 const char *old = a;
625 const char *new = b;
626 char *name = NULL;
627 int pfx_length, sfx_length;
628 int len_a = strlen(a);
629 int len_b = strlen(b);
630 int qlen_a = quote_c_style(a, NULL, NULL, 0);
631 int qlen_b = quote_c_style(b, NULL, NULL, 0);
633 if (qlen_a || qlen_b) {
634 if (qlen_a) len_a = qlen_a;
635 if (qlen_b) len_b = qlen_b;
636 name = xmalloc( len_a + len_b + 5 );
637 if (qlen_a)
638 quote_c_style(a, name, NULL, 0);
639 else
640 memcpy(name, a, len_a);
641 memcpy(name + len_a, " => ", 4);
642 if (qlen_b)
643 quote_c_style(b, name + len_a + 4, NULL, 0);
644 else
645 memcpy(name + len_a + 4, b, len_b + 1);
646 return name;
649 /* Find common prefix */
650 pfx_length = 0;
651 while (*old && *new && *old == *new) {
652 if (*old == '/')
653 pfx_length = old - a + 1;
654 old++;
655 new++;
658 /* Find common suffix */
659 old = a + len_a;
660 new = b + len_b;
661 sfx_length = 0;
662 while (a <= old && b <= new && *old == *new) {
663 if (*old == '/')
664 sfx_length = len_a - (old - a);
665 old--;
666 new--;
670 * pfx{mid-a => mid-b}sfx
671 * {pfx-a => pfx-b}sfx
672 * pfx{sfx-a => sfx-b}
673 * name-a => name-b
675 if (pfx_length + sfx_length) {
676 int a_midlen = len_a - pfx_length - sfx_length;
677 int b_midlen = len_b - pfx_length - sfx_length;
678 if (a_midlen < 0) a_midlen = 0;
679 if (b_midlen < 0) b_midlen = 0;
681 name = xmalloc(pfx_length + a_midlen + b_midlen + sfx_length + 7);
682 sprintf(name, "%.*s{%.*s => %.*s}%s",
683 pfx_length, a,
684 a_midlen, a + pfx_length,
685 b_midlen, b + pfx_length,
686 a + len_a - sfx_length);
688 else {
689 name = xmalloc(len_a + len_b + 5);
690 sprintf(name, "%s => %s", a, b);
692 return name;
695 struct diffstat_t {
696 struct xdiff_emit_state xm;
698 int nr;
699 int alloc;
700 struct diffstat_file {
701 char *name;
702 char *from_name;
703 unsigned is_unmerged:1;
704 unsigned is_binary:1;
705 unsigned is_renamed:1;
706 unsigned int added, deleted;
707 } **files;
710 static struct diffstat_file *diffstat_add(struct diffstat_t *diffstat,
711 const char *name_a,
712 const char *name_b)
714 struct diffstat_file *x;
715 x = xcalloc(sizeof (*x), 1);
716 if (diffstat->nr == diffstat->alloc) {
717 diffstat->alloc = alloc_nr(diffstat->alloc);
718 diffstat->files = xrealloc(diffstat->files,
719 diffstat->alloc * sizeof(x));
721 diffstat->files[diffstat->nr++] = x;
722 x->name = xstrdup(name_a);
723 if (name_b) {
724 x->from_name = xstrdup(name_b);
725 x->is_renamed = 1;
727 else
728 x->from_name = NULL;
729 return x;
732 static void diffstat_consume(void *priv, char *line, unsigned long len)
734 struct diffstat_t *diffstat = priv;
735 struct diffstat_file *x = diffstat->files[diffstat->nr - 1];
737 if (line[0] == '+')
738 x->added++;
739 else if (line[0] == '-')
740 x->deleted++;
743 const char mime_boundary_leader[] = "------------";
745 static int scale_linear(int it, int width, int max_change)
748 * make sure that at least one '-' is printed if there were deletions,
749 * and likewise for '+'.
751 if (max_change < 2)
752 return it;
753 return ((it - 1) * (width - 1) + max_change - 1) / (max_change - 1);
756 static void show_name(const char *prefix, const char *name, int len,
757 const char *reset, const char *set)
759 printf(" %s%s%-*s%s |", set, prefix, len, name, reset);
762 static void show_graph(char ch, int cnt, const char *set, const char *reset)
764 if (cnt <= 0)
765 return;
766 printf("%s", set);
767 while (cnt--)
768 putchar(ch);
769 printf("%s", reset);
772 static void show_stats(struct diffstat_t* data, struct diff_options *options)
774 int i, len, add, del, total, adds = 0, dels = 0;
775 int max_change = 0, max_len = 0;
776 int total_files = data->nr;
777 int width, name_width;
778 const char *reset, *set, *add_c, *del_c;
780 if (data->nr == 0)
781 return;
783 width = options->stat_width ? options->stat_width : 80;
784 name_width = options->stat_name_width ? options->stat_name_width : 50;
786 /* Sanity: give at least 5 columns to the graph,
787 * but leave at least 10 columns for the name.
789 if (width < name_width + 15) {
790 if (name_width <= 25)
791 width = name_width + 15;
792 else
793 name_width = width - 15;
796 /* Find the longest filename and max number of changes */
797 reset = diff_get_color(options->color_diff, DIFF_RESET);
798 set = diff_get_color(options->color_diff, DIFF_PLAIN);
799 add_c = diff_get_color(options->color_diff, DIFF_FILE_NEW);
800 del_c = diff_get_color(options->color_diff, DIFF_FILE_OLD);
802 for (i = 0; i < data->nr; i++) {
803 struct diffstat_file *file = data->files[i];
804 int change = file->added + file->deleted;
806 if (!file->is_renamed) { /* renames are quoted by pprint_rename */
807 len = quote_c_style(file->name, NULL, NULL, 0);
808 if (len) {
809 char *qname = xmalloc(len + 1);
810 quote_c_style(file->name, qname, NULL, 0);
811 free(file->name);
812 file->name = qname;
814 } else {
815 char *qname = pprint_rename(file->name, file->from_name);
816 free(file->name);
817 file->name = qname;
820 len = strlen(file->name);
821 if (max_len < len)
822 max_len = len;
824 if (file->is_binary || file->is_unmerged)
825 continue;
826 if (max_change < change)
827 max_change = change;
830 /* Compute the width of the graph part;
831 * 10 is for one blank at the beginning of the line plus
832 * " | count " between the name and the graph.
834 * From here on, name_width is the width of the name area,
835 * and width is the width of the graph area.
837 name_width = (name_width < max_len) ? name_width : max_len;
838 if (width < (name_width + 10) + max_change)
839 width = width - (name_width + 10);
840 else
841 width = max_change;
843 for (i = 0; i < data->nr; i++) {
844 const char *prefix = "";
845 char *name = data->files[i]->name;
846 int added = data->files[i]->added;
847 int deleted = data->files[i]->deleted;
848 int name_len;
851 * "scale" the filename
853 len = name_width;
854 name_len = strlen(name);
855 if (name_width < name_len) {
856 char *slash;
857 prefix = "...";
858 len -= 3;
859 name += name_len - len;
860 slash = strchr(name, '/');
861 if (slash)
862 name = slash;
865 if (data->files[i]->is_binary) {
866 show_name(prefix, name, len, reset, set);
867 printf(" Bin ");
868 printf("%s%d%s", del_c, deleted, reset);
869 printf(" -> ");
870 printf("%s%d%s", add_c, added, reset);
871 printf(" bytes");
872 printf("\n");
873 goto free_diffstat_file;
875 else if (data->files[i]->is_unmerged) {
876 show_name(prefix, name, len, reset, set);
877 printf(" Unmerged\n");
878 goto free_diffstat_file;
880 else if (!data->files[i]->is_renamed &&
881 (added + deleted == 0)) {
882 total_files--;
883 goto free_diffstat_file;
887 * scale the add/delete
889 add = added;
890 del = deleted;
891 total = add + del;
892 adds += add;
893 dels += del;
895 if (width <= max_change) {
896 add = scale_linear(add, width, max_change);
897 del = scale_linear(del, width, max_change);
898 total = add + del;
900 show_name(prefix, name, len, reset, set);
901 printf("%5d ", added + deleted);
902 show_graph('+', add, add_c, reset);
903 show_graph('-', del, del_c, reset);
904 putchar('\n');
905 free_diffstat_file:
906 free(data->files[i]->name);
907 free(data->files[i]);
909 free(data->files);
910 printf("%s %d files changed, %d insertions(+), %d deletions(-)%s\n",
911 set, total_files, adds, dels, reset);
914 static void show_shortstats(struct diffstat_t* data)
916 int i, adds = 0, dels = 0, total_files = data->nr;
918 if (data->nr == 0)
919 return;
921 for (i = 0; i < data->nr; i++) {
922 if (!data->files[i]->is_binary &&
923 !data->files[i]->is_unmerged) {
924 int added = data->files[i]->added;
925 int deleted= data->files[i]->deleted;
926 if (!data->files[i]->is_renamed &&
927 (added + deleted == 0)) {
928 total_files--;
929 } else {
930 adds += added;
931 dels += deleted;
934 free(data->files[i]->name);
935 free(data->files[i]);
937 free(data->files);
939 printf(" %d files changed, %d insertions(+), %d deletions(-)\n",
940 total_files, adds, dels);
943 static void show_numstat(struct diffstat_t* data, struct diff_options *options)
945 int i;
947 for (i = 0; i < data->nr; i++) {
948 struct diffstat_file *file = data->files[i];
950 if (file->is_binary)
951 printf("-\t-\t");
952 else
953 printf("%d\t%d\t", file->added, file->deleted);
954 if (options->line_termination &&
955 quote_c_style(file->name, NULL, NULL, 0))
956 quote_c_style(file->name, NULL, stdout, 0);
957 else
958 fputs(file->name, stdout);
959 if (file->is_renamed) {
960 printf("%s", options->line_termination ? "\t" : "\0\0");
961 if (options->line_termination &&
962 quote_c_style(file->from_name, NULL, NULL, 0))
963 quote_c_style(file->from_name, NULL, stdout, 0);
964 else
965 fputs(file->from_name, stdout);
967 putchar(options->line_termination);
971 struct checkdiff_t {
972 struct xdiff_emit_state xm;
973 const char *filename;
974 int lineno, color_diff;
977 static void checkdiff_consume(void *priv, char *line, unsigned long len)
979 struct checkdiff_t *data = priv;
980 const char *ws = diff_get_color(data->color_diff, DIFF_WHITESPACE);
981 const char *reset = diff_get_color(data->color_diff, DIFF_RESET);
982 const char *set = diff_get_color(data->color_diff, DIFF_FILE_NEW);
984 if (line[0] == '+') {
985 int i, spaces = 0, space_before_tab = 0, white_space_at_end = 0;
987 /* check space before tab */
988 for (i = 1; i < len && (line[i] == ' ' || line[i] == '\t'); i++)
989 if (line[i] == ' ')
990 spaces++;
991 if (line[i - 1] == '\t' && spaces)
992 space_before_tab = 1;
994 /* check white space at line end */
995 if (line[len - 1] == '\n')
996 len--;
997 if (isspace(line[len - 1]))
998 white_space_at_end = 1;
1000 if (space_before_tab || white_space_at_end) {
1001 printf("%s:%d: %s", data->filename, data->lineno, ws);
1002 if (space_before_tab) {
1003 printf("space before tab");
1004 if (white_space_at_end)
1005 putchar(',');
1007 if (white_space_at_end)
1008 printf("white space at end");
1009 printf(":%s ", reset);
1010 emit_line_with_ws(1, set, reset, ws, line, len);
1013 data->lineno++;
1014 } else if (line[0] == ' ')
1015 data->lineno++;
1016 else if (line[0] == '@') {
1017 char *plus = strchr(line, '+');
1018 if (plus)
1019 data->lineno = strtol(plus, NULL, 10);
1020 else
1021 die("invalid diff");
1025 static unsigned char *deflate_it(char *data,
1026 unsigned long size,
1027 unsigned long *result_size)
1029 int bound;
1030 unsigned char *deflated;
1031 z_stream stream;
1033 memset(&stream, 0, sizeof(stream));
1034 deflateInit(&stream, zlib_compression_level);
1035 bound = deflateBound(&stream, size);
1036 deflated = xmalloc(bound);
1037 stream.next_out = deflated;
1038 stream.avail_out = bound;
1040 stream.next_in = (unsigned char *)data;
1041 stream.avail_in = size;
1042 while (deflate(&stream, Z_FINISH) == Z_OK)
1043 ; /* nothing */
1044 deflateEnd(&stream);
1045 *result_size = stream.total_out;
1046 return deflated;
1049 static void emit_binary_diff_body(mmfile_t *one, mmfile_t *two)
1051 void *cp;
1052 void *delta;
1053 void *deflated;
1054 void *data;
1055 unsigned long orig_size;
1056 unsigned long delta_size;
1057 unsigned long deflate_size;
1058 unsigned long data_size;
1060 /* We could do deflated delta, or we could do just deflated two,
1061 * whichever is smaller.
1063 delta = NULL;
1064 deflated = deflate_it(two->ptr, two->size, &deflate_size);
1065 if (one->size && two->size) {
1066 delta = diff_delta(one->ptr, one->size,
1067 two->ptr, two->size,
1068 &delta_size, deflate_size);
1069 if (delta) {
1070 void *to_free = delta;
1071 orig_size = delta_size;
1072 delta = deflate_it(delta, delta_size, &delta_size);
1073 free(to_free);
1077 if (delta && delta_size < deflate_size) {
1078 printf("delta %lu\n", orig_size);
1079 free(deflated);
1080 data = delta;
1081 data_size = delta_size;
1083 else {
1084 printf("literal %lu\n", two->size);
1085 free(delta);
1086 data = deflated;
1087 data_size = deflate_size;
1090 /* emit data encoded in base85 */
1091 cp = data;
1092 while (data_size) {
1093 int bytes = (52 < data_size) ? 52 : data_size;
1094 char line[70];
1095 data_size -= bytes;
1096 if (bytes <= 26)
1097 line[0] = bytes + 'A' - 1;
1098 else
1099 line[0] = bytes - 26 + 'a' - 1;
1100 encode_85(line + 1, cp, bytes);
1101 cp = (char *) cp + bytes;
1102 puts(line);
1104 printf("\n");
1105 free(data);
1108 static void emit_binary_diff(mmfile_t *one, mmfile_t *two)
1110 printf("GIT binary patch\n");
1111 emit_binary_diff_body(one, two);
1112 emit_binary_diff_body(two, one);
1115 static void setup_diff_attr_check(struct git_attr_check *check)
1117 static struct git_attr *attr_diff;
1119 if (!attr_diff)
1120 attr_diff = git_attr("diff", 4);
1121 check->attr = attr_diff;
1124 #define FIRST_FEW_BYTES 8000
1125 static int file_is_binary(struct diff_filespec *one)
1127 unsigned long sz;
1128 struct git_attr_check attr_diff_check;
1130 setup_diff_attr_check(&attr_diff_check);
1131 if (!git_checkattr(one->path, 1, &attr_diff_check)) {
1132 const char *value = attr_diff_check.value;
1133 if (ATTR_TRUE(value))
1134 return 0;
1135 else if (ATTR_FALSE(value))
1136 return 1;
1139 if (!one->data) {
1140 if (!DIFF_FILE_VALID(one))
1141 return 0;
1142 diff_populate_filespec(one, 0);
1144 sz = one->size;
1145 if (FIRST_FEW_BYTES < sz)
1146 sz = FIRST_FEW_BYTES;
1147 return !!memchr(one->data, 0, sz);
1150 static void builtin_diff(const char *name_a,
1151 const char *name_b,
1152 struct diff_filespec *one,
1153 struct diff_filespec *two,
1154 const char *xfrm_msg,
1155 struct diff_options *o,
1156 int complete_rewrite)
1158 mmfile_t mf1, mf2;
1159 const char *lbl[2];
1160 char *a_one, *b_two;
1161 const char *set = diff_get_color(o->color_diff, DIFF_METAINFO);
1162 const char *reset = diff_get_color(o->color_diff, DIFF_RESET);
1164 a_one = quote_two("a/", name_a + (*name_a == '/'));
1165 b_two = quote_two("b/", name_b + (*name_b == '/'));
1166 lbl[0] = DIFF_FILE_VALID(one) ? a_one : "/dev/null";
1167 lbl[1] = DIFF_FILE_VALID(two) ? b_two : "/dev/null";
1168 printf("%sdiff --git %s %s%s\n", set, a_one, b_two, reset);
1169 if (lbl[0][0] == '/') {
1170 /* /dev/null */
1171 printf("%snew file mode %06o%s\n", set, two->mode, reset);
1172 if (xfrm_msg && xfrm_msg[0])
1173 printf("%s%s%s\n", set, xfrm_msg, reset);
1175 else if (lbl[1][0] == '/') {
1176 printf("%sdeleted file mode %06o%s\n", set, one->mode, reset);
1177 if (xfrm_msg && xfrm_msg[0])
1178 printf("%s%s%s\n", set, xfrm_msg, reset);
1180 else {
1181 if (one->mode != two->mode) {
1182 printf("%sold mode %06o%s\n", set, one->mode, reset);
1183 printf("%snew mode %06o%s\n", set, two->mode, reset);
1185 if (xfrm_msg && xfrm_msg[0])
1186 printf("%s%s%s\n", set, xfrm_msg, reset);
1188 * we do not run diff between different kind
1189 * of objects.
1191 if ((one->mode ^ two->mode) & S_IFMT)
1192 goto free_ab_and_return;
1193 if (complete_rewrite) {
1194 emit_rewrite_diff(name_a, name_b, one, two,
1195 o->color_diff);
1196 o->found_changes = 1;
1197 goto free_ab_and_return;
1201 if (fill_mmfile(&mf1, one) < 0 || fill_mmfile(&mf2, two) < 0)
1202 die("unable to read files to diff");
1204 if (!o->text && (file_is_binary(one) || file_is_binary(two))) {
1205 /* Quite common confusing case */
1206 if (mf1.size == mf2.size &&
1207 !memcmp(mf1.ptr, mf2.ptr, mf1.size))
1208 goto free_ab_and_return;
1209 if (o->binary)
1210 emit_binary_diff(&mf1, &mf2);
1211 else
1212 printf("Binary files %s and %s differ\n",
1213 lbl[0], lbl[1]);
1214 o->found_changes = 1;
1216 else {
1217 /* Crazy xdl interfaces.. */
1218 const char *diffopts = getenv("GIT_DIFF_OPTS");
1219 xpparam_t xpp;
1220 xdemitconf_t xecfg;
1221 xdemitcb_t ecb;
1222 struct emit_callback ecbdata;
1224 memset(&ecbdata, 0, sizeof(ecbdata));
1225 ecbdata.label_path = lbl;
1226 ecbdata.color_diff = o->color_diff;
1227 ecbdata.found_changesp = &o->found_changes;
1228 xpp.flags = XDF_NEED_MINIMAL | o->xdl_opts;
1229 xecfg.ctxlen = o->context;
1230 xecfg.flags = XDL_EMIT_FUNCNAMES;
1231 if (!diffopts)
1233 else if (!prefixcmp(diffopts, "--unified="))
1234 xecfg.ctxlen = strtoul(diffopts + 10, NULL, 10);
1235 else if (!prefixcmp(diffopts, "-u"))
1236 xecfg.ctxlen = strtoul(diffopts + 2, NULL, 10);
1237 ecb.outf = xdiff_outf;
1238 ecb.priv = &ecbdata;
1239 ecbdata.xm.consume = fn_out_consume;
1240 if (o->color_diff_words)
1241 ecbdata.diff_words =
1242 xcalloc(1, sizeof(struct diff_words_data));
1243 xdl_diff(&mf1, &mf2, &xpp, &xecfg, &ecb);
1244 if (o->color_diff_words)
1245 free_diff_words_data(&ecbdata);
1248 free_ab_and_return:
1249 diff_free_filespec_data(one);
1250 diff_free_filespec_data(two);
1251 free(a_one);
1252 free(b_two);
1253 return;
1256 static void builtin_diffstat(const char *name_a, const char *name_b,
1257 struct diff_filespec *one,
1258 struct diff_filespec *two,
1259 struct diffstat_t *diffstat,
1260 struct diff_options *o,
1261 int complete_rewrite)
1263 mmfile_t mf1, mf2;
1264 struct diffstat_file *data;
1266 data = diffstat_add(diffstat, name_a, name_b);
1268 if (!one || !two) {
1269 data->is_unmerged = 1;
1270 return;
1272 if (complete_rewrite) {
1273 diff_populate_filespec(one, 0);
1274 diff_populate_filespec(two, 0);
1275 data->deleted = count_lines(one->data, one->size);
1276 data->added = count_lines(two->data, two->size);
1277 goto free_and_return;
1279 if (fill_mmfile(&mf1, one) < 0 || fill_mmfile(&mf2, two) < 0)
1280 die("unable to read files to diff");
1282 if (file_is_binary(one) || file_is_binary(two)) {
1283 data->is_binary = 1;
1284 data->added = mf2.size;
1285 data->deleted = mf1.size;
1286 } else {
1287 /* Crazy xdl interfaces.. */
1288 xpparam_t xpp;
1289 xdemitconf_t xecfg;
1290 xdemitcb_t ecb;
1292 xpp.flags = XDF_NEED_MINIMAL | o->xdl_opts;
1293 xecfg.ctxlen = 0;
1294 xecfg.flags = 0;
1295 ecb.outf = xdiff_outf;
1296 ecb.priv = diffstat;
1297 xdl_diff(&mf1, &mf2, &xpp, &xecfg, &ecb);
1300 free_and_return:
1301 diff_free_filespec_data(one);
1302 diff_free_filespec_data(two);
1305 static void builtin_checkdiff(const char *name_a, const char *name_b,
1306 struct diff_filespec *one,
1307 struct diff_filespec *two, struct diff_options *o)
1309 mmfile_t mf1, mf2;
1310 struct checkdiff_t data;
1312 if (!two)
1313 return;
1315 memset(&data, 0, sizeof(data));
1316 data.xm.consume = checkdiff_consume;
1317 data.filename = name_b ? name_b : name_a;
1318 data.lineno = 0;
1319 data.color_diff = o->color_diff;
1321 if (fill_mmfile(&mf1, one) < 0 || fill_mmfile(&mf2, two) < 0)
1322 die("unable to read files to diff");
1324 if (file_is_binary(two))
1325 goto free_and_return;
1326 else {
1327 /* Crazy xdl interfaces.. */
1328 xpparam_t xpp;
1329 xdemitconf_t xecfg;
1330 xdemitcb_t ecb;
1332 xpp.flags = XDF_NEED_MINIMAL;
1333 xecfg.ctxlen = 0;
1334 xecfg.flags = 0;
1335 ecb.outf = xdiff_outf;
1336 ecb.priv = &data;
1337 xdl_diff(&mf1, &mf2, &xpp, &xecfg, &ecb);
1339 free_and_return:
1340 diff_free_filespec_data(one);
1341 diff_free_filespec_data(two);
1344 struct diff_filespec *alloc_filespec(const char *path)
1346 int namelen = strlen(path);
1347 struct diff_filespec *spec = xmalloc(sizeof(*spec) + namelen + 1);
1349 memset(spec, 0, sizeof(*spec));
1350 spec->path = (char *)(spec + 1);
1351 memcpy(spec->path, path, namelen+1);
1352 return spec;
1355 void fill_filespec(struct diff_filespec *spec, const unsigned char *sha1,
1356 unsigned short mode)
1358 if (mode) {
1359 spec->mode = canon_mode(mode);
1360 hashcpy(spec->sha1, sha1);
1361 spec->sha1_valid = !is_null_sha1(sha1);
1366 * Given a name and sha1 pair, if the dircache tells us the file in
1367 * the work tree has that object contents, return true, so that
1368 * prepare_temp_file() does not have to inflate and extract.
1370 static int reuse_worktree_file(const char *name, const unsigned char *sha1, int want_file)
1372 struct cache_entry *ce;
1373 struct stat st;
1374 int pos, len;
1376 /* We do not read the cache ourselves here, because the
1377 * benchmark with my previous version that always reads cache
1378 * shows that it makes things worse for diff-tree comparing
1379 * two linux-2.6 kernel trees in an already checked out work
1380 * tree. This is because most diff-tree comparisons deal with
1381 * only a small number of files, while reading the cache is
1382 * expensive for a large project, and its cost outweighs the
1383 * savings we get by not inflating the object to a temporary
1384 * file. Practically, this code only helps when we are used
1385 * by diff-cache --cached, which does read the cache before
1386 * calling us.
1388 if (!active_cache)
1389 return 0;
1391 /* We want to avoid the working directory if our caller
1392 * doesn't need the data in a normal file, this system
1393 * is rather slow with its stat/open/mmap/close syscalls,
1394 * and the object is contained in a pack file. The pack
1395 * is probably already open and will be faster to obtain
1396 * the data through than the working directory. Loose
1397 * objects however would tend to be slower as they need
1398 * to be individually opened and inflated.
1400 if (!FAST_WORKING_DIRECTORY && !want_file && has_sha1_pack(sha1, NULL))
1401 return 0;
1403 len = strlen(name);
1404 pos = cache_name_pos(name, len);
1405 if (pos < 0)
1406 return 0;
1407 ce = active_cache[pos];
1408 if ((lstat(name, &st) < 0) ||
1409 !S_ISREG(st.st_mode) || /* careful! */
1410 ce_match_stat(ce, &st, 0) ||
1411 hashcmp(sha1, ce->sha1))
1412 return 0;
1413 /* we return 1 only when we can stat, it is a regular file,
1414 * stat information matches, and sha1 recorded in the cache
1415 * matches. I.e. we know the file in the work tree really is
1416 * the same as the <name, sha1> pair.
1418 return 1;
1421 static int populate_from_stdin(struct diff_filespec *s)
1423 #define INCREMENT 1024
1424 char *buf;
1425 unsigned long size;
1426 ssize_t got;
1428 size = 0;
1429 buf = NULL;
1430 while (1) {
1431 buf = xrealloc(buf, size + INCREMENT);
1432 got = xread(0, buf + size, INCREMENT);
1433 if (!got)
1434 break; /* EOF */
1435 if (got < 0)
1436 return error("error while reading from stdin %s",
1437 strerror(errno));
1438 size += got;
1440 s->should_munmap = 0;
1441 s->data = buf;
1442 s->size = size;
1443 s->should_free = 1;
1444 return 0;
1447 static int diff_populate_gitlink(struct diff_filespec *s, int size_only)
1449 int len;
1450 char *data = xmalloc(100);
1451 len = snprintf(data, 100,
1452 "Subproject commit %s\n", sha1_to_hex(s->sha1));
1453 s->data = data;
1454 s->size = len;
1455 s->should_free = 1;
1456 if (size_only) {
1457 s->data = NULL;
1458 free(data);
1460 return 0;
1464 * While doing rename detection and pickaxe operation, we may need to
1465 * grab the data for the blob (or file) for our own in-core comparison.
1466 * diff_filespec has data and size fields for this purpose.
1468 int diff_populate_filespec(struct diff_filespec *s, int size_only)
1470 int err = 0;
1471 if (!DIFF_FILE_VALID(s))
1472 die("internal error: asking to populate invalid file.");
1473 if (S_ISDIR(s->mode))
1474 return -1;
1476 if (s->data)
1477 return 0;
1479 if (size_only && 0 < s->size)
1480 return 0;
1482 if (S_ISDIRLNK(s->mode))
1483 return diff_populate_gitlink(s, size_only);
1485 if (!s->sha1_valid ||
1486 reuse_worktree_file(s->path, s->sha1, 0)) {
1487 struct stat st;
1488 int fd;
1489 char *buf;
1490 unsigned long size;
1492 if (!strcmp(s->path, "-"))
1493 return populate_from_stdin(s);
1495 if (lstat(s->path, &st) < 0) {
1496 if (errno == ENOENT) {
1497 err_empty:
1498 err = -1;
1499 empty:
1500 s->data = (char *)"";
1501 s->size = 0;
1502 return err;
1505 s->size = xsize_t(st.st_size);
1506 if (!s->size)
1507 goto empty;
1508 if (size_only)
1509 return 0;
1510 if (S_ISLNK(st.st_mode)) {
1511 int ret;
1512 s->data = xmalloc(s->size);
1513 s->should_free = 1;
1514 ret = readlink(s->path, s->data, s->size);
1515 if (ret < 0) {
1516 free(s->data);
1517 goto err_empty;
1519 return 0;
1521 fd = open(s->path, O_RDONLY);
1522 if (fd < 0)
1523 goto err_empty;
1524 s->data = xmmap(NULL, s->size, PROT_READ, MAP_PRIVATE, fd, 0);
1525 close(fd);
1526 s->should_munmap = 1;
1529 * Convert from working tree format to canonical git format
1531 size = s->size;
1532 buf = convert_to_git(s->path, s->data, &size);
1533 if (buf) {
1534 munmap(s->data, s->size);
1535 s->should_munmap = 0;
1536 s->data = buf;
1537 s->size = size;
1538 s->should_free = 1;
1541 else {
1542 enum object_type type;
1543 if (size_only)
1544 type = sha1_object_info(s->sha1, &s->size);
1545 else {
1546 s->data = read_sha1_file(s->sha1, &type, &s->size);
1547 s->should_free = 1;
1550 return 0;
1553 void diff_free_filespec_data(struct diff_filespec *s)
1555 if (s->should_free)
1556 free(s->data);
1557 else if (s->should_munmap)
1558 munmap(s->data, s->size);
1560 if (s->should_free || s->should_munmap) {
1561 s->should_free = s->should_munmap = 0;
1562 s->data = NULL;
1564 free(s->cnt_data);
1565 s->cnt_data = NULL;
1568 static void prep_temp_blob(struct diff_tempfile *temp,
1569 void *blob,
1570 unsigned long size,
1571 const unsigned char *sha1,
1572 int mode)
1574 int fd;
1576 fd = git_mkstemp(temp->tmp_path, PATH_MAX, ".diff_XXXXXX");
1577 if (fd < 0)
1578 die("unable to create temp-file");
1579 if (write_in_full(fd, blob, size) != size)
1580 die("unable to write temp-file");
1581 close(fd);
1582 temp->name = temp->tmp_path;
1583 strcpy(temp->hex, sha1_to_hex(sha1));
1584 temp->hex[40] = 0;
1585 sprintf(temp->mode, "%06o", mode);
1588 static void prepare_temp_file(const char *name,
1589 struct diff_tempfile *temp,
1590 struct diff_filespec *one)
1592 if (!DIFF_FILE_VALID(one)) {
1593 not_a_valid_file:
1594 /* A '-' entry produces this for file-2, and
1595 * a '+' entry produces this for file-1.
1597 temp->name = "/dev/null";
1598 strcpy(temp->hex, ".");
1599 strcpy(temp->mode, ".");
1600 return;
1603 if (!one->sha1_valid ||
1604 reuse_worktree_file(name, one->sha1, 1)) {
1605 struct stat st;
1606 if (lstat(name, &st) < 0) {
1607 if (errno == ENOENT)
1608 goto not_a_valid_file;
1609 die("stat(%s): %s", name, strerror(errno));
1611 if (S_ISLNK(st.st_mode)) {
1612 int ret;
1613 char buf[PATH_MAX + 1]; /* ought to be SYMLINK_MAX */
1614 size_t sz = xsize_t(st.st_size);
1615 if (sizeof(buf) <= st.st_size)
1616 die("symlink too long: %s", name);
1617 ret = readlink(name, buf, sz);
1618 if (ret < 0)
1619 die("readlink(%s)", name);
1620 prep_temp_blob(temp, buf, sz,
1621 (one->sha1_valid ?
1622 one->sha1 : null_sha1),
1623 (one->sha1_valid ?
1624 one->mode : S_IFLNK));
1626 else {
1627 /* we can borrow from the file in the work tree */
1628 temp->name = name;
1629 if (!one->sha1_valid)
1630 strcpy(temp->hex, sha1_to_hex(null_sha1));
1631 else
1632 strcpy(temp->hex, sha1_to_hex(one->sha1));
1633 /* Even though we may sometimes borrow the
1634 * contents from the work tree, we always want
1635 * one->mode. mode is trustworthy even when
1636 * !(one->sha1_valid), as long as
1637 * DIFF_FILE_VALID(one).
1639 sprintf(temp->mode, "%06o", one->mode);
1641 return;
1643 else {
1644 if (diff_populate_filespec(one, 0))
1645 die("cannot read data blob for %s", one->path);
1646 prep_temp_blob(temp, one->data, one->size,
1647 one->sha1, one->mode);
1651 static void remove_tempfile(void)
1653 int i;
1655 for (i = 0; i < 2; i++)
1656 if (diff_temp[i].name == diff_temp[i].tmp_path) {
1657 unlink(diff_temp[i].name);
1658 diff_temp[i].name = NULL;
1662 static void remove_tempfile_on_signal(int signo)
1664 remove_tempfile();
1665 signal(SIGINT, SIG_DFL);
1666 raise(signo);
1669 static int spawn_prog(const char *pgm, const char **arg)
1671 pid_t pid;
1672 int status;
1674 fflush(NULL);
1675 pid = fork();
1676 if (pid < 0)
1677 die("unable to fork");
1678 if (!pid) {
1679 execvp(pgm, (char *const*) arg);
1680 exit(255);
1683 while (waitpid(pid, &status, 0) < 0) {
1684 if (errno == EINTR)
1685 continue;
1686 return -1;
1689 /* Earlier we did not check the exit status because
1690 * diff exits non-zero if files are different, and
1691 * we are not interested in knowing that. It was a
1692 * mistake which made it harder to quit a diff-*
1693 * session that uses the git-apply-patch-script as
1694 * the GIT_EXTERNAL_DIFF. A custom GIT_EXTERNAL_DIFF
1695 * should also exit non-zero only when it wants to
1696 * abort the entire diff-* session.
1698 if (WIFEXITED(status) && !WEXITSTATUS(status))
1699 return 0;
1700 return -1;
1703 /* An external diff command takes:
1705 * diff-cmd name infile1 infile1-sha1 infile1-mode \
1706 * infile2 infile2-sha1 infile2-mode [ rename-to ]
1709 static void run_external_diff(const char *pgm,
1710 const char *name,
1711 const char *other,
1712 struct diff_filespec *one,
1713 struct diff_filespec *two,
1714 const char *xfrm_msg,
1715 int complete_rewrite)
1717 const char *spawn_arg[10];
1718 struct diff_tempfile *temp = diff_temp;
1719 int retval;
1720 static int atexit_asked = 0;
1721 const char *othername;
1722 const char **arg = &spawn_arg[0];
1724 othername = (other? other : name);
1725 if (one && two) {
1726 prepare_temp_file(name, &temp[0], one);
1727 prepare_temp_file(othername, &temp[1], two);
1728 if (! atexit_asked &&
1729 (temp[0].name == temp[0].tmp_path ||
1730 temp[1].name == temp[1].tmp_path)) {
1731 atexit_asked = 1;
1732 atexit(remove_tempfile);
1734 signal(SIGINT, remove_tempfile_on_signal);
1737 if (one && two) {
1738 *arg++ = pgm;
1739 *arg++ = name;
1740 *arg++ = temp[0].name;
1741 *arg++ = temp[0].hex;
1742 *arg++ = temp[0].mode;
1743 *arg++ = temp[1].name;
1744 *arg++ = temp[1].hex;
1745 *arg++ = temp[1].mode;
1746 if (other) {
1747 *arg++ = other;
1748 *arg++ = xfrm_msg;
1750 } else {
1751 *arg++ = pgm;
1752 *arg++ = name;
1754 *arg = NULL;
1755 retval = spawn_prog(pgm, spawn_arg);
1756 remove_tempfile();
1757 if (retval) {
1758 fprintf(stderr, "external diff died, stopping at %s.\n", name);
1759 exit(1);
1763 static const char *external_diff_attr(const char *name)
1765 struct git_attr_check attr_diff_check;
1767 setup_diff_attr_check(&attr_diff_check);
1768 if (!git_checkattr(name, 1, &attr_diff_check)) {
1769 const char *value = attr_diff_check.value;
1770 if (!ATTR_TRUE(value) &&
1771 !ATTR_FALSE(value) &&
1772 !ATTR_UNSET(value)) {
1773 struct ll_diff_driver *drv;
1775 if (!user_diff_tail) {
1776 user_diff_tail = &user_diff;
1777 git_config(git_diff_ui_config);
1779 for (drv = user_diff; drv; drv = drv->next)
1780 if (!strcmp(drv->name, value))
1781 return drv->cmd;
1784 return NULL;
1787 static void run_diff_cmd(const char *pgm,
1788 const char *name,
1789 const char *other,
1790 struct diff_filespec *one,
1791 struct diff_filespec *two,
1792 const char *xfrm_msg,
1793 struct diff_options *o,
1794 int complete_rewrite)
1796 if (!o->allow_external)
1797 pgm = NULL;
1798 else {
1799 const char *cmd = external_diff_attr(name);
1800 if (cmd)
1801 pgm = cmd;
1804 if (pgm) {
1805 run_external_diff(pgm, name, other, one, two, xfrm_msg,
1806 complete_rewrite);
1807 return;
1809 if (one && two)
1810 builtin_diff(name, other ? other : name,
1811 one, two, xfrm_msg, o, complete_rewrite);
1812 else
1813 printf("* Unmerged path %s\n", name);
1816 static void diff_fill_sha1_info(struct diff_filespec *one)
1818 if (DIFF_FILE_VALID(one)) {
1819 if (!one->sha1_valid) {
1820 struct stat st;
1821 if (!strcmp(one->path, "-")) {
1822 hashcpy(one->sha1, null_sha1);
1823 return;
1825 if (lstat(one->path, &st) < 0)
1826 die("stat %s", one->path);
1827 if (index_path(one->sha1, one->path, &st, 0))
1828 die("cannot hash %s\n", one->path);
1831 else
1832 hashclr(one->sha1);
1835 static void run_diff(struct diff_filepair *p, struct diff_options *o)
1837 const char *pgm = external_diff();
1838 char msg[PATH_MAX*2+300], *xfrm_msg;
1839 struct diff_filespec *one;
1840 struct diff_filespec *two;
1841 const char *name;
1842 const char *other;
1843 char *name_munged, *other_munged;
1844 int complete_rewrite = 0;
1845 int len;
1847 if (DIFF_PAIR_UNMERGED(p)) {
1848 /* unmerged */
1849 run_diff_cmd(pgm, p->one->path, NULL, NULL, NULL, NULL, o, 0);
1850 return;
1853 name = p->one->path;
1854 other = (strcmp(name, p->two->path) ? p->two->path : NULL);
1855 name_munged = quote_one(name);
1856 other_munged = quote_one(other);
1857 one = p->one; two = p->two;
1859 diff_fill_sha1_info(one);
1860 diff_fill_sha1_info(two);
1862 len = 0;
1863 switch (p->status) {
1864 case DIFF_STATUS_COPIED:
1865 len += snprintf(msg + len, sizeof(msg) - len,
1866 "similarity index %d%%\n"
1867 "copy from %s\n"
1868 "copy to %s\n",
1869 (int)(0.5 + p->score * 100.0/MAX_SCORE),
1870 name_munged, other_munged);
1871 break;
1872 case DIFF_STATUS_RENAMED:
1873 len += snprintf(msg + len, sizeof(msg) - len,
1874 "similarity index %d%%\n"
1875 "rename from %s\n"
1876 "rename to %s\n",
1877 (int)(0.5 + p->score * 100.0/MAX_SCORE),
1878 name_munged, other_munged);
1879 break;
1880 case DIFF_STATUS_MODIFIED:
1881 if (p->score) {
1882 len += snprintf(msg + len, sizeof(msg) - len,
1883 "dissimilarity index %d%%\n",
1884 (int)(0.5 + p->score *
1885 100.0/MAX_SCORE));
1886 complete_rewrite = 1;
1887 break;
1889 /* fallthru */
1890 default:
1891 /* nothing */
1895 if (hashcmp(one->sha1, two->sha1)) {
1896 int abbrev = o->full_index ? 40 : DEFAULT_ABBREV;
1898 if (o->binary) {
1899 mmfile_t mf;
1900 if ((!fill_mmfile(&mf, one) && file_is_binary(one)) ||
1901 (!fill_mmfile(&mf, two) && file_is_binary(two)))
1902 abbrev = 40;
1904 len += snprintf(msg + len, sizeof(msg) - len,
1905 "index %.*s..%.*s",
1906 abbrev, sha1_to_hex(one->sha1),
1907 abbrev, sha1_to_hex(two->sha1));
1908 if (one->mode == two->mode)
1909 len += snprintf(msg + len, sizeof(msg) - len,
1910 " %06o", one->mode);
1911 len += snprintf(msg + len, sizeof(msg) - len, "\n");
1914 if (len)
1915 msg[--len] = 0;
1916 xfrm_msg = len ? msg : NULL;
1918 if (!pgm &&
1919 DIFF_FILE_VALID(one) && DIFF_FILE_VALID(two) &&
1920 (S_IFMT & one->mode) != (S_IFMT & two->mode)) {
1921 /* a filepair that changes between file and symlink
1922 * needs to be split into deletion and creation.
1924 struct diff_filespec *null = alloc_filespec(two->path);
1925 run_diff_cmd(NULL, name, other, one, null, xfrm_msg, o, 0);
1926 free(null);
1927 null = alloc_filespec(one->path);
1928 run_diff_cmd(NULL, name, other, null, two, xfrm_msg, o, 0);
1929 free(null);
1931 else
1932 run_diff_cmd(pgm, name, other, one, two, xfrm_msg, o,
1933 complete_rewrite);
1935 free(name_munged);
1936 free(other_munged);
1939 static void run_diffstat(struct diff_filepair *p, struct diff_options *o,
1940 struct diffstat_t *diffstat)
1942 const char *name;
1943 const char *other;
1944 int complete_rewrite = 0;
1946 if (DIFF_PAIR_UNMERGED(p)) {
1947 /* unmerged */
1948 builtin_diffstat(p->one->path, NULL, NULL, NULL, diffstat, o, 0);
1949 return;
1952 name = p->one->path;
1953 other = (strcmp(name, p->two->path) ? p->two->path : NULL);
1955 diff_fill_sha1_info(p->one);
1956 diff_fill_sha1_info(p->two);
1958 if (p->status == DIFF_STATUS_MODIFIED && p->score)
1959 complete_rewrite = 1;
1960 builtin_diffstat(name, other, p->one, p->two, diffstat, o, complete_rewrite);
1963 static void run_checkdiff(struct diff_filepair *p, struct diff_options *o)
1965 const char *name;
1966 const char *other;
1968 if (DIFF_PAIR_UNMERGED(p)) {
1969 /* unmerged */
1970 return;
1973 name = p->one->path;
1974 other = (strcmp(name, p->two->path) ? p->two->path : NULL);
1976 diff_fill_sha1_info(p->one);
1977 diff_fill_sha1_info(p->two);
1979 builtin_checkdiff(name, other, p->one, p->two, o);
1982 void diff_setup(struct diff_options *options)
1984 memset(options, 0, sizeof(*options));
1985 options->line_termination = '\n';
1986 options->break_opt = -1;
1987 options->rename_limit = -1;
1988 options->context = 3;
1989 options->msg_sep = "";
1991 options->change = diff_change;
1992 options->add_remove = diff_addremove;
1993 options->color_diff = diff_use_color_default;
1994 options->detect_rename = diff_detect_rename_default;
1997 int diff_setup_done(struct diff_options *options)
1999 int count = 0;
2001 if (options->output_format & DIFF_FORMAT_NAME)
2002 count++;
2003 if (options->output_format & DIFF_FORMAT_NAME_STATUS)
2004 count++;
2005 if (options->output_format & DIFF_FORMAT_CHECKDIFF)
2006 count++;
2007 if (options->output_format & DIFF_FORMAT_NO_OUTPUT)
2008 count++;
2009 if (count > 1)
2010 die("--name-only, --name-status, --check and -s are mutually exclusive");
2012 if (options->find_copies_harder)
2013 options->detect_rename = DIFF_DETECT_COPY;
2015 if (options->output_format & (DIFF_FORMAT_NAME |
2016 DIFF_FORMAT_NAME_STATUS |
2017 DIFF_FORMAT_CHECKDIFF |
2018 DIFF_FORMAT_NO_OUTPUT))
2019 options->output_format &= ~(DIFF_FORMAT_RAW |
2020 DIFF_FORMAT_NUMSTAT |
2021 DIFF_FORMAT_DIFFSTAT |
2022 DIFF_FORMAT_SHORTSTAT |
2023 DIFF_FORMAT_SUMMARY |
2024 DIFF_FORMAT_PATCH);
2027 * These cases always need recursive; we do not drop caller-supplied
2028 * recursive bits for other formats here.
2030 if (options->output_format & (DIFF_FORMAT_PATCH |
2031 DIFF_FORMAT_NUMSTAT |
2032 DIFF_FORMAT_DIFFSTAT |
2033 DIFF_FORMAT_SHORTSTAT |
2034 DIFF_FORMAT_SUMMARY |
2035 DIFF_FORMAT_CHECKDIFF))
2036 options->recursive = 1;
2038 * Also pickaxe would not work very well if you do not say recursive
2040 if (options->pickaxe)
2041 options->recursive = 1;
2043 if (options->detect_rename && options->rename_limit < 0)
2044 options->rename_limit = diff_rename_limit_default;
2045 if (options->setup & DIFF_SETUP_USE_CACHE) {
2046 if (!active_cache)
2047 /* read-cache does not die even when it fails
2048 * so it is safe for us to do this here. Also
2049 * it does not smudge active_cache or active_nr
2050 * when it fails, so we do not have to worry about
2051 * cleaning it up ourselves either.
2053 read_cache();
2055 if (options->abbrev <= 0 || 40 < options->abbrev)
2056 options->abbrev = 40; /* full */
2059 * It does not make sense to show the first hit we happened
2060 * to have found. It does not make sense not to return with
2061 * exit code in such a case either.
2063 if (options->quiet) {
2064 options->output_format = DIFF_FORMAT_NO_OUTPUT;
2065 options->exit_with_status = 1;
2069 * If we postprocess in diffcore, we cannot simply return
2070 * upon the first hit. We need to run diff as usual.
2072 if (options->pickaxe || options->filter)
2073 options->quiet = 0;
2075 return 0;
2078 static int opt_arg(const char *arg, int arg_short, const char *arg_long, int *val)
2080 char c, *eq;
2081 int len;
2083 if (*arg != '-')
2084 return 0;
2085 c = *++arg;
2086 if (!c)
2087 return 0;
2088 if (c == arg_short) {
2089 c = *++arg;
2090 if (!c)
2091 return 1;
2092 if (val && isdigit(c)) {
2093 char *end;
2094 int n = strtoul(arg, &end, 10);
2095 if (*end)
2096 return 0;
2097 *val = n;
2098 return 1;
2100 return 0;
2102 if (c != '-')
2103 return 0;
2104 arg++;
2105 eq = strchr(arg, '=');
2106 if (eq)
2107 len = eq - arg;
2108 else
2109 len = strlen(arg);
2110 if (!len || strncmp(arg, arg_long, len))
2111 return 0;
2112 if (eq) {
2113 int n;
2114 char *end;
2115 if (!isdigit(*++eq))
2116 return 0;
2117 n = strtoul(eq, &end, 10);
2118 if (*end)
2119 return 0;
2120 *val = n;
2122 return 1;
2125 int diff_opt_parse(struct diff_options *options, const char **av, int ac)
2127 const char *arg = av[0];
2128 if (!strcmp(arg, "-p") || !strcmp(arg, "-u"))
2129 options->output_format |= DIFF_FORMAT_PATCH;
2130 else if (opt_arg(arg, 'U', "unified", &options->context))
2131 options->output_format |= DIFF_FORMAT_PATCH;
2132 else if (!strcmp(arg, "--raw"))
2133 options->output_format |= DIFF_FORMAT_RAW;
2134 else if (!strcmp(arg, "--patch-with-raw")) {
2135 options->output_format |= DIFF_FORMAT_PATCH | DIFF_FORMAT_RAW;
2137 else if (!strcmp(arg, "--numstat")) {
2138 options->output_format |= DIFF_FORMAT_NUMSTAT;
2140 else if (!strcmp(arg, "--shortstat")) {
2141 options->output_format |= DIFF_FORMAT_SHORTSTAT;
2143 else if (!prefixcmp(arg, "--stat")) {
2144 char *end;
2145 int width = options->stat_width;
2146 int name_width = options->stat_name_width;
2147 arg += 6;
2148 end = (char *)arg;
2150 switch (*arg) {
2151 case '-':
2152 if (!prefixcmp(arg, "-width="))
2153 width = strtoul(arg + 7, &end, 10);
2154 else if (!prefixcmp(arg, "-name-width="))
2155 name_width = strtoul(arg + 12, &end, 10);
2156 break;
2157 case '=':
2158 width = strtoul(arg+1, &end, 10);
2159 if (*end == ',')
2160 name_width = strtoul(end+1, &end, 10);
2163 /* Important! This checks all the error cases! */
2164 if (*end)
2165 return 0;
2166 options->output_format |= DIFF_FORMAT_DIFFSTAT;
2167 options->stat_name_width = name_width;
2168 options->stat_width = width;
2170 else if (!strcmp(arg, "--check"))
2171 options->output_format |= DIFF_FORMAT_CHECKDIFF;
2172 else if (!strcmp(arg, "--summary"))
2173 options->output_format |= DIFF_FORMAT_SUMMARY;
2174 else if (!strcmp(arg, "--patch-with-stat")) {
2175 options->output_format |= DIFF_FORMAT_PATCH | DIFF_FORMAT_DIFFSTAT;
2177 else if (!strcmp(arg, "-z"))
2178 options->line_termination = 0;
2179 else if (!prefixcmp(arg, "-l"))
2180 options->rename_limit = strtoul(arg+2, NULL, 10);
2181 else if (!strcmp(arg, "--full-index"))
2182 options->full_index = 1;
2183 else if (!strcmp(arg, "--binary")) {
2184 options->output_format |= DIFF_FORMAT_PATCH;
2185 options->binary = 1;
2187 else if (!strcmp(arg, "-a") || !strcmp(arg, "--text")) {
2188 options->text = 1;
2190 else if (!strcmp(arg, "--name-only"))
2191 options->output_format |= DIFF_FORMAT_NAME;
2192 else if (!strcmp(arg, "--name-status"))
2193 options->output_format |= DIFF_FORMAT_NAME_STATUS;
2194 else if (!strcmp(arg, "-R"))
2195 options->reverse_diff = 1;
2196 else if (!prefixcmp(arg, "-S"))
2197 options->pickaxe = arg + 2;
2198 else if (!strcmp(arg, "-s")) {
2199 options->output_format |= DIFF_FORMAT_NO_OUTPUT;
2201 else if (!prefixcmp(arg, "-O"))
2202 options->orderfile = arg + 2;
2203 else if (!prefixcmp(arg, "--diff-filter="))
2204 options->filter = arg + 14;
2205 else if (!strcmp(arg, "--pickaxe-all"))
2206 options->pickaxe_opts = DIFF_PICKAXE_ALL;
2207 else if (!strcmp(arg, "--pickaxe-regex"))
2208 options->pickaxe_opts = DIFF_PICKAXE_REGEX;
2209 else if (!prefixcmp(arg, "-B")) {
2210 if ((options->break_opt =
2211 diff_scoreopt_parse(arg)) == -1)
2212 return -1;
2214 else if (!prefixcmp(arg, "-M")) {
2215 if ((options->rename_score =
2216 diff_scoreopt_parse(arg)) == -1)
2217 return -1;
2218 options->detect_rename = DIFF_DETECT_RENAME;
2220 else if (!prefixcmp(arg, "-C")) {
2221 if ((options->rename_score =
2222 diff_scoreopt_parse(arg)) == -1)
2223 return -1;
2224 options->detect_rename = DIFF_DETECT_COPY;
2226 else if (!strcmp(arg, "--find-copies-harder"))
2227 options->find_copies_harder = 1;
2228 else if (!strcmp(arg, "--abbrev"))
2229 options->abbrev = DEFAULT_ABBREV;
2230 else if (!prefixcmp(arg, "--abbrev=")) {
2231 options->abbrev = strtoul(arg + 9, NULL, 10);
2232 if (options->abbrev < MINIMUM_ABBREV)
2233 options->abbrev = MINIMUM_ABBREV;
2234 else if (40 < options->abbrev)
2235 options->abbrev = 40;
2237 else if (!strcmp(arg, "--color"))
2238 options->color_diff = 1;
2239 else if (!strcmp(arg, "--no-color"))
2240 options->color_diff = 0;
2241 else if (!strcmp(arg, "-w") || !strcmp(arg, "--ignore-all-space"))
2242 options->xdl_opts |= XDF_IGNORE_WHITESPACE;
2243 else if (!strcmp(arg, "-b") || !strcmp(arg, "--ignore-space-change"))
2244 options->xdl_opts |= XDF_IGNORE_WHITESPACE_CHANGE;
2245 else if (!strcmp(arg, "--ignore-space-at-eol"))
2246 options->xdl_opts |= XDF_IGNORE_WHITESPACE_AT_EOL;
2247 else if (!strcmp(arg, "--color-words"))
2248 options->color_diff = options->color_diff_words = 1;
2249 else if (!strcmp(arg, "--no-renames"))
2250 options->detect_rename = 0;
2251 else if (!strcmp(arg, "--exit-code"))
2252 options->exit_with_status = 1;
2253 else if (!strcmp(arg, "--quiet"))
2254 options->quiet = 1;
2255 else
2256 return 0;
2257 return 1;
2260 static int parse_num(const char **cp_p)
2262 unsigned long num, scale;
2263 int ch, dot;
2264 const char *cp = *cp_p;
2266 num = 0;
2267 scale = 1;
2268 dot = 0;
2269 for(;;) {
2270 ch = *cp;
2271 if ( !dot && ch == '.' ) {
2272 scale = 1;
2273 dot = 1;
2274 } else if ( ch == '%' ) {
2275 scale = dot ? scale*100 : 100;
2276 cp++; /* % is always at the end */
2277 break;
2278 } else if ( ch >= '0' && ch <= '9' ) {
2279 if ( scale < 100000 ) {
2280 scale *= 10;
2281 num = (num*10) + (ch-'0');
2283 } else {
2284 break;
2286 cp++;
2288 *cp_p = cp;
2290 /* user says num divided by scale and we say internally that
2291 * is MAX_SCORE * num / scale.
2293 return (int)((num >= scale) ? MAX_SCORE : (MAX_SCORE * num / scale));
2296 int diff_scoreopt_parse(const char *opt)
2298 int opt1, opt2, cmd;
2300 if (*opt++ != '-')
2301 return -1;
2302 cmd = *opt++;
2303 if (cmd != 'M' && cmd != 'C' && cmd != 'B')
2304 return -1; /* that is not a -M, -C nor -B option */
2306 opt1 = parse_num(&opt);
2307 if (cmd != 'B')
2308 opt2 = 0;
2309 else {
2310 if (*opt == 0)
2311 opt2 = 0;
2312 else if (*opt != '/')
2313 return -1; /* we expect -B80/99 or -B80 */
2314 else {
2315 opt++;
2316 opt2 = parse_num(&opt);
2319 if (*opt != 0)
2320 return -1;
2321 return opt1 | (opt2 << 16);
2324 struct diff_queue_struct diff_queued_diff;
2326 void diff_q(struct diff_queue_struct *queue, struct diff_filepair *dp)
2328 if (queue->alloc <= queue->nr) {
2329 queue->alloc = alloc_nr(queue->alloc);
2330 queue->queue = xrealloc(queue->queue,
2331 sizeof(dp) * queue->alloc);
2333 queue->queue[queue->nr++] = dp;
2336 struct diff_filepair *diff_queue(struct diff_queue_struct *queue,
2337 struct diff_filespec *one,
2338 struct diff_filespec *two)
2340 struct diff_filepair *dp = xcalloc(1, sizeof(*dp));
2341 dp->one = one;
2342 dp->two = two;
2343 if (queue)
2344 diff_q(queue, dp);
2345 return dp;
2348 void diff_free_filepair(struct diff_filepair *p)
2350 diff_free_filespec_data(p->one);
2351 diff_free_filespec_data(p->two);
2352 free(p->one);
2353 free(p->two);
2354 free(p);
2357 /* This is different from find_unique_abbrev() in that
2358 * it stuffs the result with dots for alignment.
2360 const char *diff_unique_abbrev(const unsigned char *sha1, int len)
2362 int abblen;
2363 const char *abbrev;
2364 if (len == 40)
2365 return sha1_to_hex(sha1);
2367 abbrev = find_unique_abbrev(sha1, len);
2368 if (!abbrev)
2369 return sha1_to_hex(sha1);
2370 abblen = strlen(abbrev);
2371 if (abblen < 37) {
2372 static char hex[41];
2373 if (len < abblen && abblen <= len + 2)
2374 sprintf(hex, "%s%.*s", abbrev, len+3-abblen, "..");
2375 else
2376 sprintf(hex, "%s...", abbrev);
2377 return hex;
2379 return sha1_to_hex(sha1);
2382 static void diff_flush_raw(struct diff_filepair *p,
2383 struct diff_options *options)
2385 int two_paths;
2386 char status[10];
2387 int abbrev = options->abbrev;
2388 const char *path_one, *path_two;
2389 int inter_name_termination = '\t';
2390 int line_termination = options->line_termination;
2392 if (!line_termination)
2393 inter_name_termination = 0;
2395 path_one = p->one->path;
2396 path_two = p->two->path;
2397 if (line_termination) {
2398 path_one = quote_one(path_one);
2399 path_two = quote_one(path_two);
2402 if (p->score)
2403 sprintf(status, "%c%03d", p->status,
2404 (int)(0.5 + p->score * 100.0/MAX_SCORE));
2405 else {
2406 status[0] = p->status;
2407 status[1] = 0;
2409 switch (p->status) {
2410 case DIFF_STATUS_COPIED:
2411 case DIFF_STATUS_RENAMED:
2412 two_paths = 1;
2413 break;
2414 case DIFF_STATUS_ADDED:
2415 case DIFF_STATUS_DELETED:
2416 two_paths = 0;
2417 break;
2418 default:
2419 two_paths = 0;
2420 break;
2422 if (!(options->output_format & DIFF_FORMAT_NAME_STATUS)) {
2423 printf(":%06o %06o %s ",
2424 p->one->mode, p->two->mode,
2425 diff_unique_abbrev(p->one->sha1, abbrev));
2426 printf("%s ",
2427 diff_unique_abbrev(p->two->sha1, abbrev));
2429 printf("%s%c%s", status, inter_name_termination, path_one);
2430 if (two_paths)
2431 printf("%c%s", inter_name_termination, path_two);
2432 putchar(line_termination);
2433 if (path_one != p->one->path)
2434 free((void*)path_one);
2435 if (path_two != p->two->path)
2436 free((void*)path_two);
2439 static void diff_flush_name(struct diff_filepair *p, struct diff_options *opt)
2441 char *path = p->two->path;
2443 if (opt->line_termination)
2444 path = quote_one(p->two->path);
2445 printf("%s%c", path, opt->line_termination);
2446 if (p->two->path != path)
2447 free(path);
2450 int diff_unmodified_pair(struct diff_filepair *p)
2452 /* This function is written stricter than necessary to support
2453 * the currently implemented transformers, but the idea is to
2454 * let transformers to produce diff_filepairs any way they want,
2455 * and filter and clean them up here before producing the output.
2457 struct diff_filespec *one, *two;
2459 if (DIFF_PAIR_UNMERGED(p))
2460 return 0; /* unmerged is interesting */
2462 one = p->one;
2463 two = p->two;
2465 /* deletion, addition, mode or type change
2466 * and rename are all interesting.
2468 if (DIFF_FILE_VALID(one) != DIFF_FILE_VALID(two) ||
2469 DIFF_PAIR_MODE_CHANGED(p) ||
2470 strcmp(one->path, two->path))
2471 return 0;
2473 /* both are valid and point at the same path. that is, we are
2474 * dealing with a change.
2476 if (one->sha1_valid && two->sha1_valid &&
2477 !hashcmp(one->sha1, two->sha1))
2478 return 1; /* no change */
2479 if (!one->sha1_valid && !two->sha1_valid)
2480 return 1; /* both look at the same file on the filesystem. */
2481 return 0;
2484 static void diff_flush_patch(struct diff_filepair *p, struct diff_options *o)
2486 if (diff_unmodified_pair(p))
2487 return;
2489 if ((DIFF_FILE_VALID(p->one) && S_ISDIR(p->one->mode)) ||
2490 (DIFF_FILE_VALID(p->two) && S_ISDIR(p->two->mode)))
2491 return; /* no tree diffs in patch format */
2493 run_diff(p, o);
2496 static void diff_flush_stat(struct diff_filepair *p, struct diff_options *o,
2497 struct diffstat_t *diffstat)
2499 if (diff_unmodified_pair(p))
2500 return;
2502 if ((DIFF_FILE_VALID(p->one) && S_ISDIR(p->one->mode)) ||
2503 (DIFF_FILE_VALID(p->two) && S_ISDIR(p->two->mode)))
2504 return; /* no tree diffs in patch format */
2506 run_diffstat(p, o, diffstat);
2509 static void diff_flush_checkdiff(struct diff_filepair *p,
2510 struct diff_options *o)
2512 if (diff_unmodified_pair(p))
2513 return;
2515 if ((DIFF_FILE_VALID(p->one) && S_ISDIR(p->one->mode)) ||
2516 (DIFF_FILE_VALID(p->two) && S_ISDIR(p->two->mode)))
2517 return; /* no tree diffs in patch format */
2519 run_checkdiff(p, o);
2522 int diff_queue_is_empty(void)
2524 struct diff_queue_struct *q = &diff_queued_diff;
2525 int i;
2526 for (i = 0; i < q->nr; i++)
2527 if (!diff_unmodified_pair(q->queue[i]))
2528 return 0;
2529 return 1;
2532 #if DIFF_DEBUG
2533 void diff_debug_filespec(struct diff_filespec *s, int x, const char *one)
2535 fprintf(stderr, "queue[%d] %s (%s) %s %06o %s\n",
2536 x, one ? one : "",
2537 s->path,
2538 DIFF_FILE_VALID(s) ? "valid" : "invalid",
2539 s->mode,
2540 s->sha1_valid ? sha1_to_hex(s->sha1) : "");
2541 fprintf(stderr, "queue[%d] %s size %lu flags %d\n",
2542 x, one ? one : "",
2543 s->size, s->xfrm_flags);
2546 void diff_debug_filepair(const struct diff_filepair *p, int i)
2548 diff_debug_filespec(p->one, i, "one");
2549 diff_debug_filespec(p->two, i, "two");
2550 fprintf(stderr, "score %d, status %c stays %d broken %d\n",
2551 p->score, p->status ? p->status : '?',
2552 p->source_stays, p->broken_pair);
2555 void diff_debug_queue(const char *msg, struct diff_queue_struct *q)
2557 int i;
2558 if (msg)
2559 fprintf(stderr, "%s\n", msg);
2560 fprintf(stderr, "q->nr = %d\n", q->nr);
2561 for (i = 0; i < q->nr; i++) {
2562 struct diff_filepair *p = q->queue[i];
2563 diff_debug_filepair(p, i);
2566 #endif
2568 static void diff_resolve_rename_copy(void)
2570 int i, j;
2571 struct diff_filepair *p, *pp;
2572 struct diff_queue_struct *q = &diff_queued_diff;
2574 diff_debug_queue("resolve-rename-copy", q);
2576 for (i = 0; i < q->nr; i++) {
2577 p = q->queue[i];
2578 p->status = 0; /* undecided */
2579 if (DIFF_PAIR_UNMERGED(p))
2580 p->status = DIFF_STATUS_UNMERGED;
2581 else if (!DIFF_FILE_VALID(p->one))
2582 p->status = DIFF_STATUS_ADDED;
2583 else if (!DIFF_FILE_VALID(p->two))
2584 p->status = DIFF_STATUS_DELETED;
2585 else if (DIFF_PAIR_TYPE_CHANGED(p))
2586 p->status = DIFF_STATUS_TYPE_CHANGED;
2588 /* from this point on, we are dealing with a pair
2589 * whose both sides are valid and of the same type, i.e.
2590 * either in-place edit or rename/copy edit.
2592 else if (DIFF_PAIR_RENAME(p)) {
2593 if (p->source_stays) {
2594 p->status = DIFF_STATUS_COPIED;
2595 continue;
2597 /* See if there is some other filepair that
2598 * copies from the same source as us. If so
2599 * we are a copy. Otherwise we are either a
2600 * copy if the path stays, or a rename if it
2601 * does not, but we already handled "stays" case.
2603 for (j = i + 1; j < q->nr; j++) {
2604 pp = q->queue[j];
2605 if (strcmp(pp->one->path, p->one->path))
2606 continue; /* not us */
2607 if (!DIFF_PAIR_RENAME(pp))
2608 continue; /* not a rename/copy */
2609 /* pp is a rename/copy from the same source */
2610 p->status = DIFF_STATUS_COPIED;
2611 break;
2613 if (!p->status)
2614 p->status = DIFF_STATUS_RENAMED;
2616 else if (hashcmp(p->one->sha1, p->two->sha1) ||
2617 p->one->mode != p->two->mode ||
2618 is_null_sha1(p->one->sha1))
2619 p->status = DIFF_STATUS_MODIFIED;
2620 else {
2621 /* This is a "no-change" entry and should not
2622 * happen anymore, but prepare for broken callers.
2624 error("feeding unmodified %s to diffcore",
2625 p->one->path);
2626 p->status = DIFF_STATUS_UNKNOWN;
2629 diff_debug_queue("resolve-rename-copy done", q);
2632 static int check_pair_status(struct diff_filepair *p)
2634 switch (p->status) {
2635 case DIFF_STATUS_UNKNOWN:
2636 return 0;
2637 case 0:
2638 die("internal error in diff-resolve-rename-copy");
2639 default:
2640 return 1;
2644 static void flush_one_pair(struct diff_filepair *p, struct diff_options *opt)
2646 int fmt = opt->output_format;
2648 if (fmt & DIFF_FORMAT_CHECKDIFF)
2649 diff_flush_checkdiff(p, opt);
2650 else if (fmt & (DIFF_FORMAT_RAW | DIFF_FORMAT_NAME_STATUS))
2651 diff_flush_raw(p, opt);
2652 else if (fmt & DIFF_FORMAT_NAME)
2653 diff_flush_name(p, opt);
2656 static void show_file_mode_name(const char *newdelete, struct diff_filespec *fs)
2658 char *name = quote_one(fs->path);
2659 if (fs->mode)
2660 printf(" %s mode %06o %s\n", newdelete, fs->mode, name);
2661 else
2662 printf(" %s %s\n", newdelete, name);
2663 free(name);
2667 static void show_mode_change(struct diff_filepair *p, int show_name)
2669 if (p->one->mode && p->two->mode && p->one->mode != p->two->mode) {
2670 if (show_name) {
2671 char *name = quote_one(p->two->path);
2672 printf(" mode change %06o => %06o %s\n",
2673 p->one->mode, p->two->mode, name);
2674 free(name);
2676 else
2677 printf(" mode change %06o => %06o\n",
2678 p->one->mode, p->two->mode);
2682 static void show_rename_copy(const char *renamecopy, struct diff_filepair *p)
2684 char *names = pprint_rename(p->one->path, p->two->path);
2686 printf(" %s %s (%d%%)\n", renamecopy, names,
2687 (int)(0.5 + p->score * 100.0/MAX_SCORE));
2688 free(names);
2689 show_mode_change(p, 0);
2692 static void diff_summary(struct diff_filepair *p)
2694 switch(p->status) {
2695 case DIFF_STATUS_DELETED:
2696 show_file_mode_name("delete", p->one);
2697 break;
2698 case DIFF_STATUS_ADDED:
2699 show_file_mode_name("create", p->two);
2700 break;
2701 case DIFF_STATUS_COPIED:
2702 show_rename_copy("copy", p);
2703 break;
2704 case DIFF_STATUS_RENAMED:
2705 show_rename_copy("rename", p);
2706 break;
2707 default:
2708 if (p->score) {
2709 char *name = quote_one(p->two->path);
2710 printf(" rewrite %s (%d%%)\n", name,
2711 (int)(0.5 + p->score * 100.0/MAX_SCORE));
2712 free(name);
2713 show_mode_change(p, 0);
2714 } else show_mode_change(p, 1);
2715 break;
2719 struct patch_id_t {
2720 struct xdiff_emit_state xm;
2721 SHA_CTX *ctx;
2722 int patchlen;
2725 static int remove_space(char *line, int len)
2727 int i;
2728 char *dst = line;
2729 unsigned char c;
2731 for (i = 0; i < len; i++)
2732 if (!isspace((c = line[i])))
2733 *dst++ = c;
2735 return dst - line;
2738 static void patch_id_consume(void *priv, char *line, unsigned long len)
2740 struct patch_id_t *data = priv;
2741 int new_len;
2743 /* Ignore line numbers when computing the SHA1 of the patch */
2744 if (!prefixcmp(line, "@@ -"))
2745 return;
2747 new_len = remove_space(line, len);
2749 SHA1_Update(data->ctx, line, new_len);
2750 data->patchlen += new_len;
2753 /* returns 0 upon success, and writes result into sha1 */
2754 static int diff_get_patch_id(struct diff_options *options, unsigned char *sha1)
2756 struct diff_queue_struct *q = &diff_queued_diff;
2757 int i;
2758 SHA_CTX ctx;
2759 struct patch_id_t data;
2760 char buffer[PATH_MAX * 4 + 20];
2762 SHA1_Init(&ctx);
2763 memset(&data, 0, sizeof(struct patch_id_t));
2764 data.ctx = &ctx;
2765 data.xm.consume = patch_id_consume;
2767 for (i = 0; i < q->nr; i++) {
2768 xpparam_t xpp;
2769 xdemitconf_t xecfg;
2770 xdemitcb_t ecb;
2771 mmfile_t mf1, mf2;
2772 struct diff_filepair *p = q->queue[i];
2773 int len1, len2;
2775 if (p->status == 0)
2776 return error("internal diff status error");
2777 if (p->status == DIFF_STATUS_UNKNOWN)
2778 continue;
2779 if (diff_unmodified_pair(p))
2780 continue;
2781 if ((DIFF_FILE_VALID(p->one) && S_ISDIR(p->one->mode)) ||
2782 (DIFF_FILE_VALID(p->two) && S_ISDIR(p->two->mode)))
2783 continue;
2784 if (DIFF_PAIR_UNMERGED(p))
2785 continue;
2787 diff_fill_sha1_info(p->one);
2788 diff_fill_sha1_info(p->two);
2789 if (fill_mmfile(&mf1, p->one) < 0 ||
2790 fill_mmfile(&mf2, p->two) < 0)
2791 return error("unable to read files to diff");
2793 /* Maybe hash p->two? into the patch id? */
2794 if (file_is_binary(p->two))
2795 continue;
2797 len1 = remove_space(p->one->path, strlen(p->one->path));
2798 len2 = remove_space(p->two->path, strlen(p->two->path));
2799 if (p->one->mode == 0)
2800 len1 = snprintf(buffer, sizeof(buffer),
2801 "diff--gita/%.*sb/%.*s"
2802 "newfilemode%06o"
2803 "---/dev/null"
2804 "+++b/%.*s",
2805 len1, p->one->path,
2806 len2, p->two->path,
2807 p->two->mode,
2808 len2, p->two->path);
2809 else if (p->two->mode == 0)
2810 len1 = snprintf(buffer, sizeof(buffer),
2811 "diff--gita/%.*sb/%.*s"
2812 "deletedfilemode%06o"
2813 "---a/%.*s"
2814 "+++/dev/null",
2815 len1, p->one->path,
2816 len2, p->two->path,
2817 p->one->mode,
2818 len1, p->one->path);
2819 else
2820 len1 = snprintf(buffer, sizeof(buffer),
2821 "diff--gita/%.*sb/%.*s"
2822 "---a/%.*s"
2823 "+++b/%.*s",
2824 len1, p->one->path,
2825 len2, p->two->path,
2826 len1, p->one->path,
2827 len2, p->two->path);
2828 SHA1_Update(&ctx, buffer, len1);
2830 xpp.flags = XDF_NEED_MINIMAL;
2831 xecfg.ctxlen = 3;
2832 xecfg.flags = XDL_EMIT_FUNCNAMES;
2833 ecb.outf = xdiff_outf;
2834 ecb.priv = &data;
2835 xdl_diff(&mf1, &mf2, &xpp, &xecfg, &ecb);
2838 SHA1_Final(sha1, &ctx);
2839 return 0;
2842 int diff_flush_patch_id(struct diff_options *options, unsigned char *sha1)
2844 struct diff_queue_struct *q = &diff_queued_diff;
2845 int i;
2846 int result = diff_get_patch_id(options, sha1);
2848 for (i = 0; i < q->nr; i++)
2849 diff_free_filepair(q->queue[i]);
2851 free(q->queue);
2852 q->queue = NULL;
2853 q->nr = q->alloc = 0;
2855 return result;
2858 static int is_summary_empty(const struct diff_queue_struct *q)
2860 int i;
2862 for (i = 0; i < q->nr; i++) {
2863 const struct diff_filepair *p = q->queue[i];
2865 switch (p->status) {
2866 case DIFF_STATUS_DELETED:
2867 case DIFF_STATUS_ADDED:
2868 case DIFF_STATUS_COPIED:
2869 case DIFF_STATUS_RENAMED:
2870 return 0;
2871 default:
2872 if (p->score)
2873 return 0;
2874 if (p->one->mode && p->two->mode &&
2875 p->one->mode != p->two->mode)
2876 return 0;
2877 break;
2880 return 1;
2883 void diff_flush(struct diff_options *options)
2885 struct diff_queue_struct *q = &diff_queued_diff;
2886 int i, output_format = options->output_format;
2887 int separator = 0;
2890 * Order: raw, stat, summary, patch
2891 * or: name/name-status/checkdiff (other bits clear)
2893 if (!q->nr)
2894 goto free_queue;
2896 if (output_format & (DIFF_FORMAT_RAW |
2897 DIFF_FORMAT_NAME |
2898 DIFF_FORMAT_NAME_STATUS |
2899 DIFF_FORMAT_CHECKDIFF)) {
2900 for (i = 0; i < q->nr; i++) {
2901 struct diff_filepair *p = q->queue[i];
2902 if (check_pair_status(p))
2903 flush_one_pair(p, options);
2905 separator++;
2908 if (output_format & (DIFF_FORMAT_DIFFSTAT|DIFF_FORMAT_SHORTSTAT|DIFF_FORMAT_NUMSTAT)) {
2909 struct diffstat_t diffstat;
2911 memset(&diffstat, 0, sizeof(struct diffstat_t));
2912 diffstat.xm.consume = diffstat_consume;
2913 for (i = 0; i < q->nr; i++) {
2914 struct diff_filepair *p = q->queue[i];
2915 if (check_pair_status(p))
2916 diff_flush_stat(p, options, &diffstat);
2918 if (output_format & DIFF_FORMAT_NUMSTAT)
2919 show_numstat(&diffstat, options);
2920 if (output_format & DIFF_FORMAT_DIFFSTAT)
2921 show_stats(&diffstat, options);
2922 else if (output_format & DIFF_FORMAT_SHORTSTAT)
2923 show_shortstats(&diffstat);
2924 separator++;
2927 if (output_format & DIFF_FORMAT_SUMMARY && !is_summary_empty(q)) {
2928 for (i = 0; i < q->nr; i++)
2929 diff_summary(q->queue[i]);
2930 separator++;
2933 if (output_format & DIFF_FORMAT_PATCH) {
2934 if (separator) {
2935 if (options->stat_sep) {
2936 /* attach patch instead of inline */
2937 fputs(options->stat_sep, stdout);
2938 } else {
2939 putchar(options->line_termination);
2943 for (i = 0; i < q->nr; i++) {
2944 struct diff_filepair *p = q->queue[i];
2945 if (check_pair_status(p))
2946 diff_flush_patch(p, options);
2950 if (output_format & DIFF_FORMAT_CALLBACK)
2951 options->format_callback(q, options, options->format_callback_data);
2953 for (i = 0; i < q->nr; i++)
2954 diff_free_filepair(q->queue[i]);
2955 free_queue:
2956 free(q->queue);
2957 q->queue = NULL;
2958 q->nr = q->alloc = 0;
2961 static void diffcore_apply_filter(const char *filter)
2963 int i;
2964 struct diff_queue_struct *q = &diff_queued_diff;
2965 struct diff_queue_struct outq;
2966 outq.queue = NULL;
2967 outq.nr = outq.alloc = 0;
2969 if (!filter)
2970 return;
2972 if (strchr(filter, DIFF_STATUS_FILTER_AON)) {
2973 int found;
2974 for (i = found = 0; !found && i < q->nr; i++) {
2975 struct diff_filepair *p = q->queue[i];
2976 if (((p->status == DIFF_STATUS_MODIFIED) &&
2977 ((p->score &&
2978 strchr(filter, DIFF_STATUS_FILTER_BROKEN)) ||
2979 (!p->score &&
2980 strchr(filter, DIFF_STATUS_MODIFIED)))) ||
2981 ((p->status != DIFF_STATUS_MODIFIED) &&
2982 strchr(filter, p->status)))
2983 found++;
2985 if (found)
2986 return;
2988 /* otherwise we will clear the whole queue
2989 * by copying the empty outq at the end of this
2990 * function, but first clear the current entries
2991 * in the queue.
2993 for (i = 0; i < q->nr; i++)
2994 diff_free_filepair(q->queue[i]);
2996 else {
2997 /* Only the matching ones */
2998 for (i = 0; i < q->nr; i++) {
2999 struct diff_filepair *p = q->queue[i];
3001 if (((p->status == DIFF_STATUS_MODIFIED) &&
3002 ((p->score &&
3003 strchr(filter, DIFF_STATUS_FILTER_BROKEN)) ||
3004 (!p->score &&
3005 strchr(filter, DIFF_STATUS_MODIFIED)))) ||
3006 ((p->status != DIFF_STATUS_MODIFIED) &&
3007 strchr(filter, p->status)))
3008 diff_q(&outq, p);
3009 else
3010 diff_free_filepair(p);
3013 free(q->queue);
3014 *q = outq;
3017 void diffcore_std(struct diff_options *options)
3019 if (options->quiet)
3020 return;
3021 if (options->break_opt != -1)
3022 diffcore_break(options->break_opt);
3023 if (options->detect_rename)
3024 diffcore_rename(options);
3025 if (options->break_opt != -1)
3026 diffcore_merge_broken();
3027 if (options->pickaxe)
3028 diffcore_pickaxe(options->pickaxe, options->pickaxe_opts);
3029 if (options->orderfile)
3030 diffcore_order(options->orderfile);
3031 diff_resolve_rename_copy();
3032 diffcore_apply_filter(options->filter);
3034 options->has_changes = !!diff_queued_diff.nr;
3038 void diff_addremove(struct diff_options *options,
3039 int addremove, unsigned mode,
3040 const unsigned char *sha1,
3041 const char *base, const char *path)
3043 char concatpath[PATH_MAX];
3044 struct diff_filespec *one, *two;
3046 /* This may look odd, but it is a preparation for
3047 * feeding "there are unchanged files which should
3048 * not produce diffs, but when you are doing copy
3049 * detection you would need them, so here they are"
3050 * entries to the diff-core. They will be prefixed
3051 * with something like '=' or '*' (I haven't decided
3052 * which but should not make any difference).
3053 * Feeding the same new and old to diff_change()
3054 * also has the same effect.
3055 * Before the final output happens, they are pruned after
3056 * merged into rename/copy pairs as appropriate.
3058 if (options->reverse_diff)
3059 addremove = (addremove == '+' ? '-' :
3060 addremove == '-' ? '+' : addremove);
3062 if (!path) path = "";
3063 sprintf(concatpath, "%s%s", base, path);
3064 one = alloc_filespec(concatpath);
3065 two = alloc_filespec(concatpath);
3067 if (addremove != '+')
3068 fill_filespec(one, sha1, mode);
3069 if (addremove != '-')
3070 fill_filespec(two, sha1, mode);
3072 diff_queue(&diff_queued_diff, one, two);
3073 options->has_changes = 1;
3076 void diff_change(struct diff_options *options,
3077 unsigned old_mode, unsigned new_mode,
3078 const unsigned char *old_sha1,
3079 const unsigned char *new_sha1,
3080 const char *base, const char *path)
3082 char concatpath[PATH_MAX];
3083 struct diff_filespec *one, *two;
3085 if (options->reverse_diff) {
3086 unsigned tmp;
3087 const unsigned char *tmp_c;
3088 tmp = old_mode; old_mode = new_mode; new_mode = tmp;
3089 tmp_c = old_sha1; old_sha1 = new_sha1; new_sha1 = tmp_c;
3091 if (!path) path = "";
3092 sprintf(concatpath, "%s%s", base, path);
3093 one = alloc_filespec(concatpath);
3094 two = alloc_filespec(concatpath);
3095 fill_filespec(one, old_sha1, old_mode);
3096 fill_filespec(two, new_sha1, new_mode);
3098 diff_queue(&diff_queued_diff, one, two);
3099 options->has_changes = 1;
3102 void diff_unmerge(struct diff_options *options,
3103 const char *path,
3104 unsigned mode, const unsigned char *sha1)
3106 struct diff_filespec *one, *two;
3107 one = alloc_filespec(path);
3108 two = alloc_filespec(path);
3109 fill_filespec(one, sha1, mode);
3110 diff_queue(&diff_queued_diff, one, two)->is_unmerged = 1;