Documentation: Correct synopsis for git-add command
[git/jnareb-git.git] / diff.c
blob65bfc374d99e2b0be32c6116130776fa761e32dd
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 #define TEMPFILE_PATH_LEN 50
191 static struct diff_tempfile {
192 const char *name; /* filename external diff should read from */
193 char hex[41];
194 char mode[10];
195 char tmp_path[TEMPFILE_PATH_LEN];
196 } diff_temp[2];
198 static int count_lines(const char *data, int size)
200 int count, ch, completely_empty = 1, nl_just_seen = 0;
201 count = 0;
202 while (0 < size--) {
203 ch = *data++;
204 if (ch == '\n') {
205 count++;
206 nl_just_seen = 1;
207 completely_empty = 0;
209 else {
210 nl_just_seen = 0;
211 completely_empty = 0;
214 if (completely_empty)
215 return 0;
216 if (!nl_just_seen)
217 count++; /* no trailing newline */
218 return count;
221 static void print_line_count(int count)
223 switch (count) {
224 case 0:
225 printf("0,0");
226 break;
227 case 1:
228 printf("1");
229 break;
230 default:
231 printf("1,%d", count);
232 break;
236 static void copy_file(int prefix, const char *data, int size,
237 const char *set, const char *reset)
239 int ch, nl_just_seen = 1;
240 while (0 < size--) {
241 ch = *data++;
242 if (nl_just_seen) {
243 fputs(set, stdout);
244 putchar(prefix);
246 if (ch == '\n') {
247 nl_just_seen = 1;
248 fputs(reset, stdout);
249 } else
250 nl_just_seen = 0;
251 putchar(ch);
253 if (!nl_just_seen)
254 printf("%s\n\\ No newline at end of file\n", reset);
257 static void emit_rewrite_diff(const char *name_a,
258 const char *name_b,
259 struct diff_filespec *one,
260 struct diff_filespec *two,
261 int color_diff)
263 int lc_a, lc_b;
264 const char *name_a_tab, *name_b_tab;
265 const char *metainfo = diff_get_color(color_diff, DIFF_METAINFO);
266 const char *fraginfo = diff_get_color(color_diff, DIFF_FRAGINFO);
267 const char *old = diff_get_color(color_diff, DIFF_FILE_OLD);
268 const char *new = diff_get_color(color_diff, DIFF_FILE_NEW);
269 const char *reset = diff_get_color(color_diff, DIFF_RESET);
271 name_a += (*name_a == '/');
272 name_b += (*name_b == '/');
273 name_a_tab = strchr(name_a, ' ') ? "\t" : "";
274 name_b_tab = strchr(name_b, ' ') ? "\t" : "";
276 diff_populate_filespec(one, 0);
277 diff_populate_filespec(two, 0);
278 lc_a = count_lines(one->data, one->size);
279 lc_b = count_lines(two->data, two->size);
280 printf("%s--- a/%s%s%s\n%s+++ b/%s%s%s\n%s@@ -",
281 metainfo, name_a, name_a_tab, reset,
282 metainfo, name_b, name_b_tab, reset, fraginfo);
283 print_line_count(lc_a);
284 printf(" +");
285 print_line_count(lc_b);
286 printf(" @@%s\n", reset);
287 if (lc_a)
288 copy_file('-', one->data, one->size, old, reset);
289 if (lc_b)
290 copy_file('+', two->data, two->size, new, reset);
293 static int fill_mmfile(mmfile_t *mf, struct diff_filespec *one)
295 if (!DIFF_FILE_VALID(one)) {
296 mf->ptr = (char *)""; /* does not matter */
297 mf->size = 0;
298 return 0;
300 else if (diff_populate_filespec(one, 0))
301 return -1;
302 mf->ptr = one->data;
303 mf->size = one->size;
304 return 0;
307 struct diff_words_buffer {
308 mmfile_t text;
309 long alloc;
310 long current; /* output pointer */
311 int suppressed_newline;
314 static void diff_words_append(char *line, unsigned long len,
315 struct diff_words_buffer *buffer)
317 if (buffer->text.size + len > buffer->alloc) {
318 buffer->alloc = (buffer->text.size + len) * 3 / 2;
319 buffer->text.ptr = xrealloc(buffer->text.ptr, buffer->alloc);
321 line++;
322 len--;
323 memcpy(buffer->text.ptr + buffer->text.size, line, len);
324 buffer->text.size += len;
327 struct diff_words_data {
328 struct xdiff_emit_state xm;
329 struct diff_words_buffer minus, plus;
332 static void print_word(struct diff_words_buffer *buffer, int len, int color,
333 int suppress_newline)
335 const char *ptr;
336 int eol = 0;
338 if (len == 0)
339 return;
341 ptr = buffer->text.ptr + buffer->current;
342 buffer->current += len;
344 if (ptr[len - 1] == '\n') {
345 eol = 1;
346 len--;
349 fputs(diff_get_color(1, color), stdout);
350 fwrite(ptr, len, 1, stdout);
351 fputs(diff_get_color(1, DIFF_RESET), stdout);
353 if (eol) {
354 if (suppress_newline)
355 buffer->suppressed_newline = 1;
356 else
357 putchar('\n');
361 static void fn_out_diff_words_aux(void *priv, char *line, unsigned long len)
363 struct diff_words_data *diff_words = priv;
365 if (diff_words->minus.suppressed_newline) {
366 if (line[0] != '+')
367 putchar('\n');
368 diff_words->minus.suppressed_newline = 0;
371 len--;
372 switch (line[0]) {
373 case '-':
374 print_word(&diff_words->minus, len, DIFF_FILE_OLD, 1);
375 break;
376 case '+':
377 print_word(&diff_words->plus, len, DIFF_FILE_NEW, 0);
378 break;
379 case ' ':
380 print_word(&diff_words->plus, len, DIFF_PLAIN, 0);
381 diff_words->minus.current += len;
382 break;
386 /* this executes the word diff on the accumulated buffers */
387 static void diff_words_show(struct diff_words_data *diff_words)
389 xpparam_t xpp;
390 xdemitconf_t xecfg;
391 xdemitcb_t ecb;
392 mmfile_t minus, plus;
393 int i;
395 minus.size = diff_words->minus.text.size;
396 minus.ptr = xmalloc(minus.size);
397 memcpy(minus.ptr, diff_words->minus.text.ptr, minus.size);
398 for (i = 0; i < minus.size; i++)
399 if (isspace(minus.ptr[i]))
400 minus.ptr[i] = '\n';
401 diff_words->minus.current = 0;
403 plus.size = diff_words->plus.text.size;
404 plus.ptr = xmalloc(plus.size);
405 memcpy(plus.ptr, diff_words->plus.text.ptr, plus.size);
406 for (i = 0; i < plus.size; i++)
407 if (isspace(plus.ptr[i]))
408 plus.ptr[i] = '\n';
409 diff_words->plus.current = 0;
411 xpp.flags = XDF_NEED_MINIMAL;
412 xecfg.ctxlen = diff_words->minus.alloc + diff_words->plus.alloc;
413 xecfg.flags = 0;
414 ecb.outf = xdiff_outf;
415 ecb.priv = diff_words;
416 diff_words->xm.consume = fn_out_diff_words_aux;
417 xdl_diff(&minus, &plus, &xpp, &xecfg, &ecb);
419 free(minus.ptr);
420 free(plus.ptr);
421 diff_words->minus.text.size = diff_words->plus.text.size = 0;
423 if (diff_words->minus.suppressed_newline) {
424 putchar('\n');
425 diff_words->minus.suppressed_newline = 0;
429 struct emit_callback {
430 struct xdiff_emit_state xm;
431 int nparents, color_diff;
432 const char **label_path;
433 struct diff_words_data *diff_words;
434 int *found_changesp;
437 static void free_diff_words_data(struct emit_callback *ecbdata)
439 if (ecbdata->diff_words) {
440 /* flush buffers */
441 if (ecbdata->diff_words->minus.text.size ||
442 ecbdata->diff_words->plus.text.size)
443 diff_words_show(ecbdata->diff_words);
445 if (ecbdata->diff_words->minus.text.ptr)
446 free (ecbdata->diff_words->minus.text.ptr);
447 if (ecbdata->diff_words->plus.text.ptr)
448 free (ecbdata->diff_words->plus.text.ptr);
449 free(ecbdata->diff_words);
450 ecbdata->diff_words = NULL;
454 const char *diff_get_color(int diff_use_color, enum color_diff ix)
456 if (diff_use_color)
457 return diff_colors[ix];
458 return "";
461 static void emit_line(const char *set, const char *reset, const char *line, int len)
463 if (len > 0 && line[len-1] == '\n')
464 len--;
465 fputs(set, stdout);
466 fwrite(line, len, 1, stdout);
467 puts(reset);
470 static void emit_line_with_ws(int nparents,
471 const char *set, const char *reset, const char *ws,
472 const char *line, int len)
474 int col0 = nparents;
475 int last_tab_in_indent = -1;
476 int last_space_in_indent = -1;
477 int i;
478 int tail = len;
479 int need_highlight_leading_space = 0;
480 /* The line is a newly added line. Does it have funny leading
481 * whitespaces? In indent, SP should never precede a TAB.
483 for (i = col0; i < len; i++) {
484 if (line[i] == '\t') {
485 last_tab_in_indent = i;
486 if (0 <= last_space_in_indent)
487 need_highlight_leading_space = 1;
489 else if (line[i] == ' ')
490 last_space_in_indent = i;
491 else
492 break;
494 fputs(set, stdout);
495 fwrite(line, col0, 1, stdout);
496 fputs(reset, stdout);
497 if (((i == len) || line[i] == '\n') && i != col0) {
498 /* The whole line was indent */
499 emit_line(ws, reset, line + col0, len - col0);
500 return;
502 i = col0;
503 if (need_highlight_leading_space) {
504 while (i < last_tab_in_indent) {
505 if (line[i] == ' ') {
506 fputs(ws, stdout);
507 putchar(' ');
508 fputs(reset, stdout);
510 else
511 putchar(line[i]);
512 i++;
515 tail = len - 1;
516 if (line[tail] == '\n' && i < tail)
517 tail--;
518 while (i < tail) {
519 if (!isspace(line[tail]))
520 break;
521 tail--;
523 if ((i < tail && line[tail + 1] != '\n')) {
524 /* This has whitespace between tail+1..len */
525 fputs(set, stdout);
526 fwrite(line + i, tail - i + 1, 1, stdout);
527 fputs(reset, stdout);
528 emit_line(ws, reset, line + tail + 1, len - tail - 1);
530 else
531 emit_line(set, reset, line + i, len - i);
534 static void emit_add_line(const char *reset, struct emit_callback *ecbdata, const char *line, int len)
536 const char *ws = diff_get_color(ecbdata->color_diff, DIFF_WHITESPACE);
537 const char *set = diff_get_color(ecbdata->color_diff, DIFF_FILE_NEW);
539 if (!*ws)
540 emit_line(set, reset, line, len);
541 else
542 emit_line_with_ws(ecbdata->nparents, set, reset, ws,
543 line, len);
546 static void fn_out_consume(void *priv, char *line, unsigned long len)
548 int i;
549 int color;
550 struct emit_callback *ecbdata = priv;
551 const char *set = diff_get_color(ecbdata->color_diff, DIFF_METAINFO);
552 const char *reset = diff_get_color(ecbdata->color_diff, DIFF_RESET);
554 *(ecbdata->found_changesp) = 1;
556 if (ecbdata->label_path[0]) {
557 const char *name_a_tab, *name_b_tab;
559 name_a_tab = strchr(ecbdata->label_path[0], ' ') ? "\t" : "";
560 name_b_tab = strchr(ecbdata->label_path[1], ' ') ? "\t" : "";
562 printf("%s--- %s%s%s\n",
563 set, ecbdata->label_path[0], reset, name_a_tab);
564 printf("%s+++ %s%s%s\n",
565 set, ecbdata->label_path[1], reset, name_b_tab);
566 ecbdata->label_path[0] = ecbdata->label_path[1] = NULL;
569 /* This is not really necessary for now because
570 * this codepath only deals with two-way diffs.
572 for (i = 0; i < len && line[i] == '@'; i++)
574 if (2 <= i && i < len && line[i] == ' ') {
575 ecbdata->nparents = i - 1;
576 emit_line(diff_get_color(ecbdata->color_diff, DIFF_FRAGINFO),
577 reset, line, len);
578 return;
581 if (len < ecbdata->nparents) {
582 set = reset;
583 emit_line(reset, reset, line, len);
584 return;
587 color = DIFF_PLAIN;
588 if (ecbdata->diff_words && ecbdata->nparents != 1)
589 /* fall back to normal diff */
590 free_diff_words_data(ecbdata);
591 if (ecbdata->diff_words) {
592 if (line[0] == '-') {
593 diff_words_append(line, len,
594 &ecbdata->diff_words->minus);
595 return;
596 } else if (line[0] == '+') {
597 diff_words_append(line, len,
598 &ecbdata->diff_words->plus);
599 return;
601 if (ecbdata->diff_words->minus.text.size ||
602 ecbdata->diff_words->plus.text.size)
603 diff_words_show(ecbdata->diff_words);
604 line++;
605 len--;
606 emit_line(set, reset, line, len);
607 return;
609 for (i = 0; i < ecbdata->nparents && len; i++) {
610 if (line[i] == '-')
611 color = DIFF_FILE_OLD;
612 else if (line[i] == '+')
613 color = DIFF_FILE_NEW;
616 if (color != DIFF_FILE_NEW) {
617 emit_line(diff_get_color(ecbdata->color_diff, color),
618 reset, line, len);
619 return;
621 emit_add_line(reset, ecbdata, line, len);
624 static char *pprint_rename(const char *a, const char *b)
626 const char *old = a;
627 const char *new = b;
628 char *name = NULL;
629 int pfx_length, sfx_length;
630 int len_a = strlen(a);
631 int len_b = strlen(b);
632 int qlen_a = quote_c_style(a, NULL, NULL, 0);
633 int qlen_b = quote_c_style(b, NULL, NULL, 0);
635 if (qlen_a || qlen_b) {
636 if (qlen_a) len_a = qlen_a;
637 if (qlen_b) len_b = qlen_b;
638 name = xmalloc( len_a + len_b + 5 );
639 if (qlen_a)
640 quote_c_style(a, name, NULL, 0);
641 else
642 memcpy(name, a, len_a);
643 memcpy(name + len_a, " => ", 4);
644 if (qlen_b)
645 quote_c_style(b, name + len_a + 4, NULL, 0);
646 else
647 memcpy(name + len_a + 4, b, len_b + 1);
648 return name;
651 /* Find common prefix */
652 pfx_length = 0;
653 while (*old && *new && *old == *new) {
654 if (*old == '/')
655 pfx_length = old - a + 1;
656 old++;
657 new++;
660 /* Find common suffix */
661 old = a + len_a;
662 new = b + len_b;
663 sfx_length = 0;
664 while (a <= old && b <= new && *old == *new) {
665 if (*old == '/')
666 sfx_length = len_a - (old - a);
667 old--;
668 new--;
672 * pfx{mid-a => mid-b}sfx
673 * {pfx-a => pfx-b}sfx
674 * pfx{sfx-a => sfx-b}
675 * name-a => name-b
677 if (pfx_length + sfx_length) {
678 int a_midlen = len_a - pfx_length - sfx_length;
679 int b_midlen = len_b - pfx_length - sfx_length;
680 if (a_midlen < 0) a_midlen = 0;
681 if (b_midlen < 0) b_midlen = 0;
683 name = xmalloc(pfx_length + a_midlen + b_midlen + sfx_length + 7);
684 sprintf(name, "%.*s{%.*s => %.*s}%s",
685 pfx_length, a,
686 a_midlen, a + pfx_length,
687 b_midlen, b + pfx_length,
688 a + len_a - sfx_length);
690 else {
691 name = xmalloc(len_a + len_b + 5);
692 sprintf(name, "%s => %s", a, b);
694 return name;
697 struct diffstat_t {
698 struct xdiff_emit_state xm;
700 int nr;
701 int alloc;
702 struct diffstat_file {
703 char *name;
704 char *from_name;
705 unsigned is_unmerged:1;
706 unsigned is_binary:1;
707 unsigned is_renamed:1;
708 unsigned int added, deleted;
709 } **files;
712 static struct diffstat_file *diffstat_add(struct diffstat_t *diffstat,
713 const char *name_a,
714 const char *name_b)
716 struct diffstat_file *x;
717 x = xcalloc(sizeof (*x), 1);
718 if (diffstat->nr == diffstat->alloc) {
719 diffstat->alloc = alloc_nr(diffstat->alloc);
720 diffstat->files = xrealloc(diffstat->files,
721 diffstat->alloc * sizeof(x));
723 diffstat->files[diffstat->nr++] = x;
724 x->name = xstrdup(name_a);
725 if (name_b) {
726 x->from_name = xstrdup(name_b);
727 x->is_renamed = 1;
729 else
730 x->from_name = NULL;
731 return x;
734 static void diffstat_consume(void *priv, char *line, unsigned long len)
736 struct diffstat_t *diffstat = priv;
737 struct diffstat_file *x = diffstat->files[diffstat->nr - 1];
739 if (line[0] == '+')
740 x->added++;
741 else if (line[0] == '-')
742 x->deleted++;
745 const char mime_boundary_leader[] = "------------";
747 static int scale_linear(int it, int width, int max_change)
750 * make sure that at least one '-' is printed if there were deletions,
751 * and likewise for '+'.
753 if (max_change < 2)
754 return it;
755 return ((it - 1) * (width - 1) + max_change - 1) / (max_change - 1);
758 static void show_name(const char *prefix, const char *name, int len,
759 const char *reset, const char *set)
761 printf(" %s%s%-*s%s |", set, prefix, len, name, reset);
764 static void show_graph(char ch, int cnt, const char *set, const char *reset)
766 if (cnt <= 0)
767 return;
768 printf("%s", set);
769 while (cnt--)
770 putchar(ch);
771 printf("%s", reset);
774 static void show_stats(struct diffstat_t* data, struct diff_options *options)
776 int i, len, add, del, total, adds = 0, dels = 0;
777 int max_change = 0, max_len = 0;
778 int total_files = data->nr;
779 int width, name_width;
780 const char *reset, *set, *add_c, *del_c;
782 if (data->nr == 0)
783 return;
785 width = options->stat_width ? options->stat_width : 80;
786 name_width = options->stat_name_width ? options->stat_name_width : 50;
788 /* Sanity: give at least 5 columns to the graph,
789 * but leave at least 10 columns for the name.
791 if (width < name_width + 15) {
792 if (name_width <= 25)
793 width = name_width + 15;
794 else
795 name_width = width - 15;
798 /* Find the longest filename and max number of changes */
799 reset = diff_get_color(options->color_diff, DIFF_RESET);
800 set = diff_get_color(options->color_diff, DIFF_PLAIN);
801 add_c = diff_get_color(options->color_diff, DIFF_FILE_NEW);
802 del_c = diff_get_color(options->color_diff, DIFF_FILE_OLD);
804 for (i = 0; i < data->nr; i++) {
805 struct diffstat_file *file = data->files[i];
806 int change = file->added + file->deleted;
808 if (!file->is_renamed) { /* renames are quoted by pprint_rename */
809 len = quote_c_style(file->name, NULL, NULL, 0);
810 if (len) {
811 char *qname = xmalloc(len + 1);
812 quote_c_style(file->name, qname, NULL, 0);
813 free(file->name);
814 file->name = qname;
816 } else {
817 char *qname = pprint_rename(file->name, file->from_name);
818 free(file->name);
819 file->name = qname;
822 len = strlen(file->name);
823 if (max_len < len)
824 max_len = len;
826 if (file->is_binary || file->is_unmerged)
827 continue;
828 if (max_change < change)
829 max_change = change;
832 /* Compute the width of the graph part;
833 * 10 is for one blank at the beginning of the line plus
834 * " | count " between the name and the graph.
836 * From here on, name_width is the width of the name area,
837 * and width is the width of the graph area.
839 name_width = (name_width < max_len) ? name_width : max_len;
840 if (width < (name_width + 10) + max_change)
841 width = width - (name_width + 10);
842 else
843 width = max_change;
845 for (i = 0; i < data->nr; i++) {
846 const char *prefix = "";
847 char *name = data->files[i]->name;
848 int added = data->files[i]->added;
849 int deleted = data->files[i]->deleted;
850 int name_len;
853 * "scale" the filename
855 len = name_width;
856 name_len = strlen(name);
857 if (name_width < name_len) {
858 char *slash;
859 prefix = "...";
860 len -= 3;
861 name += name_len - len;
862 slash = strchr(name, '/');
863 if (slash)
864 name = slash;
867 if (data->files[i]->is_binary) {
868 show_name(prefix, name, len, reset, set);
869 printf(" Bin ");
870 printf("%s%d%s", del_c, deleted, reset);
871 printf(" -> ");
872 printf("%s%d%s", add_c, added, reset);
873 printf(" bytes");
874 printf("\n");
875 goto free_diffstat_file;
877 else if (data->files[i]->is_unmerged) {
878 show_name(prefix, name, len, reset, set);
879 printf(" Unmerged\n");
880 goto free_diffstat_file;
882 else if (!data->files[i]->is_renamed &&
883 (added + deleted == 0)) {
884 total_files--;
885 goto free_diffstat_file;
889 * scale the add/delete
891 add = added;
892 del = deleted;
893 total = add + del;
894 adds += add;
895 dels += del;
897 if (width <= max_change) {
898 add = scale_linear(add, width, max_change);
899 del = scale_linear(del, width, max_change);
900 total = add + del;
902 show_name(prefix, name, len, reset, set);
903 printf("%5d ", added + deleted);
904 show_graph('+', add, add_c, reset);
905 show_graph('-', del, del_c, reset);
906 putchar('\n');
907 free_diffstat_file:
908 free(data->files[i]->name);
909 free(data->files[i]);
911 free(data->files);
912 printf("%s %d files changed, %d insertions(+), %d deletions(-)%s\n",
913 set, total_files, adds, dels, reset);
916 static void show_shortstats(struct diffstat_t* data)
918 int i, adds = 0, dels = 0, total_files = data->nr;
920 if (data->nr == 0)
921 return;
923 for (i = 0; i < data->nr; i++) {
924 if (!data->files[i]->is_binary &&
925 !data->files[i]->is_unmerged) {
926 int added = data->files[i]->added;
927 int deleted= data->files[i]->deleted;
928 if (!data->files[i]->is_renamed &&
929 (added + deleted == 0)) {
930 total_files--;
931 } else {
932 adds += added;
933 dels += deleted;
936 free(data->files[i]->name);
937 free(data->files[i]);
939 free(data->files);
941 printf(" %d files changed, %d insertions(+), %d deletions(-)\n",
942 total_files, adds, dels);
945 static void show_numstat(struct diffstat_t* data, struct diff_options *options)
947 int i;
949 for (i = 0; i < data->nr; i++) {
950 struct diffstat_file *file = data->files[i];
952 if (file->is_binary)
953 printf("-\t-\t");
954 else
955 printf("%d\t%d\t", file->added, file->deleted);
956 if (options->line_termination &&
957 quote_c_style(file->name, NULL, NULL, 0))
958 quote_c_style(file->name, NULL, stdout, 0);
959 else
960 fputs(file->name, stdout);
961 if (file->is_renamed) {
962 printf("%s", options->line_termination ? "\t" : "\0\0");
963 if (options->line_termination &&
964 quote_c_style(file->from_name, NULL, NULL, 0))
965 quote_c_style(file->from_name, NULL, stdout, 0);
966 else
967 fputs(file->from_name, stdout);
969 putchar(options->line_termination);
973 struct checkdiff_t {
974 struct xdiff_emit_state xm;
975 const char *filename;
976 int lineno, color_diff;
979 static void checkdiff_consume(void *priv, char *line, unsigned long len)
981 struct checkdiff_t *data = priv;
982 const char *ws = diff_get_color(data->color_diff, DIFF_WHITESPACE);
983 const char *reset = diff_get_color(data->color_diff, DIFF_RESET);
984 const char *set = diff_get_color(data->color_diff, DIFF_FILE_NEW);
986 if (line[0] == '+') {
987 int i, spaces = 0, space_before_tab = 0, white_space_at_end = 0;
989 /* check space before tab */
990 for (i = 1; i < len && (line[i] == ' ' || line[i] == '\t'); i++)
991 if (line[i] == ' ')
992 spaces++;
993 if (line[i - 1] == '\t' && spaces)
994 space_before_tab = 1;
996 /* check white space at line end */
997 if (line[len - 1] == '\n')
998 len--;
999 if (isspace(line[len - 1]))
1000 white_space_at_end = 1;
1002 if (space_before_tab || white_space_at_end) {
1003 printf("%s:%d: %s", data->filename, data->lineno, ws);
1004 if (space_before_tab) {
1005 printf("space before tab");
1006 if (white_space_at_end)
1007 putchar(',');
1009 if (white_space_at_end)
1010 printf("white space at end");
1011 printf(":%s ", reset);
1012 emit_line_with_ws(1, set, reset, ws, line, len);
1015 data->lineno++;
1016 } else if (line[0] == ' ')
1017 data->lineno++;
1018 else if (line[0] == '@') {
1019 char *plus = strchr(line, '+');
1020 if (plus)
1021 data->lineno = strtol(plus, NULL, 10);
1022 else
1023 die("invalid diff");
1027 static unsigned char *deflate_it(char *data,
1028 unsigned long size,
1029 unsigned long *result_size)
1031 int bound;
1032 unsigned char *deflated;
1033 z_stream stream;
1035 memset(&stream, 0, sizeof(stream));
1036 deflateInit(&stream, zlib_compression_level);
1037 bound = deflateBound(&stream, size);
1038 deflated = xmalloc(bound);
1039 stream.next_out = deflated;
1040 stream.avail_out = bound;
1042 stream.next_in = (unsigned char *)data;
1043 stream.avail_in = size;
1044 while (deflate(&stream, Z_FINISH) == Z_OK)
1045 ; /* nothing */
1046 deflateEnd(&stream);
1047 *result_size = stream.total_out;
1048 return deflated;
1051 static void emit_binary_diff_body(mmfile_t *one, mmfile_t *two)
1053 void *cp;
1054 void *delta;
1055 void *deflated;
1056 void *data;
1057 unsigned long orig_size;
1058 unsigned long delta_size;
1059 unsigned long deflate_size;
1060 unsigned long data_size;
1062 /* We could do deflated delta, or we could do just deflated two,
1063 * whichever is smaller.
1065 delta = NULL;
1066 deflated = deflate_it(two->ptr, two->size, &deflate_size);
1067 if (one->size && two->size) {
1068 delta = diff_delta(one->ptr, one->size,
1069 two->ptr, two->size,
1070 &delta_size, deflate_size);
1071 if (delta) {
1072 void *to_free = delta;
1073 orig_size = delta_size;
1074 delta = deflate_it(delta, delta_size, &delta_size);
1075 free(to_free);
1079 if (delta && delta_size < deflate_size) {
1080 printf("delta %lu\n", orig_size);
1081 free(deflated);
1082 data = delta;
1083 data_size = delta_size;
1085 else {
1086 printf("literal %lu\n", two->size);
1087 free(delta);
1088 data = deflated;
1089 data_size = deflate_size;
1092 /* emit data encoded in base85 */
1093 cp = data;
1094 while (data_size) {
1095 int bytes = (52 < data_size) ? 52 : data_size;
1096 char line[70];
1097 data_size -= bytes;
1098 if (bytes <= 26)
1099 line[0] = bytes + 'A' - 1;
1100 else
1101 line[0] = bytes - 26 + 'a' - 1;
1102 encode_85(line + 1, cp, bytes);
1103 cp = (char *) cp + bytes;
1104 puts(line);
1106 printf("\n");
1107 free(data);
1110 static void emit_binary_diff(mmfile_t *one, mmfile_t *two)
1112 printf("GIT binary patch\n");
1113 emit_binary_diff_body(one, two);
1114 emit_binary_diff_body(two, one);
1117 static void setup_diff_attr_check(struct git_attr_check *check)
1119 static struct git_attr *attr_diff;
1121 if (!attr_diff)
1122 attr_diff = git_attr("diff", 4);
1123 check->attr = attr_diff;
1126 #define FIRST_FEW_BYTES 8000
1127 static int file_is_binary(struct diff_filespec *one)
1129 unsigned long sz;
1130 struct git_attr_check attr_diff_check;
1132 setup_diff_attr_check(&attr_diff_check);
1133 if (!git_checkattr(one->path, 1, &attr_diff_check)) {
1134 const char *value = attr_diff_check.value;
1135 if (ATTR_TRUE(value))
1136 return 0;
1137 else if (ATTR_FALSE(value))
1138 return 1;
1141 if (!one->data) {
1142 if (!DIFF_FILE_VALID(one))
1143 return 0;
1144 diff_populate_filespec(one, 0);
1146 sz = one->size;
1147 if (FIRST_FEW_BYTES < sz)
1148 sz = FIRST_FEW_BYTES;
1149 return !!memchr(one->data, 0, sz);
1152 static void builtin_diff(const char *name_a,
1153 const char *name_b,
1154 struct diff_filespec *one,
1155 struct diff_filespec *two,
1156 const char *xfrm_msg,
1157 struct diff_options *o,
1158 int complete_rewrite)
1160 mmfile_t mf1, mf2;
1161 const char *lbl[2];
1162 char *a_one, *b_two;
1163 const char *set = diff_get_color(o->color_diff, DIFF_METAINFO);
1164 const char *reset = diff_get_color(o->color_diff, DIFF_RESET);
1166 a_one = quote_two("a/", name_a + (*name_a == '/'));
1167 b_two = quote_two("b/", name_b + (*name_b == '/'));
1168 lbl[0] = DIFF_FILE_VALID(one) ? a_one : "/dev/null";
1169 lbl[1] = DIFF_FILE_VALID(two) ? b_two : "/dev/null";
1170 printf("%sdiff --git %s %s%s\n", set, a_one, b_two, reset);
1171 if (lbl[0][0] == '/') {
1172 /* /dev/null */
1173 printf("%snew file mode %06o%s\n", set, two->mode, reset);
1174 if (xfrm_msg && xfrm_msg[0])
1175 printf("%s%s%s\n", set, xfrm_msg, reset);
1177 else if (lbl[1][0] == '/') {
1178 printf("%sdeleted file mode %06o%s\n", set, one->mode, reset);
1179 if (xfrm_msg && xfrm_msg[0])
1180 printf("%s%s%s\n", set, xfrm_msg, reset);
1182 else {
1183 if (one->mode != two->mode) {
1184 printf("%sold mode %06o%s\n", set, one->mode, reset);
1185 printf("%snew mode %06o%s\n", set, two->mode, reset);
1187 if (xfrm_msg && xfrm_msg[0])
1188 printf("%s%s%s\n", set, xfrm_msg, reset);
1190 * we do not run diff between different kind
1191 * of objects.
1193 if ((one->mode ^ two->mode) & S_IFMT)
1194 goto free_ab_and_return;
1195 if (complete_rewrite) {
1196 emit_rewrite_diff(name_a, name_b, one, two,
1197 o->color_diff);
1198 o->found_changes = 1;
1199 goto free_ab_and_return;
1203 if (fill_mmfile(&mf1, one) < 0 || fill_mmfile(&mf2, two) < 0)
1204 die("unable to read files to diff");
1206 if (!o->text && (file_is_binary(one) || file_is_binary(two))) {
1207 /* Quite common confusing case */
1208 if (mf1.size == mf2.size &&
1209 !memcmp(mf1.ptr, mf2.ptr, mf1.size))
1210 goto free_ab_and_return;
1211 if (o->binary)
1212 emit_binary_diff(&mf1, &mf2);
1213 else
1214 printf("Binary files %s and %s differ\n",
1215 lbl[0], lbl[1]);
1216 o->found_changes = 1;
1218 else {
1219 /* Crazy xdl interfaces.. */
1220 const char *diffopts = getenv("GIT_DIFF_OPTS");
1221 xpparam_t xpp;
1222 xdemitconf_t xecfg;
1223 xdemitcb_t ecb;
1224 struct emit_callback ecbdata;
1226 memset(&ecbdata, 0, sizeof(ecbdata));
1227 ecbdata.label_path = lbl;
1228 ecbdata.color_diff = o->color_diff;
1229 ecbdata.found_changesp = &o->found_changes;
1230 xpp.flags = XDF_NEED_MINIMAL | o->xdl_opts;
1231 xecfg.ctxlen = o->context;
1232 xecfg.flags = XDL_EMIT_FUNCNAMES;
1233 if (!diffopts)
1235 else if (!prefixcmp(diffopts, "--unified="))
1236 xecfg.ctxlen = strtoul(diffopts + 10, NULL, 10);
1237 else if (!prefixcmp(diffopts, "-u"))
1238 xecfg.ctxlen = strtoul(diffopts + 2, NULL, 10);
1239 ecb.outf = xdiff_outf;
1240 ecb.priv = &ecbdata;
1241 ecbdata.xm.consume = fn_out_consume;
1242 if (o->color_diff_words)
1243 ecbdata.diff_words =
1244 xcalloc(1, sizeof(struct diff_words_data));
1245 xdl_diff(&mf1, &mf2, &xpp, &xecfg, &ecb);
1246 if (o->color_diff_words)
1247 free_diff_words_data(&ecbdata);
1250 free_ab_and_return:
1251 diff_free_filespec_data(one);
1252 diff_free_filespec_data(two);
1253 free(a_one);
1254 free(b_two);
1255 return;
1258 static void builtin_diffstat(const char *name_a, const char *name_b,
1259 struct diff_filespec *one,
1260 struct diff_filespec *two,
1261 struct diffstat_t *diffstat,
1262 struct diff_options *o,
1263 int complete_rewrite)
1265 mmfile_t mf1, mf2;
1266 struct diffstat_file *data;
1268 data = diffstat_add(diffstat, name_a, name_b);
1270 if (!one || !two) {
1271 data->is_unmerged = 1;
1272 return;
1274 if (complete_rewrite) {
1275 diff_populate_filespec(one, 0);
1276 diff_populate_filespec(two, 0);
1277 data->deleted = count_lines(one->data, one->size);
1278 data->added = count_lines(two->data, two->size);
1279 goto free_and_return;
1281 if (fill_mmfile(&mf1, one) < 0 || fill_mmfile(&mf2, two) < 0)
1282 die("unable to read files to diff");
1284 if (file_is_binary(one) || file_is_binary(two)) {
1285 data->is_binary = 1;
1286 data->added = mf2.size;
1287 data->deleted = mf1.size;
1288 } else {
1289 /* Crazy xdl interfaces.. */
1290 xpparam_t xpp;
1291 xdemitconf_t xecfg;
1292 xdemitcb_t ecb;
1294 xpp.flags = XDF_NEED_MINIMAL | o->xdl_opts;
1295 xecfg.ctxlen = 0;
1296 xecfg.flags = 0;
1297 ecb.outf = xdiff_outf;
1298 ecb.priv = diffstat;
1299 xdl_diff(&mf1, &mf2, &xpp, &xecfg, &ecb);
1302 free_and_return:
1303 diff_free_filespec_data(one);
1304 diff_free_filespec_data(two);
1307 static void builtin_checkdiff(const char *name_a, const char *name_b,
1308 struct diff_filespec *one,
1309 struct diff_filespec *two, struct diff_options *o)
1311 mmfile_t mf1, mf2;
1312 struct checkdiff_t data;
1314 if (!two)
1315 return;
1317 memset(&data, 0, sizeof(data));
1318 data.xm.consume = checkdiff_consume;
1319 data.filename = name_b ? name_b : name_a;
1320 data.lineno = 0;
1321 data.color_diff = o->color_diff;
1323 if (fill_mmfile(&mf1, one) < 0 || fill_mmfile(&mf2, two) < 0)
1324 die("unable to read files to diff");
1326 if (file_is_binary(two))
1327 goto free_and_return;
1328 else {
1329 /* Crazy xdl interfaces.. */
1330 xpparam_t xpp;
1331 xdemitconf_t xecfg;
1332 xdemitcb_t ecb;
1334 xpp.flags = XDF_NEED_MINIMAL;
1335 xecfg.ctxlen = 0;
1336 xecfg.flags = 0;
1337 ecb.outf = xdiff_outf;
1338 ecb.priv = &data;
1339 xdl_diff(&mf1, &mf2, &xpp, &xecfg, &ecb);
1341 free_and_return:
1342 diff_free_filespec_data(one);
1343 diff_free_filespec_data(two);
1346 struct diff_filespec *alloc_filespec(const char *path)
1348 int namelen = strlen(path);
1349 struct diff_filespec *spec = xmalloc(sizeof(*spec) + namelen + 1);
1351 memset(spec, 0, sizeof(*spec));
1352 spec->path = (char *)(spec + 1);
1353 memcpy(spec->path, path, namelen+1);
1354 return spec;
1357 void fill_filespec(struct diff_filespec *spec, const unsigned char *sha1,
1358 unsigned short mode)
1360 if (mode) {
1361 spec->mode = canon_mode(mode);
1362 hashcpy(spec->sha1, sha1);
1363 spec->sha1_valid = !is_null_sha1(sha1);
1368 * Given a name and sha1 pair, if the dircache tells us the file in
1369 * the work tree has that object contents, return true, so that
1370 * prepare_temp_file() does not have to inflate and extract.
1372 static int reuse_worktree_file(const char *name, const unsigned char *sha1, int want_file)
1374 struct cache_entry *ce;
1375 struct stat st;
1376 int pos, len;
1378 /* We do not read the cache ourselves here, because the
1379 * benchmark with my previous version that always reads cache
1380 * shows that it makes things worse for diff-tree comparing
1381 * two linux-2.6 kernel trees in an already checked out work
1382 * tree. This is because most diff-tree comparisons deal with
1383 * only a small number of files, while reading the cache is
1384 * expensive for a large project, and its cost outweighs the
1385 * savings we get by not inflating the object to a temporary
1386 * file. Practically, this code only helps when we are used
1387 * by diff-cache --cached, which does read the cache before
1388 * calling us.
1390 if (!active_cache)
1391 return 0;
1393 /* We want to avoid the working directory if our caller
1394 * doesn't need the data in a normal file, this system
1395 * is rather slow with its stat/open/mmap/close syscalls,
1396 * and the object is contained in a pack file. The pack
1397 * is probably already open and will be faster to obtain
1398 * the data through than the working directory. Loose
1399 * objects however would tend to be slower as they need
1400 * to be individually opened and inflated.
1402 if (!FAST_WORKING_DIRECTORY && !want_file && has_sha1_pack(sha1, NULL))
1403 return 0;
1405 len = strlen(name);
1406 pos = cache_name_pos(name, len);
1407 if (pos < 0)
1408 return 0;
1409 ce = active_cache[pos];
1410 if ((lstat(name, &st) < 0) ||
1411 !S_ISREG(st.st_mode) || /* careful! */
1412 ce_match_stat(ce, &st, 0) ||
1413 hashcmp(sha1, ce->sha1))
1414 return 0;
1415 /* we return 1 only when we can stat, it is a regular file,
1416 * stat information matches, and sha1 recorded in the cache
1417 * matches. I.e. we know the file in the work tree really is
1418 * the same as the <name, sha1> pair.
1420 return 1;
1423 static int populate_from_stdin(struct diff_filespec *s)
1425 #define INCREMENT 1024
1426 char *buf;
1427 unsigned long size;
1428 int got;
1430 size = 0;
1431 buf = NULL;
1432 while (1) {
1433 buf = xrealloc(buf, size + INCREMENT);
1434 got = xread(0, buf + size, INCREMENT);
1435 if (!got)
1436 break; /* EOF */
1437 if (got < 0)
1438 return error("error while reading from stdin %s",
1439 strerror(errno));
1440 size += got;
1442 s->should_munmap = 0;
1443 s->data = buf;
1444 s->size = size;
1445 s->should_free = 1;
1446 return 0;
1449 static int diff_populate_gitlink(struct diff_filespec *s, int size_only)
1451 int len;
1452 char *data = xmalloc(100);
1453 len = snprintf(data, 100,
1454 "Subproject commit %s\n", sha1_to_hex(s->sha1));
1455 s->data = data;
1456 s->size = len;
1457 s->should_free = 1;
1458 if (size_only) {
1459 s->data = NULL;
1460 free(data);
1462 return 0;
1466 * While doing rename detection and pickaxe operation, we may need to
1467 * grab the data for the blob (or file) for our own in-core comparison.
1468 * diff_filespec has data and size fields for this purpose.
1470 int diff_populate_filespec(struct diff_filespec *s, int size_only)
1472 int err = 0;
1473 if (!DIFF_FILE_VALID(s))
1474 die("internal error: asking to populate invalid file.");
1475 if (S_ISDIR(s->mode))
1476 return -1;
1478 if (s->data)
1479 return 0;
1481 if (size_only && 0 < s->size)
1482 return 0;
1484 if (S_ISDIRLNK(s->mode))
1485 return diff_populate_gitlink(s, size_only);
1487 if (!s->sha1_valid ||
1488 reuse_worktree_file(s->path, s->sha1, 0)) {
1489 struct stat st;
1490 int fd;
1491 char *buf;
1492 unsigned long size;
1494 if (!strcmp(s->path, "-"))
1495 return populate_from_stdin(s);
1497 if (lstat(s->path, &st) < 0) {
1498 if (errno == ENOENT) {
1499 err_empty:
1500 err = -1;
1501 empty:
1502 s->data = (char *)"";
1503 s->size = 0;
1504 return err;
1507 s->size = xsize_t(st.st_size);
1508 if (!s->size)
1509 goto empty;
1510 if (size_only)
1511 return 0;
1512 if (S_ISLNK(st.st_mode)) {
1513 int ret;
1514 s->data = xmalloc(s->size);
1515 s->should_free = 1;
1516 ret = readlink(s->path, s->data, s->size);
1517 if (ret < 0) {
1518 free(s->data);
1519 goto err_empty;
1521 return 0;
1523 fd = open(s->path, O_RDONLY);
1524 if (fd < 0)
1525 goto err_empty;
1526 s->data = xmmap(NULL, s->size, PROT_READ, MAP_PRIVATE, fd, 0);
1527 close(fd);
1528 s->should_munmap = 1;
1531 * Convert from working tree format to canonical git format
1533 size = s->size;
1534 buf = convert_to_git(s->path, s->data, &size);
1535 if (buf) {
1536 munmap(s->data, s->size);
1537 s->should_munmap = 0;
1538 s->data = buf;
1539 s->size = size;
1540 s->should_free = 1;
1543 else {
1544 enum object_type type;
1545 if (size_only)
1546 type = sha1_object_info(s->sha1, &s->size);
1547 else {
1548 s->data = read_sha1_file(s->sha1, &type, &s->size);
1549 s->should_free = 1;
1552 return 0;
1555 void diff_free_filespec_data(struct diff_filespec *s)
1557 if (s->should_free)
1558 free(s->data);
1559 else if (s->should_munmap)
1560 munmap(s->data, s->size);
1562 if (s->should_free || s->should_munmap) {
1563 s->should_free = s->should_munmap = 0;
1564 s->data = NULL;
1566 free(s->cnt_data);
1567 s->cnt_data = NULL;
1570 static void prep_temp_blob(struct diff_tempfile *temp,
1571 void *blob,
1572 unsigned long size,
1573 const unsigned char *sha1,
1574 int mode)
1576 int fd;
1578 fd = git_mkstemp(temp->tmp_path, TEMPFILE_PATH_LEN, ".diff_XXXXXX");
1579 if (fd < 0)
1580 die("unable to create temp-file");
1581 if (write_in_full(fd, blob, size) != size)
1582 die("unable to write temp-file");
1583 close(fd);
1584 temp->name = temp->tmp_path;
1585 strcpy(temp->hex, sha1_to_hex(sha1));
1586 temp->hex[40] = 0;
1587 sprintf(temp->mode, "%06o", mode);
1590 static void prepare_temp_file(const char *name,
1591 struct diff_tempfile *temp,
1592 struct diff_filespec *one)
1594 if (!DIFF_FILE_VALID(one)) {
1595 not_a_valid_file:
1596 /* A '-' entry produces this for file-2, and
1597 * a '+' entry produces this for file-1.
1599 temp->name = "/dev/null";
1600 strcpy(temp->hex, ".");
1601 strcpy(temp->mode, ".");
1602 return;
1605 if (!one->sha1_valid ||
1606 reuse_worktree_file(name, one->sha1, 1)) {
1607 struct stat st;
1608 if (lstat(name, &st) < 0) {
1609 if (errno == ENOENT)
1610 goto not_a_valid_file;
1611 die("stat(%s): %s", name, strerror(errno));
1613 if (S_ISLNK(st.st_mode)) {
1614 int ret;
1615 char buf[PATH_MAX + 1]; /* ought to be SYMLINK_MAX */
1616 size_t sz = xsize_t(st.st_size);
1617 if (sizeof(buf) <= st.st_size)
1618 die("symlink too long: %s", name);
1619 ret = readlink(name, buf, sz);
1620 if (ret < 0)
1621 die("readlink(%s)", name);
1622 prep_temp_blob(temp, buf, sz,
1623 (one->sha1_valid ?
1624 one->sha1 : null_sha1),
1625 (one->sha1_valid ?
1626 one->mode : S_IFLNK));
1628 else {
1629 /* we can borrow from the file in the work tree */
1630 temp->name = name;
1631 if (!one->sha1_valid)
1632 strcpy(temp->hex, sha1_to_hex(null_sha1));
1633 else
1634 strcpy(temp->hex, sha1_to_hex(one->sha1));
1635 /* Even though we may sometimes borrow the
1636 * contents from the work tree, we always want
1637 * one->mode. mode is trustworthy even when
1638 * !(one->sha1_valid), as long as
1639 * DIFF_FILE_VALID(one).
1641 sprintf(temp->mode, "%06o", one->mode);
1643 return;
1645 else {
1646 if (diff_populate_filespec(one, 0))
1647 die("cannot read data blob for %s", one->path);
1648 prep_temp_blob(temp, one->data, one->size,
1649 one->sha1, one->mode);
1653 static void remove_tempfile(void)
1655 int i;
1657 for (i = 0; i < 2; i++)
1658 if (diff_temp[i].name == diff_temp[i].tmp_path) {
1659 unlink(diff_temp[i].name);
1660 diff_temp[i].name = NULL;
1664 static void remove_tempfile_on_signal(int signo)
1666 remove_tempfile();
1667 signal(SIGINT, SIG_DFL);
1668 raise(signo);
1671 static int spawn_prog(const char *pgm, const char **arg)
1673 pid_t pid;
1674 int status;
1676 fflush(NULL);
1677 pid = fork();
1678 if (pid < 0)
1679 die("unable to fork");
1680 if (!pid) {
1681 execvp(pgm, (char *const*) arg);
1682 exit(255);
1685 while (waitpid(pid, &status, 0) < 0) {
1686 if (errno == EINTR)
1687 continue;
1688 return -1;
1691 /* Earlier we did not check the exit status because
1692 * diff exits non-zero if files are different, and
1693 * we are not interested in knowing that. It was a
1694 * mistake which made it harder to quit a diff-*
1695 * session that uses the git-apply-patch-script as
1696 * the GIT_EXTERNAL_DIFF. A custom GIT_EXTERNAL_DIFF
1697 * should also exit non-zero only when it wants to
1698 * abort the entire diff-* session.
1700 if (WIFEXITED(status) && !WEXITSTATUS(status))
1701 return 0;
1702 return -1;
1705 /* An external diff command takes:
1707 * diff-cmd name infile1 infile1-sha1 infile1-mode \
1708 * infile2 infile2-sha1 infile2-mode [ rename-to ]
1711 static void run_external_diff(const char *pgm,
1712 const char *name,
1713 const char *other,
1714 struct diff_filespec *one,
1715 struct diff_filespec *two,
1716 const char *xfrm_msg,
1717 int complete_rewrite)
1719 const char *spawn_arg[10];
1720 struct diff_tempfile *temp = diff_temp;
1721 int retval;
1722 static int atexit_asked = 0;
1723 const char *othername;
1724 const char **arg = &spawn_arg[0];
1726 othername = (other? other : name);
1727 if (one && two) {
1728 prepare_temp_file(name, &temp[0], one);
1729 prepare_temp_file(othername, &temp[1], two);
1730 if (! atexit_asked &&
1731 (temp[0].name == temp[0].tmp_path ||
1732 temp[1].name == temp[1].tmp_path)) {
1733 atexit_asked = 1;
1734 atexit(remove_tempfile);
1736 signal(SIGINT, remove_tempfile_on_signal);
1739 if (one && two) {
1740 *arg++ = pgm;
1741 *arg++ = name;
1742 *arg++ = temp[0].name;
1743 *arg++ = temp[0].hex;
1744 *arg++ = temp[0].mode;
1745 *arg++ = temp[1].name;
1746 *arg++ = temp[1].hex;
1747 *arg++ = temp[1].mode;
1748 if (other) {
1749 *arg++ = other;
1750 *arg++ = xfrm_msg;
1752 } else {
1753 *arg++ = pgm;
1754 *arg++ = name;
1756 *arg = NULL;
1757 retval = spawn_prog(pgm, spawn_arg);
1758 remove_tempfile();
1759 if (retval) {
1760 fprintf(stderr, "external diff died, stopping at %s.\n", name);
1761 exit(1);
1765 static const char *external_diff_attr(const char *name)
1767 struct git_attr_check attr_diff_check;
1769 setup_diff_attr_check(&attr_diff_check);
1770 if (!git_checkattr(name, 1, &attr_diff_check)) {
1771 const char *value = attr_diff_check.value;
1772 if (!ATTR_TRUE(value) &&
1773 !ATTR_FALSE(value) &&
1774 !ATTR_UNSET(value)) {
1775 struct ll_diff_driver *drv;
1777 if (!user_diff_tail) {
1778 user_diff_tail = &user_diff;
1779 git_config(git_diff_ui_config);
1781 for (drv = user_diff; drv; drv = drv->next)
1782 if (!strcmp(drv->name, value))
1783 return drv->cmd;
1786 return NULL;
1789 static void run_diff_cmd(const char *pgm,
1790 const char *name,
1791 const char *other,
1792 struct diff_filespec *one,
1793 struct diff_filespec *two,
1794 const char *xfrm_msg,
1795 struct diff_options *o,
1796 int complete_rewrite)
1798 if (!o->allow_external)
1799 pgm = NULL;
1800 else {
1801 const char *cmd = external_diff_attr(name);
1802 if (cmd)
1803 pgm = cmd;
1806 if (pgm) {
1807 run_external_diff(pgm, name, other, one, two, xfrm_msg,
1808 complete_rewrite);
1809 return;
1811 if (one && two)
1812 builtin_diff(name, other ? other : name,
1813 one, two, xfrm_msg, o, complete_rewrite);
1814 else
1815 printf("* Unmerged path %s\n", name);
1818 static void diff_fill_sha1_info(struct diff_filespec *one)
1820 if (DIFF_FILE_VALID(one)) {
1821 if (!one->sha1_valid) {
1822 struct stat st;
1823 if (!strcmp(one->path, "-")) {
1824 hashcpy(one->sha1, null_sha1);
1825 return;
1827 if (lstat(one->path, &st) < 0)
1828 die("stat %s", one->path);
1829 if (index_path(one->sha1, one->path, &st, 0))
1830 die("cannot hash %s\n", one->path);
1833 else
1834 hashclr(one->sha1);
1837 static void run_diff(struct diff_filepair *p, struct diff_options *o)
1839 const char *pgm = external_diff();
1840 char msg[PATH_MAX*2+300], *xfrm_msg;
1841 struct diff_filespec *one;
1842 struct diff_filespec *two;
1843 const char *name;
1844 const char *other;
1845 char *name_munged, *other_munged;
1846 int complete_rewrite = 0;
1847 int len;
1849 if (DIFF_PAIR_UNMERGED(p)) {
1850 /* unmerged */
1851 run_diff_cmd(pgm, p->one->path, NULL, NULL, NULL, NULL, o, 0);
1852 return;
1855 name = p->one->path;
1856 other = (strcmp(name, p->two->path) ? p->two->path : NULL);
1857 name_munged = quote_one(name);
1858 other_munged = quote_one(other);
1859 one = p->one; two = p->two;
1861 diff_fill_sha1_info(one);
1862 diff_fill_sha1_info(two);
1864 len = 0;
1865 switch (p->status) {
1866 case DIFF_STATUS_COPIED:
1867 len += snprintf(msg + len, sizeof(msg) - len,
1868 "similarity index %d%%\n"
1869 "copy from %s\n"
1870 "copy to %s\n",
1871 (int)(0.5 + p->score * 100.0/MAX_SCORE),
1872 name_munged, other_munged);
1873 break;
1874 case DIFF_STATUS_RENAMED:
1875 len += snprintf(msg + len, sizeof(msg) - len,
1876 "similarity index %d%%\n"
1877 "rename from %s\n"
1878 "rename to %s\n",
1879 (int)(0.5 + p->score * 100.0/MAX_SCORE),
1880 name_munged, other_munged);
1881 break;
1882 case DIFF_STATUS_MODIFIED:
1883 if (p->score) {
1884 len += snprintf(msg + len, sizeof(msg) - len,
1885 "dissimilarity index %d%%\n",
1886 (int)(0.5 + p->score *
1887 100.0/MAX_SCORE));
1888 complete_rewrite = 1;
1889 break;
1891 /* fallthru */
1892 default:
1893 /* nothing */
1897 if (hashcmp(one->sha1, two->sha1)) {
1898 int abbrev = o->full_index ? 40 : DEFAULT_ABBREV;
1900 if (o->binary) {
1901 mmfile_t mf;
1902 if ((!fill_mmfile(&mf, one) && file_is_binary(one)) ||
1903 (!fill_mmfile(&mf, two) && file_is_binary(two)))
1904 abbrev = 40;
1906 len += snprintf(msg + len, sizeof(msg) - len,
1907 "index %.*s..%.*s",
1908 abbrev, sha1_to_hex(one->sha1),
1909 abbrev, sha1_to_hex(two->sha1));
1910 if (one->mode == two->mode)
1911 len += snprintf(msg + len, sizeof(msg) - len,
1912 " %06o", one->mode);
1913 len += snprintf(msg + len, sizeof(msg) - len, "\n");
1916 if (len)
1917 msg[--len] = 0;
1918 xfrm_msg = len ? msg : NULL;
1920 if (!pgm &&
1921 DIFF_FILE_VALID(one) && DIFF_FILE_VALID(two) &&
1922 (S_IFMT & one->mode) != (S_IFMT & two->mode)) {
1923 /* a filepair that changes between file and symlink
1924 * needs to be split into deletion and creation.
1926 struct diff_filespec *null = alloc_filespec(two->path);
1927 run_diff_cmd(NULL, name, other, one, null, xfrm_msg, o, 0);
1928 free(null);
1929 null = alloc_filespec(one->path);
1930 run_diff_cmd(NULL, name, other, null, two, xfrm_msg, o, 0);
1931 free(null);
1933 else
1934 run_diff_cmd(pgm, name, other, one, two, xfrm_msg, o,
1935 complete_rewrite);
1937 free(name_munged);
1938 free(other_munged);
1941 static void run_diffstat(struct diff_filepair *p, struct diff_options *o,
1942 struct diffstat_t *diffstat)
1944 const char *name;
1945 const char *other;
1946 int complete_rewrite = 0;
1948 if (DIFF_PAIR_UNMERGED(p)) {
1949 /* unmerged */
1950 builtin_diffstat(p->one->path, NULL, NULL, NULL, diffstat, o, 0);
1951 return;
1954 name = p->one->path;
1955 other = (strcmp(name, p->two->path) ? p->two->path : NULL);
1957 diff_fill_sha1_info(p->one);
1958 diff_fill_sha1_info(p->two);
1960 if (p->status == DIFF_STATUS_MODIFIED && p->score)
1961 complete_rewrite = 1;
1962 builtin_diffstat(name, other, p->one, p->two, diffstat, o, complete_rewrite);
1965 static void run_checkdiff(struct diff_filepair *p, struct diff_options *o)
1967 const char *name;
1968 const char *other;
1970 if (DIFF_PAIR_UNMERGED(p)) {
1971 /* unmerged */
1972 return;
1975 name = p->one->path;
1976 other = (strcmp(name, p->two->path) ? p->two->path : NULL);
1978 diff_fill_sha1_info(p->one);
1979 diff_fill_sha1_info(p->two);
1981 builtin_checkdiff(name, other, p->one, p->two, o);
1984 void diff_setup(struct diff_options *options)
1986 memset(options, 0, sizeof(*options));
1987 options->line_termination = '\n';
1988 options->break_opt = -1;
1989 options->rename_limit = -1;
1990 options->context = 3;
1991 options->msg_sep = "";
1993 options->change = diff_change;
1994 options->add_remove = diff_addremove;
1995 options->color_diff = diff_use_color_default;
1996 options->detect_rename = diff_detect_rename_default;
1999 int diff_setup_done(struct diff_options *options)
2001 int count = 0;
2003 if (options->output_format & DIFF_FORMAT_NAME)
2004 count++;
2005 if (options->output_format & DIFF_FORMAT_NAME_STATUS)
2006 count++;
2007 if (options->output_format & DIFF_FORMAT_CHECKDIFF)
2008 count++;
2009 if (options->output_format & DIFF_FORMAT_NO_OUTPUT)
2010 count++;
2011 if (count > 1)
2012 die("--name-only, --name-status, --check and -s are mutually exclusive");
2014 if (options->find_copies_harder)
2015 options->detect_rename = DIFF_DETECT_COPY;
2017 if (options->output_format & (DIFF_FORMAT_NAME |
2018 DIFF_FORMAT_NAME_STATUS |
2019 DIFF_FORMAT_CHECKDIFF |
2020 DIFF_FORMAT_NO_OUTPUT))
2021 options->output_format &= ~(DIFF_FORMAT_RAW |
2022 DIFF_FORMAT_NUMSTAT |
2023 DIFF_FORMAT_DIFFSTAT |
2024 DIFF_FORMAT_SHORTSTAT |
2025 DIFF_FORMAT_SUMMARY |
2026 DIFF_FORMAT_PATCH);
2029 * These cases always need recursive; we do not drop caller-supplied
2030 * recursive bits for other formats here.
2032 if (options->output_format & (DIFF_FORMAT_PATCH |
2033 DIFF_FORMAT_NUMSTAT |
2034 DIFF_FORMAT_DIFFSTAT |
2035 DIFF_FORMAT_SHORTSTAT |
2036 DIFF_FORMAT_SUMMARY |
2037 DIFF_FORMAT_CHECKDIFF))
2038 options->recursive = 1;
2040 * Also pickaxe would not work very well if you do not say recursive
2042 if (options->pickaxe)
2043 options->recursive = 1;
2045 if (options->detect_rename && options->rename_limit < 0)
2046 options->rename_limit = diff_rename_limit_default;
2047 if (options->setup & DIFF_SETUP_USE_CACHE) {
2048 if (!active_cache)
2049 /* read-cache does not die even when it fails
2050 * so it is safe for us to do this here. Also
2051 * it does not smudge active_cache or active_nr
2052 * when it fails, so we do not have to worry about
2053 * cleaning it up ourselves either.
2055 read_cache();
2057 if (options->abbrev <= 0 || 40 < options->abbrev)
2058 options->abbrev = 40; /* full */
2061 * It does not make sense to show the first hit we happened
2062 * to have found. It does not make sense not to return with
2063 * exit code in such a case either.
2065 if (options->quiet) {
2066 options->output_format = DIFF_FORMAT_NO_OUTPUT;
2067 options->exit_with_status = 1;
2071 * If we postprocess in diffcore, we cannot simply return
2072 * upon the first hit. We need to run diff as usual.
2074 if (options->pickaxe || options->filter)
2075 options->quiet = 0;
2077 return 0;
2080 static int opt_arg(const char *arg, int arg_short, const char *arg_long, int *val)
2082 char c, *eq;
2083 int len;
2085 if (*arg != '-')
2086 return 0;
2087 c = *++arg;
2088 if (!c)
2089 return 0;
2090 if (c == arg_short) {
2091 c = *++arg;
2092 if (!c)
2093 return 1;
2094 if (val && isdigit(c)) {
2095 char *end;
2096 int n = strtoul(arg, &end, 10);
2097 if (*end)
2098 return 0;
2099 *val = n;
2100 return 1;
2102 return 0;
2104 if (c != '-')
2105 return 0;
2106 arg++;
2107 eq = strchr(arg, '=');
2108 if (eq)
2109 len = eq - arg;
2110 else
2111 len = strlen(arg);
2112 if (!len || strncmp(arg, arg_long, len))
2113 return 0;
2114 if (eq) {
2115 int n;
2116 char *end;
2117 if (!isdigit(*++eq))
2118 return 0;
2119 n = strtoul(eq, &end, 10);
2120 if (*end)
2121 return 0;
2122 *val = n;
2124 return 1;
2127 int diff_opt_parse(struct diff_options *options, const char **av, int ac)
2129 const char *arg = av[0];
2130 if (!strcmp(arg, "-p") || !strcmp(arg, "-u"))
2131 options->output_format |= DIFF_FORMAT_PATCH;
2132 else if (opt_arg(arg, 'U', "unified", &options->context))
2133 options->output_format |= DIFF_FORMAT_PATCH;
2134 else if (!strcmp(arg, "--raw"))
2135 options->output_format |= DIFF_FORMAT_RAW;
2136 else if (!strcmp(arg, "--patch-with-raw")) {
2137 options->output_format |= DIFF_FORMAT_PATCH | DIFF_FORMAT_RAW;
2139 else if (!strcmp(arg, "--numstat")) {
2140 options->output_format |= DIFF_FORMAT_NUMSTAT;
2142 else if (!strcmp(arg, "--shortstat")) {
2143 options->output_format |= DIFF_FORMAT_SHORTSTAT;
2145 else if (!prefixcmp(arg, "--stat")) {
2146 char *end;
2147 int width = options->stat_width;
2148 int name_width = options->stat_name_width;
2149 arg += 6;
2150 end = (char *)arg;
2152 switch (*arg) {
2153 case '-':
2154 if (!prefixcmp(arg, "-width="))
2155 width = strtoul(arg + 7, &end, 10);
2156 else if (!prefixcmp(arg, "-name-width="))
2157 name_width = strtoul(arg + 12, &end, 10);
2158 break;
2159 case '=':
2160 width = strtoul(arg+1, &end, 10);
2161 if (*end == ',')
2162 name_width = strtoul(end+1, &end, 10);
2165 /* Important! This checks all the error cases! */
2166 if (*end)
2167 return 0;
2168 options->output_format |= DIFF_FORMAT_DIFFSTAT;
2169 options->stat_name_width = name_width;
2170 options->stat_width = width;
2172 else if (!strcmp(arg, "--check"))
2173 options->output_format |= DIFF_FORMAT_CHECKDIFF;
2174 else if (!strcmp(arg, "--summary"))
2175 options->output_format |= DIFF_FORMAT_SUMMARY;
2176 else if (!strcmp(arg, "--patch-with-stat")) {
2177 options->output_format |= DIFF_FORMAT_PATCH | DIFF_FORMAT_DIFFSTAT;
2179 else if (!strcmp(arg, "-z"))
2180 options->line_termination = 0;
2181 else if (!prefixcmp(arg, "-l"))
2182 options->rename_limit = strtoul(arg+2, NULL, 10);
2183 else if (!strcmp(arg, "--full-index"))
2184 options->full_index = 1;
2185 else if (!strcmp(arg, "--binary")) {
2186 options->output_format |= DIFF_FORMAT_PATCH;
2187 options->binary = 1;
2189 else if (!strcmp(arg, "-a") || !strcmp(arg, "--text")) {
2190 options->text = 1;
2192 else if (!strcmp(arg, "--name-only"))
2193 options->output_format |= DIFF_FORMAT_NAME;
2194 else if (!strcmp(arg, "--name-status"))
2195 options->output_format |= DIFF_FORMAT_NAME_STATUS;
2196 else if (!strcmp(arg, "-R"))
2197 options->reverse_diff = 1;
2198 else if (!prefixcmp(arg, "-S"))
2199 options->pickaxe = arg + 2;
2200 else if (!strcmp(arg, "-s")) {
2201 options->output_format |= DIFF_FORMAT_NO_OUTPUT;
2203 else if (!prefixcmp(arg, "-O"))
2204 options->orderfile = arg + 2;
2205 else if (!prefixcmp(arg, "--diff-filter="))
2206 options->filter = arg + 14;
2207 else if (!strcmp(arg, "--pickaxe-all"))
2208 options->pickaxe_opts = DIFF_PICKAXE_ALL;
2209 else if (!strcmp(arg, "--pickaxe-regex"))
2210 options->pickaxe_opts = DIFF_PICKAXE_REGEX;
2211 else if (!prefixcmp(arg, "-B")) {
2212 if ((options->break_opt =
2213 diff_scoreopt_parse(arg)) == -1)
2214 return -1;
2216 else if (!prefixcmp(arg, "-M")) {
2217 if ((options->rename_score =
2218 diff_scoreopt_parse(arg)) == -1)
2219 return -1;
2220 options->detect_rename = DIFF_DETECT_RENAME;
2222 else if (!prefixcmp(arg, "-C")) {
2223 if ((options->rename_score =
2224 diff_scoreopt_parse(arg)) == -1)
2225 return -1;
2226 options->detect_rename = DIFF_DETECT_COPY;
2228 else if (!strcmp(arg, "--find-copies-harder"))
2229 options->find_copies_harder = 1;
2230 else if (!strcmp(arg, "--abbrev"))
2231 options->abbrev = DEFAULT_ABBREV;
2232 else if (!prefixcmp(arg, "--abbrev=")) {
2233 options->abbrev = strtoul(arg + 9, NULL, 10);
2234 if (options->abbrev < MINIMUM_ABBREV)
2235 options->abbrev = MINIMUM_ABBREV;
2236 else if (40 < options->abbrev)
2237 options->abbrev = 40;
2239 else if (!strcmp(arg, "--color"))
2240 options->color_diff = 1;
2241 else if (!strcmp(arg, "--no-color"))
2242 options->color_diff = 0;
2243 else if (!strcmp(arg, "-w") || !strcmp(arg, "--ignore-all-space"))
2244 options->xdl_opts |= XDF_IGNORE_WHITESPACE;
2245 else if (!strcmp(arg, "-b") || !strcmp(arg, "--ignore-space-change"))
2246 options->xdl_opts |= XDF_IGNORE_WHITESPACE_CHANGE;
2247 else if (!strcmp(arg, "--ignore-space-at-eol"))
2248 options->xdl_opts |= XDF_IGNORE_WHITESPACE_AT_EOL;
2249 else if (!strcmp(arg, "--color-words"))
2250 options->color_diff = options->color_diff_words = 1;
2251 else if (!strcmp(arg, "--no-renames"))
2252 options->detect_rename = 0;
2253 else if (!strcmp(arg, "--exit-code"))
2254 options->exit_with_status = 1;
2255 else if (!strcmp(arg, "--quiet"))
2256 options->quiet = 1;
2257 else
2258 return 0;
2259 return 1;
2262 static int parse_num(const char **cp_p)
2264 unsigned long num, scale;
2265 int ch, dot;
2266 const char *cp = *cp_p;
2268 num = 0;
2269 scale = 1;
2270 dot = 0;
2271 for(;;) {
2272 ch = *cp;
2273 if ( !dot && ch == '.' ) {
2274 scale = 1;
2275 dot = 1;
2276 } else if ( ch == '%' ) {
2277 scale = dot ? scale*100 : 100;
2278 cp++; /* % is always at the end */
2279 break;
2280 } else if ( ch >= '0' && ch <= '9' ) {
2281 if ( scale < 100000 ) {
2282 scale *= 10;
2283 num = (num*10) + (ch-'0');
2285 } else {
2286 break;
2288 cp++;
2290 *cp_p = cp;
2292 /* user says num divided by scale and we say internally that
2293 * is MAX_SCORE * num / scale.
2295 return (int)((num >= scale) ? MAX_SCORE : (MAX_SCORE * num / scale));
2298 int diff_scoreopt_parse(const char *opt)
2300 int opt1, opt2, cmd;
2302 if (*opt++ != '-')
2303 return -1;
2304 cmd = *opt++;
2305 if (cmd != 'M' && cmd != 'C' && cmd != 'B')
2306 return -1; /* that is not a -M, -C nor -B option */
2308 opt1 = parse_num(&opt);
2309 if (cmd != 'B')
2310 opt2 = 0;
2311 else {
2312 if (*opt == 0)
2313 opt2 = 0;
2314 else if (*opt != '/')
2315 return -1; /* we expect -B80/99 or -B80 */
2316 else {
2317 opt++;
2318 opt2 = parse_num(&opt);
2321 if (*opt != 0)
2322 return -1;
2323 return opt1 | (opt2 << 16);
2326 struct diff_queue_struct diff_queued_diff;
2328 void diff_q(struct diff_queue_struct *queue, struct diff_filepair *dp)
2330 if (queue->alloc <= queue->nr) {
2331 queue->alloc = alloc_nr(queue->alloc);
2332 queue->queue = xrealloc(queue->queue,
2333 sizeof(dp) * queue->alloc);
2335 queue->queue[queue->nr++] = dp;
2338 struct diff_filepair *diff_queue(struct diff_queue_struct *queue,
2339 struct diff_filespec *one,
2340 struct diff_filespec *two)
2342 struct diff_filepair *dp = xcalloc(1, sizeof(*dp));
2343 dp->one = one;
2344 dp->two = two;
2345 if (queue)
2346 diff_q(queue, dp);
2347 return dp;
2350 void diff_free_filepair(struct diff_filepair *p)
2352 diff_free_filespec_data(p->one);
2353 diff_free_filespec_data(p->two);
2354 free(p->one);
2355 free(p->two);
2356 free(p);
2359 /* This is different from find_unique_abbrev() in that
2360 * it stuffs the result with dots for alignment.
2362 const char *diff_unique_abbrev(const unsigned char *sha1, int len)
2364 int abblen;
2365 const char *abbrev;
2366 if (len == 40)
2367 return sha1_to_hex(sha1);
2369 abbrev = find_unique_abbrev(sha1, len);
2370 if (!abbrev)
2371 return sha1_to_hex(sha1);
2372 abblen = strlen(abbrev);
2373 if (abblen < 37) {
2374 static char hex[41];
2375 if (len < abblen && abblen <= len + 2)
2376 sprintf(hex, "%s%.*s", abbrev, len+3-abblen, "..");
2377 else
2378 sprintf(hex, "%s...", abbrev);
2379 return hex;
2381 return sha1_to_hex(sha1);
2384 static void diff_flush_raw(struct diff_filepair *p,
2385 struct diff_options *options)
2387 int two_paths;
2388 char status[10];
2389 int abbrev = options->abbrev;
2390 const char *path_one, *path_two;
2391 int inter_name_termination = '\t';
2392 int line_termination = options->line_termination;
2394 if (!line_termination)
2395 inter_name_termination = 0;
2397 path_one = p->one->path;
2398 path_two = p->two->path;
2399 if (line_termination) {
2400 path_one = quote_one(path_one);
2401 path_two = quote_one(path_two);
2404 if (p->score)
2405 sprintf(status, "%c%03d", p->status,
2406 (int)(0.5 + p->score * 100.0/MAX_SCORE));
2407 else {
2408 status[0] = p->status;
2409 status[1] = 0;
2411 switch (p->status) {
2412 case DIFF_STATUS_COPIED:
2413 case DIFF_STATUS_RENAMED:
2414 two_paths = 1;
2415 break;
2416 case DIFF_STATUS_ADDED:
2417 case DIFF_STATUS_DELETED:
2418 two_paths = 0;
2419 break;
2420 default:
2421 two_paths = 0;
2422 break;
2424 if (!(options->output_format & DIFF_FORMAT_NAME_STATUS)) {
2425 printf(":%06o %06o %s ",
2426 p->one->mode, p->two->mode,
2427 diff_unique_abbrev(p->one->sha1, abbrev));
2428 printf("%s ",
2429 diff_unique_abbrev(p->two->sha1, abbrev));
2431 printf("%s%c%s", status, inter_name_termination, path_one);
2432 if (two_paths)
2433 printf("%c%s", inter_name_termination, path_two);
2434 putchar(line_termination);
2435 if (path_one != p->one->path)
2436 free((void*)path_one);
2437 if (path_two != p->two->path)
2438 free((void*)path_two);
2441 static void diff_flush_name(struct diff_filepair *p, struct diff_options *opt)
2443 char *path = p->two->path;
2445 if (opt->line_termination)
2446 path = quote_one(p->two->path);
2447 printf("%s%c", path, opt->line_termination);
2448 if (p->two->path != path)
2449 free(path);
2452 int diff_unmodified_pair(struct diff_filepair *p)
2454 /* This function is written stricter than necessary to support
2455 * the currently implemented transformers, but the idea is to
2456 * let transformers to produce diff_filepairs any way they want,
2457 * and filter and clean them up here before producing the output.
2459 struct diff_filespec *one, *two;
2461 if (DIFF_PAIR_UNMERGED(p))
2462 return 0; /* unmerged is interesting */
2464 one = p->one;
2465 two = p->two;
2467 /* deletion, addition, mode or type change
2468 * and rename are all interesting.
2470 if (DIFF_FILE_VALID(one) != DIFF_FILE_VALID(two) ||
2471 DIFF_PAIR_MODE_CHANGED(p) ||
2472 strcmp(one->path, two->path))
2473 return 0;
2475 /* both are valid and point at the same path. that is, we are
2476 * dealing with a change.
2478 if (one->sha1_valid && two->sha1_valid &&
2479 !hashcmp(one->sha1, two->sha1))
2480 return 1; /* no change */
2481 if (!one->sha1_valid && !two->sha1_valid)
2482 return 1; /* both look at the same file on the filesystem. */
2483 return 0;
2486 static void diff_flush_patch(struct diff_filepair *p, struct diff_options *o)
2488 if (diff_unmodified_pair(p))
2489 return;
2491 if ((DIFF_FILE_VALID(p->one) && S_ISDIR(p->one->mode)) ||
2492 (DIFF_FILE_VALID(p->two) && S_ISDIR(p->two->mode)))
2493 return; /* no tree diffs in patch format */
2495 run_diff(p, o);
2498 static void diff_flush_stat(struct diff_filepair *p, struct diff_options *o,
2499 struct diffstat_t *diffstat)
2501 if (diff_unmodified_pair(p))
2502 return;
2504 if ((DIFF_FILE_VALID(p->one) && S_ISDIR(p->one->mode)) ||
2505 (DIFF_FILE_VALID(p->two) && S_ISDIR(p->two->mode)))
2506 return; /* no tree diffs in patch format */
2508 run_diffstat(p, o, diffstat);
2511 static void diff_flush_checkdiff(struct diff_filepair *p,
2512 struct diff_options *o)
2514 if (diff_unmodified_pair(p))
2515 return;
2517 if ((DIFF_FILE_VALID(p->one) && S_ISDIR(p->one->mode)) ||
2518 (DIFF_FILE_VALID(p->two) && S_ISDIR(p->two->mode)))
2519 return; /* no tree diffs in patch format */
2521 run_checkdiff(p, o);
2524 int diff_queue_is_empty(void)
2526 struct diff_queue_struct *q = &diff_queued_diff;
2527 int i;
2528 for (i = 0; i < q->nr; i++)
2529 if (!diff_unmodified_pair(q->queue[i]))
2530 return 0;
2531 return 1;
2534 #if DIFF_DEBUG
2535 void diff_debug_filespec(struct diff_filespec *s, int x, const char *one)
2537 fprintf(stderr, "queue[%d] %s (%s) %s %06o %s\n",
2538 x, one ? one : "",
2539 s->path,
2540 DIFF_FILE_VALID(s) ? "valid" : "invalid",
2541 s->mode,
2542 s->sha1_valid ? sha1_to_hex(s->sha1) : "");
2543 fprintf(stderr, "queue[%d] %s size %lu flags %d\n",
2544 x, one ? one : "",
2545 s->size, s->xfrm_flags);
2548 void diff_debug_filepair(const struct diff_filepair *p, int i)
2550 diff_debug_filespec(p->one, i, "one");
2551 diff_debug_filespec(p->two, i, "two");
2552 fprintf(stderr, "score %d, status %c stays %d broken %d\n",
2553 p->score, p->status ? p->status : '?',
2554 p->source_stays, p->broken_pair);
2557 void diff_debug_queue(const char *msg, struct diff_queue_struct *q)
2559 int i;
2560 if (msg)
2561 fprintf(stderr, "%s\n", msg);
2562 fprintf(stderr, "q->nr = %d\n", q->nr);
2563 for (i = 0; i < q->nr; i++) {
2564 struct diff_filepair *p = q->queue[i];
2565 diff_debug_filepair(p, i);
2568 #endif
2570 static void diff_resolve_rename_copy(void)
2572 int i, j;
2573 struct diff_filepair *p, *pp;
2574 struct diff_queue_struct *q = &diff_queued_diff;
2576 diff_debug_queue("resolve-rename-copy", q);
2578 for (i = 0; i < q->nr; i++) {
2579 p = q->queue[i];
2580 p->status = 0; /* undecided */
2581 if (DIFF_PAIR_UNMERGED(p))
2582 p->status = DIFF_STATUS_UNMERGED;
2583 else if (!DIFF_FILE_VALID(p->one))
2584 p->status = DIFF_STATUS_ADDED;
2585 else if (!DIFF_FILE_VALID(p->two))
2586 p->status = DIFF_STATUS_DELETED;
2587 else if (DIFF_PAIR_TYPE_CHANGED(p))
2588 p->status = DIFF_STATUS_TYPE_CHANGED;
2590 /* from this point on, we are dealing with a pair
2591 * whose both sides are valid and of the same type, i.e.
2592 * either in-place edit or rename/copy edit.
2594 else if (DIFF_PAIR_RENAME(p)) {
2595 if (p->source_stays) {
2596 p->status = DIFF_STATUS_COPIED;
2597 continue;
2599 /* See if there is some other filepair that
2600 * copies from the same source as us. If so
2601 * we are a copy. Otherwise we are either a
2602 * copy if the path stays, or a rename if it
2603 * does not, but we already handled "stays" case.
2605 for (j = i + 1; j < q->nr; j++) {
2606 pp = q->queue[j];
2607 if (strcmp(pp->one->path, p->one->path))
2608 continue; /* not us */
2609 if (!DIFF_PAIR_RENAME(pp))
2610 continue; /* not a rename/copy */
2611 /* pp is a rename/copy from the same source */
2612 p->status = DIFF_STATUS_COPIED;
2613 break;
2615 if (!p->status)
2616 p->status = DIFF_STATUS_RENAMED;
2618 else if (hashcmp(p->one->sha1, p->two->sha1) ||
2619 p->one->mode != p->two->mode ||
2620 is_null_sha1(p->one->sha1))
2621 p->status = DIFF_STATUS_MODIFIED;
2622 else {
2623 /* This is a "no-change" entry and should not
2624 * happen anymore, but prepare for broken callers.
2626 error("feeding unmodified %s to diffcore",
2627 p->one->path);
2628 p->status = DIFF_STATUS_UNKNOWN;
2631 diff_debug_queue("resolve-rename-copy done", q);
2634 static int check_pair_status(struct diff_filepair *p)
2636 switch (p->status) {
2637 case DIFF_STATUS_UNKNOWN:
2638 return 0;
2639 case 0:
2640 die("internal error in diff-resolve-rename-copy");
2641 default:
2642 return 1;
2646 static void flush_one_pair(struct diff_filepair *p, struct diff_options *opt)
2648 int fmt = opt->output_format;
2650 if (fmt & DIFF_FORMAT_CHECKDIFF)
2651 diff_flush_checkdiff(p, opt);
2652 else if (fmt & (DIFF_FORMAT_RAW | DIFF_FORMAT_NAME_STATUS))
2653 diff_flush_raw(p, opt);
2654 else if (fmt & DIFF_FORMAT_NAME)
2655 diff_flush_name(p, opt);
2658 static void show_file_mode_name(const char *newdelete, struct diff_filespec *fs)
2660 char *name = quote_one(fs->path);
2661 if (fs->mode)
2662 printf(" %s mode %06o %s\n", newdelete, fs->mode, name);
2663 else
2664 printf(" %s %s\n", newdelete, name);
2665 free(name);
2669 static void show_mode_change(struct diff_filepair *p, int show_name)
2671 if (p->one->mode && p->two->mode && p->one->mode != p->two->mode) {
2672 if (show_name) {
2673 char *name = quote_one(p->two->path);
2674 printf(" mode change %06o => %06o %s\n",
2675 p->one->mode, p->two->mode, name);
2676 free(name);
2678 else
2679 printf(" mode change %06o => %06o\n",
2680 p->one->mode, p->two->mode);
2684 static void show_rename_copy(const char *renamecopy, struct diff_filepair *p)
2686 char *names = pprint_rename(p->one->path, p->two->path);
2688 printf(" %s %s (%d%%)\n", renamecopy, names,
2689 (int)(0.5 + p->score * 100.0/MAX_SCORE));
2690 free(names);
2691 show_mode_change(p, 0);
2694 static void diff_summary(struct diff_filepair *p)
2696 switch(p->status) {
2697 case DIFF_STATUS_DELETED:
2698 show_file_mode_name("delete", p->one);
2699 break;
2700 case DIFF_STATUS_ADDED:
2701 show_file_mode_name("create", p->two);
2702 break;
2703 case DIFF_STATUS_COPIED:
2704 show_rename_copy("copy", p);
2705 break;
2706 case DIFF_STATUS_RENAMED:
2707 show_rename_copy("rename", p);
2708 break;
2709 default:
2710 if (p->score) {
2711 char *name = quote_one(p->two->path);
2712 printf(" rewrite %s (%d%%)\n", name,
2713 (int)(0.5 + p->score * 100.0/MAX_SCORE));
2714 free(name);
2715 show_mode_change(p, 0);
2716 } else show_mode_change(p, 1);
2717 break;
2721 struct patch_id_t {
2722 struct xdiff_emit_state xm;
2723 SHA_CTX *ctx;
2724 int patchlen;
2727 static int remove_space(char *line, int len)
2729 int i;
2730 char *dst = line;
2731 unsigned char c;
2733 for (i = 0; i < len; i++)
2734 if (!isspace((c = line[i])))
2735 *dst++ = c;
2737 return dst - line;
2740 static void patch_id_consume(void *priv, char *line, unsigned long len)
2742 struct patch_id_t *data = priv;
2743 int new_len;
2745 /* Ignore line numbers when computing the SHA1 of the patch */
2746 if (!prefixcmp(line, "@@ -"))
2747 return;
2749 new_len = remove_space(line, len);
2751 SHA1_Update(data->ctx, line, new_len);
2752 data->patchlen += new_len;
2755 /* returns 0 upon success, and writes result into sha1 */
2756 static int diff_get_patch_id(struct diff_options *options, unsigned char *sha1)
2758 struct diff_queue_struct *q = &diff_queued_diff;
2759 int i;
2760 SHA_CTX ctx;
2761 struct patch_id_t data;
2762 char buffer[PATH_MAX * 4 + 20];
2764 SHA1_Init(&ctx);
2765 memset(&data, 0, sizeof(struct patch_id_t));
2766 data.ctx = &ctx;
2767 data.xm.consume = patch_id_consume;
2769 for (i = 0; i < q->nr; i++) {
2770 xpparam_t xpp;
2771 xdemitconf_t xecfg;
2772 xdemitcb_t ecb;
2773 mmfile_t mf1, mf2;
2774 struct diff_filepair *p = q->queue[i];
2775 int len1, len2;
2777 if (p->status == 0)
2778 return error("internal diff status error");
2779 if (p->status == DIFF_STATUS_UNKNOWN)
2780 continue;
2781 if (diff_unmodified_pair(p))
2782 continue;
2783 if ((DIFF_FILE_VALID(p->one) && S_ISDIR(p->one->mode)) ||
2784 (DIFF_FILE_VALID(p->two) && S_ISDIR(p->two->mode)))
2785 continue;
2786 if (DIFF_PAIR_UNMERGED(p))
2787 continue;
2789 diff_fill_sha1_info(p->one);
2790 diff_fill_sha1_info(p->two);
2791 if (fill_mmfile(&mf1, p->one) < 0 ||
2792 fill_mmfile(&mf2, p->two) < 0)
2793 return error("unable to read files to diff");
2795 /* Maybe hash p->two? into the patch id? */
2796 if (file_is_binary(p->two))
2797 continue;
2799 len1 = remove_space(p->one->path, strlen(p->one->path));
2800 len2 = remove_space(p->two->path, strlen(p->two->path));
2801 if (p->one->mode == 0)
2802 len1 = snprintf(buffer, sizeof(buffer),
2803 "diff--gita/%.*sb/%.*s"
2804 "newfilemode%06o"
2805 "---/dev/null"
2806 "+++b/%.*s",
2807 len1, p->one->path,
2808 len2, p->two->path,
2809 p->two->mode,
2810 len2, p->two->path);
2811 else if (p->two->mode == 0)
2812 len1 = snprintf(buffer, sizeof(buffer),
2813 "diff--gita/%.*sb/%.*s"
2814 "deletedfilemode%06o"
2815 "---a/%.*s"
2816 "+++/dev/null",
2817 len1, p->one->path,
2818 len2, p->two->path,
2819 p->one->mode,
2820 len1, p->one->path);
2821 else
2822 len1 = snprintf(buffer, sizeof(buffer),
2823 "diff--gita/%.*sb/%.*s"
2824 "---a/%.*s"
2825 "+++b/%.*s",
2826 len1, p->one->path,
2827 len2, p->two->path,
2828 len1, p->one->path,
2829 len2, p->two->path);
2830 SHA1_Update(&ctx, buffer, len1);
2832 xpp.flags = XDF_NEED_MINIMAL;
2833 xecfg.ctxlen = 3;
2834 xecfg.flags = XDL_EMIT_FUNCNAMES;
2835 ecb.outf = xdiff_outf;
2836 ecb.priv = &data;
2837 xdl_diff(&mf1, &mf2, &xpp, &xecfg, &ecb);
2840 SHA1_Final(sha1, &ctx);
2841 return 0;
2844 int diff_flush_patch_id(struct diff_options *options, unsigned char *sha1)
2846 struct diff_queue_struct *q = &diff_queued_diff;
2847 int i;
2848 int result = diff_get_patch_id(options, sha1);
2850 for (i = 0; i < q->nr; i++)
2851 diff_free_filepair(q->queue[i]);
2853 free(q->queue);
2854 q->queue = NULL;
2855 q->nr = q->alloc = 0;
2857 return result;
2860 static int is_summary_empty(const struct diff_queue_struct *q)
2862 int i;
2864 for (i = 0; i < q->nr; i++) {
2865 const struct diff_filepair *p = q->queue[i];
2867 switch (p->status) {
2868 case DIFF_STATUS_DELETED:
2869 case DIFF_STATUS_ADDED:
2870 case DIFF_STATUS_COPIED:
2871 case DIFF_STATUS_RENAMED:
2872 return 0;
2873 default:
2874 if (p->score)
2875 return 0;
2876 if (p->one->mode && p->two->mode &&
2877 p->one->mode != p->two->mode)
2878 return 0;
2879 break;
2882 return 1;
2885 void diff_flush(struct diff_options *options)
2887 struct diff_queue_struct *q = &diff_queued_diff;
2888 int i, output_format = options->output_format;
2889 int separator = 0;
2892 * Order: raw, stat, summary, patch
2893 * or: name/name-status/checkdiff (other bits clear)
2895 if (!q->nr)
2896 goto free_queue;
2898 if (output_format & (DIFF_FORMAT_RAW |
2899 DIFF_FORMAT_NAME |
2900 DIFF_FORMAT_NAME_STATUS |
2901 DIFF_FORMAT_CHECKDIFF)) {
2902 for (i = 0; i < q->nr; i++) {
2903 struct diff_filepair *p = q->queue[i];
2904 if (check_pair_status(p))
2905 flush_one_pair(p, options);
2907 separator++;
2910 if (output_format & (DIFF_FORMAT_DIFFSTAT|DIFF_FORMAT_SHORTSTAT|DIFF_FORMAT_NUMSTAT)) {
2911 struct diffstat_t diffstat;
2913 memset(&diffstat, 0, sizeof(struct diffstat_t));
2914 diffstat.xm.consume = diffstat_consume;
2915 for (i = 0; i < q->nr; i++) {
2916 struct diff_filepair *p = q->queue[i];
2917 if (check_pair_status(p))
2918 diff_flush_stat(p, options, &diffstat);
2920 if (output_format & DIFF_FORMAT_NUMSTAT)
2921 show_numstat(&diffstat, options);
2922 if (output_format & DIFF_FORMAT_DIFFSTAT)
2923 show_stats(&diffstat, options);
2924 else if (output_format & DIFF_FORMAT_SHORTSTAT)
2925 show_shortstats(&diffstat);
2926 separator++;
2929 if (output_format & DIFF_FORMAT_SUMMARY && !is_summary_empty(q)) {
2930 for (i = 0; i < q->nr; i++)
2931 diff_summary(q->queue[i]);
2932 separator++;
2935 if (output_format & DIFF_FORMAT_PATCH) {
2936 if (separator) {
2937 if (options->stat_sep) {
2938 /* attach patch instead of inline */
2939 fputs(options->stat_sep, stdout);
2940 } else {
2941 putchar(options->line_termination);
2945 for (i = 0; i < q->nr; i++) {
2946 struct diff_filepair *p = q->queue[i];
2947 if (check_pair_status(p))
2948 diff_flush_patch(p, options);
2952 if (output_format & DIFF_FORMAT_CALLBACK)
2953 options->format_callback(q, options, options->format_callback_data);
2955 for (i = 0; i < q->nr; i++)
2956 diff_free_filepair(q->queue[i]);
2957 free_queue:
2958 free(q->queue);
2959 q->queue = NULL;
2960 q->nr = q->alloc = 0;
2963 static void diffcore_apply_filter(const char *filter)
2965 int i;
2966 struct diff_queue_struct *q = &diff_queued_diff;
2967 struct diff_queue_struct outq;
2968 outq.queue = NULL;
2969 outq.nr = outq.alloc = 0;
2971 if (!filter)
2972 return;
2974 if (strchr(filter, DIFF_STATUS_FILTER_AON)) {
2975 int found;
2976 for (i = found = 0; !found && i < q->nr; i++) {
2977 struct diff_filepair *p = q->queue[i];
2978 if (((p->status == DIFF_STATUS_MODIFIED) &&
2979 ((p->score &&
2980 strchr(filter, DIFF_STATUS_FILTER_BROKEN)) ||
2981 (!p->score &&
2982 strchr(filter, DIFF_STATUS_MODIFIED)))) ||
2983 ((p->status != DIFF_STATUS_MODIFIED) &&
2984 strchr(filter, p->status)))
2985 found++;
2987 if (found)
2988 return;
2990 /* otherwise we will clear the whole queue
2991 * by copying the empty outq at the end of this
2992 * function, but first clear the current entries
2993 * in the queue.
2995 for (i = 0; i < q->nr; i++)
2996 diff_free_filepair(q->queue[i]);
2998 else {
2999 /* Only the matching ones */
3000 for (i = 0; i < q->nr; i++) {
3001 struct diff_filepair *p = q->queue[i];
3003 if (((p->status == DIFF_STATUS_MODIFIED) &&
3004 ((p->score &&
3005 strchr(filter, DIFF_STATUS_FILTER_BROKEN)) ||
3006 (!p->score &&
3007 strchr(filter, DIFF_STATUS_MODIFIED)))) ||
3008 ((p->status != DIFF_STATUS_MODIFIED) &&
3009 strchr(filter, p->status)))
3010 diff_q(&outq, p);
3011 else
3012 diff_free_filepair(p);
3015 free(q->queue);
3016 *q = outq;
3019 void diffcore_std(struct diff_options *options)
3021 if (options->quiet)
3022 return;
3023 if (options->break_opt != -1)
3024 diffcore_break(options->break_opt);
3025 if (options->detect_rename)
3026 diffcore_rename(options);
3027 if (options->break_opt != -1)
3028 diffcore_merge_broken();
3029 if (options->pickaxe)
3030 diffcore_pickaxe(options->pickaxe, options->pickaxe_opts);
3031 if (options->orderfile)
3032 diffcore_order(options->orderfile);
3033 diff_resolve_rename_copy();
3034 diffcore_apply_filter(options->filter);
3036 options->has_changes = !!diff_queued_diff.nr;
3040 void diff_addremove(struct diff_options *options,
3041 int addremove, unsigned mode,
3042 const unsigned char *sha1,
3043 const char *base, const char *path)
3045 char concatpath[PATH_MAX];
3046 struct diff_filespec *one, *two;
3048 /* This may look odd, but it is a preparation for
3049 * feeding "there are unchanged files which should
3050 * not produce diffs, but when you are doing copy
3051 * detection you would need them, so here they are"
3052 * entries to the diff-core. They will be prefixed
3053 * with something like '=' or '*' (I haven't decided
3054 * which but should not make any difference).
3055 * Feeding the same new and old to diff_change()
3056 * also has the same effect.
3057 * Before the final output happens, they are pruned after
3058 * merged into rename/copy pairs as appropriate.
3060 if (options->reverse_diff)
3061 addremove = (addremove == '+' ? '-' :
3062 addremove == '-' ? '+' : addremove);
3064 if (!path) path = "";
3065 sprintf(concatpath, "%s%s", base, path);
3066 one = alloc_filespec(concatpath);
3067 two = alloc_filespec(concatpath);
3069 if (addremove != '+')
3070 fill_filespec(one, sha1, mode);
3071 if (addremove != '-')
3072 fill_filespec(two, sha1, mode);
3074 diff_queue(&diff_queued_diff, one, two);
3075 options->has_changes = 1;
3078 void diff_change(struct diff_options *options,
3079 unsigned old_mode, unsigned new_mode,
3080 const unsigned char *old_sha1,
3081 const unsigned char *new_sha1,
3082 const char *base, const char *path)
3084 char concatpath[PATH_MAX];
3085 struct diff_filespec *one, *two;
3087 if (options->reverse_diff) {
3088 unsigned tmp;
3089 const unsigned char *tmp_c;
3090 tmp = old_mode; old_mode = new_mode; new_mode = tmp;
3091 tmp_c = old_sha1; old_sha1 = new_sha1; new_sha1 = tmp_c;
3093 if (!path) path = "";
3094 sprintf(concatpath, "%s%s", base, path);
3095 one = alloc_filespec(concatpath);
3096 two = alloc_filespec(concatpath);
3097 fill_filespec(one, old_sha1, old_mode);
3098 fill_filespec(two, new_sha1, new_mode);
3100 diff_queue(&diff_queued_diff, one, two);
3101 options->has_changes = 1;
3104 void diff_unmerge(struct diff_options *options,
3105 const char *path,
3106 unsigned mode, const unsigned char *sha1)
3108 struct diff_filespec *one, *two;
3109 one = alloc_filespec(path);
3110 two = alloc_filespec(path);
3111 fill_filespec(one, sha1, mode);
3112 diff_queue(&diff_queued_diff, one, two)->is_unmerged = 1;