gitk - Make selection highlight color configurable
[git/mingw.git] / diff.c
blob07b326638c7b087ab8ad36f26a7f50dc9ade81c0
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"
12 #include "spawn-pipe.h"
14 #ifdef NO_FAST_WORKING_DIRECTORY
15 #define FAST_WORKING_DIRECTORY 0
16 #else
17 #define FAST_WORKING_DIRECTORY 1
18 #endif
20 static int diff_detect_rename_default;
21 static int diff_rename_limit_default = -1;
22 static int diff_use_color_default;
24 static char diff_colors[][COLOR_MAXLEN] = {
25 "\033[m", /* reset */
26 "", /* PLAIN (normal) */
27 "\033[1m", /* METAINFO (bold) */
28 "\033[36m", /* FRAGINFO (cyan) */
29 "\033[31m", /* OLD (red) */
30 "\033[32m", /* NEW (green) */
31 "\033[33m", /* COMMIT (yellow) */
32 "\033[41m", /* WHITESPACE (red background) */
35 static int parse_diff_color_slot(const char *var, int ofs)
37 if (!strcasecmp(var+ofs, "plain"))
38 return DIFF_PLAIN;
39 if (!strcasecmp(var+ofs, "meta"))
40 return DIFF_METAINFO;
41 if (!strcasecmp(var+ofs, "frag"))
42 return DIFF_FRAGINFO;
43 if (!strcasecmp(var+ofs, "old"))
44 return DIFF_FILE_OLD;
45 if (!strcasecmp(var+ofs, "new"))
46 return DIFF_FILE_NEW;
47 if (!strcasecmp(var+ofs, "commit"))
48 return DIFF_COMMIT;
49 if (!strcasecmp(var+ofs, "whitespace"))
50 return DIFF_WHITESPACE;
51 die("bad config variable '%s'", var);
54 static struct ll_diff_driver {
55 const char *name;
56 struct ll_diff_driver *next;
57 char *cmd;
58 } *user_diff, **user_diff_tail;
61 * Currently there is only "diff.<drivername>.command" variable;
62 * because there are "diff.color.<slot>" variables, we are parsing
63 * this in a bit convoluted way to allow low level diff driver
64 * called "color".
66 static int parse_lldiff_command(const char *var, const char *ep, const char *value)
68 const char *name;
69 int namelen;
70 struct ll_diff_driver *drv;
72 name = var + 5;
73 namelen = ep - name;
74 for (drv = user_diff; drv; drv = drv->next)
75 if (!strncmp(drv->name, name, namelen) && !drv->name[namelen])
76 break;
77 if (!drv) {
78 char *namebuf;
79 drv = xcalloc(1, sizeof(struct ll_diff_driver));
80 namebuf = xmalloc(namelen + 1);
81 memcpy(namebuf, name, namelen);
82 namebuf[namelen] = 0;
83 drv->name = namebuf;
84 drv->next = NULL;
85 if (!user_diff_tail)
86 user_diff_tail = &user_diff;
87 *user_diff_tail = drv;
88 user_diff_tail = &(drv->next);
91 if (!value)
92 return error("%s: lacks value", var);
93 drv->cmd = strdup(value);
94 return 0;
98 * These are to give UI layer defaults.
99 * The core-level commands such as git-diff-files should
100 * never be affected by the setting of diff.renames
101 * the user happens to have in the configuration file.
103 int git_diff_ui_config(const char *var, const char *value)
105 if (!strcmp(var, "diff.renamelimit")) {
106 diff_rename_limit_default = git_config_int(var, value);
107 return 0;
109 if (!strcmp(var, "diff.color") || !strcmp(var, "color.diff")) {
110 diff_use_color_default = git_config_colorbool(var, value);
111 return 0;
113 if (!strcmp(var, "diff.renames")) {
114 if (!value)
115 diff_detect_rename_default = DIFF_DETECT_RENAME;
116 else if (!strcasecmp(value, "copies") ||
117 !strcasecmp(value, "copy"))
118 diff_detect_rename_default = DIFF_DETECT_COPY;
119 else if (git_config_bool(var,value))
120 diff_detect_rename_default = DIFF_DETECT_RENAME;
121 return 0;
123 if (!prefixcmp(var, "diff.")) {
124 const char *ep = strrchr(var, '.');
126 if (ep != var + 4 && !strcmp(ep, ".command"))
127 return parse_lldiff_command(var, ep, value);
129 if (!prefixcmp(var, "diff.color.") || !prefixcmp(var, "color.diff.")) {
130 int slot = parse_diff_color_slot(var, 11);
131 color_parse(value, var, diff_colors[slot]);
132 return 0;
135 return git_default_config(var, value);
138 static char *quote_one(const char *str)
140 int needlen;
141 char *xp;
143 if (!str)
144 return NULL;
145 needlen = quote_c_style(str, NULL, NULL, 0);
146 if (!needlen)
147 return xstrdup(str);
148 xp = xmalloc(needlen + 1);
149 quote_c_style(str, xp, NULL, 0);
150 return xp;
153 static char *quote_two(const char *one, const char *two)
155 int need_one = quote_c_style(one, NULL, NULL, 1);
156 int need_two = quote_c_style(two, NULL, NULL, 1);
157 char *xp;
159 if (need_one + need_two) {
160 if (!need_one) need_one = strlen(one);
161 if (!need_two) need_one = strlen(two);
163 xp = xmalloc(need_one + need_two + 3);
164 xp[0] = '"';
165 quote_c_style(one, xp + 1, NULL, 1);
166 quote_c_style(two, xp + need_one + 1, NULL, 1);
167 strcpy(xp + need_one + need_two + 1, "\"");
168 return xp;
170 need_one = strlen(one);
171 need_two = strlen(two);
172 xp = xmalloc(need_one + need_two + 1);
173 strcpy(xp, one);
174 strcpy(xp + need_one, two);
175 return xp;
178 static const char *external_diff(void)
180 static const char *external_diff_cmd = NULL;
181 static int done_preparing = 0;
183 if (done_preparing)
184 return external_diff_cmd;
185 external_diff_cmd = getenv("GIT_EXTERNAL_DIFF");
186 done_preparing = 1;
187 return external_diff_cmd;
190 static struct diff_tempfile {
191 const char *name; /* filename external diff should read from */
192 char hex[41];
193 char mode[10];
194 char tmp_path[PATH_MAX];
195 } diff_temp[2];
197 static int count_lines(const char *data, int size)
199 int count, ch, completely_empty = 1, nl_just_seen = 0;
200 count = 0;
201 while (0 < size--) {
202 ch = *data++;
203 if (ch == '\n') {
204 count++;
205 nl_just_seen = 1;
206 completely_empty = 0;
208 else {
209 nl_just_seen = 0;
210 completely_empty = 0;
213 if (completely_empty)
214 return 0;
215 if (!nl_just_seen)
216 count++; /* no trailing newline */
217 return count;
220 static void print_line_count(int count)
222 switch (count) {
223 case 0:
224 printf("0,0");
225 break;
226 case 1:
227 printf("1");
228 break;
229 default:
230 printf("1,%d", count);
231 break;
235 static void copy_file(int prefix, const char *data, int size,
236 const char *set, const char *reset)
238 int ch, nl_just_seen = 1;
239 while (0 < size--) {
240 ch = *data++;
241 if (nl_just_seen) {
242 fputs(set, stdout);
243 putchar(prefix);
245 if (ch == '\n') {
246 nl_just_seen = 1;
247 fputs(reset, stdout);
248 } else
249 nl_just_seen = 0;
250 putchar(ch);
252 if (!nl_just_seen)
253 printf("%s\n\\ No newline at end of file\n", reset);
256 static void emit_rewrite_diff(const char *name_a,
257 const char *name_b,
258 struct diff_filespec *one,
259 struct diff_filespec *two,
260 int color_diff)
262 int lc_a, lc_b;
263 const char *name_a_tab, *name_b_tab;
264 const char *metainfo = diff_get_color(color_diff, DIFF_METAINFO);
265 const char *fraginfo = diff_get_color(color_diff, DIFF_FRAGINFO);
266 const char *old = diff_get_color(color_diff, DIFF_FILE_OLD);
267 const char *new = diff_get_color(color_diff, DIFF_FILE_NEW);
268 const char *reset = diff_get_color(color_diff, DIFF_RESET);
270 name_a += (*name_a == '/');
271 name_b += (*name_b == '/');
272 name_a_tab = strchr(name_a, ' ') ? "\t" : "";
273 name_b_tab = strchr(name_b, ' ') ? "\t" : "";
275 diff_populate_filespec(one, 0);
276 diff_populate_filespec(two, 0);
277 lc_a = count_lines(one->data, one->size);
278 lc_b = count_lines(two->data, two->size);
279 printf("%s--- a/%s%s%s\n%s+++ b/%s%s%s\n%s@@ -",
280 metainfo, name_a, name_a_tab, reset,
281 metainfo, name_b, name_b_tab, reset, fraginfo);
282 print_line_count(lc_a);
283 printf(" +");
284 print_line_count(lc_b);
285 printf(" @@%s\n", reset);
286 if (lc_a)
287 copy_file('-', one->data, one->size, old, reset);
288 if (lc_b)
289 copy_file('+', two->data, two->size, new, reset);
292 static int fill_mmfile(mmfile_t *mf, struct diff_filespec *one)
294 if (!DIFF_FILE_VALID(one)) {
295 mf->ptr = (char *)""; /* does not matter */
296 mf->size = 0;
297 return 0;
299 else if (diff_populate_filespec(one, 0))
300 return -1;
301 mf->ptr = one->data;
302 mf->size = one->size;
303 return 0;
306 struct diff_words_buffer {
307 mmfile_t text;
308 long alloc;
309 long current; /* output pointer */
310 int suppressed_newline;
313 static void diff_words_append(char *line, unsigned long len,
314 struct diff_words_buffer *buffer)
316 if (buffer->text.size + len > buffer->alloc) {
317 buffer->alloc = (buffer->text.size + len) * 3 / 2;
318 buffer->text.ptr = xrealloc(buffer->text.ptr, buffer->alloc);
320 line++;
321 len--;
322 memcpy(buffer->text.ptr + buffer->text.size, line, len);
323 buffer->text.size += len;
326 struct diff_words_data {
327 struct xdiff_emit_state xm;
328 struct diff_words_buffer minus, plus;
331 static void print_word(struct diff_words_buffer *buffer, int len, int color,
332 int suppress_newline)
334 const char *ptr;
335 int eol = 0;
337 if (len == 0)
338 return;
340 ptr = buffer->text.ptr + buffer->current;
341 buffer->current += len;
343 if (ptr[len - 1] == '\n') {
344 eol = 1;
345 len--;
348 fputs(diff_get_color(1, color), stdout);
349 fwrite(ptr, len, 1, stdout);
350 fputs(diff_get_color(1, DIFF_RESET), stdout);
352 if (eol) {
353 if (suppress_newline)
354 buffer->suppressed_newline = 1;
355 else
356 putchar('\n');
360 static void fn_out_diff_words_aux(void *priv, char *line, unsigned long len)
362 struct diff_words_data *diff_words = priv;
364 if (diff_words->minus.suppressed_newline) {
365 if (line[0] != '+')
366 putchar('\n');
367 diff_words->minus.suppressed_newline = 0;
370 len--;
371 switch (line[0]) {
372 case '-':
373 print_word(&diff_words->minus, len, DIFF_FILE_OLD, 1);
374 break;
375 case '+':
376 print_word(&diff_words->plus, len, DIFF_FILE_NEW, 0);
377 break;
378 case ' ':
379 print_word(&diff_words->plus, len, DIFF_PLAIN, 0);
380 diff_words->minus.current += len;
381 break;
385 /* this executes the word diff on the accumulated buffers */
386 static void diff_words_show(struct diff_words_data *diff_words)
388 xpparam_t xpp;
389 xdemitconf_t xecfg;
390 xdemitcb_t ecb;
391 mmfile_t minus, plus;
392 int i;
394 minus.size = diff_words->minus.text.size;
395 minus.ptr = xmalloc(minus.size);
396 memcpy(minus.ptr, diff_words->minus.text.ptr, minus.size);
397 for (i = 0; i < minus.size; i++)
398 if (isspace(minus.ptr[i]))
399 minus.ptr[i] = '\n';
400 diff_words->minus.current = 0;
402 plus.size = diff_words->plus.text.size;
403 plus.ptr = xmalloc(plus.size);
404 memcpy(plus.ptr, diff_words->plus.text.ptr, plus.size);
405 for (i = 0; i < plus.size; i++)
406 if (isspace(plus.ptr[i]))
407 plus.ptr[i] = '\n';
408 diff_words->plus.current = 0;
410 xpp.flags = XDF_NEED_MINIMAL;
411 xecfg.ctxlen = diff_words->minus.alloc + diff_words->plus.alloc;
412 xecfg.flags = 0;
413 ecb.outf = xdiff_outf;
414 ecb.priv = diff_words;
415 diff_words->xm.consume = fn_out_diff_words_aux;
416 xdl_diff(&minus, &plus, &xpp, &xecfg, &ecb);
418 free(minus.ptr);
419 free(plus.ptr);
420 diff_words->minus.text.size = diff_words->plus.text.size = 0;
422 if (diff_words->minus.suppressed_newline) {
423 putchar('\n');
424 diff_words->minus.suppressed_newline = 0;
428 struct emit_callback {
429 struct xdiff_emit_state xm;
430 int nparents, color_diff;
431 const char **label_path;
432 struct diff_words_data *diff_words;
433 int *found_changesp;
436 static void free_diff_words_data(struct emit_callback *ecbdata)
438 if (ecbdata->diff_words) {
439 /* flush buffers */
440 if (ecbdata->diff_words->minus.text.size ||
441 ecbdata->diff_words->plus.text.size)
442 diff_words_show(ecbdata->diff_words);
444 if (ecbdata->diff_words->minus.text.ptr)
445 free (ecbdata->diff_words->minus.text.ptr);
446 if (ecbdata->diff_words->plus.text.ptr)
447 free (ecbdata->diff_words->plus.text.ptr);
448 free(ecbdata->diff_words);
449 ecbdata->diff_words = NULL;
453 const char *diff_get_color(int diff_use_color, enum color_diff ix)
455 if (diff_use_color)
456 return diff_colors[ix];
457 return "";
460 static void emit_line(const char *set, const char *reset, const char *line, int len)
462 if (len > 0 && line[len-1] == '\n')
463 len--;
464 fputs(set, stdout);
465 fwrite(line, len, 1, stdout);
466 puts(reset);
469 static void emit_line_with_ws(int nparents,
470 const char *set, const char *reset, const char *ws,
471 const char *line, int len)
473 int col0 = nparents;
474 int last_tab_in_indent = -1;
475 int last_space_in_indent = -1;
476 int i;
477 int tail = len;
478 int need_highlight_leading_space = 0;
479 /* The line is a newly added line. Does it have funny leading
480 * whitespaces? In indent, SP should never precede a TAB.
482 for (i = col0; i < len; i++) {
483 if (line[i] == '\t') {
484 last_tab_in_indent = i;
485 if (0 <= last_space_in_indent)
486 need_highlight_leading_space = 1;
488 else if (line[i] == ' ')
489 last_space_in_indent = i;
490 else
491 break;
493 fputs(set, stdout);
494 fwrite(line, col0, 1, stdout);
495 fputs(reset, stdout);
496 if (((i == len) || line[i] == '\n') && i != col0) {
497 /* The whole line was indent */
498 emit_line(ws, reset, line + col0, len - col0);
499 return;
501 i = col0;
502 if (need_highlight_leading_space) {
503 while (i < last_tab_in_indent) {
504 if (line[i] == ' ') {
505 fputs(ws, stdout);
506 putchar(' ');
507 fputs(reset, stdout);
509 else
510 putchar(line[i]);
511 i++;
514 tail = len - 1;
515 if (line[tail] == '\n' && i < tail)
516 tail--;
517 while (i < tail) {
518 if (!isspace(line[tail]))
519 break;
520 tail--;
522 if ((i < tail && line[tail + 1] != '\n')) {
523 /* This has whitespace between tail+1..len */
524 fputs(set, stdout);
525 fwrite(line + i, tail - i + 1, 1, stdout);
526 fputs(reset, stdout);
527 emit_line(ws, reset, line + tail + 1, len - tail - 1);
529 else
530 emit_line(set, reset, line + i, len - i);
533 static void emit_add_line(const char *reset, struct emit_callback *ecbdata, const char *line, int len)
535 const char *ws = diff_get_color(ecbdata->color_diff, DIFF_WHITESPACE);
536 const char *set = diff_get_color(ecbdata->color_diff, DIFF_FILE_NEW);
538 if (!*ws)
539 emit_line(set, reset, line, len);
540 else
541 emit_line_with_ws(ecbdata->nparents, set, reset, ws,
542 line, len);
545 static void fn_out_consume(void *priv, char *line, unsigned long len)
547 int i;
548 int color;
549 struct emit_callback *ecbdata = priv;
550 const char *set = diff_get_color(ecbdata->color_diff, DIFF_METAINFO);
551 const char *reset = diff_get_color(ecbdata->color_diff, DIFF_RESET);
553 *(ecbdata->found_changesp) = 1;
555 if (ecbdata->label_path[0]) {
556 const char *name_a_tab, *name_b_tab;
558 name_a_tab = strchr(ecbdata->label_path[0], ' ') ? "\t" : "";
559 name_b_tab = strchr(ecbdata->label_path[1], ' ') ? "\t" : "";
561 printf("%s--- %s%s%s\n",
562 set, ecbdata->label_path[0], reset, name_a_tab);
563 printf("%s+++ %s%s%s\n",
564 set, ecbdata->label_path[1], reset, name_b_tab);
565 ecbdata->label_path[0] = ecbdata->label_path[1] = NULL;
568 /* This is not really necessary for now because
569 * this codepath only deals with two-way diffs.
571 for (i = 0; i < len && line[i] == '@'; i++)
573 if (2 <= i && i < len && line[i] == ' ') {
574 ecbdata->nparents = i - 1;
575 emit_line(diff_get_color(ecbdata->color_diff, DIFF_FRAGINFO),
576 reset, line, len);
577 return;
580 if (len < ecbdata->nparents) {
581 set = reset;
582 emit_line(reset, reset, line, len);
583 return;
586 color = DIFF_PLAIN;
587 if (ecbdata->diff_words && ecbdata->nparents != 1)
588 /* fall back to normal diff */
589 free_diff_words_data(ecbdata);
590 if (ecbdata->diff_words) {
591 if (line[0] == '-') {
592 diff_words_append(line, len,
593 &ecbdata->diff_words->minus);
594 return;
595 } else if (line[0] == '+') {
596 diff_words_append(line, len,
597 &ecbdata->diff_words->plus);
598 return;
600 if (ecbdata->diff_words->minus.text.size ||
601 ecbdata->diff_words->plus.text.size)
602 diff_words_show(ecbdata->diff_words);
603 line++;
604 len--;
605 emit_line(set, reset, line, len);
606 return;
608 for (i = 0; i < ecbdata->nparents && len; i++) {
609 if (line[i] == '-')
610 color = DIFF_FILE_OLD;
611 else if (line[i] == '+')
612 color = DIFF_FILE_NEW;
615 if (color != DIFF_FILE_NEW) {
616 emit_line(diff_get_color(ecbdata->color_diff, color),
617 reset, line, len);
618 return;
620 emit_add_line(reset, ecbdata, line, len);
623 static char *pprint_rename(const char *a, const char *b)
625 const char *old = a;
626 const char *new = b;
627 char *name = NULL;
628 int pfx_length, sfx_length;
629 int len_a = strlen(a);
630 int len_b = strlen(b);
631 int qlen_a = quote_c_style(a, NULL, NULL, 0);
632 int qlen_b = quote_c_style(b, NULL, NULL, 0);
634 if (qlen_a || qlen_b) {
635 if (qlen_a) len_a = qlen_a;
636 if (qlen_b) len_b = qlen_b;
637 name = xmalloc( len_a + len_b + 5 );
638 if (qlen_a)
639 quote_c_style(a, name, NULL, 0);
640 else
641 memcpy(name, a, len_a);
642 memcpy(name + len_a, " => ", 4);
643 if (qlen_b)
644 quote_c_style(b, name + len_a + 4, NULL, 0);
645 else
646 memcpy(name + len_a + 4, b, len_b + 1);
647 return name;
650 /* Find common prefix */
651 pfx_length = 0;
652 while (*old && *new && *old == *new) {
653 if (*old == '/')
654 pfx_length = old - a + 1;
655 old++;
656 new++;
659 /* Find common suffix */
660 old = a + len_a;
661 new = b + len_b;
662 sfx_length = 0;
663 while (a <= old && b <= new && *old == *new) {
664 if (*old == '/')
665 sfx_length = len_a - (old - a);
666 old--;
667 new--;
671 * pfx{mid-a => mid-b}sfx
672 * {pfx-a => pfx-b}sfx
673 * pfx{sfx-a => sfx-b}
674 * name-a => name-b
676 if (pfx_length + sfx_length) {
677 int a_midlen = len_a - pfx_length - sfx_length;
678 int b_midlen = len_b - pfx_length - sfx_length;
679 if (a_midlen < 0) a_midlen = 0;
680 if (b_midlen < 0) b_midlen = 0;
682 name = xmalloc(pfx_length + a_midlen + b_midlen + sfx_length + 7);
683 sprintf(name, "%.*s{%.*s => %.*s}%s",
684 pfx_length, a,
685 a_midlen, a + pfx_length,
686 b_midlen, b + pfx_length,
687 a + len_a - sfx_length);
689 else {
690 name = xmalloc(len_a + len_b + 5);
691 sprintf(name, "%s => %s", a, b);
693 return name;
696 struct diffstat_t {
697 struct xdiff_emit_state xm;
699 int nr;
700 int alloc;
701 struct diffstat_file {
702 char *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 if (name_b) {
723 x->name = pprint_rename(name_a, name_b);
724 x->is_renamed = 1;
726 else
727 x->name = xstrdup(name_a);
728 return x;
731 static void diffstat_consume(void *priv, char *line, unsigned long len)
733 struct diffstat_t *diffstat = priv;
734 struct diffstat_file *x = diffstat->files[diffstat->nr - 1];
736 if (line[0] == '+')
737 x->added++;
738 else if (line[0] == '-')
739 x->deleted++;
742 const char mime_boundary_leader[] = "------------";
744 static int scale_linear(int it, int width, int max_change)
747 * make sure that at least one '-' is printed if there were deletions,
748 * and likewise for '+'.
750 if (max_change < 2)
751 return it;
752 return ((it - 1) * (width - 1) + max_change - 1) / (max_change - 1);
755 static void show_name(const char *prefix, const char *name, int len,
756 const char *reset, const char *set)
758 printf(" %s%s%-*s%s |", set, prefix, len, name, reset);
761 static void show_graph(char ch, int cnt, const char *set, const char *reset)
763 if (cnt <= 0)
764 return;
765 printf("%s", set);
766 while (cnt--)
767 putchar(ch);
768 printf("%s", reset);
771 static void show_stats(struct diffstat_t* data, struct diff_options *options)
773 int i, len, add, del, total, adds = 0, dels = 0;
774 int max_change = 0, max_len = 0;
775 int total_files = data->nr;
776 int width, name_width;
777 const char *reset, *set, *add_c, *del_c;
779 if (data->nr == 0)
780 return;
782 width = options->stat_width ? options->stat_width : 80;
783 name_width = options->stat_name_width ? options->stat_name_width : 50;
785 /* Sanity: give at least 5 columns to the graph,
786 * but leave at least 10 columns for the name.
788 if (width < name_width + 15) {
789 if (name_width <= 25)
790 width = name_width + 15;
791 else
792 name_width = width - 15;
795 /* Find the longest filename and max number of changes */
796 reset = diff_get_color(options->color_diff, DIFF_RESET);
797 set = diff_get_color(options->color_diff, DIFF_PLAIN);
798 add_c = diff_get_color(options->color_diff, DIFF_FILE_NEW);
799 del_c = diff_get_color(options->color_diff, DIFF_FILE_OLD);
801 for (i = 0; i < data->nr; i++) {
802 struct diffstat_file *file = data->files[i];
803 int change = file->added + file->deleted;
805 if (!file->is_renamed) { /* renames are already quoted by pprint_rename */
806 len = quote_c_style(file->name, NULL, NULL, 0);
807 if (len) {
808 char *qname = xmalloc(len + 1);
809 quote_c_style(file->name, qname, NULL, 0);
810 free(file->name);
811 file->name = qname;
815 len = strlen(file->name);
816 if (max_len < len)
817 max_len = len;
819 if (file->is_binary || file->is_unmerged)
820 continue;
821 if (max_change < change)
822 max_change = change;
825 /* Compute the width of the graph part;
826 * 10 is for one blank at the beginning of the line plus
827 * " | count " between the name and the graph.
829 * From here on, name_width is the width of the name area,
830 * and width is the width of the graph area.
832 name_width = (name_width < max_len) ? name_width : max_len;
833 if (width < (name_width + 10) + max_change)
834 width = width - (name_width + 10);
835 else
836 width = max_change;
838 for (i = 0; i < data->nr; i++) {
839 const char *prefix = "";
840 char *name = data->files[i]->name;
841 int added = data->files[i]->added;
842 int deleted = data->files[i]->deleted;
843 int name_len;
846 * "scale" the filename
848 len = name_width;
849 name_len = strlen(name);
850 if (name_width < name_len) {
851 char *slash;
852 prefix = "...";
853 len -= 3;
854 name += name_len - len;
855 slash = strchr(name, '/');
856 if (slash)
857 name = slash;
860 if (data->files[i]->is_binary) {
861 show_name(prefix, name, len, reset, set);
862 printf(" Bin ");
863 printf("%s%d%s", del_c, deleted, reset);
864 printf(" -> ");
865 printf("%s%d%s", add_c, added, reset);
866 printf(" bytes");
867 printf("\n");
868 goto free_diffstat_file;
870 else if (data->files[i]->is_unmerged) {
871 show_name(prefix, name, len, reset, set);
872 printf(" Unmerged\n");
873 goto free_diffstat_file;
875 else if (!data->files[i]->is_renamed &&
876 (added + deleted == 0)) {
877 total_files--;
878 goto free_diffstat_file;
882 * scale the add/delete
884 add = added;
885 del = deleted;
886 total = add + del;
887 adds += add;
888 dels += del;
890 if (width <= max_change) {
891 add = scale_linear(add, width, max_change);
892 del = scale_linear(del, width, max_change);
893 total = add + del;
895 show_name(prefix, name, len, reset, set);
896 printf("%5d ", added + deleted);
897 show_graph('+', add, add_c, reset);
898 show_graph('-', del, del_c, reset);
899 putchar('\n');
900 free_diffstat_file:
901 free(data->files[i]->name);
902 free(data->files[i]);
904 free(data->files);
905 printf("%s %d files changed, %d insertions(+), %d deletions(-)%s\n",
906 set, total_files, adds, dels, reset);
909 static void show_shortstats(struct diffstat_t* data)
911 int i, adds = 0, dels = 0, total_files = data->nr;
913 if (data->nr == 0)
914 return;
916 for (i = 0; i < data->nr; i++) {
917 if (!data->files[i]->is_binary &&
918 !data->files[i]->is_unmerged) {
919 int added = data->files[i]->added;
920 int deleted= data->files[i]->deleted;
921 if (!data->files[i]->is_renamed &&
922 (added + deleted == 0)) {
923 total_files--;
924 } else {
925 adds += added;
926 dels += deleted;
929 free(data->files[i]->name);
930 free(data->files[i]);
932 free(data->files);
934 printf(" %d files changed, %d insertions(+), %d deletions(-)\n",
935 total_files, adds, dels);
938 static void show_numstat(struct diffstat_t* data, struct diff_options *options)
940 int i;
942 for (i = 0; i < data->nr; i++) {
943 struct diffstat_file *file = data->files[i];
945 if (file->is_binary)
946 printf("-\t-\t");
947 else
948 printf("%d\t%d\t", file->added, file->deleted);
949 if (options->line_termination && !file->is_renamed &&
950 quote_c_style(file->name, NULL, NULL, 0))
951 quote_c_style(file->name, NULL, stdout, 0);
952 else
953 fputs(file->name, stdout);
954 putchar(options->line_termination);
958 struct checkdiff_t {
959 struct xdiff_emit_state xm;
960 const char *filename;
961 int lineno, color_diff;
964 static void checkdiff_consume(void *priv, char *line, unsigned long len)
966 struct checkdiff_t *data = priv;
967 const char *ws = diff_get_color(data->color_diff, DIFF_WHITESPACE);
968 const char *reset = diff_get_color(data->color_diff, DIFF_RESET);
969 const char *set = diff_get_color(data->color_diff, DIFF_FILE_NEW);
971 if (line[0] == '+') {
972 int i, spaces = 0, space_before_tab = 0, white_space_at_end = 0;
974 /* check space before tab */
975 for (i = 1; i < len && (line[i] == ' ' || line[i] == '\t'); i++)
976 if (line[i] == ' ')
977 spaces++;
978 if (line[i - 1] == '\t' && spaces)
979 space_before_tab = 1;
981 /* check white space at line end */
982 if (line[len - 1] == '\n')
983 len--;
984 if (isspace(line[len - 1]))
985 white_space_at_end = 1;
987 if (space_before_tab || white_space_at_end) {
988 printf("%s:%d: %s", data->filename, data->lineno, ws);
989 if (space_before_tab) {
990 printf("space before tab");
991 if (white_space_at_end)
992 putchar(',');
994 if (white_space_at_end)
995 printf("white space at end");
996 printf(":%s ", reset);
997 emit_line_with_ws(1, set, reset, ws, line, len);
1000 data->lineno++;
1001 } else if (line[0] == ' ')
1002 data->lineno++;
1003 else if (line[0] == '@') {
1004 char *plus = strchr(line, '+');
1005 if (plus)
1006 data->lineno = strtol(plus, NULL, 10);
1007 else
1008 die("invalid diff");
1012 static unsigned char *deflate_it(char *data,
1013 unsigned long size,
1014 unsigned long *result_size)
1016 int bound;
1017 unsigned char *deflated;
1018 z_stream stream;
1020 memset(&stream, 0, sizeof(stream));
1021 deflateInit(&stream, zlib_compression_level);
1022 bound = deflateBound(&stream, size);
1023 deflated = xmalloc(bound);
1024 stream.next_out = deflated;
1025 stream.avail_out = bound;
1027 stream.next_in = (unsigned char *)data;
1028 stream.avail_in = size;
1029 while (deflate(&stream, Z_FINISH) == Z_OK)
1030 ; /* nothing */
1031 deflateEnd(&stream);
1032 *result_size = stream.total_out;
1033 return deflated;
1036 static void emit_binary_diff_body(mmfile_t *one, mmfile_t *two)
1038 void *cp;
1039 void *delta;
1040 void *deflated;
1041 void *data;
1042 unsigned long orig_size;
1043 unsigned long delta_size;
1044 unsigned long deflate_size;
1045 unsigned long data_size;
1047 /* We could do deflated delta, or we could do just deflated two,
1048 * whichever is smaller.
1050 delta = NULL;
1051 deflated = deflate_it(two->ptr, two->size, &deflate_size);
1052 if (one->size && two->size) {
1053 delta = diff_delta(one->ptr, one->size,
1054 two->ptr, two->size,
1055 &delta_size, deflate_size);
1056 if (delta) {
1057 void *to_free = delta;
1058 orig_size = delta_size;
1059 delta = deflate_it(delta, delta_size, &delta_size);
1060 free(to_free);
1064 if (delta && delta_size < deflate_size) {
1065 printf("delta %lu\n", orig_size);
1066 free(deflated);
1067 data = delta;
1068 data_size = delta_size;
1070 else {
1071 printf("literal %lu\n", two->size);
1072 free(delta);
1073 data = deflated;
1074 data_size = deflate_size;
1077 /* emit data encoded in base85 */
1078 cp = data;
1079 while (data_size) {
1080 int bytes = (52 < data_size) ? 52 : data_size;
1081 char line[70];
1082 data_size -= bytes;
1083 if (bytes <= 26)
1084 line[0] = bytes + 'A' - 1;
1085 else
1086 line[0] = bytes - 26 + 'a' - 1;
1087 encode_85(line + 1, cp, bytes);
1088 cp = (char *) cp + bytes;
1089 puts(line);
1091 printf("\n");
1092 free(data);
1095 static void emit_binary_diff(mmfile_t *one, mmfile_t *two)
1097 printf("GIT binary patch\n");
1098 emit_binary_diff_body(one, two);
1099 emit_binary_diff_body(two, one);
1102 static void setup_diff_attr_check(struct git_attr_check *check)
1104 static struct git_attr *attr_diff;
1106 if (!attr_diff)
1107 attr_diff = git_attr("diff", 4);
1108 check->attr = attr_diff;
1111 static int file_is_binary(struct diff_filespec *one)
1113 struct git_attr_check attr_diff_check;
1115 setup_diff_attr_check(&attr_diff_check);
1116 if (!git_checkattr(one->path, 1, &attr_diff_check)) {
1117 const char *value = attr_diff_check.value;
1118 if (ATTR_TRUE(value))
1119 return 0;
1120 else if (ATTR_FALSE(value))
1121 return 1;
1124 if (!one->data) {
1125 if (!DIFF_FILE_VALID(one))
1126 return 0;
1127 diff_populate_filespec(one, 0);
1129 return buffer_is_binary(one->data, one->size);
1132 static void builtin_diff(const char *name_a,
1133 const char *name_b,
1134 struct diff_filespec *one,
1135 struct diff_filespec *two,
1136 const char *xfrm_msg,
1137 struct diff_options *o,
1138 int complete_rewrite)
1140 mmfile_t mf1, mf2;
1141 const char *lbl[2];
1142 char *a_one, *b_two;
1143 const char *set = diff_get_color(o->color_diff, DIFF_METAINFO);
1144 const char *reset = diff_get_color(o->color_diff, DIFF_RESET);
1146 a_one = quote_two("a/", name_a + (*name_a == '/'));
1147 b_two = quote_two("b/", name_b + (*name_b == '/'));
1148 lbl[0] = DIFF_FILE_VALID(one) ? a_one : "/dev/null";
1149 lbl[1] = DIFF_FILE_VALID(two) ? b_two : "/dev/null";
1150 printf("%sdiff --git %s %s%s\n", set, a_one, b_two, reset);
1151 if (lbl[0][0] == '/') {
1152 /* /dev/null */
1153 printf("%snew file mode %06o%s\n", set, two->mode, reset);
1154 if (xfrm_msg && xfrm_msg[0])
1155 printf("%s%s%s\n", set, xfrm_msg, reset);
1157 else if (lbl[1][0] == '/') {
1158 printf("%sdeleted file mode %06o%s\n", set, one->mode, reset);
1159 if (xfrm_msg && xfrm_msg[0])
1160 printf("%s%s%s\n", set, xfrm_msg, reset);
1162 else {
1163 if (one->mode != two->mode) {
1164 printf("%sold mode %06o%s\n", set, one->mode, reset);
1165 printf("%snew 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 * we do not run diff between different kind
1171 * of objects.
1173 if ((one->mode ^ two->mode) & S_IFMT)
1174 goto free_ab_and_return;
1175 if (complete_rewrite) {
1176 emit_rewrite_diff(name_a, name_b, one, two,
1177 o->color_diff);
1178 o->found_changes = 1;
1179 goto free_ab_and_return;
1183 if (fill_mmfile(&mf1, one) < 0 || fill_mmfile(&mf2, two) < 0)
1184 die("unable to read files to diff");
1186 if (!o->text && (file_is_binary(one) || file_is_binary(two))) {
1187 /* Quite common confusing case */
1188 if (mf1.size == mf2.size &&
1189 !memcmp(mf1.ptr, mf2.ptr, mf1.size))
1190 goto free_ab_and_return;
1191 if (o->binary)
1192 emit_binary_diff(&mf1, &mf2);
1193 else
1194 printf("Binary files %s and %s differ\n",
1195 lbl[0], lbl[1]);
1196 o->found_changes = 1;
1198 else {
1199 /* Crazy xdl interfaces.. */
1200 const char *diffopts = getenv("GIT_DIFF_OPTS");
1201 xpparam_t xpp;
1202 xdemitconf_t xecfg;
1203 xdemitcb_t ecb;
1204 struct emit_callback ecbdata;
1206 memset(&ecbdata, 0, sizeof(ecbdata));
1207 ecbdata.label_path = lbl;
1208 ecbdata.color_diff = o->color_diff;
1209 ecbdata.found_changesp = &o->found_changes;
1210 xpp.flags = XDF_NEED_MINIMAL | o->xdl_opts;
1211 xecfg.ctxlen = o->context;
1212 xecfg.flags = XDL_EMIT_FUNCNAMES;
1213 if (!diffopts)
1215 else if (!prefixcmp(diffopts, "--unified="))
1216 xecfg.ctxlen = strtoul(diffopts + 10, NULL, 10);
1217 else if (!prefixcmp(diffopts, "-u"))
1218 xecfg.ctxlen = strtoul(diffopts + 2, NULL, 10);
1219 ecb.outf = xdiff_outf;
1220 ecb.priv = &ecbdata;
1221 ecbdata.xm.consume = fn_out_consume;
1222 if (o->color_diff_words)
1223 ecbdata.diff_words =
1224 xcalloc(1, sizeof(struct diff_words_data));
1225 xdl_diff(&mf1, &mf2, &xpp, &xecfg, &ecb);
1226 if (o->color_diff_words)
1227 free_diff_words_data(&ecbdata);
1230 free_ab_and_return:
1231 diff_free_filespec_data(one);
1232 diff_free_filespec_data(two);
1233 free(a_one);
1234 free(b_two);
1235 return;
1238 static void builtin_diffstat(const char *name_a, const char *name_b,
1239 struct diff_filespec *one,
1240 struct diff_filespec *two,
1241 struct diffstat_t *diffstat,
1242 struct diff_options *o,
1243 int complete_rewrite)
1245 mmfile_t mf1, mf2;
1246 struct diffstat_file *data;
1248 data = diffstat_add(diffstat, name_a, name_b);
1250 if (!one || !two) {
1251 data->is_unmerged = 1;
1252 return;
1254 if (complete_rewrite) {
1255 diff_populate_filespec(one, 0);
1256 diff_populate_filespec(two, 0);
1257 data->deleted = count_lines(one->data, one->size);
1258 data->added = count_lines(two->data, two->size);
1259 goto free_and_return;
1261 if (fill_mmfile(&mf1, one) < 0 || fill_mmfile(&mf2, two) < 0)
1262 die("unable to read files to diff");
1264 if (file_is_binary(one) || file_is_binary(two)) {
1265 data->is_binary = 1;
1266 data->added = mf2.size;
1267 data->deleted = mf1.size;
1268 } else {
1269 /* Crazy xdl interfaces.. */
1270 xpparam_t xpp;
1271 xdemitconf_t xecfg;
1272 xdemitcb_t ecb;
1274 xpp.flags = XDF_NEED_MINIMAL | o->xdl_opts;
1275 xecfg.ctxlen = 0;
1276 xecfg.flags = 0;
1277 ecb.outf = xdiff_outf;
1278 ecb.priv = diffstat;
1279 xdl_diff(&mf1, &mf2, &xpp, &xecfg, &ecb);
1282 free_and_return:
1283 diff_free_filespec_data(one);
1284 diff_free_filespec_data(two);
1287 static void builtin_checkdiff(const char *name_a, const char *name_b,
1288 struct diff_filespec *one,
1289 struct diff_filespec *two, struct diff_options *o)
1291 mmfile_t mf1, mf2;
1292 struct checkdiff_t data;
1294 if (!two)
1295 return;
1297 memset(&data, 0, sizeof(data));
1298 data.xm.consume = checkdiff_consume;
1299 data.filename = name_b ? name_b : name_a;
1300 data.lineno = 0;
1301 data.color_diff = o->color_diff;
1303 if (fill_mmfile(&mf1, one) < 0 || fill_mmfile(&mf2, two) < 0)
1304 die("unable to read files to diff");
1306 if (file_is_binary(two))
1307 goto free_and_return;
1308 else {
1309 /* Crazy xdl interfaces.. */
1310 xpparam_t xpp;
1311 xdemitconf_t xecfg;
1312 xdemitcb_t ecb;
1314 xpp.flags = XDF_NEED_MINIMAL;
1315 xecfg.ctxlen = 0;
1316 xecfg.flags = 0;
1317 ecb.outf = xdiff_outf;
1318 ecb.priv = &data;
1319 xdl_diff(&mf1, &mf2, &xpp, &xecfg, &ecb);
1321 free_and_return:
1322 diff_free_filespec_data(one);
1323 diff_free_filespec_data(two);
1326 struct diff_filespec *alloc_filespec(const char *path)
1328 int namelen = strlen(path);
1329 struct diff_filespec *spec = xmalloc(sizeof(*spec) + namelen + 1);
1331 memset(spec, 0, sizeof(*spec));
1332 spec->path = (char *)(spec + 1);
1333 memcpy(spec->path, path, namelen+1);
1334 return spec;
1337 void fill_filespec(struct diff_filespec *spec, const unsigned char *sha1,
1338 unsigned short mode)
1340 if (mode) {
1341 spec->mode = canon_mode(mode);
1342 hashcpy(spec->sha1, sha1);
1343 spec->sha1_valid = !is_null_sha1(sha1);
1348 * Given a name and sha1 pair, if the index tells us the file in
1349 * the work tree has that object contents, return true, so that
1350 * prepare_temp_file() does not have to inflate and extract.
1352 static int reuse_worktree_file(const char *name, const unsigned char *sha1, int want_file)
1354 struct cache_entry *ce;
1355 struct stat st;
1356 int pos, len;
1358 /* We do not read the cache ourselves here, because the
1359 * benchmark with my previous version that always reads cache
1360 * shows that it makes things worse for diff-tree comparing
1361 * two linux-2.6 kernel trees in an already checked out work
1362 * tree. This is because most diff-tree comparisons deal with
1363 * only a small number of files, while reading the cache is
1364 * expensive for a large project, and its cost outweighs the
1365 * savings we get by not inflating the object to a temporary
1366 * file. Practically, this code only helps when we are used
1367 * by diff-cache --cached, which does read the cache before
1368 * calling us.
1370 if (!active_cache)
1371 return 0;
1373 /* We want to avoid the working directory if our caller
1374 * doesn't need the data in a normal file, this system
1375 * is rather slow with its stat/open/mmap/close syscalls,
1376 * and the object is contained in a pack file. The pack
1377 * is probably already open and will be faster to obtain
1378 * the data through than the working directory. Loose
1379 * objects however would tend to be slower as they need
1380 * to be individually opened and inflated.
1382 if (!FAST_WORKING_DIRECTORY && !want_file && has_sha1_pack(sha1, NULL))
1383 return 0;
1385 len = strlen(name);
1386 pos = cache_name_pos(name, len);
1387 if (pos < 0)
1388 return 0;
1389 ce = active_cache[pos];
1390 if ((lstat(name, &st) < 0) ||
1391 !S_ISREG(st.st_mode) || /* careful! */
1392 ce_match_stat(ce, &st, 0) ||
1393 hashcmp(sha1, ce->sha1))
1394 return 0;
1395 /* we return 1 only when we can stat, it is a regular file,
1396 * stat information matches, and sha1 recorded in the cache
1397 * matches. I.e. we know the file in the work tree really is
1398 * the same as the <name, sha1> pair.
1400 return 1;
1403 static int populate_from_stdin(struct diff_filespec *s)
1405 #define INCREMENT 1024
1406 char *buf;
1407 unsigned long size;
1408 ssize_t got;
1410 size = 0;
1411 buf = NULL;
1412 while (1) {
1413 buf = xrealloc(buf, size + INCREMENT);
1414 got = xread(0, buf + size, INCREMENT);
1415 if (!got)
1416 break; /* EOF */
1417 if (got < 0)
1418 return error("error while reading from stdin %s",
1419 strerror(errno));
1420 size += got;
1422 s->should_munmap = 0;
1423 s->data = buf;
1424 s->size = size;
1425 s->should_free = 1;
1426 return 0;
1429 static int diff_populate_gitlink(struct diff_filespec *s, int size_only)
1431 int len;
1432 char *data = xmalloc(100);
1433 len = snprintf(data, 100,
1434 "Subproject commit %s\n", sha1_to_hex(s->sha1));
1435 s->data = data;
1436 s->size = len;
1437 s->should_free = 1;
1438 if (size_only) {
1439 s->data = NULL;
1440 free(data);
1442 return 0;
1446 * While doing rename detection and pickaxe operation, we may need to
1447 * grab the data for the blob (or file) for our own in-core comparison.
1448 * diff_filespec has data and size fields for this purpose.
1450 int diff_populate_filespec(struct diff_filespec *s, int size_only)
1452 int err = 0;
1453 if (!DIFF_FILE_VALID(s))
1454 die("internal error: asking to populate invalid file.");
1455 if (S_ISDIR(s->mode))
1456 return -1;
1458 if (s->data)
1459 return 0;
1461 if (size_only && 0 < s->size)
1462 return 0;
1464 if (S_ISDIRLNK(s->mode))
1465 return diff_populate_gitlink(s, size_only);
1467 if (!s->sha1_valid ||
1468 reuse_worktree_file(s->path, s->sha1, 0)) {
1469 struct stat st;
1470 int fd;
1471 char *buf;
1472 unsigned long size;
1474 if (!strcmp(s->path, "-"))
1475 return populate_from_stdin(s);
1477 if (lstat(s->path, &st) < 0) {
1478 if (errno == ENOENT) {
1479 err_empty:
1480 err = -1;
1481 empty:
1482 s->data = (char *)"";
1483 s->size = 0;
1484 return err;
1487 s->size = xsize_t(st.st_size);
1488 if (!s->size)
1489 goto empty;
1490 if (size_only)
1491 return 0;
1492 if (S_ISLNK(st.st_mode)) {
1493 int ret;
1494 s->data = xmalloc(s->size);
1495 s->should_free = 1;
1496 ret = readlink(s->path, s->data, s->size);
1497 if (ret < 0) {
1498 free(s->data);
1499 goto err_empty;
1501 return 0;
1503 fd = open(s->path, O_RDONLY);
1504 if (fd < 0)
1505 goto err_empty;
1506 s->data = xmmap(NULL, s->size, PROT_READ, MAP_PRIVATE, fd, 0);
1507 close(fd);
1508 s->should_munmap = 1;
1511 * Convert from working tree format to canonical git format
1513 size = s->size;
1514 buf = convert_to_git(s->path, s->data, &size);
1515 if (buf) {
1516 munmap(s->data, s->size);
1517 s->should_munmap = 0;
1518 s->data = buf;
1519 s->size = size;
1520 s->should_free = 1;
1523 else {
1524 enum object_type type;
1525 if (size_only)
1526 type = sha1_object_info(s->sha1, &s->size);
1527 else {
1528 s->data = read_sha1_file(s->sha1, &type, &s->size);
1529 s->should_free = 1;
1532 return 0;
1535 void diff_free_filespec_data(struct diff_filespec *s)
1537 if (s->should_free)
1538 free(s->data);
1539 else if (s->should_munmap)
1540 munmap(s->data, s->size);
1542 if (s->should_free || s->should_munmap) {
1543 s->should_free = s->should_munmap = 0;
1544 s->data = NULL;
1546 free(s->cnt_data);
1547 s->cnt_data = NULL;
1550 static void prep_temp_blob(struct diff_tempfile *temp,
1551 void *blob,
1552 unsigned long size,
1553 const unsigned char *sha1,
1554 int mode)
1556 int fd;
1558 fd = git_mkstemp(temp->tmp_path, PATH_MAX, ".diff_XXXXXX");
1559 if (fd < 0)
1560 die("unable to create temp-file");
1561 if (write_in_full(fd, blob, size) != size)
1562 die("unable to write temp-file");
1563 close(fd);
1564 temp->name = temp->tmp_path;
1565 strcpy(temp->hex, sha1_to_hex(sha1));
1566 temp->hex[40] = 0;
1567 sprintf(temp->mode, "%06o", mode);
1570 static void prepare_temp_file(const char *name,
1571 struct diff_tempfile *temp,
1572 struct diff_filespec *one)
1574 if (!DIFF_FILE_VALID(one)) {
1575 not_a_valid_file:
1576 /* A '-' entry produces this for file-2, and
1577 * a '+' entry produces this for file-1.
1579 temp->name = "/dev/null";
1580 strcpy(temp->hex, ".");
1581 strcpy(temp->mode, ".");
1582 return;
1585 if (!one->sha1_valid ||
1586 reuse_worktree_file(name, one->sha1, 1)) {
1587 struct stat st;
1588 if (lstat(name, &st) < 0) {
1589 if (errno == ENOENT)
1590 goto not_a_valid_file;
1591 die("stat(%s): %s", name, strerror(errno));
1593 if (S_ISLNK(st.st_mode)) {
1594 int ret;
1595 char buf[PATH_MAX + 1]; /* ought to be SYMLINK_MAX */
1596 size_t sz = xsize_t(st.st_size);
1597 if (sizeof(buf) <= st.st_size)
1598 die("symlink too long: %s", name);
1599 ret = readlink(name, buf, sz);
1600 if (ret < 0)
1601 die("readlink(%s)", name);
1602 prep_temp_blob(temp, buf, sz,
1603 (one->sha1_valid ?
1604 one->sha1 : null_sha1),
1605 (one->sha1_valid ?
1606 one->mode : S_IFLNK));
1608 else {
1609 /* we can borrow from the file in the work tree */
1610 temp->name = name;
1611 if (!one->sha1_valid)
1612 strcpy(temp->hex, sha1_to_hex(null_sha1));
1613 else
1614 strcpy(temp->hex, sha1_to_hex(one->sha1));
1615 /* Even though we may sometimes borrow the
1616 * contents from the work tree, we always want
1617 * one->mode. mode is trustworthy even when
1618 * !(one->sha1_valid), as long as
1619 * DIFF_FILE_VALID(one).
1621 sprintf(temp->mode, "%06o", one->mode);
1623 return;
1625 else {
1626 if (diff_populate_filespec(one, 0))
1627 die("cannot read data blob for %s", one->path);
1628 prep_temp_blob(temp, one->data, one->size,
1629 one->sha1, one->mode);
1633 static void remove_tempfile(void)
1635 int i;
1637 for (i = 0; i < 2; i++)
1638 if (diff_temp[i].name == diff_temp[i].tmp_path) {
1639 unlink(diff_temp[i].name);
1640 diff_temp[i].name = NULL;
1644 static void remove_tempfile_on_signal(int signo)
1646 remove_tempfile();
1647 signal(SIGINT, SIG_DFL);
1648 raise(signo);
1651 static int spawn_prog(const char *pgm, const char **arg)
1653 pid_t pid;
1654 int status;
1656 fflush(NULL);
1657 pid = spawnvpe_pipe(pgm, arg, environ, NULL, NULL);
1658 if (pid < 0)
1659 die("unable to fork");
1661 while (waitpid(pid, &status, 0) < 0) {
1662 if (errno == EINTR)
1663 continue;
1664 return -1;
1667 /* Earlier we did not check the exit status because
1668 * diff exits non-zero if files are different, and
1669 * we are not interested in knowing that. It was a
1670 * mistake which made it harder to quit a diff-*
1671 * session that uses the git-apply-patch-script as
1672 * the GIT_EXTERNAL_DIFF. A custom GIT_EXTERNAL_DIFF
1673 * should also exit non-zero only when it wants to
1674 * abort the entire diff-* session.
1676 if (WIFEXITED(status) && !WEXITSTATUS(status))
1677 return 0;
1678 return -1;
1681 /* An external diff command takes:
1683 * diff-cmd name infile1 infile1-sha1 infile1-mode \
1684 * infile2 infile2-sha1 infile2-mode [ rename-to ]
1687 static void run_external_diff(const char *pgm,
1688 const char *name,
1689 const char *other,
1690 struct diff_filespec *one,
1691 struct diff_filespec *two,
1692 const char *xfrm_msg,
1693 int complete_rewrite)
1695 const char *spawn_arg[10];
1696 struct diff_tempfile *temp = diff_temp;
1697 int retval;
1698 static int atexit_asked = 0;
1699 const char *othername;
1700 const char **arg = &spawn_arg[1];
1702 othername = (other? other : name);
1703 if (one && two) {
1704 prepare_temp_file(name, &temp[0], one);
1705 prepare_temp_file(othername, &temp[1], two);
1706 if (! atexit_asked &&
1707 (temp[0].name == temp[0].tmp_path ||
1708 temp[1].name == temp[1].tmp_path)) {
1709 atexit_asked = 1;
1710 atexit(remove_tempfile);
1712 signal(SIGINT, remove_tempfile_on_signal);
1715 if (one && two) {
1716 *arg++ = name;
1717 *arg++ = temp[0].name;
1718 *arg++ = temp[0].hex;
1719 *arg++ = temp[0].mode;
1720 *arg++ = temp[1].name;
1721 *arg++ = temp[1].hex;
1722 *arg++ = temp[1].mode;
1723 if (other) {
1724 *arg++ = other;
1725 *arg++ = xfrm_msg;
1727 } else {
1728 *arg++ = name;
1730 *arg = NULL;
1731 retval = spawn_prog(pgm, spawn_arg);
1732 remove_tempfile();
1733 if (retval) {
1734 fprintf(stderr, "external diff died, stopping at %s.\n", name);
1735 exit(1);
1739 static const char *external_diff_attr(const char *name)
1741 struct git_attr_check attr_diff_check;
1743 setup_diff_attr_check(&attr_diff_check);
1744 if (!git_checkattr(name, 1, &attr_diff_check)) {
1745 const char *value = attr_diff_check.value;
1746 if (!ATTR_TRUE(value) &&
1747 !ATTR_FALSE(value) &&
1748 !ATTR_UNSET(value)) {
1749 struct ll_diff_driver *drv;
1751 if (!user_diff_tail) {
1752 user_diff_tail = &user_diff;
1753 git_config(git_diff_ui_config);
1755 for (drv = user_diff; drv; drv = drv->next)
1756 if (!strcmp(drv->name, value))
1757 return drv->cmd;
1760 return NULL;
1763 static void run_diff_cmd(const char *pgm,
1764 const char *name,
1765 const char *other,
1766 struct diff_filespec *one,
1767 struct diff_filespec *two,
1768 const char *xfrm_msg,
1769 struct diff_options *o,
1770 int complete_rewrite)
1772 if (!o->allow_external)
1773 pgm = NULL;
1774 else {
1775 const char *cmd = external_diff_attr(name);
1776 if (cmd)
1777 pgm = cmd;
1780 if (pgm) {
1781 run_external_diff(pgm, name, other, one, two, xfrm_msg,
1782 complete_rewrite);
1783 return;
1785 if (one && two)
1786 builtin_diff(name, other ? other : name,
1787 one, two, xfrm_msg, o, complete_rewrite);
1788 else
1789 printf("* Unmerged path %s\n", name);
1792 static void diff_fill_sha1_info(struct diff_filespec *one)
1794 if (DIFF_FILE_VALID(one)) {
1795 if (!one->sha1_valid) {
1796 struct stat st;
1797 if (!strcmp(one->path, "-")) {
1798 hashcpy(one->sha1, null_sha1);
1799 return;
1801 if (lstat(one->path, &st) < 0)
1802 die("stat %s", one->path);
1803 if (index_path(one->sha1, one->path, &st, 0))
1804 die("cannot hash %s\n", one->path);
1807 else
1808 hashclr(one->sha1);
1811 static void run_diff(struct diff_filepair *p, struct diff_options *o)
1813 const char *pgm = external_diff();
1814 char msg[PATH_MAX*2+300], *xfrm_msg;
1815 struct diff_filespec *one;
1816 struct diff_filespec *two;
1817 const char *name;
1818 const char *other;
1819 char *name_munged, *other_munged;
1820 int complete_rewrite = 0;
1821 int len;
1823 if (DIFF_PAIR_UNMERGED(p)) {
1824 /* unmerged */
1825 run_diff_cmd(pgm, p->one->path, NULL, NULL, NULL, NULL, o, 0);
1826 return;
1829 name = p->one->path;
1830 other = (strcmp(name, p->two->path) ? p->two->path : NULL);
1831 name_munged = quote_one(name);
1832 other_munged = quote_one(other);
1833 one = p->one; two = p->two;
1835 diff_fill_sha1_info(one);
1836 diff_fill_sha1_info(two);
1838 len = 0;
1839 switch (p->status) {
1840 case DIFF_STATUS_COPIED:
1841 len += snprintf(msg + len, sizeof(msg) - len,
1842 "similarity index %d%%\n"
1843 "copy from %s\n"
1844 "copy to %s\n",
1845 (int)(0.5 + p->score * 100.0/MAX_SCORE),
1846 name_munged, other_munged);
1847 break;
1848 case DIFF_STATUS_RENAMED:
1849 len += snprintf(msg + len, sizeof(msg) - len,
1850 "similarity index %d%%\n"
1851 "rename from %s\n"
1852 "rename to %s\n",
1853 (int)(0.5 + p->score * 100.0/MAX_SCORE),
1854 name_munged, other_munged);
1855 break;
1856 case DIFF_STATUS_MODIFIED:
1857 if (p->score) {
1858 len += snprintf(msg + len, sizeof(msg) - len,
1859 "dissimilarity index %d%%\n",
1860 (int)(0.5 + p->score *
1861 100.0/MAX_SCORE));
1862 complete_rewrite = 1;
1863 break;
1865 /* fallthru */
1866 default:
1867 /* nothing */
1871 if (hashcmp(one->sha1, two->sha1)) {
1872 int abbrev = o->full_index ? 40 : DEFAULT_ABBREV;
1874 if (o->binary) {
1875 mmfile_t mf;
1876 if ((!fill_mmfile(&mf, one) && file_is_binary(one)) ||
1877 (!fill_mmfile(&mf, two) && file_is_binary(two)))
1878 abbrev = 40;
1880 len += snprintf(msg + len, sizeof(msg) - len,
1881 "index %.*s..%.*s",
1882 abbrev, sha1_to_hex(one->sha1),
1883 abbrev, sha1_to_hex(two->sha1));
1884 if (one->mode == two->mode)
1885 len += snprintf(msg + len, sizeof(msg) - len,
1886 " %06o", one->mode);
1887 len += snprintf(msg + len, sizeof(msg) - len, "\n");
1890 if (len)
1891 msg[--len] = 0;
1892 xfrm_msg = len ? msg : NULL;
1894 if (!pgm &&
1895 DIFF_FILE_VALID(one) && DIFF_FILE_VALID(two) &&
1896 (S_IFMT & one->mode) != (S_IFMT & two->mode)) {
1897 /* a filepair that changes between file and symlink
1898 * needs to be split into deletion and creation.
1900 struct diff_filespec *null = alloc_filespec(two->path);
1901 run_diff_cmd(NULL, name, other, one, null, xfrm_msg, o, 0);
1902 free(null);
1903 null = alloc_filespec(one->path);
1904 run_diff_cmd(NULL, name, other, null, two, xfrm_msg, o, 0);
1905 free(null);
1907 else
1908 run_diff_cmd(pgm, name, other, one, two, xfrm_msg, o,
1909 complete_rewrite);
1911 free(name_munged);
1912 free(other_munged);
1915 static void run_diffstat(struct diff_filepair *p, struct diff_options *o,
1916 struct diffstat_t *diffstat)
1918 const char *name;
1919 const char *other;
1920 int complete_rewrite = 0;
1922 if (DIFF_PAIR_UNMERGED(p)) {
1923 /* unmerged */
1924 builtin_diffstat(p->one->path, NULL, NULL, NULL, diffstat, o, 0);
1925 return;
1928 name = p->one->path;
1929 other = (strcmp(name, p->two->path) ? p->two->path : NULL);
1931 diff_fill_sha1_info(p->one);
1932 diff_fill_sha1_info(p->two);
1934 if (p->status == DIFF_STATUS_MODIFIED && p->score)
1935 complete_rewrite = 1;
1936 builtin_diffstat(name, other, p->one, p->two, diffstat, o, complete_rewrite);
1939 static void run_checkdiff(struct diff_filepair *p, struct diff_options *o)
1941 const char *name;
1942 const char *other;
1944 if (DIFF_PAIR_UNMERGED(p)) {
1945 /* unmerged */
1946 return;
1949 name = p->one->path;
1950 other = (strcmp(name, p->two->path) ? p->two->path : NULL);
1952 diff_fill_sha1_info(p->one);
1953 diff_fill_sha1_info(p->two);
1955 builtin_checkdiff(name, other, p->one, p->two, o);
1958 void diff_setup(struct diff_options *options)
1960 memset(options, 0, sizeof(*options));
1961 options->line_termination = '\n';
1962 options->break_opt = -1;
1963 options->rename_limit = -1;
1964 options->context = 3;
1965 options->msg_sep = "";
1967 options->change = diff_change;
1968 options->add_remove = diff_addremove;
1969 options->color_diff = diff_use_color_default;
1970 options->detect_rename = diff_detect_rename_default;
1973 int diff_setup_done(struct diff_options *options)
1975 int count = 0;
1977 if (options->output_format & DIFF_FORMAT_NAME)
1978 count++;
1979 if (options->output_format & DIFF_FORMAT_NAME_STATUS)
1980 count++;
1981 if (options->output_format & DIFF_FORMAT_CHECKDIFF)
1982 count++;
1983 if (options->output_format & DIFF_FORMAT_NO_OUTPUT)
1984 count++;
1985 if (count > 1)
1986 die("--name-only, --name-status, --check and -s are mutually exclusive");
1988 if (options->find_copies_harder)
1989 options->detect_rename = DIFF_DETECT_COPY;
1991 if (options->output_format & (DIFF_FORMAT_NAME |
1992 DIFF_FORMAT_NAME_STATUS |
1993 DIFF_FORMAT_CHECKDIFF |
1994 DIFF_FORMAT_NO_OUTPUT))
1995 options->output_format &= ~(DIFF_FORMAT_RAW |
1996 DIFF_FORMAT_NUMSTAT |
1997 DIFF_FORMAT_DIFFSTAT |
1998 DIFF_FORMAT_SHORTSTAT |
1999 DIFF_FORMAT_SUMMARY |
2000 DIFF_FORMAT_PATCH);
2003 * These cases always need recursive; we do not drop caller-supplied
2004 * recursive bits for other formats here.
2006 if (options->output_format & (DIFF_FORMAT_PATCH |
2007 DIFF_FORMAT_NUMSTAT |
2008 DIFF_FORMAT_DIFFSTAT |
2009 DIFF_FORMAT_SHORTSTAT |
2010 DIFF_FORMAT_SUMMARY |
2011 DIFF_FORMAT_CHECKDIFF))
2012 options->recursive = 1;
2014 * Also pickaxe would not work very well if you do not say recursive
2016 if (options->pickaxe)
2017 options->recursive = 1;
2019 if (options->detect_rename && options->rename_limit < 0)
2020 options->rename_limit = diff_rename_limit_default;
2021 if (options->setup & DIFF_SETUP_USE_CACHE) {
2022 if (!active_cache)
2023 /* read-cache does not die even when it fails
2024 * so it is safe for us to do this here. Also
2025 * it does not smudge active_cache or active_nr
2026 * when it fails, so we do not have to worry about
2027 * cleaning it up ourselves either.
2029 read_cache();
2031 if (options->abbrev <= 0 || 40 < options->abbrev)
2032 options->abbrev = 40; /* full */
2035 * It does not make sense to show the first hit we happened
2036 * to have found. It does not make sense not to return with
2037 * exit code in such a case either.
2039 if (options->quiet) {
2040 options->output_format = DIFF_FORMAT_NO_OUTPUT;
2041 options->exit_with_status = 1;
2045 * If we postprocess in diffcore, we cannot simply return
2046 * upon the first hit. We need to run diff as usual.
2048 if (options->pickaxe || options->filter)
2049 options->quiet = 0;
2051 return 0;
2054 static int opt_arg(const char *arg, int arg_short, const char *arg_long, int *val)
2056 char c, *eq;
2057 int len;
2059 if (*arg != '-')
2060 return 0;
2061 c = *++arg;
2062 if (!c)
2063 return 0;
2064 if (c == arg_short) {
2065 c = *++arg;
2066 if (!c)
2067 return 1;
2068 if (val && isdigit(c)) {
2069 char *end;
2070 int n = strtoul(arg, &end, 10);
2071 if (*end)
2072 return 0;
2073 *val = n;
2074 return 1;
2076 return 0;
2078 if (c != '-')
2079 return 0;
2080 arg++;
2081 eq = strchr(arg, '=');
2082 if (eq)
2083 len = eq - arg;
2084 else
2085 len = strlen(arg);
2086 if (!len || strncmp(arg, arg_long, len))
2087 return 0;
2088 if (eq) {
2089 int n;
2090 char *end;
2091 if (!isdigit(*++eq))
2092 return 0;
2093 n = strtoul(eq, &end, 10);
2094 if (*end)
2095 return 0;
2096 *val = n;
2098 return 1;
2101 int diff_opt_parse(struct diff_options *options, const char **av, int ac)
2103 const char *arg = av[0];
2104 if (!strcmp(arg, "-p") || !strcmp(arg, "-u"))
2105 options->output_format |= DIFF_FORMAT_PATCH;
2106 else if (opt_arg(arg, 'U', "unified", &options->context))
2107 options->output_format |= DIFF_FORMAT_PATCH;
2108 else if (!strcmp(arg, "--raw"))
2109 options->output_format |= DIFF_FORMAT_RAW;
2110 else if (!strcmp(arg, "--patch-with-raw")) {
2111 options->output_format |= DIFF_FORMAT_PATCH | DIFF_FORMAT_RAW;
2113 else if (!strcmp(arg, "--numstat")) {
2114 options->output_format |= DIFF_FORMAT_NUMSTAT;
2116 else if (!strcmp(arg, "--shortstat")) {
2117 options->output_format |= DIFF_FORMAT_SHORTSTAT;
2119 else if (!prefixcmp(arg, "--stat")) {
2120 char *end;
2121 int width = options->stat_width;
2122 int name_width = options->stat_name_width;
2123 arg += 6;
2124 end = (char *)arg;
2126 switch (*arg) {
2127 case '-':
2128 if (!prefixcmp(arg, "-width="))
2129 width = strtoul(arg + 7, &end, 10);
2130 else if (!prefixcmp(arg, "-name-width="))
2131 name_width = strtoul(arg + 12, &end, 10);
2132 break;
2133 case '=':
2134 width = strtoul(arg+1, &end, 10);
2135 if (*end == ',')
2136 name_width = strtoul(end+1, &end, 10);
2139 /* Important! This checks all the error cases! */
2140 if (*end)
2141 return 0;
2142 options->output_format |= DIFF_FORMAT_DIFFSTAT;
2143 options->stat_name_width = name_width;
2144 options->stat_width = width;
2146 else if (!strcmp(arg, "--check"))
2147 options->output_format |= DIFF_FORMAT_CHECKDIFF;
2148 else if (!strcmp(arg, "--summary"))
2149 options->output_format |= DIFF_FORMAT_SUMMARY;
2150 else if (!strcmp(arg, "--patch-with-stat")) {
2151 options->output_format |= DIFF_FORMAT_PATCH | DIFF_FORMAT_DIFFSTAT;
2153 else if (!strcmp(arg, "-z"))
2154 options->line_termination = 0;
2155 else if (!prefixcmp(arg, "-l"))
2156 options->rename_limit = strtoul(arg+2, NULL, 10);
2157 else if (!strcmp(arg, "--full-index"))
2158 options->full_index = 1;
2159 else if (!strcmp(arg, "--binary")) {
2160 options->output_format |= DIFF_FORMAT_PATCH;
2161 options->binary = 1;
2163 else if (!strcmp(arg, "-a") || !strcmp(arg, "--text")) {
2164 options->text = 1;
2166 else if (!strcmp(arg, "--name-only"))
2167 options->output_format |= DIFF_FORMAT_NAME;
2168 else if (!strcmp(arg, "--name-status"))
2169 options->output_format |= DIFF_FORMAT_NAME_STATUS;
2170 else if (!strcmp(arg, "-R"))
2171 options->reverse_diff = 1;
2172 else if (!prefixcmp(arg, "-S"))
2173 options->pickaxe = arg + 2;
2174 else if (!strcmp(arg, "-s")) {
2175 options->output_format |= DIFF_FORMAT_NO_OUTPUT;
2177 else if (!prefixcmp(arg, "-O"))
2178 options->orderfile = arg + 2;
2179 else if (!prefixcmp(arg, "--diff-filter="))
2180 options->filter = arg + 14;
2181 else if (!strcmp(arg, "--pickaxe-all"))
2182 options->pickaxe_opts = DIFF_PICKAXE_ALL;
2183 else if (!strcmp(arg, "--pickaxe-regex"))
2184 options->pickaxe_opts = DIFF_PICKAXE_REGEX;
2185 else if (!prefixcmp(arg, "-B")) {
2186 if ((options->break_opt =
2187 diff_scoreopt_parse(arg)) == -1)
2188 return -1;
2190 else if (!prefixcmp(arg, "-M")) {
2191 if ((options->rename_score =
2192 diff_scoreopt_parse(arg)) == -1)
2193 return -1;
2194 options->detect_rename = DIFF_DETECT_RENAME;
2196 else if (!prefixcmp(arg, "-C")) {
2197 if ((options->rename_score =
2198 diff_scoreopt_parse(arg)) == -1)
2199 return -1;
2200 options->detect_rename = DIFF_DETECT_COPY;
2202 else if (!strcmp(arg, "--find-copies-harder"))
2203 options->find_copies_harder = 1;
2204 else if (!strcmp(arg, "--abbrev"))
2205 options->abbrev = DEFAULT_ABBREV;
2206 else if (!prefixcmp(arg, "--abbrev=")) {
2207 options->abbrev = strtoul(arg + 9, NULL, 10);
2208 if (options->abbrev < MINIMUM_ABBREV)
2209 options->abbrev = MINIMUM_ABBREV;
2210 else if (40 < options->abbrev)
2211 options->abbrev = 40;
2213 else if (!strcmp(arg, "--color"))
2214 options->color_diff = 1;
2215 else if (!strcmp(arg, "--no-color"))
2216 options->color_diff = 0;
2217 else if (!strcmp(arg, "-w") || !strcmp(arg, "--ignore-all-space"))
2218 options->xdl_opts |= XDF_IGNORE_WHITESPACE;
2219 else if (!strcmp(arg, "-b") || !strcmp(arg, "--ignore-space-change"))
2220 options->xdl_opts |= XDF_IGNORE_WHITESPACE_CHANGE;
2221 else if (!strcmp(arg, "--ignore-space-at-eol"))
2222 options->xdl_opts |= XDF_IGNORE_WHITESPACE_AT_EOL;
2223 else if (!strcmp(arg, "--color-words"))
2224 options->color_diff = options->color_diff_words = 1;
2225 else if (!strcmp(arg, "--no-renames"))
2226 options->detect_rename = 0;
2227 else if (!strcmp(arg, "--exit-code"))
2228 options->exit_with_status = 1;
2229 else if (!strcmp(arg, "--quiet"))
2230 options->quiet = 1;
2231 else
2232 return 0;
2233 return 1;
2236 static int parse_num(const char **cp_p)
2238 unsigned long num, scale;
2239 int ch, dot;
2240 const char *cp = *cp_p;
2242 num = 0;
2243 scale = 1;
2244 dot = 0;
2245 for(;;) {
2246 ch = *cp;
2247 if ( !dot && ch == '.' ) {
2248 scale = 1;
2249 dot = 1;
2250 } else if ( ch == '%' ) {
2251 scale = dot ? scale*100 : 100;
2252 cp++; /* % is always at the end */
2253 break;
2254 } else if ( ch >= '0' && ch <= '9' ) {
2255 if ( scale < 100000 ) {
2256 scale *= 10;
2257 num = (num*10) + (ch-'0');
2259 } else {
2260 break;
2262 cp++;
2264 *cp_p = cp;
2266 /* user says num divided by scale and we say internally that
2267 * is MAX_SCORE * num / scale.
2269 return (int)((num >= scale) ? MAX_SCORE : (MAX_SCORE * num / scale));
2272 int diff_scoreopt_parse(const char *opt)
2274 int opt1, opt2, cmd;
2276 if (*opt++ != '-')
2277 return -1;
2278 cmd = *opt++;
2279 if (cmd != 'M' && cmd != 'C' && cmd != 'B')
2280 return -1; /* that is not a -M, -C nor -B option */
2282 opt1 = parse_num(&opt);
2283 if (cmd != 'B')
2284 opt2 = 0;
2285 else {
2286 if (*opt == 0)
2287 opt2 = 0;
2288 else if (*opt != '/')
2289 return -1; /* we expect -B80/99 or -B80 */
2290 else {
2291 opt++;
2292 opt2 = parse_num(&opt);
2295 if (*opt != 0)
2296 return -1;
2297 return opt1 | (opt2 << 16);
2300 struct diff_queue_struct diff_queued_diff;
2302 void diff_q(struct diff_queue_struct *queue, struct diff_filepair *dp)
2304 if (queue->alloc <= queue->nr) {
2305 queue->alloc = alloc_nr(queue->alloc);
2306 queue->queue = xrealloc(queue->queue,
2307 sizeof(dp) * queue->alloc);
2309 queue->queue[queue->nr++] = dp;
2312 struct diff_filepair *diff_queue(struct diff_queue_struct *queue,
2313 struct diff_filespec *one,
2314 struct diff_filespec *two)
2316 struct diff_filepair *dp = xcalloc(1, sizeof(*dp));
2317 dp->one = one;
2318 dp->two = two;
2319 if (queue)
2320 diff_q(queue, dp);
2321 return dp;
2324 void diff_free_filepair(struct diff_filepair *p)
2326 diff_free_filespec_data(p->one);
2327 diff_free_filespec_data(p->two);
2328 free(p->one);
2329 free(p->two);
2330 free(p);
2333 /* This is different from find_unique_abbrev() in that
2334 * it stuffs the result with dots for alignment.
2336 const char *diff_unique_abbrev(const unsigned char *sha1, int len)
2338 int abblen;
2339 const char *abbrev;
2340 if (len == 40)
2341 return sha1_to_hex(sha1);
2343 abbrev = find_unique_abbrev(sha1, len);
2344 if (!abbrev)
2345 return sha1_to_hex(sha1);
2346 abblen = strlen(abbrev);
2347 if (abblen < 37) {
2348 static char hex[41];
2349 if (len < abblen && abblen <= len + 2)
2350 sprintf(hex, "%s%.*s", abbrev, len+3-abblen, "..");
2351 else
2352 sprintf(hex, "%s...", abbrev);
2353 return hex;
2355 return sha1_to_hex(sha1);
2358 static void diff_flush_raw(struct diff_filepair *p,
2359 struct diff_options *options)
2361 int two_paths;
2362 char status[10];
2363 int abbrev = options->abbrev;
2364 const char *path_one, *path_two;
2365 int inter_name_termination = '\t';
2366 int line_termination = options->line_termination;
2368 if (!line_termination)
2369 inter_name_termination = 0;
2371 path_one = p->one->path;
2372 path_two = p->two->path;
2373 if (line_termination) {
2374 path_one = quote_one(path_one);
2375 path_two = quote_one(path_two);
2378 if (p->score)
2379 sprintf(status, "%c%03d", p->status,
2380 (int)(0.5 + p->score * 100.0/MAX_SCORE));
2381 else {
2382 status[0] = p->status;
2383 status[1] = 0;
2385 switch (p->status) {
2386 case DIFF_STATUS_COPIED:
2387 case DIFF_STATUS_RENAMED:
2388 two_paths = 1;
2389 break;
2390 case DIFF_STATUS_ADDED:
2391 case DIFF_STATUS_DELETED:
2392 two_paths = 0;
2393 break;
2394 default:
2395 two_paths = 0;
2396 break;
2398 if (!(options->output_format & DIFF_FORMAT_NAME_STATUS)) {
2399 printf(":%06o %06o %s ",
2400 p->one->mode, p->two->mode,
2401 diff_unique_abbrev(p->one->sha1, abbrev));
2402 printf("%s ",
2403 diff_unique_abbrev(p->two->sha1, abbrev));
2405 printf("%s%c%s", status, inter_name_termination, path_one);
2406 if (two_paths)
2407 printf("%c%s", inter_name_termination, path_two);
2408 putchar(line_termination);
2409 if (path_one != p->one->path)
2410 free((void*)path_one);
2411 if (path_two != p->two->path)
2412 free((void*)path_two);
2415 static void diff_flush_name(struct diff_filepair *p, struct diff_options *opt)
2417 char *path = p->two->path;
2419 if (opt->line_termination)
2420 path = quote_one(p->two->path);
2421 printf("%s%c", path, opt->line_termination);
2422 if (p->two->path != path)
2423 free(path);
2426 int diff_unmodified_pair(struct diff_filepair *p)
2428 /* This function is written stricter than necessary to support
2429 * the currently implemented transformers, but the idea is to
2430 * let transformers to produce diff_filepairs any way they want,
2431 * and filter and clean them up here before producing the output.
2433 struct diff_filespec *one, *two;
2435 if (DIFF_PAIR_UNMERGED(p))
2436 return 0; /* unmerged is interesting */
2438 one = p->one;
2439 two = p->two;
2441 /* deletion, addition, mode or type change
2442 * and rename are all interesting.
2444 if (DIFF_FILE_VALID(one) != DIFF_FILE_VALID(two) ||
2445 DIFF_PAIR_MODE_CHANGED(p) ||
2446 strcmp(one->path, two->path))
2447 return 0;
2449 /* both are valid and point at the same path. that is, we are
2450 * dealing with a change.
2452 if (one->sha1_valid && two->sha1_valid &&
2453 !hashcmp(one->sha1, two->sha1))
2454 return 1; /* no change */
2455 if (!one->sha1_valid && !two->sha1_valid)
2456 return 1; /* both look at the same file on the filesystem. */
2457 return 0;
2460 static void diff_flush_patch(struct diff_filepair *p, struct diff_options *o)
2462 if (diff_unmodified_pair(p))
2463 return;
2465 if ((DIFF_FILE_VALID(p->one) && S_ISDIR(p->one->mode)) ||
2466 (DIFF_FILE_VALID(p->two) && S_ISDIR(p->two->mode)))
2467 return; /* no tree diffs in patch format */
2469 run_diff(p, o);
2472 static void diff_flush_stat(struct diff_filepair *p, struct diff_options *o,
2473 struct diffstat_t *diffstat)
2475 if (diff_unmodified_pair(p))
2476 return;
2478 if ((DIFF_FILE_VALID(p->one) && S_ISDIR(p->one->mode)) ||
2479 (DIFF_FILE_VALID(p->two) && S_ISDIR(p->two->mode)))
2480 return; /* no tree diffs in patch format */
2482 run_diffstat(p, o, diffstat);
2485 static void diff_flush_checkdiff(struct diff_filepair *p,
2486 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_checkdiff(p, o);
2498 int diff_queue_is_empty(void)
2500 struct diff_queue_struct *q = &diff_queued_diff;
2501 int i;
2502 for (i = 0; i < q->nr; i++)
2503 if (!diff_unmodified_pair(q->queue[i]))
2504 return 0;
2505 return 1;
2508 #if DIFF_DEBUG
2509 void diff_debug_filespec(struct diff_filespec *s, int x, const char *one)
2511 fprintf(stderr, "queue[%d] %s (%s) %s %06o %s\n",
2512 x, one ? one : "",
2513 s->path,
2514 DIFF_FILE_VALID(s) ? "valid" : "invalid",
2515 s->mode,
2516 s->sha1_valid ? sha1_to_hex(s->sha1) : "");
2517 fprintf(stderr, "queue[%d] %s size %lu flags %d\n",
2518 x, one ? one : "",
2519 s->size, s->xfrm_flags);
2522 void diff_debug_filepair(const struct diff_filepair *p, int i)
2524 diff_debug_filespec(p->one, i, "one");
2525 diff_debug_filespec(p->two, i, "two");
2526 fprintf(stderr, "score %d, status %c stays %d broken %d\n",
2527 p->score, p->status ? p->status : '?',
2528 p->source_stays, p->broken_pair);
2531 void diff_debug_queue(const char *msg, struct diff_queue_struct *q)
2533 int i;
2534 if (msg)
2535 fprintf(stderr, "%s\n", msg);
2536 fprintf(stderr, "q->nr = %d\n", q->nr);
2537 for (i = 0; i < q->nr; i++) {
2538 struct diff_filepair *p = q->queue[i];
2539 diff_debug_filepair(p, i);
2542 #endif
2544 static void diff_resolve_rename_copy(void)
2546 int i, j;
2547 struct diff_filepair *p, *pp;
2548 struct diff_queue_struct *q = &diff_queued_diff;
2550 diff_debug_queue("resolve-rename-copy", q);
2552 for (i = 0; i < q->nr; i++) {
2553 p = q->queue[i];
2554 p->status = 0; /* undecided */
2555 if (DIFF_PAIR_UNMERGED(p))
2556 p->status = DIFF_STATUS_UNMERGED;
2557 else if (!DIFF_FILE_VALID(p->one))
2558 p->status = DIFF_STATUS_ADDED;
2559 else if (!DIFF_FILE_VALID(p->two))
2560 p->status = DIFF_STATUS_DELETED;
2561 else if (DIFF_PAIR_TYPE_CHANGED(p))
2562 p->status = DIFF_STATUS_TYPE_CHANGED;
2564 /* from this point on, we are dealing with a pair
2565 * whose both sides are valid and of the same type, i.e.
2566 * either in-place edit or rename/copy edit.
2568 else if (DIFF_PAIR_RENAME(p)) {
2569 if (p->source_stays) {
2570 p->status = DIFF_STATUS_COPIED;
2571 continue;
2573 /* See if there is some other filepair that
2574 * copies from the same source as us. If so
2575 * we are a copy. Otherwise we are either a
2576 * copy if the path stays, or a rename if it
2577 * does not, but we already handled "stays" case.
2579 for (j = i + 1; j < q->nr; j++) {
2580 pp = q->queue[j];
2581 if (strcmp(pp->one->path, p->one->path))
2582 continue; /* not us */
2583 if (!DIFF_PAIR_RENAME(pp))
2584 continue; /* not a rename/copy */
2585 /* pp is a rename/copy from the same source */
2586 p->status = DIFF_STATUS_COPIED;
2587 break;
2589 if (!p->status)
2590 p->status = DIFF_STATUS_RENAMED;
2592 else if (hashcmp(p->one->sha1, p->two->sha1) ||
2593 p->one->mode != p->two->mode ||
2594 is_null_sha1(p->one->sha1))
2595 p->status = DIFF_STATUS_MODIFIED;
2596 else {
2597 /* This is a "no-change" entry and should not
2598 * happen anymore, but prepare for broken callers.
2600 error("feeding unmodified %s to diffcore",
2601 p->one->path);
2602 p->status = DIFF_STATUS_UNKNOWN;
2605 diff_debug_queue("resolve-rename-copy done", q);
2608 static int check_pair_status(struct diff_filepair *p)
2610 switch (p->status) {
2611 case DIFF_STATUS_UNKNOWN:
2612 return 0;
2613 case 0:
2614 die("internal error in diff-resolve-rename-copy");
2615 default:
2616 return 1;
2620 static void flush_one_pair(struct diff_filepair *p, struct diff_options *opt)
2622 int fmt = opt->output_format;
2624 if (fmt & DIFF_FORMAT_CHECKDIFF)
2625 diff_flush_checkdiff(p, opt);
2626 else if (fmt & (DIFF_FORMAT_RAW | DIFF_FORMAT_NAME_STATUS))
2627 diff_flush_raw(p, opt);
2628 else if (fmt & DIFF_FORMAT_NAME)
2629 diff_flush_name(p, opt);
2632 static void show_file_mode_name(const char *newdelete, struct diff_filespec *fs)
2634 char *name = quote_one(fs->path);
2635 if (fs->mode)
2636 printf(" %s mode %06o %s\n", newdelete, fs->mode, name);
2637 else
2638 printf(" %s %s\n", newdelete, name);
2639 free(name);
2643 static void show_mode_change(struct diff_filepair *p, int show_name)
2645 if (p->one->mode && p->two->mode && p->one->mode != p->two->mode) {
2646 if (show_name) {
2647 char *name = quote_one(p->two->path);
2648 printf(" mode change %06o => %06o %s\n",
2649 p->one->mode, p->two->mode, name);
2650 free(name);
2652 else
2653 printf(" mode change %06o => %06o\n",
2654 p->one->mode, p->two->mode);
2658 static void show_rename_copy(const char *renamecopy, struct diff_filepair *p)
2660 char *names = pprint_rename(p->one->path, p->two->path);
2662 printf(" %s %s (%d%%)\n", renamecopy, names,
2663 (int)(0.5 + p->score * 100.0/MAX_SCORE));
2664 free(names);
2665 show_mode_change(p, 0);
2668 static void diff_summary(struct diff_filepair *p)
2670 switch(p->status) {
2671 case DIFF_STATUS_DELETED:
2672 show_file_mode_name("delete", p->one);
2673 break;
2674 case DIFF_STATUS_ADDED:
2675 show_file_mode_name("create", p->two);
2676 break;
2677 case DIFF_STATUS_COPIED:
2678 show_rename_copy("copy", p);
2679 break;
2680 case DIFF_STATUS_RENAMED:
2681 show_rename_copy("rename", p);
2682 break;
2683 default:
2684 if (p->score) {
2685 char *name = quote_one(p->two->path);
2686 printf(" rewrite %s (%d%%)\n", name,
2687 (int)(0.5 + p->score * 100.0/MAX_SCORE));
2688 free(name);
2689 show_mode_change(p, 0);
2690 } else show_mode_change(p, 1);
2691 break;
2695 struct patch_id_t {
2696 struct xdiff_emit_state xm;
2697 SHA_CTX *ctx;
2698 int patchlen;
2701 static int remove_space(char *line, int len)
2703 int i;
2704 char *dst = line;
2705 unsigned char c;
2707 for (i = 0; i < len; i++)
2708 if (!isspace((c = line[i])))
2709 *dst++ = c;
2711 return dst - line;
2714 static void patch_id_consume(void *priv, char *line, unsigned long len)
2716 struct patch_id_t *data = priv;
2717 int new_len;
2719 /* Ignore line numbers when computing the SHA1 of the patch */
2720 if (!prefixcmp(line, "@@ -"))
2721 return;
2723 new_len = remove_space(line, len);
2725 SHA1_Update(data->ctx, line, new_len);
2726 data->patchlen += new_len;
2729 /* returns 0 upon success, and writes result into sha1 */
2730 static int diff_get_patch_id(struct diff_options *options, unsigned char *sha1)
2732 struct diff_queue_struct *q = &diff_queued_diff;
2733 int i;
2734 SHA_CTX ctx;
2735 struct patch_id_t data;
2736 char buffer[PATH_MAX * 4 + 20];
2738 SHA1_Init(&ctx);
2739 memset(&data, 0, sizeof(struct patch_id_t));
2740 data.ctx = &ctx;
2741 data.xm.consume = patch_id_consume;
2743 for (i = 0; i < q->nr; i++) {
2744 xpparam_t xpp;
2745 xdemitconf_t xecfg;
2746 xdemitcb_t ecb;
2747 mmfile_t mf1, mf2;
2748 struct diff_filepair *p = q->queue[i];
2749 int len1, len2;
2751 if (p->status == 0)
2752 return error("internal diff status error");
2753 if (p->status == DIFF_STATUS_UNKNOWN)
2754 continue;
2755 if (diff_unmodified_pair(p))
2756 continue;
2757 if ((DIFF_FILE_VALID(p->one) && S_ISDIR(p->one->mode)) ||
2758 (DIFF_FILE_VALID(p->two) && S_ISDIR(p->two->mode)))
2759 continue;
2760 if (DIFF_PAIR_UNMERGED(p))
2761 continue;
2763 diff_fill_sha1_info(p->one);
2764 diff_fill_sha1_info(p->two);
2765 if (fill_mmfile(&mf1, p->one) < 0 ||
2766 fill_mmfile(&mf2, p->two) < 0)
2767 return error("unable to read files to diff");
2769 /* Maybe hash p->two? into the patch id? */
2770 if (file_is_binary(p->two))
2771 continue;
2773 len1 = remove_space(p->one->path, strlen(p->one->path));
2774 len2 = remove_space(p->two->path, strlen(p->two->path));
2775 if (p->one->mode == 0)
2776 len1 = snprintf(buffer, sizeof(buffer),
2777 "diff--gita/%.*sb/%.*s"
2778 "newfilemode%06o"
2779 "---/dev/null"
2780 "+++b/%.*s",
2781 len1, p->one->path,
2782 len2, p->two->path,
2783 p->two->mode,
2784 len2, p->two->path);
2785 else if (p->two->mode == 0)
2786 len1 = snprintf(buffer, sizeof(buffer),
2787 "diff--gita/%.*sb/%.*s"
2788 "deletedfilemode%06o"
2789 "---a/%.*s"
2790 "+++/dev/null",
2791 len1, p->one->path,
2792 len2, p->two->path,
2793 p->one->mode,
2794 len1, p->one->path);
2795 else
2796 len1 = snprintf(buffer, sizeof(buffer),
2797 "diff--gita/%.*sb/%.*s"
2798 "---a/%.*s"
2799 "+++b/%.*s",
2800 len1, p->one->path,
2801 len2, p->two->path,
2802 len1, p->one->path,
2803 len2, p->two->path);
2804 SHA1_Update(&ctx, buffer, len1);
2806 xpp.flags = XDF_NEED_MINIMAL;
2807 xecfg.ctxlen = 3;
2808 xecfg.flags = XDL_EMIT_FUNCNAMES;
2809 ecb.outf = xdiff_outf;
2810 ecb.priv = &data;
2811 xdl_diff(&mf1, &mf2, &xpp, &xecfg, &ecb);
2814 SHA1_Final(sha1, &ctx);
2815 return 0;
2818 int diff_flush_patch_id(struct diff_options *options, unsigned char *sha1)
2820 struct diff_queue_struct *q = &diff_queued_diff;
2821 int i;
2822 int result = diff_get_patch_id(options, sha1);
2824 for (i = 0; i < q->nr; i++)
2825 diff_free_filepair(q->queue[i]);
2827 free(q->queue);
2828 q->queue = NULL;
2829 q->nr = q->alloc = 0;
2831 return result;
2834 static int is_summary_empty(const struct diff_queue_struct *q)
2836 int i;
2838 for (i = 0; i < q->nr; i++) {
2839 const struct diff_filepair *p = q->queue[i];
2841 switch (p->status) {
2842 case DIFF_STATUS_DELETED:
2843 case DIFF_STATUS_ADDED:
2844 case DIFF_STATUS_COPIED:
2845 case DIFF_STATUS_RENAMED:
2846 return 0;
2847 default:
2848 if (p->score)
2849 return 0;
2850 if (p->one->mode && p->two->mode &&
2851 p->one->mode != p->two->mode)
2852 return 0;
2853 break;
2856 return 1;
2859 void diff_flush(struct diff_options *options)
2861 struct diff_queue_struct *q = &diff_queued_diff;
2862 int i, output_format = options->output_format;
2863 int separator = 0;
2866 * Order: raw, stat, summary, patch
2867 * or: name/name-status/checkdiff (other bits clear)
2869 if (!q->nr)
2870 goto free_queue;
2872 if (output_format & (DIFF_FORMAT_RAW |
2873 DIFF_FORMAT_NAME |
2874 DIFF_FORMAT_NAME_STATUS |
2875 DIFF_FORMAT_CHECKDIFF)) {
2876 for (i = 0; i < q->nr; i++) {
2877 struct diff_filepair *p = q->queue[i];
2878 if (check_pair_status(p))
2879 flush_one_pair(p, options);
2881 separator++;
2884 if (output_format & (DIFF_FORMAT_DIFFSTAT|DIFF_FORMAT_SHORTSTAT|DIFF_FORMAT_NUMSTAT)) {
2885 struct diffstat_t diffstat;
2887 memset(&diffstat, 0, sizeof(struct diffstat_t));
2888 diffstat.xm.consume = diffstat_consume;
2889 for (i = 0; i < q->nr; i++) {
2890 struct diff_filepair *p = q->queue[i];
2891 if (check_pair_status(p))
2892 diff_flush_stat(p, options, &diffstat);
2894 if (output_format & DIFF_FORMAT_NUMSTAT)
2895 show_numstat(&diffstat, options);
2896 if (output_format & DIFF_FORMAT_DIFFSTAT)
2897 show_stats(&diffstat, options);
2898 else if (output_format & DIFF_FORMAT_SHORTSTAT)
2899 show_shortstats(&diffstat);
2900 separator++;
2903 if (output_format & DIFF_FORMAT_SUMMARY && !is_summary_empty(q)) {
2904 for (i = 0; i < q->nr; i++)
2905 diff_summary(q->queue[i]);
2906 separator++;
2909 if (output_format & DIFF_FORMAT_PATCH) {
2910 if (separator) {
2911 if (options->stat_sep) {
2912 /* attach patch instead of inline */
2913 fputs(options->stat_sep, stdout);
2914 } else {
2915 putchar(options->line_termination);
2919 for (i = 0; i < q->nr; i++) {
2920 struct diff_filepair *p = q->queue[i];
2921 if (check_pair_status(p))
2922 diff_flush_patch(p, options);
2926 if (output_format & DIFF_FORMAT_CALLBACK)
2927 options->format_callback(q, options, options->format_callback_data);
2929 for (i = 0; i < q->nr; i++)
2930 diff_free_filepair(q->queue[i]);
2931 free_queue:
2932 free(q->queue);
2933 q->queue = NULL;
2934 q->nr = q->alloc = 0;
2937 static void diffcore_apply_filter(const char *filter)
2939 int i;
2940 struct diff_queue_struct *q = &diff_queued_diff;
2941 struct diff_queue_struct outq;
2942 outq.queue = NULL;
2943 outq.nr = outq.alloc = 0;
2945 if (!filter)
2946 return;
2948 if (strchr(filter, DIFF_STATUS_FILTER_AON)) {
2949 int found;
2950 for (i = found = 0; !found && i < q->nr; i++) {
2951 struct diff_filepair *p = q->queue[i];
2952 if (((p->status == DIFF_STATUS_MODIFIED) &&
2953 ((p->score &&
2954 strchr(filter, DIFF_STATUS_FILTER_BROKEN)) ||
2955 (!p->score &&
2956 strchr(filter, DIFF_STATUS_MODIFIED)))) ||
2957 ((p->status != DIFF_STATUS_MODIFIED) &&
2958 strchr(filter, p->status)))
2959 found++;
2961 if (found)
2962 return;
2964 /* otherwise we will clear the whole queue
2965 * by copying the empty outq at the end of this
2966 * function, but first clear the current entries
2967 * in the queue.
2969 for (i = 0; i < q->nr; i++)
2970 diff_free_filepair(q->queue[i]);
2972 else {
2973 /* Only the matching ones */
2974 for (i = 0; i < q->nr; i++) {
2975 struct diff_filepair *p = q->queue[i];
2977 if (((p->status == DIFF_STATUS_MODIFIED) &&
2978 ((p->score &&
2979 strchr(filter, DIFF_STATUS_FILTER_BROKEN)) ||
2980 (!p->score &&
2981 strchr(filter, DIFF_STATUS_MODIFIED)))) ||
2982 ((p->status != DIFF_STATUS_MODIFIED) &&
2983 strchr(filter, p->status)))
2984 diff_q(&outq, p);
2985 else
2986 diff_free_filepair(p);
2989 free(q->queue);
2990 *q = outq;
2993 void diffcore_std(struct diff_options *options)
2995 if (options->quiet)
2996 return;
2997 if (options->break_opt != -1)
2998 diffcore_break(options->break_opt);
2999 if (options->detect_rename)
3000 diffcore_rename(options);
3001 if (options->break_opt != -1)
3002 diffcore_merge_broken();
3003 if (options->pickaxe)
3004 diffcore_pickaxe(options->pickaxe, options->pickaxe_opts);
3005 if (options->orderfile)
3006 diffcore_order(options->orderfile);
3007 diff_resolve_rename_copy();
3008 diffcore_apply_filter(options->filter);
3010 options->has_changes = !!diff_queued_diff.nr;
3014 void diff_addremove(struct diff_options *options,
3015 int addremove, unsigned mode,
3016 const unsigned char *sha1,
3017 const char *base, const char *path)
3019 char concatpath[PATH_MAX];
3020 struct diff_filespec *one, *two;
3022 /* This may look odd, but it is a preparation for
3023 * feeding "there are unchanged files which should
3024 * not produce diffs, but when you are doing copy
3025 * detection you would need them, so here they are"
3026 * entries to the diff-core. They will be prefixed
3027 * with something like '=' or '*' (I haven't decided
3028 * which but should not make any difference).
3029 * Feeding the same new and old to diff_change()
3030 * also has the same effect.
3031 * Before the final output happens, they are pruned after
3032 * merged into rename/copy pairs as appropriate.
3034 if (options->reverse_diff)
3035 addremove = (addremove == '+' ? '-' :
3036 addremove == '-' ? '+' : addremove);
3038 if (!path) path = "";
3039 sprintf(concatpath, "%s%s", base, path);
3040 one = alloc_filespec(concatpath);
3041 two = alloc_filespec(concatpath);
3043 if (addremove != '+')
3044 fill_filespec(one, sha1, mode);
3045 if (addremove != '-')
3046 fill_filespec(two, sha1, mode);
3048 diff_queue(&diff_queued_diff, one, two);
3049 options->has_changes = 1;
3052 void diff_change(struct diff_options *options,
3053 unsigned old_mode, unsigned new_mode,
3054 const unsigned char *old_sha1,
3055 const unsigned char *new_sha1,
3056 const char *base, const char *path)
3058 char concatpath[PATH_MAX];
3059 struct diff_filespec *one, *two;
3061 if (options->reverse_diff) {
3062 unsigned tmp;
3063 const unsigned char *tmp_c;
3064 tmp = old_mode; old_mode = new_mode; new_mode = tmp;
3065 tmp_c = old_sha1; old_sha1 = new_sha1; new_sha1 = tmp_c;
3067 if (!path) path = "";
3068 sprintf(concatpath, "%s%s", base, path);
3069 one = alloc_filespec(concatpath);
3070 two = alloc_filespec(concatpath);
3071 fill_filespec(one, old_sha1, old_mode);
3072 fill_filespec(two, new_sha1, new_mode);
3074 diff_queue(&diff_queued_diff, one, two);
3075 options->has_changes = 1;
3078 void diff_unmerge(struct diff_options *options,
3079 const char *path,
3080 unsigned mode, const unsigned char *sha1)
3082 struct diff_filespec *one, *two;
3083 one = alloc_filespec(path);
3084 two = alloc_filespec(path);
3085 fill_filespec(one, sha1, mode);
3086 diff_queue(&diff_queued_diff, one, two)->is_unmerged = 1;