Merge branch 'master' of git://git.kernel.org/pub/scm/git/git
[git/jnareb-git.git] / diff.c
blob57165e24722603267d06bcd2ba889dee12022e77
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 static int file_is_binary(struct diff_filespec *one)
1126 struct git_attr_check attr_diff_check;
1128 setup_diff_attr_check(&attr_diff_check);
1129 if (!git_checkattr(one->path, 1, &attr_diff_check)) {
1130 const char *value = attr_diff_check.value;
1131 if (ATTR_TRUE(value))
1132 return 0;
1133 else if (ATTR_FALSE(value))
1134 return 1;
1137 if (!one->data) {
1138 if (!DIFF_FILE_VALID(one))
1139 return 0;
1140 diff_populate_filespec(one, 0);
1142 return buffer_is_binary(one->data, one->size);
1145 static void builtin_diff(const char *name_a,
1146 const char *name_b,
1147 struct diff_filespec *one,
1148 struct diff_filespec *two,
1149 const char *xfrm_msg,
1150 struct diff_options *o,
1151 int complete_rewrite)
1153 mmfile_t mf1, mf2;
1154 const char *lbl[2];
1155 char *a_one, *b_two;
1156 const char *set = diff_get_color(o->color_diff, DIFF_METAINFO);
1157 const char *reset = diff_get_color(o->color_diff, DIFF_RESET);
1159 a_one = quote_two("a/", name_a + (*name_a == '/'));
1160 b_two = quote_two("b/", name_b + (*name_b == '/'));
1161 lbl[0] = DIFF_FILE_VALID(one) ? a_one : "/dev/null";
1162 lbl[1] = DIFF_FILE_VALID(two) ? b_two : "/dev/null";
1163 printf("%sdiff --git %s %s%s\n", set, a_one, b_two, reset);
1164 if (lbl[0][0] == '/') {
1165 /* /dev/null */
1166 printf("%snew file mode %06o%s\n", set, two->mode, reset);
1167 if (xfrm_msg && xfrm_msg[0])
1168 printf("%s%s%s\n", set, xfrm_msg, reset);
1170 else if (lbl[1][0] == '/') {
1171 printf("%sdeleted file mode %06o%s\n", set, one->mode, reset);
1172 if (xfrm_msg && xfrm_msg[0])
1173 printf("%s%s%s\n", set, xfrm_msg, reset);
1175 else {
1176 if (one->mode != two->mode) {
1177 printf("%sold mode %06o%s\n", set, one->mode, reset);
1178 printf("%snew mode %06o%s\n", set, two->mode, reset);
1180 if (xfrm_msg && xfrm_msg[0])
1181 printf("%s%s%s\n", set, xfrm_msg, reset);
1183 * we do not run diff between different kind
1184 * of objects.
1186 if ((one->mode ^ two->mode) & S_IFMT)
1187 goto free_ab_and_return;
1188 if (complete_rewrite) {
1189 emit_rewrite_diff(name_a, name_b, one, two,
1190 o->color_diff);
1191 o->found_changes = 1;
1192 goto free_ab_and_return;
1196 if (fill_mmfile(&mf1, one) < 0 || fill_mmfile(&mf2, two) < 0)
1197 die("unable to read files to diff");
1199 if (!o->text && (file_is_binary(one) || file_is_binary(two))) {
1200 /* Quite common confusing case */
1201 if (mf1.size == mf2.size &&
1202 !memcmp(mf1.ptr, mf2.ptr, mf1.size))
1203 goto free_ab_and_return;
1204 if (o->binary)
1205 emit_binary_diff(&mf1, &mf2);
1206 else
1207 printf("Binary files %s and %s differ\n",
1208 lbl[0], lbl[1]);
1209 o->found_changes = 1;
1211 else {
1212 /* Crazy xdl interfaces.. */
1213 const char *diffopts = getenv("GIT_DIFF_OPTS");
1214 xpparam_t xpp;
1215 xdemitconf_t xecfg;
1216 xdemitcb_t ecb;
1217 struct emit_callback ecbdata;
1219 memset(&ecbdata, 0, sizeof(ecbdata));
1220 ecbdata.label_path = lbl;
1221 ecbdata.color_diff = o->color_diff;
1222 ecbdata.found_changesp = &o->found_changes;
1223 xpp.flags = XDF_NEED_MINIMAL | o->xdl_opts;
1224 xecfg.ctxlen = o->context;
1225 xecfg.flags = XDL_EMIT_FUNCNAMES;
1226 if (!diffopts)
1228 else if (!prefixcmp(diffopts, "--unified="))
1229 xecfg.ctxlen = strtoul(diffopts + 10, NULL, 10);
1230 else if (!prefixcmp(diffopts, "-u"))
1231 xecfg.ctxlen = strtoul(diffopts + 2, NULL, 10);
1232 ecb.outf = xdiff_outf;
1233 ecb.priv = &ecbdata;
1234 ecbdata.xm.consume = fn_out_consume;
1235 if (o->color_diff_words)
1236 ecbdata.diff_words =
1237 xcalloc(1, sizeof(struct diff_words_data));
1238 xdl_diff(&mf1, &mf2, &xpp, &xecfg, &ecb);
1239 if (o->color_diff_words)
1240 free_diff_words_data(&ecbdata);
1243 free_ab_and_return:
1244 diff_free_filespec_data(one);
1245 diff_free_filespec_data(two);
1246 free(a_one);
1247 free(b_two);
1248 return;
1251 static void builtin_diffstat(const char *name_a, const char *name_b,
1252 struct diff_filespec *one,
1253 struct diff_filespec *two,
1254 struct diffstat_t *diffstat,
1255 struct diff_options *o,
1256 int complete_rewrite)
1258 mmfile_t mf1, mf2;
1259 struct diffstat_file *data;
1261 data = diffstat_add(diffstat, name_a, name_b);
1263 if (!one || !two) {
1264 data->is_unmerged = 1;
1265 return;
1267 if (complete_rewrite) {
1268 diff_populate_filespec(one, 0);
1269 diff_populate_filespec(two, 0);
1270 data->deleted = count_lines(one->data, one->size);
1271 data->added = count_lines(two->data, two->size);
1272 goto free_and_return;
1274 if (fill_mmfile(&mf1, one) < 0 || fill_mmfile(&mf2, two) < 0)
1275 die("unable to read files to diff");
1277 if (file_is_binary(one) || file_is_binary(two)) {
1278 data->is_binary = 1;
1279 data->added = mf2.size;
1280 data->deleted = mf1.size;
1281 } else {
1282 /* Crazy xdl interfaces.. */
1283 xpparam_t xpp;
1284 xdemitconf_t xecfg;
1285 xdemitcb_t ecb;
1287 xpp.flags = XDF_NEED_MINIMAL | o->xdl_opts;
1288 xecfg.ctxlen = 0;
1289 xecfg.flags = 0;
1290 ecb.outf = xdiff_outf;
1291 ecb.priv = diffstat;
1292 xdl_diff(&mf1, &mf2, &xpp, &xecfg, &ecb);
1295 free_and_return:
1296 diff_free_filespec_data(one);
1297 diff_free_filespec_data(two);
1300 static void builtin_checkdiff(const char *name_a, const char *name_b,
1301 struct diff_filespec *one,
1302 struct diff_filespec *two, struct diff_options *o)
1304 mmfile_t mf1, mf2;
1305 struct checkdiff_t data;
1307 if (!two)
1308 return;
1310 memset(&data, 0, sizeof(data));
1311 data.xm.consume = checkdiff_consume;
1312 data.filename = name_b ? name_b : name_a;
1313 data.lineno = 0;
1314 data.color_diff = o->color_diff;
1316 if (fill_mmfile(&mf1, one) < 0 || fill_mmfile(&mf2, two) < 0)
1317 die("unable to read files to diff");
1319 if (file_is_binary(two))
1320 goto free_and_return;
1321 else {
1322 /* Crazy xdl interfaces.. */
1323 xpparam_t xpp;
1324 xdemitconf_t xecfg;
1325 xdemitcb_t ecb;
1327 xpp.flags = XDF_NEED_MINIMAL;
1328 xecfg.ctxlen = 0;
1329 xecfg.flags = 0;
1330 ecb.outf = xdiff_outf;
1331 ecb.priv = &data;
1332 xdl_diff(&mf1, &mf2, &xpp, &xecfg, &ecb);
1334 free_and_return:
1335 diff_free_filespec_data(one);
1336 diff_free_filespec_data(two);
1339 struct diff_filespec *alloc_filespec(const char *path)
1341 int namelen = strlen(path);
1342 struct diff_filespec *spec = xmalloc(sizeof(*spec) + namelen + 1);
1344 memset(spec, 0, sizeof(*spec));
1345 spec->path = (char *)(spec + 1);
1346 memcpy(spec->path, path, namelen+1);
1347 return spec;
1350 void fill_filespec(struct diff_filespec *spec, const unsigned char *sha1,
1351 unsigned short mode)
1353 if (mode) {
1354 spec->mode = canon_mode(mode);
1355 hashcpy(spec->sha1, sha1);
1356 spec->sha1_valid = !is_null_sha1(sha1);
1361 * Given a name and sha1 pair, if the index tells us the file in
1362 * the work tree has that object contents, return true, so that
1363 * prepare_temp_file() does not have to inflate and extract.
1365 static int reuse_worktree_file(const char *name, const unsigned char *sha1, int want_file)
1367 struct cache_entry *ce;
1368 struct stat st;
1369 int pos, len;
1371 /* We do not read the cache ourselves here, because the
1372 * benchmark with my previous version that always reads cache
1373 * shows that it makes things worse for diff-tree comparing
1374 * two linux-2.6 kernel trees in an already checked out work
1375 * tree. This is because most diff-tree comparisons deal with
1376 * only a small number of files, while reading the cache is
1377 * expensive for a large project, and its cost outweighs the
1378 * savings we get by not inflating the object to a temporary
1379 * file. Practically, this code only helps when we are used
1380 * by diff-cache --cached, which does read the cache before
1381 * calling us.
1383 if (!active_cache)
1384 return 0;
1386 /* We want to avoid the working directory if our caller
1387 * doesn't need the data in a normal file, this system
1388 * is rather slow with its stat/open/mmap/close syscalls,
1389 * and the object is contained in a pack file. The pack
1390 * is probably already open and will be faster to obtain
1391 * the data through than the working directory. Loose
1392 * objects however would tend to be slower as they need
1393 * to be individually opened and inflated.
1395 if (!FAST_WORKING_DIRECTORY && !want_file && has_sha1_pack(sha1, NULL))
1396 return 0;
1398 len = strlen(name);
1399 pos = cache_name_pos(name, len);
1400 if (pos < 0)
1401 return 0;
1402 ce = active_cache[pos];
1403 if ((lstat(name, &st) < 0) ||
1404 !S_ISREG(st.st_mode) || /* careful! */
1405 ce_match_stat(ce, &st, 0) ||
1406 hashcmp(sha1, ce->sha1))
1407 return 0;
1408 /* we return 1 only when we can stat, it is a regular file,
1409 * stat information matches, and sha1 recorded in the cache
1410 * matches. I.e. we know the file in the work tree really is
1411 * the same as the <name, sha1> pair.
1413 return 1;
1416 static int populate_from_stdin(struct diff_filespec *s)
1418 #define INCREMENT 1024
1419 char *buf;
1420 unsigned long size;
1421 ssize_t got;
1423 size = 0;
1424 buf = NULL;
1425 while (1) {
1426 buf = xrealloc(buf, size + INCREMENT);
1427 got = xread(0, buf + size, INCREMENT);
1428 if (!got)
1429 break; /* EOF */
1430 if (got < 0)
1431 return error("error while reading from stdin %s",
1432 strerror(errno));
1433 size += got;
1435 s->should_munmap = 0;
1436 s->data = buf;
1437 s->size = size;
1438 s->should_free = 1;
1439 return 0;
1442 static int diff_populate_gitlink(struct diff_filespec *s, int size_only)
1444 int len;
1445 char *data = xmalloc(100);
1446 len = snprintf(data, 100,
1447 "Subproject commit %s\n", sha1_to_hex(s->sha1));
1448 s->data = data;
1449 s->size = len;
1450 s->should_free = 1;
1451 if (size_only) {
1452 s->data = NULL;
1453 free(data);
1455 return 0;
1459 * While doing rename detection and pickaxe operation, we may need to
1460 * grab the data for the blob (or file) for our own in-core comparison.
1461 * diff_filespec has data and size fields for this purpose.
1463 int diff_populate_filespec(struct diff_filespec *s, int size_only)
1465 int err = 0;
1466 if (!DIFF_FILE_VALID(s))
1467 die("internal error: asking to populate invalid file.");
1468 if (S_ISDIR(s->mode))
1469 return -1;
1471 if (s->data)
1472 return 0;
1474 if (size_only && 0 < s->size)
1475 return 0;
1477 if (S_ISGITLINK(s->mode))
1478 return diff_populate_gitlink(s, size_only);
1480 if (!s->sha1_valid ||
1481 reuse_worktree_file(s->path, s->sha1, 0)) {
1482 struct stat st;
1483 int fd;
1484 char *buf;
1485 unsigned long size;
1487 if (!strcmp(s->path, "-"))
1488 return populate_from_stdin(s);
1490 if (lstat(s->path, &st) < 0) {
1491 if (errno == ENOENT) {
1492 err_empty:
1493 err = -1;
1494 empty:
1495 s->data = (char *)"";
1496 s->size = 0;
1497 return err;
1500 s->size = xsize_t(st.st_size);
1501 if (!s->size)
1502 goto empty;
1503 if (size_only)
1504 return 0;
1505 if (S_ISLNK(st.st_mode)) {
1506 int ret;
1507 s->data = xmalloc(s->size);
1508 s->should_free = 1;
1509 ret = readlink(s->path, s->data, s->size);
1510 if (ret < 0) {
1511 free(s->data);
1512 goto err_empty;
1514 return 0;
1516 fd = open(s->path, O_RDONLY);
1517 if (fd < 0)
1518 goto err_empty;
1519 s->data = xmmap(NULL, s->size, PROT_READ, MAP_PRIVATE, fd, 0);
1520 close(fd);
1521 s->should_munmap = 1;
1524 * Convert from working tree format to canonical git format
1526 size = s->size;
1527 buf = convert_to_git(s->path, s->data, &size);
1528 if (buf) {
1529 munmap(s->data, s->size);
1530 s->should_munmap = 0;
1531 s->data = buf;
1532 s->size = size;
1533 s->should_free = 1;
1536 else {
1537 enum object_type type;
1538 if (size_only)
1539 type = sha1_object_info(s->sha1, &s->size);
1540 else {
1541 s->data = read_sha1_file(s->sha1, &type, &s->size);
1542 s->should_free = 1;
1545 return 0;
1548 void diff_free_filespec_data(struct diff_filespec *s)
1550 if (s->should_free)
1551 free(s->data);
1552 else if (s->should_munmap)
1553 munmap(s->data, s->size);
1555 if (s->should_free || s->should_munmap) {
1556 s->should_free = s->should_munmap = 0;
1557 s->data = NULL;
1559 free(s->cnt_data);
1560 s->cnt_data = NULL;
1563 static void prep_temp_blob(struct diff_tempfile *temp,
1564 void *blob,
1565 unsigned long size,
1566 const unsigned char *sha1,
1567 int mode)
1569 int fd;
1571 fd = git_mkstemp(temp->tmp_path, PATH_MAX, ".diff_XXXXXX");
1572 if (fd < 0)
1573 die("unable to create temp-file");
1574 if (write_in_full(fd, blob, size) != size)
1575 die("unable to write temp-file");
1576 close(fd);
1577 temp->name = temp->tmp_path;
1578 strcpy(temp->hex, sha1_to_hex(sha1));
1579 temp->hex[40] = 0;
1580 sprintf(temp->mode, "%06o", mode);
1583 static void prepare_temp_file(const char *name,
1584 struct diff_tempfile *temp,
1585 struct diff_filespec *one)
1587 if (!DIFF_FILE_VALID(one)) {
1588 not_a_valid_file:
1589 /* A '-' entry produces this for file-2, and
1590 * a '+' entry produces this for file-1.
1592 temp->name = "/dev/null";
1593 strcpy(temp->hex, ".");
1594 strcpy(temp->mode, ".");
1595 return;
1598 if (!one->sha1_valid ||
1599 reuse_worktree_file(name, one->sha1, 1)) {
1600 struct stat st;
1601 if (lstat(name, &st) < 0) {
1602 if (errno == ENOENT)
1603 goto not_a_valid_file;
1604 die("stat(%s): %s", name, strerror(errno));
1606 if (S_ISLNK(st.st_mode)) {
1607 int ret;
1608 char buf[PATH_MAX + 1]; /* ought to be SYMLINK_MAX */
1609 size_t sz = xsize_t(st.st_size);
1610 if (sizeof(buf) <= st.st_size)
1611 die("symlink too long: %s", name);
1612 ret = readlink(name, buf, sz);
1613 if (ret < 0)
1614 die("readlink(%s)", name);
1615 prep_temp_blob(temp, buf, sz,
1616 (one->sha1_valid ?
1617 one->sha1 : null_sha1),
1618 (one->sha1_valid ?
1619 one->mode : S_IFLNK));
1621 else {
1622 /* we can borrow from the file in the work tree */
1623 temp->name = name;
1624 if (!one->sha1_valid)
1625 strcpy(temp->hex, sha1_to_hex(null_sha1));
1626 else
1627 strcpy(temp->hex, sha1_to_hex(one->sha1));
1628 /* Even though we may sometimes borrow the
1629 * contents from the work tree, we always want
1630 * one->mode. mode is trustworthy even when
1631 * !(one->sha1_valid), as long as
1632 * DIFF_FILE_VALID(one).
1634 sprintf(temp->mode, "%06o", one->mode);
1636 return;
1638 else {
1639 if (diff_populate_filespec(one, 0))
1640 die("cannot read data blob for %s", one->path);
1641 prep_temp_blob(temp, one->data, one->size,
1642 one->sha1, one->mode);
1646 static void remove_tempfile(void)
1648 int i;
1650 for (i = 0; i < 2; i++)
1651 if (diff_temp[i].name == diff_temp[i].tmp_path) {
1652 unlink(diff_temp[i].name);
1653 diff_temp[i].name = NULL;
1657 static void remove_tempfile_on_signal(int signo)
1659 remove_tempfile();
1660 signal(SIGINT, SIG_DFL);
1661 raise(signo);
1664 static int spawn_prog(const char *pgm, const char **arg)
1666 pid_t pid;
1667 int status;
1669 fflush(NULL);
1670 pid = fork();
1671 if (pid < 0)
1672 die("unable to fork");
1673 if (!pid) {
1674 execvp(pgm, (char *const*) arg);
1675 exit(255);
1678 while (waitpid(pid, &status, 0) < 0) {
1679 if (errno == EINTR)
1680 continue;
1681 return -1;
1684 /* Earlier we did not check the exit status because
1685 * diff exits non-zero if files are different, and
1686 * we are not interested in knowing that. It was a
1687 * mistake which made it harder to quit a diff-*
1688 * session that uses the git-apply-patch-script as
1689 * the GIT_EXTERNAL_DIFF. A custom GIT_EXTERNAL_DIFF
1690 * should also exit non-zero only when it wants to
1691 * abort the entire diff-* session.
1693 if (WIFEXITED(status) && !WEXITSTATUS(status))
1694 return 0;
1695 return -1;
1698 /* An external diff command takes:
1700 * diff-cmd name infile1 infile1-sha1 infile1-mode \
1701 * infile2 infile2-sha1 infile2-mode [ rename-to ]
1704 static void run_external_diff(const char *pgm,
1705 const char *name,
1706 const char *other,
1707 struct diff_filespec *one,
1708 struct diff_filespec *two,
1709 const char *xfrm_msg,
1710 int complete_rewrite)
1712 const char *spawn_arg[10];
1713 struct diff_tempfile *temp = diff_temp;
1714 int retval;
1715 static int atexit_asked = 0;
1716 const char *othername;
1717 const char **arg = &spawn_arg[0];
1719 othername = (other? other : name);
1720 if (one && two) {
1721 prepare_temp_file(name, &temp[0], one);
1722 prepare_temp_file(othername, &temp[1], two);
1723 if (! atexit_asked &&
1724 (temp[0].name == temp[0].tmp_path ||
1725 temp[1].name == temp[1].tmp_path)) {
1726 atexit_asked = 1;
1727 atexit(remove_tempfile);
1729 signal(SIGINT, remove_tempfile_on_signal);
1732 if (one && two) {
1733 *arg++ = pgm;
1734 *arg++ = name;
1735 *arg++ = temp[0].name;
1736 *arg++ = temp[0].hex;
1737 *arg++ = temp[0].mode;
1738 *arg++ = temp[1].name;
1739 *arg++ = temp[1].hex;
1740 *arg++ = temp[1].mode;
1741 if (other) {
1742 *arg++ = other;
1743 *arg++ = xfrm_msg;
1745 } else {
1746 *arg++ = pgm;
1747 *arg++ = name;
1749 *arg = NULL;
1750 retval = spawn_prog(pgm, spawn_arg);
1751 remove_tempfile();
1752 if (retval) {
1753 fprintf(stderr, "external diff died, stopping at %s.\n", name);
1754 exit(1);
1758 static const char *external_diff_attr(const char *name)
1760 struct git_attr_check attr_diff_check;
1762 setup_diff_attr_check(&attr_diff_check);
1763 if (!git_checkattr(name, 1, &attr_diff_check)) {
1764 const char *value = attr_diff_check.value;
1765 if (!ATTR_TRUE(value) &&
1766 !ATTR_FALSE(value) &&
1767 !ATTR_UNSET(value)) {
1768 struct ll_diff_driver *drv;
1770 if (!user_diff_tail) {
1771 user_diff_tail = &user_diff;
1772 git_config(git_diff_ui_config);
1774 for (drv = user_diff; drv; drv = drv->next)
1775 if (!strcmp(drv->name, value))
1776 return drv->cmd;
1779 return NULL;
1782 static void run_diff_cmd(const char *pgm,
1783 const char *name,
1784 const char *other,
1785 struct diff_filespec *one,
1786 struct diff_filespec *two,
1787 const char *xfrm_msg,
1788 struct diff_options *o,
1789 int complete_rewrite)
1791 if (!o->allow_external)
1792 pgm = NULL;
1793 else {
1794 const char *cmd = external_diff_attr(name);
1795 if (cmd)
1796 pgm = cmd;
1799 if (pgm) {
1800 run_external_diff(pgm, name, other, one, two, xfrm_msg,
1801 complete_rewrite);
1802 return;
1804 if (one && two)
1805 builtin_diff(name, other ? other : name,
1806 one, two, xfrm_msg, o, complete_rewrite);
1807 else
1808 printf("* Unmerged path %s\n", name);
1811 static void diff_fill_sha1_info(struct diff_filespec *one)
1813 if (DIFF_FILE_VALID(one)) {
1814 if (!one->sha1_valid) {
1815 struct stat st;
1816 if (!strcmp(one->path, "-")) {
1817 hashcpy(one->sha1, null_sha1);
1818 return;
1820 if (lstat(one->path, &st) < 0)
1821 die("stat %s", one->path);
1822 if (index_path(one->sha1, one->path, &st, 0))
1823 die("cannot hash %s\n", one->path);
1826 else
1827 hashclr(one->sha1);
1830 static void run_diff(struct diff_filepair *p, struct diff_options *o)
1832 const char *pgm = external_diff();
1833 char msg[PATH_MAX*2+300], *xfrm_msg;
1834 struct diff_filespec *one;
1835 struct diff_filespec *two;
1836 const char *name;
1837 const char *other;
1838 char *name_munged, *other_munged;
1839 int complete_rewrite = 0;
1840 int len;
1842 if (DIFF_PAIR_UNMERGED(p)) {
1843 /* unmerged */
1844 run_diff_cmd(pgm, p->one->path, NULL, NULL, NULL, NULL, o, 0);
1845 return;
1848 name = p->one->path;
1849 other = (strcmp(name, p->two->path) ? p->two->path : NULL);
1850 name_munged = quote_one(name);
1851 other_munged = quote_one(other);
1852 one = p->one; two = p->two;
1854 diff_fill_sha1_info(one);
1855 diff_fill_sha1_info(two);
1857 len = 0;
1858 switch (p->status) {
1859 case DIFF_STATUS_COPIED:
1860 len += snprintf(msg + len, sizeof(msg) - len,
1861 "similarity index %d%%\n"
1862 "copy from %s\n"
1863 "copy to %s\n",
1864 (int)(0.5 + p->score * 100.0/MAX_SCORE),
1865 name_munged, other_munged);
1866 break;
1867 case DIFF_STATUS_RENAMED:
1868 len += snprintf(msg + len, sizeof(msg) - len,
1869 "similarity index %d%%\n"
1870 "rename from %s\n"
1871 "rename to %s\n",
1872 (int)(0.5 + p->score * 100.0/MAX_SCORE),
1873 name_munged, other_munged);
1874 break;
1875 case DIFF_STATUS_MODIFIED:
1876 if (p->score) {
1877 len += snprintf(msg + len, sizeof(msg) - len,
1878 "dissimilarity index %d%%\n",
1879 (int)(0.5 + p->score *
1880 100.0/MAX_SCORE));
1881 complete_rewrite = 1;
1882 break;
1884 /* fallthru */
1885 default:
1886 /* nothing */
1890 if (hashcmp(one->sha1, two->sha1)) {
1891 int abbrev = o->full_index ? 40 : DEFAULT_ABBREV;
1893 if (o->binary) {
1894 mmfile_t mf;
1895 if ((!fill_mmfile(&mf, one) && file_is_binary(one)) ||
1896 (!fill_mmfile(&mf, two) && file_is_binary(two)))
1897 abbrev = 40;
1899 len += snprintf(msg + len, sizeof(msg) - len,
1900 "index %.*s..%.*s",
1901 abbrev, sha1_to_hex(one->sha1),
1902 abbrev, sha1_to_hex(two->sha1));
1903 if (one->mode == two->mode)
1904 len += snprintf(msg + len, sizeof(msg) - len,
1905 " %06o", one->mode);
1906 len += snprintf(msg + len, sizeof(msg) - len, "\n");
1909 if (len)
1910 msg[--len] = 0;
1911 xfrm_msg = len ? msg : NULL;
1913 if (!pgm &&
1914 DIFF_FILE_VALID(one) && DIFF_FILE_VALID(two) &&
1915 (S_IFMT & one->mode) != (S_IFMT & two->mode)) {
1916 /* a filepair that changes between file and symlink
1917 * needs to be split into deletion and creation.
1919 struct diff_filespec *null = alloc_filespec(two->path);
1920 run_diff_cmd(NULL, name, other, one, null, xfrm_msg, o, 0);
1921 free(null);
1922 null = alloc_filespec(one->path);
1923 run_diff_cmd(NULL, name, other, null, two, xfrm_msg, o, 0);
1924 free(null);
1926 else
1927 run_diff_cmd(pgm, name, other, one, two, xfrm_msg, o,
1928 complete_rewrite);
1930 free(name_munged);
1931 free(other_munged);
1934 static void run_diffstat(struct diff_filepair *p, struct diff_options *o,
1935 struct diffstat_t *diffstat)
1937 const char *name;
1938 const char *other;
1939 int complete_rewrite = 0;
1941 if (DIFF_PAIR_UNMERGED(p)) {
1942 /* unmerged */
1943 builtin_diffstat(p->one->path, NULL, NULL, NULL, diffstat, o, 0);
1944 return;
1947 name = p->one->path;
1948 other = (strcmp(name, p->two->path) ? p->two->path : NULL);
1950 diff_fill_sha1_info(p->one);
1951 diff_fill_sha1_info(p->two);
1953 if (p->status == DIFF_STATUS_MODIFIED && p->score)
1954 complete_rewrite = 1;
1955 builtin_diffstat(name, other, p->one, p->two, diffstat, o, complete_rewrite);
1958 static void run_checkdiff(struct diff_filepair *p, struct diff_options *o)
1960 const char *name;
1961 const char *other;
1963 if (DIFF_PAIR_UNMERGED(p)) {
1964 /* unmerged */
1965 return;
1968 name = p->one->path;
1969 other = (strcmp(name, p->two->path) ? p->two->path : NULL);
1971 diff_fill_sha1_info(p->one);
1972 diff_fill_sha1_info(p->two);
1974 builtin_checkdiff(name, other, p->one, p->two, o);
1977 void diff_setup(struct diff_options *options)
1979 memset(options, 0, sizeof(*options));
1980 options->line_termination = '\n';
1981 options->break_opt = -1;
1982 options->rename_limit = -1;
1983 options->context = 3;
1984 options->msg_sep = "";
1986 options->change = diff_change;
1987 options->add_remove = diff_addremove;
1988 options->color_diff = diff_use_color_default;
1989 options->detect_rename = diff_detect_rename_default;
1992 int diff_setup_done(struct diff_options *options)
1994 int count = 0;
1996 if (options->output_format & DIFF_FORMAT_NAME)
1997 count++;
1998 if (options->output_format & DIFF_FORMAT_NAME_STATUS)
1999 count++;
2000 if (options->output_format & DIFF_FORMAT_CHECKDIFF)
2001 count++;
2002 if (options->output_format & DIFF_FORMAT_NO_OUTPUT)
2003 count++;
2004 if (count > 1)
2005 die("--name-only, --name-status, --check and -s are mutually exclusive");
2007 if (options->find_copies_harder)
2008 options->detect_rename = DIFF_DETECT_COPY;
2010 if (options->output_format & (DIFF_FORMAT_NAME |
2011 DIFF_FORMAT_NAME_STATUS |
2012 DIFF_FORMAT_CHECKDIFF |
2013 DIFF_FORMAT_NO_OUTPUT))
2014 options->output_format &= ~(DIFF_FORMAT_RAW |
2015 DIFF_FORMAT_NUMSTAT |
2016 DIFF_FORMAT_DIFFSTAT |
2017 DIFF_FORMAT_SHORTSTAT |
2018 DIFF_FORMAT_SUMMARY |
2019 DIFF_FORMAT_PATCH);
2022 * These cases always need recursive; we do not drop caller-supplied
2023 * recursive bits for other formats here.
2025 if (options->output_format & (DIFF_FORMAT_PATCH |
2026 DIFF_FORMAT_NUMSTAT |
2027 DIFF_FORMAT_DIFFSTAT |
2028 DIFF_FORMAT_SHORTSTAT |
2029 DIFF_FORMAT_SUMMARY |
2030 DIFF_FORMAT_CHECKDIFF))
2031 options->recursive = 1;
2033 * Also pickaxe would not work very well if you do not say recursive
2035 if (options->pickaxe)
2036 options->recursive = 1;
2038 if (options->detect_rename && options->rename_limit < 0)
2039 options->rename_limit = diff_rename_limit_default;
2040 if (options->setup & DIFF_SETUP_USE_CACHE) {
2041 if (!active_cache)
2042 /* read-cache does not die even when it fails
2043 * so it is safe for us to do this here. Also
2044 * it does not smudge active_cache or active_nr
2045 * when it fails, so we do not have to worry about
2046 * cleaning it up ourselves either.
2048 read_cache();
2050 if (options->abbrev <= 0 || 40 < options->abbrev)
2051 options->abbrev = 40; /* full */
2054 * It does not make sense to show the first hit we happened
2055 * to have found. It does not make sense not to return with
2056 * exit code in such a case either.
2058 if (options->quiet) {
2059 options->output_format = DIFF_FORMAT_NO_OUTPUT;
2060 options->exit_with_status = 1;
2064 * If we postprocess in diffcore, we cannot simply return
2065 * upon the first hit. We need to run diff as usual.
2067 if (options->pickaxe || options->filter)
2068 options->quiet = 0;
2070 return 0;
2073 static int opt_arg(const char *arg, int arg_short, const char *arg_long, int *val)
2075 char c, *eq;
2076 int len;
2078 if (*arg != '-')
2079 return 0;
2080 c = *++arg;
2081 if (!c)
2082 return 0;
2083 if (c == arg_short) {
2084 c = *++arg;
2085 if (!c)
2086 return 1;
2087 if (val && isdigit(c)) {
2088 char *end;
2089 int n = strtoul(arg, &end, 10);
2090 if (*end)
2091 return 0;
2092 *val = n;
2093 return 1;
2095 return 0;
2097 if (c != '-')
2098 return 0;
2099 arg++;
2100 eq = strchr(arg, '=');
2101 if (eq)
2102 len = eq - arg;
2103 else
2104 len = strlen(arg);
2105 if (!len || strncmp(arg, arg_long, len))
2106 return 0;
2107 if (eq) {
2108 int n;
2109 char *end;
2110 if (!isdigit(*++eq))
2111 return 0;
2112 n = strtoul(eq, &end, 10);
2113 if (*end)
2114 return 0;
2115 *val = n;
2117 return 1;
2120 static int diff_scoreopt_parse(const char *opt);
2122 int diff_opt_parse(struct diff_options *options, const char **av, int ac)
2124 const char *arg = av[0];
2125 if (!strcmp(arg, "-p") || !strcmp(arg, "-u"))
2126 options->output_format |= DIFF_FORMAT_PATCH;
2127 else if (opt_arg(arg, 'U', "unified", &options->context))
2128 options->output_format |= DIFF_FORMAT_PATCH;
2129 else if (!strcmp(arg, "--raw"))
2130 options->output_format |= DIFF_FORMAT_RAW;
2131 else if (!strcmp(arg, "--patch-with-raw")) {
2132 options->output_format |= DIFF_FORMAT_PATCH | DIFF_FORMAT_RAW;
2134 else if (!strcmp(arg, "--numstat")) {
2135 options->output_format |= DIFF_FORMAT_NUMSTAT;
2137 else if (!strcmp(arg, "--shortstat")) {
2138 options->output_format |= DIFF_FORMAT_SHORTSTAT;
2140 else if (!prefixcmp(arg, "--stat")) {
2141 char *end;
2142 int width = options->stat_width;
2143 int name_width = options->stat_name_width;
2144 arg += 6;
2145 end = (char *)arg;
2147 switch (*arg) {
2148 case '-':
2149 if (!prefixcmp(arg, "-width="))
2150 width = strtoul(arg + 7, &end, 10);
2151 else if (!prefixcmp(arg, "-name-width="))
2152 name_width = strtoul(arg + 12, &end, 10);
2153 break;
2154 case '=':
2155 width = strtoul(arg+1, &end, 10);
2156 if (*end == ',')
2157 name_width = strtoul(end+1, &end, 10);
2160 /* Important! This checks all the error cases! */
2161 if (*end)
2162 return 0;
2163 options->output_format |= DIFF_FORMAT_DIFFSTAT;
2164 options->stat_name_width = name_width;
2165 options->stat_width = width;
2167 else if (!strcmp(arg, "--check"))
2168 options->output_format |= DIFF_FORMAT_CHECKDIFF;
2169 else if (!strcmp(arg, "--summary"))
2170 options->output_format |= DIFF_FORMAT_SUMMARY;
2171 else if (!strcmp(arg, "--patch-with-stat")) {
2172 options->output_format |= DIFF_FORMAT_PATCH | DIFF_FORMAT_DIFFSTAT;
2174 else if (!strcmp(arg, "-z"))
2175 options->line_termination = 0;
2176 else if (!prefixcmp(arg, "-l"))
2177 options->rename_limit = strtoul(arg+2, NULL, 10);
2178 else if (!strcmp(arg, "--full-index"))
2179 options->full_index = 1;
2180 else if (!strcmp(arg, "--binary")) {
2181 options->output_format |= DIFF_FORMAT_PATCH;
2182 options->binary = 1;
2184 else if (!strcmp(arg, "-a") || !strcmp(arg, "--text")) {
2185 options->text = 1;
2187 else if (!strcmp(arg, "--name-only"))
2188 options->output_format |= DIFF_FORMAT_NAME;
2189 else if (!strcmp(arg, "--name-status"))
2190 options->output_format |= DIFF_FORMAT_NAME_STATUS;
2191 else if (!strcmp(arg, "-R"))
2192 options->reverse_diff = 1;
2193 else if (!prefixcmp(arg, "-S"))
2194 options->pickaxe = arg + 2;
2195 else if (!strcmp(arg, "-s")) {
2196 options->output_format |= DIFF_FORMAT_NO_OUTPUT;
2198 else if (!prefixcmp(arg, "-O"))
2199 options->orderfile = arg + 2;
2200 else if (!prefixcmp(arg, "--diff-filter="))
2201 options->filter = arg + 14;
2202 else if (!strcmp(arg, "--pickaxe-all"))
2203 options->pickaxe_opts = DIFF_PICKAXE_ALL;
2204 else if (!strcmp(arg, "--pickaxe-regex"))
2205 options->pickaxe_opts = DIFF_PICKAXE_REGEX;
2206 else if (!prefixcmp(arg, "-B")) {
2207 if ((options->break_opt =
2208 diff_scoreopt_parse(arg)) == -1)
2209 return -1;
2211 else if (!prefixcmp(arg, "-M")) {
2212 if ((options->rename_score =
2213 diff_scoreopt_parse(arg)) == -1)
2214 return -1;
2215 options->detect_rename = DIFF_DETECT_RENAME;
2217 else if (!prefixcmp(arg, "-C")) {
2218 if ((options->rename_score =
2219 diff_scoreopt_parse(arg)) == -1)
2220 return -1;
2221 options->detect_rename = DIFF_DETECT_COPY;
2223 else if (!strcmp(arg, "--find-copies-harder"))
2224 options->find_copies_harder = 1;
2225 else if (!strcmp(arg, "--abbrev"))
2226 options->abbrev = DEFAULT_ABBREV;
2227 else if (!prefixcmp(arg, "--abbrev=")) {
2228 options->abbrev = strtoul(arg + 9, NULL, 10);
2229 if (options->abbrev < MINIMUM_ABBREV)
2230 options->abbrev = MINIMUM_ABBREV;
2231 else if (40 < options->abbrev)
2232 options->abbrev = 40;
2234 else if (!strcmp(arg, "--color"))
2235 options->color_diff = 1;
2236 else if (!strcmp(arg, "--no-color"))
2237 options->color_diff = 0;
2238 else if (!strcmp(arg, "-w") || !strcmp(arg, "--ignore-all-space"))
2239 options->xdl_opts |= XDF_IGNORE_WHITESPACE;
2240 else if (!strcmp(arg, "-b") || !strcmp(arg, "--ignore-space-change"))
2241 options->xdl_opts |= XDF_IGNORE_WHITESPACE_CHANGE;
2242 else if (!strcmp(arg, "--ignore-space-at-eol"))
2243 options->xdl_opts |= XDF_IGNORE_WHITESPACE_AT_EOL;
2244 else if (!strcmp(arg, "--color-words"))
2245 options->color_diff = options->color_diff_words = 1;
2246 else if (!strcmp(arg, "--no-renames"))
2247 options->detect_rename = 0;
2248 else if (!strcmp(arg, "--exit-code"))
2249 options->exit_with_status = 1;
2250 else if (!strcmp(arg, "--quiet"))
2251 options->quiet = 1;
2252 else
2253 return 0;
2254 return 1;
2257 static int parse_num(const char **cp_p)
2259 unsigned long num, scale;
2260 int ch, dot;
2261 const char *cp = *cp_p;
2263 num = 0;
2264 scale = 1;
2265 dot = 0;
2266 for(;;) {
2267 ch = *cp;
2268 if ( !dot && ch == '.' ) {
2269 scale = 1;
2270 dot = 1;
2271 } else if ( ch == '%' ) {
2272 scale = dot ? scale*100 : 100;
2273 cp++; /* % is always at the end */
2274 break;
2275 } else if ( ch >= '0' && ch <= '9' ) {
2276 if ( scale < 100000 ) {
2277 scale *= 10;
2278 num = (num*10) + (ch-'0');
2280 } else {
2281 break;
2283 cp++;
2285 *cp_p = cp;
2287 /* user says num divided by scale and we say internally that
2288 * is MAX_SCORE * num / scale.
2290 return (int)((num >= scale) ? MAX_SCORE : (MAX_SCORE * num / scale));
2293 static int diff_scoreopt_parse(const char *opt)
2295 int opt1, opt2, cmd;
2297 if (*opt++ != '-')
2298 return -1;
2299 cmd = *opt++;
2300 if (cmd != 'M' && cmd != 'C' && cmd != 'B')
2301 return -1; /* that is not a -M, -C nor -B option */
2303 opt1 = parse_num(&opt);
2304 if (cmd != 'B')
2305 opt2 = 0;
2306 else {
2307 if (*opt == 0)
2308 opt2 = 0;
2309 else if (*opt != '/')
2310 return -1; /* we expect -B80/99 or -B80 */
2311 else {
2312 opt++;
2313 opt2 = parse_num(&opt);
2316 if (*opt != 0)
2317 return -1;
2318 return opt1 | (opt2 << 16);
2321 struct diff_queue_struct diff_queued_diff;
2323 void diff_q(struct diff_queue_struct *queue, struct diff_filepair *dp)
2325 if (queue->alloc <= queue->nr) {
2326 queue->alloc = alloc_nr(queue->alloc);
2327 queue->queue = xrealloc(queue->queue,
2328 sizeof(dp) * queue->alloc);
2330 queue->queue[queue->nr++] = dp;
2333 struct diff_filepair *diff_queue(struct diff_queue_struct *queue,
2334 struct diff_filespec *one,
2335 struct diff_filespec *two)
2337 struct diff_filepair *dp = xcalloc(1, sizeof(*dp));
2338 dp->one = one;
2339 dp->two = two;
2340 if (queue)
2341 diff_q(queue, dp);
2342 return dp;
2345 void diff_free_filepair(struct diff_filepair *p)
2347 diff_free_filespec_data(p->one);
2348 diff_free_filespec_data(p->two);
2349 free(p->one);
2350 free(p->two);
2351 free(p);
2354 /* This is different from find_unique_abbrev() in that
2355 * it stuffs the result with dots for alignment.
2357 const char *diff_unique_abbrev(const unsigned char *sha1, int len)
2359 int abblen;
2360 const char *abbrev;
2361 if (len == 40)
2362 return sha1_to_hex(sha1);
2364 abbrev = find_unique_abbrev(sha1, len);
2365 if (!abbrev)
2366 return sha1_to_hex(sha1);
2367 abblen = strlen(abbrev);
2368 if (abblen < 37) {
2369 static char hex[41];
2370 if (len < abblen && abblen <= len + 2)
2371 sprintf(hex, "%s%.*s", abbrev, len+3-abblen, "..");
2372 else
2373 sprintf(hex, "%s...", abbrev);
2374 return hex;
2376 return sha1_to_hex(sha1);
2379 static void diff_flush_raw(struct diff_filepair *p,
2380 struct diff_options *options)
2382 int two_paths;
2383 char status[10];
2384 int abbrev = options->abbrev;
2385 const char *path_one, *path_two;
2386 int inter_name_termination = '\t';
2387 int line_termination = options->line_termination;
2389 if (!line_termination)
2390 inter_name_termination = 0;
2392 path_one = p->one->path;
2393 path_two = p->two->path;
2394 if (line_termination) {
2395 path_one = quote_one(path_one);
2396 path_two = quote_one(path_two);
2399 if (p->score)
2400 sprintf(status, "%c%03d", p->status,
2401 (int)(0.5 + p->score * 100.0/MAX_SCORE));
2402 else {
2403 status[0] = p->status;
2404 status[1] = 0;
2406 switch (p->status) {
2407 case DIFF_STATUS_COPIED:
2408 case DIFF_STATUS_RENAMED:
2409 two_paths = 1;
2410 break;
2411 case DIFF_STATUS_ADDED:
2412 case DIFF_STATUS_DELETED:
2413 two_paths = 0;
2414 break;
2415 default:
2416 two_paths = 0;
2417 break;
2419 if (!(options->output_format & DIFF_FORMAT_NAME_STATUS)) {
2420 printf(":%06o %06o %s ",
2421 p->one->mode, p->two->mode,
2422 diff_unique_abbrev(p->one->sha1, abbrev));
2423 printf("%s ",
2424 diff_unique_abbrev(p->two->sha1, abbrev));
2426 printf("%s%c%s", status, inter_name_termination, path_one);
2427 if (two_paths)
2428 printf("%c%s", inter_name_termination, path_two);
2429 putchar(line_termination);
2430 if (path_one != p->one->path)
2431 free((void*)path_one);
2432 if (path_two != p->two->path)
2433 free((void*)path_two);
2436 static void diff_flush_name(struct diff_filepair *p, struct diff_options *opt)
2438 char *path = p->two->path;
2440 if (opt->line_termination)
2441 path = quote_one(p->two->path);
2442 printf("%s%c", path, opt->line_termination);
2443 if (p->two->path != path)
2444 free(path);
2447 int diff_unmodified_pair(struct diff_filepair *p)
2449 /* This function is written stricter than necessary to support
2450 * the currently implemented transformers, but the idea is to
2451 * let transformers to produce diff_filepairs any way they want,
2452 * and filter and clean them up here before producing the output.
2454 struct diff_filespec *one, *two;
2456 if (DIFF_PAIR_UNMERGED(p))
2457 return 0; /* unmerged is interesting */
2459 one = p->one;
2460 two = p->two;
2462 /* deletion, addition, mode or type change
2463 * and rename are all interesting.
2465 if (DIFF_FILE_VALID(one) != DIFF_FILE_VALID(two) ||
2466 DIFF_PAIR_MODE_CHANGED(p) ||
2467 strcmp(one->path, two->path))
2468 return 0;
2470 /* both are valid and point at the same path. that is, we are
2471 * dealing with a change.
2473 if (one->sha1_valid && two->sha1_valid &&
2474 !hashcmp(one->sha1, two->sha1))
2475 return 1; /* no change */
2476 if (!one->sha1_valid && !two->sha1_valid)
2477 return 1; /* both look at the same file on the filesystem. */
2478 return 0;
2481 static void diff_flush_patch(struct diff_filepair *p, struct diff_options *o)
2483 if (diff_unmodified_pair(p))
2484 return;
2486 if ((DIFF_FILE_VALID(p->one) && S_ISDIR(p->one->mode)) ||
2487 (DIFF_FILE_VALID(p->two) && S_ISDIR(p->two->mode)))
2488 return; /* no tree diffs in patch format */
2490 run_diff(p, o);
2493 static void diff_flush_stat(struct diff_filepair *p, struct diff_options *o,
2494 struct diffstat_t *diffstat)
2496 if (diff_unmodified_pair(p))
2497 return;
2499 if ((DIFF_FILE_VALID(p->one) && S_ISDIR(p->one->mode)) ||
2500 (DIFF_FILE_VALID(p->two) && S_ISDIR(p->two->mode)))
2501 return; /* no tree diffs in patch format */
2503 run_diffstat(p, o, diffstat);
2506 static void diff_flush_checkdiff(struct diff_filepair *p,
2507 struct diff_options *o)
2509 if (diff_unmodified_pair(p))
2510 return;
2512 if ((DIFF_FILE_VALID(p->one) && S_ISDIR(p->one->mode)) ||
2513 (DIFF_FILE_VALID(p->two) && S_ISDIR(p->two->mode)))
2514 return; /* no tree diffs in patch format */
2516 run_checkdiff(p, o);
2519 int diff_queue_is_empty(void)
2521 struct diff_queue_struct *q = &diff_queued_diff;
2522 int i;
2523 for (i = 0; i < q->nr; i++)
2524 if (!diff_unmodified_pair(q->queue[i]))
2525 return 0;
2526 return 1;
2529 #if DIFF_DEBUG
2530 void diff_debug_filespec(struct diff_filespec *s, int x, const char *one)
2532 fprintf(stderr, "queue[%d] %s (%s) %s %06o %s\n",
2533 x, one ? one : "",
2534 s->path,
2535 DIFF_FILE_VALID(s) ? "valid" : "invalid",
2536 s->mode,
2537 s->sha1_valid ? sha1_to_hex(s->sha1) : "");
2538 fprintf(stderr, "queue[%d] %s size %lu flags %d\n",
2539 x, one ? one : "",
2540 s->size, s->xfrm_flags);
2543 void diff_debug_filepair(const struct diff_filepair *p, int i)
2545 diff_debug_filespec(p->one, i, "one");
2546 diff_debug_filespec(p->two, i, "two");
2547 fprintf(stderr, "score %d, status %c stays %d broken %d\n",
2548 p->score, p->status ? p->status : '?',
2549 p->source_stays, p->broken_pair);
2552 void diff_debug_queue(const char *msg, struct diff_queue_struct *q)
2554 int i;
2555 if (msg)
2556 fprintf(stderr, "%s\n", msg);
2557 fprintf(stderr, "q->nr = %d\n", q->nr);
2558 for (i = 0; i < q->nr; i++) {
2559 struct diff_filepair *p = q->queue[i];
2560 diff_debug_filepair(p, i);
2563 #endif
2565 static void diff_resolve_rename_copy(void)
2567 int i, j;
2568 struct diff_filepair *p, *pp;
2569 struct diff_queue_struct *q = &diff_queued_diff;
2571 diff_debug_queue("resolve-rename-copy", q);
2573 for (i = 0; i < q->nr; i++) {
2574 p = q->queue[i];
2575 p->status = 0; /* undecided */
2576 if (DIFF_PAIR_UNMERGED(p))
2577 p->status = DIFF_STATUS_UNMERGED;
2578 else if (!DIFF_FILE_VALID(p->one))
2579 p->status = DIFF_STATUS_ADDED;
2580 else if (!DIFF_FILE_VALID(p->two))
2581 p->status = DIFF_STATUS_DELETED;
2582 else if (DIFF_PAIR_TYPE_CHANGED(p))
2583 p->status = DIFF_STATUS_TYPE_CHANGED;
2585 /* from this point on, we are dealing with a pair
2586 * whose both sides are valid and of the same type, i.e.
2587 * either in-place edit or rename/copy edit.
2589 else if (DIFF_PAIR_RENAME(p)) {
2590 if (p->source_stays) {
2591 p->status = DIFF_STATUS_COPIED;
2592 continue;
2594 /* See if there is some other filepair that
2595 * copies from the same source as us. If so
2596 * we are a copy. Otherwise we are either a
2597 * copy if the path stays, or a rename if it
2598 * does not, but we already handled "stays" case.
2600 for (j = i + 1; j < q->nr; j++) {
2601 pp = q->queue[j];
2602 if (strcmp(pp->one->path, p->one->path))
2603 continue; /* not us */
2604 if (!DIFF_PAIR_RENAME(pp))
2605 continue; /* not a rename/copy */
2606 /* pp is a rename/copy from the same source */
2607 p->status = DIFF_STATUS_COPIED;
2608 break;
2610 if (!p->status)
2611 p->status = DIFF_STATUS_RENAMED;
2613 else if (hashcmp(p->one->sha1, p->two->sha1) ||
2614 p->one->mode != p->two->mode ||
2615 is_null_sha1(p->one->sha1))
2616 p->status = DIFF_STATUS_MODIFIED;
2617 else {
2618 /* This is a "no-change" entry and should not
2619 * happen anymore, but prepare for broken callers.
2621 error("feeding unmodified %s to diffcore",
2622 p->one->path);
2623 p->status = DIFF_STATUS_UNKNOWN;
2626 diff_debug_queue("resolve-rename-copy done", q);
2629 static int check_pair_status(struct diff_filepair *p)
2631 switch (p->status) {
2632 case DIFF_STATUS_UNKNOWN:
2633 return 0;
2634 case 0:
2635 die("internal error in diff-resolve-rename-copy");
2636 default:
2637 return 1;
2641 static void flush_one_pair(struct diff_filepair *p, struct diff_options *opt)
2643 int fmt = opt->output_format;
2645 if (fmt & DIFF_FORMAT_CHECKDIFF)
2646 diff_flush_checkdiff(p, opt);
2647 else if (fmt & (DIFF_FORMAT_RAW | DIFF_FORMAT_NAME_STATUS))
2648 diff_flush_raw(p, opt);
2649 else if (fmt & DIFF_FORMAT_NAME)
2650 diff_flush_name(p, opt);
2653 static void show_file_mode_name(const char *newdelete, struct diff_filespec *fs)
2655 char *name = quote_one(fs->path);
2656 if (fs->mode)
2657 printf(" %s mode %06o %s\n", newdelete, fs->mode, name);
2658 else
2659 printf(" %s %s\n", newdelete, name);
2660 free(name);
2664 static void show_mode_change(struct diff_filepair *p, int show_name)
2666 if (p->one->mode && p->two->mode && p->one->mode != p->two->mode) {
2667 if (show_name) {
2668 char *name = quote_one(p->two->path);
2669 printf(" mode change %06o => %06o %s\n",
2670 p->one->mode, p->two->mode, name);
2671 free(name);
2673 else
2674 printf(" mode change %06o => %06o\n",
2675 p->one->mode, p->two->mode);
2679 static void show_rename_copy(const char *renamecopy, struct diff_filepair *p)
2681 char *names = pprint_rename(p->one->path, p->two->path);
2683 printf(" %s %s (%d%%)\n", renamecopy, names,
2684 (int)(0.5 + p->score * 100.0/MAX_SCORE));
2685 free(names);
2686 show_mode_change(p, 0);
2689 static void diff_summary(struct diff_filepair *p)
2691 switch(p->status) {
2692 case DIFF_STATUS_DELETED:
2693 show_file_mode_name("delete", p->one);
2694 break;
2695 case DIFF_STATUS_ADDED:
2696 show_file_mode_name("create", p->two);
2697 break;
2698 case DIFF_STATUS_COPIED:
2699 show_rename_copy("copy", p);
2700 break;
2701 case DIFF_STATUS_RENAMED:
2702 show_rename_copy("rename", p);
2703 break;
2704 default:
2705 if (p->score) {
2706 char *name = quote_one(p->two->path);
2707 printf(" rewrite %s (%d%%)\n", name,
2708 (int)(0.5 + p->score * 100.0/MAX_SCORE));
2709 free(name);
2710 show_mode_change(p, 0);
2711 } else show_mode_change(p, 1);
2712 break;
2716 struct patch_id_t {
2717 struct xdiff_emit_state xm;
2718 SHA_CTX *ctx;
2719 int patchlen;
2722 static int remove_space(char *line, int len)
2724 int i;
2725 char *dst = line;
2726 unsigned char c;
2728 for (i = 0; i < len; i++)
2729 if (!isspace((c = line[i])))
2730 *dst++ = c;
2732 return dst - line;
2735 static void patch_id_consume(void *priv, char *line, unsigned long len)
2737 struct patch_id_t *data = priv;
2738 int new_len;
2740 /* Ignore line numbers when computing the SHA1 of the patch */
2741 if (!prefixcmp(line, "@@ -"))
2742 return;
2744 new_len = remove_space(line, len);
2746 SHA1_Update(data->ctx, line, new_len);
2747 data->patchlen += new_len;
2750 /* returns 0 upon success, and writes result into sha1 */
2751 static int diff_get_patch_id(struct diff_options *options, unsigned char *sha1)
2753 struct diff_queue_struct *q = &diff_queued_diff;
2754 int i;
2755 SHA_CTX ctx;
2756 struct patch_id_t data;
2757 char buffer[PATH_MAX * 4 + 20];
2759 SHA1_Init(&ctx);
2760 memset(&data, 0, sizeof(struct patch_id_t));
2761 data.ctx = &ctx;
2762 data.xm.consume = patch_id_consume;
2764 for (i = 0; i < q->nr; i++) {
2765 xpparam_t xpp;
2766 xdemitconf_t xecfg;
2767 xdemitcb_t ecb;
2768 mmfile_t mf1, mf2;
2769 struct diff_filepair *p = q->queue[i];
2770 int len1, len2;
2772 if (p->status == 0)
2773 return error("internal diff status error");
2774 if (p->status == DIFF_STATUS_UNKNOWN)
2775 continue;
2776 if (diff_unmodified_pair(p))
2777 continue;
2778 if ((DIFF_FILE_VALID(p->one) && S_ISDIR(p->one->mode)) ||
2779 (DIFF_FILE_VALID(p->two) && S_ISDIR(p->two->mode)))
2780 continue;
2781 if (DIFF_PAIR_UNMERGED(p))
2782 continue;
2784 diff_fill_sha1_info(p->one);
2785 diff_fill_sha1_info(p->two);
2786 if (fill_mmfile(&mf1, p->one) < 0 ||
2787 fill_mmfile(&mf2, p->two) < 0)
2788 return error("unable to read files to diff");
2790 /* Maybe hash p->two? into the patch id? */
2791 if (file_is_binary(p->two))
2792 continue;
2794 len1 = remove_space(p->one->path, strlen(p->one->path));
2795 len2 = remove_space(p->two->path, strlen(p->two->path));
2796 if (p->one->mode == 0)
2797 len1 = snprintf(buffer, sizeof(buffer),
2798 "diff--gita/%.*sb/%.*s"
2799 "newfilemode%06o"
2800 "---/dev/null"
2801 "+++b/%.*s",
2802 len1, p->one->path,
2803 len2, p->two->path,
2804 p->two->mode,
2805 len2, p->two->path);
2806 else if (p->two->mode == 0)
2807 len1 = snprintf(buffer, sizeof(buffer),
2808 "diff--gita/%.*sb/%.*s"
2809 "deletedfilemode%06o"
2810 "---a/%.*s"
2811 "+++/dev/null",
2812 len1, p->one->path,
2813 len2, p->two->path,
2814 p->one->mode,
2815 len1, p->one->path);
2816 else
2817 len1 = snprintf(buffer, sizeof(buffer),
2818 "diff--gita/%.*sb/%.*s"
2819 "---a/%.*s"
2820 "+++b/%.*s",
2821 len1, p->one->path,
2822 len2, p->two->path,
2823 len1, p->one->path,
2824 len2, p->two->path);
2825 SHA1_Update(&ctx, buffer, len1);
2827 xpp.flags = XDF_NEED_MINIMAL;
2828 xecfg.ctxlen = 3;
2829 xecfg.flags = XDL_EMIT_FUNCNAMES;
2830 ecb.outf = xdiff_outf;
2831 ecb.priv = &data;
2832 xdl_diff(&mf1, &mf2, &xpp, &xecfg, &ecb);
2835 SHA1_Final(sha1, &ctx);
2836 return 0;
2839 int diff_flush_patch_id(struct diff_options *options, unsigned char *sha1)
2841 struct diff_queue_struct *q = &diff_queued_diff;
2842 int i;
2843 int result = diff_get_patch_id(options, sha1);
2845 for (i = 0; i < q->nr; i++)
2846 diff_free_filepair(q->queue[i]);
2848 free(q->queue);
2849 q->queue = NULL;
2850 q->nr = q->alloc = 0;
2852 return result;
2855 static int is_summary_empty(const struct diff_queue_struct *q)
2857 int i;
2859 for (i = 0; i < q->nr; i++) {
2860 const struct diff_filepair *p = q->queue[i];
2862 switch (p->status) {
2863 case DIFF_STATUS_DELETED:
2864 case DIFF_STATUS_ADDED:
2865 case DIFF_STATUS_COPIED:
2866 case DIFF_STATUS_RENAMED:
2867 return 0;
2868 default:
2869 if (p->score)
2870 return 0;
2871 if (p->one->mode && p->two->mode &&
2872 p->one->mode != p->two->mode)
2873 return 0;
2874 break;
2877 return 1;
2880 void diff_flush(struct diff_options *options)
2882 struct diff_queue_struct *q = &diff_queued_diff;
2883 int i, output_format = options->output_format;
2884 int separator = 0;
2887 * Order: raw, stat, summary, patch
2888 * or: name/name-status/checkdiff (other bits clear)
2890 if (!q->nr)
2891 goto free_queue;
2893 if (output_format & (DIFF_FORMAT_RAW |
2894 DIFF_FORMAT_NAME |
2895 DIFF_FORMAT_NAME_STATUS |
2896 DIFF_FORMAT_CHECKDIFF)) {
2897 for (i = 0; i < q->nr; i++) {
2898 struct diff_filepair *p = q->queue[i];
2899 if (check_pair_status(p))
2900 flush_one_pair(p, options);
2902 separator++;
2905 if (output_format & (DIFF_FORMAT_DIFFSTAT|DIFF_FORMAT_SHORTSTAT|DIFF_FORMAT_NUMSTAT)) {
2906 struct diffstat_t diffstat;
2908 memset(&diffstat, 0, sizeof(struct diffstat_t));
2909 diffstat.xm.consume = diffstat_consume;
2910 for (i = 0; i < q->nr; i++) {
2911 struct diff_filepair *p = q->queue[i];
2912 if (check_pair_status(p))
2913 diff_flush_stat(p, options, &diffstat);
2915 if (output_format & DIFF_FORMAT_NUMSTAT)
2916 show_numstat(&diffstat, options);
2917 if (output_format & DIFF_FORMAT_DIFFSTAT)
2918 show_stats(&diffstat, options);
2919 else if (output_format & DIFF_FORMAT_SHORTSTAT)
2920 show_shortstats(&diffstat);
2921 separator++;
2924 if (output_format & DIFF_FORMAT_SUMMARY && !is_summary_empty(q)) {
2925 for (i = 0; i < q->nr; i++)
2926 diff_summary(q->queue[i]);
2927 separator++;
2930 if (output_format & DIFF_FORMAT_PATCH) {
2931 if (separator) {
2932 if (options->stat_sep) {
2933 /* attach patch instead of inline */
2934 fputs(options->stat_sep, stdout);
2935 } else {
2936 putchar(options->line_termination);
2940 for (i = 0; i < q->nr; i++) {
2941 struct diff_filepair *p = q->queue[i];
2942 if (check_pair_status(p))
2943 diff_flush_patch(p, options);
2947 if (output_format & DIFF_FORMAT_CALLBACK)
2948 options->format_callback(q, options, options->format_callback_data);
2950 for (i = 0; i < q->nr; i++)
2951 diff_free_filepair(q->queue[i]);
2952 free_queue:
2953 free(q->queue);
2954 q->queue = NULL;
2955 q->nr = q->alloc = 0;
2958 static void diffcore_apply_filter(const char *filter)
2960 int i;
2961 struct diff_queue_struct *q = &diff_queued_diff;
2962 struct diff_queue_struct outq;
2963 outq.queue = NULL;
2964 outq.nr = outq.alloc = 0;
2966 if (!filter)
2967 return;
2969 if (strchr(filter, DIFF_STATUS_FILTER_AON)) {
2970 int found;
2971 for (i = found = 0; !found && i < q->nr; i++) {
2972 struct diff_filepair *p = q->queue[i];
2973 if (((p->status == DIFF_STATUS_MODIFIED) &&
2974 ((p->score &&
2975 strchr(filter, DIFF_STATUS_FILTER_BROKEN)) ||
2976 (!p->score &&
2977 strchr(filter, DIFF_STATUS_MODIFIED)))) ||
2978 ((p->status != DIFF_STATUS_MODIFIED) &&
2979 strchr(filter, p->status)))
2980 found++;
2982 if (found)
2983 return;
2985 /* otherwise we will clear the whole queue
2986 * by copying the empty outq at the end of this
2987 * function, but first clear the current entries
2988 * in the queue.
2990 for (i = 0; i < q->nr; i++)
2991 diff_free_filepair(q->queue[i]);
2993 else {
2994 /* Only the matching ones */
2995 for (i = 0; i < q->nr; i++) {
2996 struct diff_filepair *p = q->queue[i];
2998 if (((p->status == DIFF_STATUS_MODIFIED) &&
2999 ((p->score &&
3000 strchr(filter, DIFF_STATUS_FILTER_BROKEN)) ||
3001 (!p->score &&
3002 strchr(filter, DIFF_STATUS_MODIFIED)))) ||
3003 ((p->status != DIFF_STATUS_MODIFIED) &&
3004 strchr(filter, p->status)))
3005 diff_q(&outq, p);
3006 else
3007 diff_free_filepair(p);
3010 free(q->queue);
3011 *q = outq;
3014 void diffcore_std(struct diff_options *options)
3016 if (options->quiet)
3017 return;
3018 if (options->break_opt != -1)
3019 diffcore_break(options->break_opt);
3020 if (options->detect_rename)
3021 diffcore_rename(options);
3022 if (options->break_opt != -1)
3023 diffcore_merge_broken();
3024 if (options->pickaxe)
3025 diffcore_pickaxe(options->pickaxe, options->pickaxe_opts);
3026 if (options->orderfile)
3027 diffcore_order(options->orderfile);
3028 diff_resolve_rename_copy();
3029 diffcore_apply_filter(options->filter);
3031 options->has_changes = !!diff_queued_diff.nr;
3035 void diff_addremove(struct diff_options *options,
3036 int addremove, unsigned mode,
3037 const unsigned char *sha1,
3038 const char *base, const char *path)
3040 char concatpath[PATH_MAX];
3041 struct diff_filespec *one, *two;
3043 /* This may look odd, but it is a preparation for
3044 * feeding "there are unchanged files which should
3045 * not produce diffs, but when you are doing copy
3046 * detection you would need them, so here they are"
3047 * entries to the diff-core. They will be prefixed
3048 * with something like '=' or '*' (I haven't decided
3049 * which but should not make any difference).
3050 * Feeding the same new and old to diff_change()
3051 * also has the same effect.
3052 * Before the final output happens, they are pruned after
3053 * merged into rename/copy pairs as appropriate.
3055 if (options->reverse_diff)
3056 addremove = (addremove == '+' ? '-' :
3057 addremove == '-' ? '+' : addremove);
3059 if (!path) path = "";
3060 sprintf(concatpath, "%s%s", base, path);
3061 one = alloc_filespec(concatpath);
3062 two = alloc_filespec(concatpath);
3064 if (addremove != '+')
3065 fill_filespec(one, sha1, mode);
3066 if (addremove != '-')
3067 fill_filespec(two, sha1, mode);
3069 diff_queue(&diff_queued_diff, one, two);
3070 options->has_changes = 1;
3073 void diff_change(struct diff_options *options,
3074 unsigned old_mode, unsigned new_mode,
3075 const unsigned char *old_sha1,
3076 const unsigned char *new_sha1,
3077 const char *base, const char *path)
3079 char concatpath[PATH_MAX];
3080 struct diff_filespec *one, *two;
3082 if (options->reverse_diff) {
3083 unsigned tmp;
3084 const unsigned char *tmp_c;
3085 tmp = old_mode; old_mode = new_mode; new_mode = tmp;
3086 tmp_c = old_sha1; old_sha1 = new_sha1; new_sha1 = tmp_c;
3088 if (!path) path = "";
3089 sprintf(concatpath, "%s%s", base, path);
3090 one = alloc_filespec(concatpath);
3091 two = alloc_filespec(concatpath);
3092 fill_filespec(one, old_sha1, old_mode);
3093 fill_filespec(two, new_sha1, new_mode);
3095 diff_queue(&diff_queued_diff, one, two);
3096 options->has_changes = 1;
3099 void diff_unmerge(struct diff_options *options,
3100 const char *path,
3101 unsigned mode, const unsigned char *sha1)
3103 struct diff_filespec *one, *two;
3104 one = alloc_filespec(path);
3105 two = alloc_filespec(path);
3106 fill_filespec(one, sha1, mode);
3107 diff_queue(&diff_queued_diff, one, two)->is_unmerged = 1;