Teach diff -a as shorthand for --text
[git/repo.git] / diff.c
blobb423491f60e7c0dc397e9db9a6864cd3903e2f28
1 /*
2 * Copyright (C) 2005 Junio C Hamano
3 */
4 #include <sys/types.h>
5 #include <sys/wait.h>
6 #include <signal.h>
7 #include "cache.h"
8 #include "quote.h"
9 #include "diff.h"
10 #include "diffcore.h"
11 #include "delta.h"
12 #include "xdiff-interface.h"
14 static int use_size_cache;
16 static int diff_rename_limit_default = -1;
17 static int diff_use_color_default = 0;
19 enum color_diff {
20 DIFF_RESET = 0,
21 DIFF_PLAIN = 1,
22 DIFF_METAINFO = 2,
23 DIFF_FRAGINFO = 3,
24 DIFF_FILE_OLD = 4,
25 DIFF_FILE_NEW = 5,
28 #define COLOR_NORMAL ""
29 #define COLOR_BOLD "\033[1m"
30 #define COLOR_DIM "\033[2m"
31 #define COLOR_UL "\033[4m"
32 #define COLOR_BLINK "\033[5m"
33 #define COLOR_REVERSE "\033[7m"
34 #define COLOR_RESET "\033[m"
36 #define COLOR_BLACK "\033[30m"
37 #define COLOR_RED "\033[31m"
38 #define COLOR_GREEN "\033[32m"
39 #define COLOR_YELLOW "\033[33m"
40 #define COLOR_BLUE "\033[34m"
41 #define COLOR_MAGENTA "\033[35m"
42 #define COLOR_CYAN "\033[36m"
43 #define COLOR_WHITE "\033[37m"
45 static const char *diff_colors[] = {
46 [DIFF_RESET] = COLOR_RESET,
47 [DIFF_PLAIN] = COLOR_NORMAL,
48 [DIFF_METAINFO] = COLOR_BOLD,
49 [DIFF_FRAGINFO] = COLOR_CYAN,
50 [DIFF_FILE_OLD] = COLOR_RED,
51 [DIFF_FILE_NEW] = COLOR_GREEN,
54 static int parse_diff_color_slot(const char *var, int ofs)
56 if (!strcasecmp(var+ofs, "plain"))
57 return DIFF_PLAIN;
58 if (!strcasecmp(var+ofs, "meta"))
59 return DIFF_METAINFO;
60 if (!strcasecmp(var+ofs, "frag"))
61 return DIFF_FRAGINFO;
62 if (!strcasecmp(var+ofs, "old"))
63 return DIFF_FILE_OLD;
64 if (!strcasecmp(var+ofs, "new"))
65 return DIFF_FILE_NEW;
66 die("bad config variable '%s'", var);
69 static const char *parse_diff_color_value(const char *value, const char *var)
71 if (!strcasecmp(value, "normal"))
72 return COLOR_NORMAL;
73 if (!strcasecmp(value, "bold"))
74 return COLOR_BOLD;
75 if (!strcasecmp(value, "dim"))
76 return COLOR_DIM;
77 if (!strcasecmp(value, "ul"))
78 return COLOR_UL;
79 if (!strcasecmp(value, "blink"))
80 return COLOR_BLINK;
81 if (!strcasecmp(value, "reverse"))
82 return COLOR_REVERSE;
83 if (!strcasecmp(value, "reset"))
84 return COLOR_RESET;
85 if (!strcasecmp(value, "black"))
86 return COLOR_BLACK;
87 if (!strcasecmp(value, "red"))
88 return COLOR_RED;
89 if (!strcasecmp(value, "green"))
90 return COLOR_GREEN;
91 if (!strcasecmp(value, "yellow"))
92 return COLOR_YELLOW;
93 if (!strcasecmp(value, "blue"))
94 return COLOR_BLUE;
95 if (!strcasecmp(value, "magenta"))
96 return COLOR_MAGENTA;
97 if (!strcasecmp(value, "cyan"))
98 return COLOR_CYAN;
99 if (!strcasecmp(value, "white"))
100 return COLOR_WHITE;
101 die("bad config value '%s' for variable '%s'", value, var);
104 int git_diff_config(const char *var, const char *value)
106 if (!strcmp(var, "diff.renamelimit")) {
107 diff_rename_limit_default = git_config_int(var, value);
108 return 0;
110 if (!strcmp(var, "diff.color")) {
111 if (!value)
112 diff_use_color_default = 1; /* bool */
113 else if (!strcasecmp(value, "auto"))
114 diff_use_color_default = isatty(1);
115 else if (!strcasecmp(value, "never"))
116 diff_use_color_default = 0;
117 else if (!strcasecmp(value, "always"))
118 diff_use_color_default = 1;
119 else
120 diff_use_color_default = git_config_bool(var, value);
121 return 0;
123 if (!strncmp(var, "diff.color.", 11)) {
124 int slot = parse_diff_color_slot(var, 11);
125 diff_colors[slot] = parse_diff_color_value(value, var);
126 return 0;
128 return git_default_config(var, value);
131 static char *quote_one(const char *str)
133 int needlen;
134 char *xp;
136 if (!str)
137 return NULL;
138 needlen = quote_c_style(str, NULL, NULL, 0);
139 if (!needlen)
140 return strdup(str);
141 xp = xmalloc(needlen + 1);
142 quote_c_style(str, xp, NULL, 0);
143 return xp;
146 static char *quote_two(const char *one, const char *two)
148 int need_one = quote_c_style(one, NULL, NULL, 1);
149 int need_two = quote_c_style(two, NULL, NULL, 1);
150 char *xp;
152 if (need_one + need_two) {
153 if (!need_one) need_one = strlen(one);
154 if (!need_two) need_one = strlen(two);
156 xp = xmalloc(need_one + need_two + 3);
157 xp[0] = '"';
158 quote_c_style(one, xp + 1, NULL, 1);
159 quote_c_style(two, xp + need_one + 1, NULL, 1);
160 strcpy(xp + need_one + need_two + 1, "\"");
161 return xp;
163 need_one = strlen(one);
164 need_two = strlen(two);
165 xp = xmalloc(need_one + need_two + 1);
166 strcpy(xp, one);
167 strcpy(xp + need_one, two);
168 return xp;
171 static const char *external_diff(void)
173 static const char *external_diff_cmd = NULL;
174 static int done_preparing = 0;
176 if (done_preparing)
177 return external_diff_cmd;
178 external_diff_cmd = getenv("GIT_EXTERNAL_DIFF");
179 done_preparing = 1;
180 return external_diff_cmd;
183 #define TEMPFILE_PATH_LEN 50
185 static struct diff_tempfile {
186 const char *name; /* filename external diff should read from */
187 char hex[41];
188 char mode[10];
189 char tmp_path[TEMPFILE_PATH_LEN];
190 } diff_temp[2];
192 static int count_lines(const char *data, int size)
194 int count, ch, completely_empty = 1, nl_just_seen = 0;
195 count = 0;
196 while (0 < size--) {
197 ch = *data++;
198 if (ch == '\n') {
199 count++;
200 nl_just_seen = 1;
201 completely_empty = 0;
203 else {
204 nl_just_seen = 0;
205 completely_empty = 0;
208 if (completely_empty)
209 return 0;
210 if (!nl_just_seen)
211 count++; /* no trailing newline */
212 return count;
215 static void print_line_count(int count)
217 switch (count) {
218 case 0:
219 printf("0,0");
220 break;
221 case 1:
222 printf("1");
223 break;
224 default:
225 printf("1,%d", count);
226 break;
230 static void copy_file(int prefix, const char *data, int size)
232 int ch, nl_just_seen = 1;
233 while (0 < size--) {
234 ch = *data++;
235 if (nl_just_seen)
236 putchar(prefix);
237 putchar(ch);
238 if (ch == '\n')
239 nl_just_seen = 1;
240 else
241 nl_just_seen = 0;
243 if (!nl_just_seen)
244 printf("\n\\ No newline at end of file\n");
247 static void emit_rewrite_diff(const char *name_a,
248 const char *name_b,
249 struct diff_filespec *one,
250 struct diff_filespec *two)
252 int lc_a, lc_b;
253 diff_populate_filespec(one, 0);
254 diff_populate_filespec(two, 0);
255 lc_a = count_lines(one->data, one->size);
256 lc_b = count_lines(two->data, two->size);
257 printf("--- %s\n+++ %s\n@@ -", name_a, name_b);
258 print_line_count(lc_a);
259 printf(" +");
260 print_line_count(lc_b);
261 printf(" @@\n");
262 if (lc_a)
263 copy_file('-', one->data, one->size);
264 if (lc_b)
265 copy_file('+', two->data, two->size);
268 static int fill_mmfile(mmfile_t *mf, struct diff_filespec *one)
270 if (!DIFF_FILE_VALID(one)) {
271 mf->ptr = (char *)""; /* does not matter */
272 mf->size = 0;
273 return 0;
275 else if (diff_populate_filespec(one, 0))
276 return -1;
277 mf->ptr = one->data;
278 mf->size = one->size;
279 return 0;
282 struct emit_callback {
283 struct xdiff_emit_state xm;
284 int nparents, color_diff;
285 const char **label_path;
288 static inline const char *get_color(int diff_use_color, enum color_diff ix)
290 if (diff_use_color)
291 return diff_colors[ix];
292 return "";
295 static void fn_out_consume(void *priv, char *line, unsigned long len)
297 int i;
298 struct emit_callback *ecbdata = priv;
299 const char *set = get_color(ecbdata->color_diff, DIFF_METAINFO);
300 const char *reset = get_color(ecbdata->color_diff, DIFF_RESET);
302 if (ecbdata->label_path[0]) {
303 printf("%s--- %s%s\n", set, ecbdata->label_path[0], reset);
304 printf("%s+++ %s%s\n", set, ecbdata->label_path[1], reset);
305 ecbdata->label_path[0] = ecbdata->label_path[1] = NULL;
308 /* This is not really necessary for now because
309 * this codepath only deals with two-way diffs.
311 for (i = 0; i < len && line[i] == '@'; i++)
313 if (2 <= i && i < len && line[i] == ' ') {
314 ecbdata->nparents = i - 1;
315 set = get_color(ecbdata->color_diff, DIFF_FRAGINFO);
317 else if (len < ecbdata->nparents)
318 set = reset;
319 else {
320 int nparents = ecbdata->nparents;
321 int color = DIFF_PLAIN;
322 for (i = 0; i < nparents && len; i++) {
323 if (line[i] == '-')
324 color = DIFF_FILE_OLD;
325 else if (line[i] == '+')
326 color = DIFF_FILE_NEW;
328 set = get_color(ecbdata->color_diff, color);
330 if (len > 0 && line[len-1] == '\n')
331 len--;
332 fputs (set, stdout);
333 fwrite (line, len, 1, stdout);
334 puts (reset);
337 static char *pprint_rename(const char *a, const char *b)
339 const char *old = a;
340 const char *new = b;
341 char *name = NULL;
342 int pfx_length, sfx_length;
343 int len_a = strlen(a);
344 int len_b = strlen(b);
346 /* Find common prefix */
347 pfx_length = 0;
348 while (*old && *new && *old == *new) {
349 if (*old == '/')
350 pfx_length = old - a + 1;
351 old++;
352 new++;
355 /* Find common suffix */
356 old = a + len_a;
357 new = b + len_b;
358 sfx_length = 0;
359 while (a <= old && b <= new && *old == *new) {
360 if (*old == '/')
361 sfx_length = len_a - (old - a);
362 old--;
363 new--;
367 * pfx{mid-a => mid-b}sfx
368 * {pfx-a => pfx-b}sfx
369 * pfx{sfx-a => sfx-b}
370 * name-a => name-b
372 if (pfx_length + sfx_length) {
373 int a_midlen = len_a - pfx_length - sfx_length;
374 int b_midlen = len_b - pfx_length - sfx_length;
375 if (a_midlen < 0) a_midlen = 0;
376 if (b_midlen < 0) b_midlen = 0;
378 name = xmalloc(pfx_length + a_midlen + b_midlen + sfx_length + 7);
379 sprintf(name, "%.*s{%.*s => %.*s}%s",
380 pfx_length, a,
381 a_midlen, a + pfx_length,
382 b_midlen, b + pfx_length,
383 a + len_a - sfx_length);
385 else {
386 name = xmalloc(len_a + len_b + 5);
387 sprintf(name, "%s => %s", a, b);
389 return name;
392 struct diffstat_t {
393 struct xdiff_emit_state xm;
395 int nr;
396 int alloc;
397 struct diffstat_file {
398 char *name;
399 unsigned is_unmerged:1;
400 unsigned is_binary:1;
401 unsigned is_renamed:1;
402 unsigned int added, deleted;
403 } **files;
406 static struct diffstat_file *diffstat_add(struct diffstat_t *diffstat,
407 const char *name_a,
408 const char *name_b)
410 struct diffstat_file *x;
411 x = xcalloc(sizeof (*x), 1);
412 if (diffstat->nr == diffstat->alloc) {
413 diffstat->alloc = alloc_nr(diffstat->alloc);
414 diffstat->files = xrealloc(diffstat->files,
415 diffstat->alloc * sizeof(x));
417 diffstat->files[diffstat->nr++] = x;
418 if (name_b) {
419 x->name = pprint_rename(name_a, name_b);
420 x->is_renamed = 1;
422 else
423 x->name = strdup(name_a);
424 return x;
427 static void diffstat_consume(void *priv, char *line, unsigned long len)
429 struct diffstat_t *diffstat = priv;
430 struct diffstat_file *x = diffstat->files[diffstat->nr - 1];
432 if (line[0] == '+')
433 x->added++;
434 else if (line[0] == '-')
435 x->deleted++;
438 static const char pluses[] = "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++";
439 static const char minuses[]= "----------------------------------------------------------------------";
440 const char mime_boundary_leader[] = "------------";
442 static void show_stats(struct diffstat_t* data)
444 int i, len, add, del, total, adds = 0, dels = 0;
445 int max, max_change = 0, max_len = 0;
446 int total_files = data->nr;
448 if (data->nr == 0)
449 return;
451 for (i = 0; i < data->nr; i++) {
452 struct diffstat_file *file = data->files[i];
454 len = strlen(file->name);
455 if (max_len < len)
456 max_len = len;
458 if (file->is_binary || file->is_unmerged)
459 continue;
460 if (max_change < file->added + file->deleted)
461 max_change = file->added + file->deleted;
464 for (i = 0; i < data->nr; i++) {
465 const char *prefix = "";
466 char *name = data->files[i]->name;
467 int added = data->files[i]->added;
468 int deleted = data->files[i]->deleted;
470 if (0 < (len = quote_c_style(name, NULL, NULL, 0))) {
471 char *qname = xmalloc(len + 1);
472 quote_c_style(name, qname, NULL, 0);
473 free(name);
474 data->files[i]->name = name = qname;
478 * "scale" the filename
480 len = strlen(name);
481 max = max_len;
482 if (max > 50)
483 max = 50;
484 if (len > max) {
485 char *slash;
486 prefix = "...";
487 max -= 3;
488 name += len - max;
489 slash = strchr(name, '/');
490 if (slash)
491 name = slash;
493 len = max;
496 * scale the add/delete
498 max = max_change;
499 if (max + len > 70)
500 max = 70 - len;
502 if (data->files[i]->is_binary) {
503 printf(" %s%-*s | Bin\n", prefix, len, name);
504 goto free_diffstat_file;
506 else if (data->files[i]->is_unmerged) {
507 printf(" %s%-*s | Unmerged\n", prefix, len, name);
508 goto free_diffstat_file;
510 else if (!data->files[i]->is_renamed &&
511 (added + deleted == 0)) {
512 total_files--;
513 goto free_diffstat_file;
516 add = added;
517 del = deleted;
518 total = add + del;
519 adds += add;
520 dels += del;
522 if (max_change > 0) {
523 total = (total * max + max_change / 2) / max_change;
524 add = (add * max + max_change / 2) / max_change;
525 del = total - add;
527 printf(" %s%-*s |%5d %.*s%.*s\n", prefix,
528 len, name, added + deleted,
529 add, pluses, del, minuses);
530 free_diffstat_file:
531 free(data->files[i]->name);
532 free(data->files[i]);
534 free(data->files);
535 printf(" %d files changed, %d insertions(+), %d deletions(-)\n",
536 total_files, adds, dels);
539 struct checkdiff_t {
540 struct xdiff_emit_state xm;
541 const char *filename;
542 int lineno;
545 static void checkdiff_consume(void *priv, char *line, unsigned long len)
547 struct checkdiff_t *data = priv;
549 if (line[0] == '+') {
550 int i, spaces = 0;
552 data->lineno++;
554 /* check space before tab */
555 for (i = 1; i < len && (line[i] == ' ' || line[i] == '\t'); i++)
556 if (line[i] == ' ')
557 spaces++;
558 if (line[i - 1] == '\t' && spaces)
559 printf("%s:%d: space before tab:%.*s\n",
560 data->filename, data->lineno, (int)len, line);
562 /* check white space at line end */
563 if (line[len - 1] == '\n')
564 len--;
565 if (isspace(line[len - 1]))
566 printf("%s:%d: white space at end: %.*s\n",
567 data->filename, data->lineno, (int)len, line);
568 } else if (line[0] == ' ')
569 data->lineno++;
570 else if (line[0] == '@') {
571 char *plus = strchr(line, '+');
572 if (plus)
573 data->lineno = strtol(plus, NULL, 10);
574 else
575 die("invalid diff");
579 static unsigned char *deflate_it(char *data,
580 unsigned long size,
581 unsigned long *result_size)
583 int bound;
584 unsigned char *deflated;
585 z_stream stream;
587 memset(&stream, 0, sizeof(stream));
588 deflateInit(&stream, zlib_compression_level);
589 bound = deflateBound(&stream, size);
590 deflated = xmalloc(bound);
591 stream.next_out = deflated;
592 stream.avail_out = bound;
594 stream.next_in = (unsigned char *)data;
595 stream.avail_in = size;
596 while (deflate(&stream, Z_FINISH) == Z_OK)
597 ; /* nothing */
598 deflateEnd(&stream);
599 *result_size = stream.total_out;
600 return deflated;
603 static void emit_binary_diff(mmfile_t *one, mmfile_t *two)
605 void *cp;
606 void *delta;
607 void *deflated;
608 void *data;
609 unsigned long orig_size;
610 unsigned long delta_size;
611 unsigned long deflate_size;
612 unsigned long data_size;
614 printf("GIT binary patch\n");
615 /* We could do deflated delta, or we could do just deflated two,
616 * whichever is smaller.
618 delta = NULL;
619 deflated = deflate_it(two->ptr, two->size, &deflate_size);
620 if (one->size && two->size) {
621 delta = diff_delta(one->ptr, one->size,
622 two->ptr, two->size,
623 &delta_size, deflate_size);
624 if (delta) {
625 void *to_free = delta;
626 orig_size = delta_size;
627 delta = deflate_it(delta, delta_size, &delta_size);
628 free(to_free);
632 if (delta && delta_size < deflate_size) {
633 printf("delta %lu\n", orig_size);
634 free(deflated);
635 data = delta;
636 data_size = delta_size;
638 else {
639 printf("literal %lu\n", two->size);
640 free(delta);
641 data = deflated;
642 data_size = deflate_size;
645 /* emit data encoded in base85 */
646 cp = data;
647 while (data_size) {
648 int bytes = (52 < data_size) ? 52 : data_size;
649 char line[70];
650 data_size -= bytes;
651 if (bytes <= 26)
652 line[0] = bytes + 'A' - 1;
653 else
654 line[0] = bytes - 26 + 'a' - 1;
655 encode_85(line + 1, cp, bytes);
656 cp = (char *) cp + bytes;
657 puts(line);
659 printf("\n");
660 free(data);
663 #define FIRST_FEW_BYTES 8000
664 static int mmfile_is_binary(mmfile_t *mf)
666 long sz = mf->size;
667 if (FIRST_FEW_BYTES < sz)
668 sz = FIRST_FEW_BYTES;
669 if (memchr(mf->ptr, 0, sz))
670 return 1;
671 return 0;
674 static void builtin_diff(const char *name_a,
675 const char *name_b,
676 struct diff_filespec *one,
677 struct diff_filespec *two,
678 const char *xfrm_msg,
679 struct diff_options *o,
680 int complete_rewrite)
682 mmfile_t mf1, mf2;
683 const char *lbl[2];
684 char *a_one, *b_two;
685 const char *set = get_color(o->color_diff, DIFF_METAINFO);
686 const char *reset = get_color(o->color_diff, DIFF_RESET);
688 a_one = quote_two("a/", name_a);
689 b_two = quote_two("b/", name_b);
690 lbl[0] = DIFF_FILE_VALID(one) ? a_one : "/dev/null";
691 lbl[1] = DIFF_FILE_VALID(two) ? b_two : "/dev/null";
692 printf("%sdiff --git %s %s%s\n", set, a_one, b_two, reset);
693 if (lbl[0][0] == '/') {
694 /* /dev/null */
695 printf("%snew file mode %06o%s\n", set, two->mode, reset);
696 if (xfrm_msg && xfrm_msg[0])
697 printf("%s%s%s\n", set, xfrm_msg, reset);
699 else if (lbl[1][0] == '/') {
700 printf("%sdeleted file mode %06o%s\n", set, one->mode, reset);
701 if (xfrm_msg && xfrm_msg[0])
702 printf("%s%s%s\n", set, xfrm_msg, reset);
704 else {
705 if (one->mode != two->mode) {
706 printf("%sold mode %06o%s\n", set, one->mode, reset);
707 printf("%snew mode %06o%s\n", set, two->mode, reset);
709 if (xfrm_msg && xfrm_msg[0])
710 printf("%s%s%s\n", set, xfrm_msg, reset);
712 * we do not run diff between different kind
713 * of objects.
715 if ((one->mode ^ two->mode) & S_IFMT)
716 goto free_ab_and_return;
717 if (complete_rewrite) {
718 emit_rewrite_diff(name_a, name_b, one, two);
719 goto free_ab_and_return;
723 if (fill_mmfile(&mf1, one) < 0 || fill_mmfile(&mf2, two) < 0)
724 die("unable to read files to diff");
726 if (!o->text && (mmfile_is_binary(&mf1) || mmfile_is_binary(&mf2))) {
727 /* Quite common confusing case */
728 if (mf1.size == mf2.size &&
729 !memcmp(mf1.ptr, mf2.ptr, mf1.size))
730 goto free_ab_and_return;
731 if (o->binary)
732 emit_binary_diff(&mf1, &mf2);
733 else
734 printf("Binary files %s and %s differ\n",
735 lbl[0], lbl[1]);
737 else {
738 /* Crazy xdl interfaces.. */
739 const char *diffopts = getenv("GIT_DIFF_OPTS");
740 xpparam_t xpp;
741 xdemitconf_t xecfg;
742 xdemitcb_t ecb;
743 struct emit_callback ecbdata;
745 memset(&ecbdata, 0, sizeof(ecbdata));
746 ecbdata.label_path = lbl;
747 ecbdata.color_diff = o->color_diff;
748 xpp.flags = XDF_NEED_MINIMAL | o->xdl_opts;
749 xecfg.ctxlen = o->context;
750 xecfg.flags = XDL_EMIT_FUNCNAMES;
751 if (!diffopts)
753 else if (!strncmp(diffopts, "--unified=", 10))
754 xecfg.ctxlen = strtoul(diffopts + 10, NULL, 10);
755 else if (!strncmp(diffopts, "-u", 2))
756 xecfg.ctxlen = strtoul(diffopts + 2, NULL, 10);
757 ecb.outf = xdiff_outf;
758 ecb.priv = &ecbdata;
759 ecbdata.xm.consume = fn_out_consume;
760 xdl_diff(&mf1, &mf2, &xpp, &xecfg, &ecb);
763 free_ab_and_return:
764 free(a_one);
765 free(b_two);
766 return;
769 static void builtin_diffstat(const char *name_a, const char *name_b,
770 struct diff_filespec *one,
771 struct diff_filespec *two,
772 struct diffstat_t *diffstat,
773 struct diff_options *o,
774 int complete_rewrite)
776 mmfile_t mf1, mf2;
777 struct diffstat_file *data;
779 data = diffstat_add(diffstat, name_a, name_b);
781 if (!one || !two) {
782 data->is_unmerged = 1;
783 return;
785 if (complete_rewrite) {
786 diff_populate_filespec(one, 0);
787 diff_populate_filespec(two, 0);
788 data->deleted = count_lines(one->data, one->size);
789 data->added = count_lines(two->data, two->size);
790 return;
792 if (fill_mmfile(&mf1, one) < 0 || fill_mmfile(&mf2, two) < 0)
793 die("unable to read files to diff");
795 if (mmfile_is_binary(&mf1) || mmfile_is_binary(&mf2))
796 data->is_binary = 1;
797 else {
798 /* Crazy xdl interfaces.. */
799 xpparam_t xpp;
800 xdemitconf_t xecfg;
801 xdemitcb_t ecb;
803 xpp.flags = XDF_NEED_MINIMAL | o->xdl_opts;
804 xecfg.ctxlen = 0;
805 xecfg.flags = 0;
806 ecb.outf = xdiff_outf;
807 ecb.priv = diffstat;
808 xdl_diff(&mf1, &mf2, &xpp, &xecfg, &ecb);
812 static void builtin_checkdiff(const char *name_a, const char *name_b,
813 struct diff_filespec *one,
814 struct diff_filespec *two)
816 mmfile_t mf1, mf2;
817 struct checkdiff_t data;
819 if (!two)
820 return;
822 memset(&data, 0, sizeof(data));
823 data.xm.consume = checkdiff_consume;
824 data.filename = name_b ? name_b : name_a;
825 data.lineno = 0;
827 if (fill_mmfile(&mf1, one) < 0 || fill_mmfile(&mf2, two) < 0)
828 die("unable to read files to diff");
830 if (mmfile_is_binary(&mf2))
831 return;
832 else {
833 /* Crazy xdl interfaces.. */
834 xpparam_t xpp;
835 xdemitconf_t xecfg;
836 xdemitcb_t ecb;
838 xpp.flags = XDF_NEED_MINIMAL;
839 xecfg.ctxlen = 0;
840 xecfg.flags = 0;
841 ecb.outf = xdiff_outf;
842 ecb.priv = &data;
843 xdl_diff(&mf1, &mf2, &xpp, &xecfg, &ecb);
847 struct diff_filespec *alloc_filespec(const char *path)
849 int namelen = strlen(path);
850 struct diff_filespec *spec = xmalloc(sizeof(*spec) + namelen + 1);
852 memset(spec, 0, sizeof(*spec));
853 spec->path = (char *)(spec + 1);
854 memcpy(spec->path, path, namelen+1);
855 return spec;
858 void fill_filespec(struct diff_filespec *spec, const unsigned char *sha1,
859 unsigned short mode)
861 if (mode) {
862 spec->mode = canon_mode(mode);
863 memcpy(spec->sha1, sha1, 20);
864 spec->sha1_valid = !!memcmp(sha1, null_sha1, 20);
869 * Given a name and sha1 pair, if the dircache tells us the file in
870 * the work tree has that object contents, return true, so that
871 * prepare_temp_file() does not have to inflate and extract.
873 static int work_tree_matches(const char *name, const unsigned char *sha1)
875 struct cache_entry *ce;
876 struct stat st;
877 int pos, len;
879 /* We do not read the cache ourselves here, because the
880 * benchmark with my previous version that always reads cache
881 * shows that it makes things worse for diff-tree comparing
882 * two linux-2.6 kernel trees in an already checked out work
883 * tree. This is because most diff-tree comparisons deal with
884 * only a small number of files, while reading the cache is
885 * expensive for a large project, and its cost outweighs the
886 * savings we get by not inflating the object to a temporary
887 * file. Practically, this code only helps when we are used
888 * by diff-cache --cached, which does read the cache before
889 * calling us.
891 if (!active_cache)
892 return 0;
894 len = strlen(name);
895 pos = cache_name_pos(name, len);
896 if (pos < 0)
897 return 0;
898 ce = active_cache[pos];
899 if ((lstat(name, &st) < 0) ||
900 !S_ISREG(st.st_mode) || /* careful! */
901 ce_match_stat(ce, &st, 0) ||
902 memcmp(sha1, ce->sha1, 20))
903 return 0;
904 /* we return 1 only when we can stat, it is a regular file,
905 * stat information matches, and sha1 recorded in the cache
906 * matches. I.e. we know the file in the work tree really is
907 * the same as the <name, sha1> pair.
909 return 1;
912 static struct sha1_size_cache {
913 unsigned char sha1[20];
914 unsigned long size;
915 } **sha1_size_cache;
916 static int sha1_size_cache_nr, sha1_size_cache_alloc;
918 static struct sha1_size_cache *locate_size_cache(unsigned char *sha1,
919 int find_only,
920 unsigned long size)
922 int first, last;
923 struct sha1_size_cache *e;
925 first = 0;
926 last = sha1_size_cache_nr;
927 while (last > first) {
928 int cmp, next = (last + first) >> 1;
929 e = sha1_size_cache[next];
930 cmp = memcmp(e->sha1, sha1, 20);
931 if (!cmp)
932 return e;
933 if (cmp < 0) {
934 last = next;
935 continue;
937 first = next+1;
939 /* not found */
940 if (find_only)
941 return NULL;
942 /* insert to make it at "first" */
943 if (sha1_size_cache_alloc <= sha1_size_cache_nr) {
944 sha1_size_cache_alloc = alloc_nr(sha1_size_cache_alloc);
945 sha1_size_cache = xrealloc(sha1_size_cache,
946 sha1_size_cache_alloc *
947 sizeof(*sha1_size_cache));
949 sha1_size_cache_nr++;
950 if (first < sha1_size_cache_nr)
951 memmove(sha1_size_cache + first + 1, sha1_size_cache + first,
952 (sha1_size_cache_nr - first - 1) *
953 sizeof(*sha1_size_cache));
954 e = xmalloc(sizeof(struct sha1_size_cache));
955 sha1_size_cache[first] = e;
956 memcpy(e->sha1, sha1, 20);
957 e->size = size;
958 return e;
962 * While doing rename detection and pickaxe operation, we may need to
963 * grab the data for the blob (or file) for our own in-core comparison.
964 * diff_filespec has data and size fields for this purpose.
966 int diff_populate_filespec(struct diff_filespec *s, int size_only)
968 int err = 0;
969 if (!DIFF_FILE_VALID(s))
970 die("internal error: asking to populate invalid file.");
971 if (S_ISDIR(s->mode))
972 return -1;
974 if (!use_size_cache)
975 size_only = 0;
977 if (s->data)
978 return err;
979 if (!s->sha1_valid ||
980 work_tree_matches(s->path, s->sha1)) {
981 struct stat st;
982 int fd;
983 if (lstat(s->path, &st) < 0) {
984 if (errno == ENOENT) {
985 err_empty:
986 err = -1;
987 empty:
988 s->data = (char *)"";
989 s->size = 0;
990 return err;
993 s->size = st.st_size;
994 if (!s->size)
995 goto empty;
996 if (size_only)
997 return 0;
998 if (S_ISLNK(st.st_mode)) {
999 int ret;
1000 s->data = xmalloc(s->size);
1001 s->should_free = 1;
1002 ret = readlink(s->path, s->data, s->size);
1003 if (ret < 0) {
1004 free(s->data);
1005 goto err_empty;
1007 return 0;
1009 fd = open(s->path, O_RDONLY);
1010 if (fd < 0)
1011 goto err_empty;
1012 s->data = mmap(NULL, s->size, PROT_READ, MAP_PRIVATE, fd, 0);
1013 close(fd);
1014 if (s->data == MAP_FAILED)
1015 goto err_empty;
1016 s->should_munmap = 1;
1018 else {
1019 char type[20];
1020 struct sha1_size_cache *e;
1022 if (size_only) {
1023 e = locate_size_cache(s->sha1, 1, 0);
1024 if (e) {
1025 s->size = e->size;
1026 return 0;
1028 if (!sha1_object_info(s->sha1, type, &s->size))
1029 locate_size_cache(s->sha1, 0, s->size);
1031 else {
1032 s->data = read_sha1_file(s->sha1, type, &s->size);
1033 s->should_free = 1;
1036 return 0;
1039 void diff_free_filespec_data(struct diff_filespec *s)
1041 if (s->should_free)
1042 free(s->data);
1043 else if (s->should_munmap)
1044 munmap(s->data, s->size);
1045 s->should_free = s->should_munmap = 0;
1046 s->data = NULL;
1047 free(s->cnt_data);
1048 s->cnt_data = NULL;
1051 static void prep_temp_blob(struct diff_tempfile *temp,
1052 void *blob,
1053 unsigned long size,
1054 const unsigned char *sha1,
1055 int mode)
1057 int fd;
1059 fd = git_mkstemp(temp->tmp_path, TEMPFILE_PATH_LEN, ".diff_XXXXXX");
1060 if (fd < 0)
1061 die("unable to create temp-file");
1062 if (write(fd, blob, size) != size)
1063 die("unable to write temp-file");
1064 close(fd);
1065 temp->name = temp->tmp_path;
1066 strcpy(temp->hex, sha1_to_hex(sha1));
1067 temp->hex[40] = 0;
1068 sprintf(temp->mode, "%06o", mode);
1071 static void prepare_temp_file(const char *name,
1072 struct diff_tempfile *temp,
1073 struct diff_filespec *one)
1075 if (!DIFF_FILE_VALID(one)) {
1076 not_a_valid_file:
1077 /* A '-' entry produces this for file-2, and
1078 * a '+' entry produces this for file-1.
1080 temp->name = "/dev/null";
1081 strcpy(temp->hex, ".");
1082 strcpy(temp->mode, ".");
1083 return;
1086 if (!one->sha1_valid ||
1087 work_tree_matches(name, one->sha1)) {
1088 struct stat st;
1089 if (lstat(name, &st) < 0) {
1090 if (errno == ENOENT)
1091 goto not_a_valid_file;
1092 die("stat(%s): %s", name, strerror(errno));
1094 if (S_ISLNK(st.st_mode)) {
1095 int ret;
1096 char buf[PATH_MAX + 1]; /* ought to be SYMLINK_MAX */
1097 if (sizeof(buf) <= st.st_size)
1098 die("symlink too long: %s", name);
1099 ret = readlink(name, buf, st.st_size);
1100 if (ret < 0)
1101 die("readlink(%s)", name);
1102 prep_temp_blob(temp, buf, st.st_size,
1103 (one->sha1_valid ?
1104 one->sha1 : null_sha1),
1105 (one->sha1_valid ?
1106 one->mode : S_IFLNK));
1108 else {
1109 /* we can borrow from the file in the work tree */
1110 temp->name = name;
1111 if (!one->sha1_valid)
1112 strcpy(temp->hex, sha1_to_hex(null_sha1));
1113 else
1114 strcpy(temp->hex, sha1_to_hex(one->sha1));
1115 /* Even though we may sometimes borrow the
1116 * contents from the work tree, we always want
1117 * one->mode. mode is trustworthy even when
1118 * !(one->sha1_valid), as long as
1119 * DIFF_FILE_VALID(one).
1121 sprintf(temp->mode, "%06o", one->mode);
1123 return;
1125 else {
1126 if (diff_populate_filespec(one, 0))
1127 die("cannot read data blob for %s", one->path);
1128 prep_temp_blob(temp, one->data, one->size,
1129 one->sha1, one->mode);
1133 static void remove_tempfile(void)
1135 int i;
1137 for (i = 0; i < 2; i++)
1138 if (diff_temp[i].name == diff_temp[i].tmp_path) {
1139 unlink(diff_temp[i].name);
1140 diff_temp[i].name = NULL;
1144 static void remove_tempfile_on_signal(int signo)
1146 remove_tempfile();
1147 signal(SIGINT, SIG_DFL);
1148 raise(signo);
1151 static int spawn_prog(const char *pgm, const char **arg)
1153 pid_t pid;
1154 int status;
1156 fflush(NULL);
1157 pid = fork();
1158 if (pid < 0)
1159 die("unable to fork");
1160 if (!pid) {
1161 execvp(pgm, (char *const*) arg);
1162 exit(255);
1165 while (waitpid(pid, &status, 0) < 0) {
1166 if (errno == EINTR)
1167 continue;
1168 return -1;
1171 /* Earlier we did not check the exit status because
1172 * diff exits non-zero if files are different, and
1173 * we are not interested in knowing that. It was a
1174 * mistake which made it harder to quit a diff-*
1175 * session that uses the git-apply-patch-script as
1176 * the GIT_EXTERNAL_DIFF. A custom GIT_EXTERNAL_DIFF
1177 * should also exit non-zero only when it wants to
1178 * abort the entire diff-* session.
1180 if (WIFEXITED(status) && !WEXITSTATUS(status))
1181 return 0;
1182 return -1;
1185 /* An external diff command takes:
1187 * diff-cmd name infile1 infile1-sha1 infile1-mode \
1188 * infile2 infile2-sha1 infile2-mode [ rename-to ]
1191 static void run_external_diff(const char *pgm,
1192 const char *name,
1193 const char *other,
1194 struct diff_filespec *one,
1195 struct diff_filespec *two,
1196 const char *xfrm_msg,
1197 int complete_rewrite)
1199 const char *spawn_arg[10];
1200 struct diff_tempfile *temp = diff_temp;
1201 int retval;
1202 static int atexit_asked = 0;
1203 const char *othername;
1204 const char **arg = &spawn_arg[0];
1206 othername = (other? other : name);
1207 if (one && two) {
1208 prepare_temp_file(name, &temp[0], one);
1209 prepare_temp_file(othername, &temp[1], two);
1210 if (! atexit_asked &&
1211 (temp[0].name == temp[0].tmp_path ||
1212 temp[1].name == temp[1].tmp_path)) {
1213 atexit_asked = 1;
1214 atexit(remove_tempfile);
1216 signal(SIGINT, remove_tempfile_on_signal);
1219 if (one && two) {
1220 *arg++ = pgm;
1221 *arg++ = name;
1222 *arg++ = temp[0].name;
1223 *arg++ = temp[0].hex;
1224 *arg++ = temp[0].mode;
1225 *arg++ = temp[1].name;
1226 *arg++ = temp[1].hex;
1227 *arg++ = temp[1].mode;
1228 if (other) {
1229 *arg++ = other;
1230 *arg++ = xfrm_msg;
1232 } else {
1233 *arg++ = pgm;
1234 *arg++ = name;
1236 *arg = NULL;
1237 retval = spawn_prog(pgm, spawn_arg);
1238 remove_tempfile();
1239 if (retval) {
1240 fprintf(stderr, "external diff died, stopping at %s.\n", name);
1241 exit(1);
1245 static void run_diff_cmd(const char *pgm,
1246 const char *name,
1247 const char *other,
1248 struct diff_filespec *one,
1249 struct diff_filespec *two,
1250 const char *xfrm_msg,
1251 struct diff_options *o,
1252 int complete_rewrite)
1254 if (pgm) {
1255 run_external_diff(pgm, name, other, one, two, xfrm_msg,
1256 complete_rewrite);
1257 return;
1259 if (one && two)
1260 builtin_diff(name, other ? other : name,
1261 one, two, xfrm_msg, o, complete_rewrite);
1262 else
1263 printf("* Unmerged path %s\n", name);
1266 static void diff_fill_sha1_info(struct diff_filespec *one)
1268 if (DIFF_FILE_VALID(one)) {
1269 if (!one->sha1_valid) {
1270 struct stat st;
1271 if (lstat(one->path, &st) < 0)
1272 die("stat %s", one->path);
1273 if (index_path(one->sha1, one->path, &st, 0))
1274 die("cannot hash %s\n", one->path);
1277 else
1278 memset(one->sha1, 0, 20);
1281 static void run_diff(struct diff_filepair *p, struct diff_options *o)
1283 const char *pgm = external_diff();
1284 char msg[PATH_MAX*2+300], *xfrm_msg;
1285 struct diff_filespec *one;
1286 struct diff_filespec *two;
1287 const char *name;
1288 const char *other;
1289 char *name_munged, *other_munged;
1290 int complete_rewrite = 0;
1291 int len;
1293 if (DIFF_PAIR_UNMERGED(p)) {
1294 /* unmerged */
1295 run_diff_cmd(pgm, p->one->path, NULL, NULL, NULL, NULL, o, 0);
1296 return;
1299 name = p->one->path;
1300 other = (strcmp(name, p->two->path) ? p->two->path : NULL);
1301 name_munged = quote_one(name);
1302 other_munged = quote_one(other);
1303 one = p->one; two = p->two;
1305 diff_fill_sha1_info(one);
1306 diff_fill_sha1_info(two);
1308 len = 0;
1309 switch (p->status) {
1310 case DIFF_STATUS_COPIED:
1311 len += snprintf(msg + len, sizeof(msg) - len,
1312 "similarity index %d%%\n"
1313 "copy from %s\n"
1314 "copy to %s\n",
1315 (int)(0.5 + p->score * 100.0/MAX_SCORE),
1316 name_munged, other_munged);
1317 break;
1318 case DIFF_STATUS_RENAMED:
1319 len += snprintf(msg + len, sizeof(msg) - len,
1320 "similarity index %d%%\n"
1321 "rename from %s\n"
1322 "rename to %s\n",
1323 (int)(0.5 + p->score * 100.0/MAX_SCORE),
1324 name_munged, other_munged);
1325 break;
1326 case DIFF_STATUS_MODIFIED:
1327 if (p->score) {
1328 len += snprintf(msg + len, sizeof(msg) - len,
1329 "dissimilarity index %d%%\n",
1330 (int)(0.5 + p->score *
1331 100.0/MAX_SCORE));
1332 complete_rewrite = 1;
1333 break;
1335 /* fallthru */
1336 default:
1337 /* nothing */
1341 if (memcmp(one->sha1, two->sha1, 20)) {
1342 int abbrev = o->full_index ? 40 : DEFAULT_ABBREV;
1344 len += snprintf(msg + len, sizeof(msg) - len,
1345 "index %.*s..%.*s",
1346 abbrev, sha1_to_hex(one->sha1),
1347 abbrev, sha1_to_hex(two->sha1));
1348 if (one->mode == two->mode)
1349 len += snprintf(msg + len, sizeof(msg) - len,
1350 " %06o", one->mode);
1351 len += snprintf(msg + len, sizeof(msg) - len, "\n");
1354 if (len)
1355 msg[--len] = 0;
1356 xfrm_msg = len ? msg : NULL;
1358 if (!pgm &&
1359 DIFF_FILE_VALID(one) && DIFF_FILE_VALID(two) &&
1360 (S_IFMT & one->mode) != (S_IFMT & two->mode)) {
1361 /* a filepair that changes between file and symlink
1362 * needs to be split into deletion and creation.
1364 struct diff_filespec *null = alloc_filespec(two->path);
1365 run_diff_cmd(NULL, name, other, one, null, xfrm_msg, o, 0);
1366 free(null);
1367 null = alloc_filespec(one->path);
1368 run_diff_cmd(NULL, name, other, null, two, xfrm_msg, o, 0);
1369 free(null);
1371 else
1372 run_diff_cmd(pgm, name, other, one, two, xfrm_msg, o,
1373 complete_rewrite);
1375 free(name_munged);
1376 free(other_munged);
1379 static void run_diffstat(struct diff_filepair *p, struct diff_options *o,
1380 struct diffstat_t *diffstat)
1382 const char *name;
1383 const char *other;
1384 int complete_rewrite = 0;
1386 if (DIFF_PAIR_UNMERGED(p)) {
1387 /* unmerged */
1388 builtin_diffstat(p->one->path, NULL, NULL, NULL, diffstat, o, 0);
1389 return;
1392 name = p->one->path;
1393 other = (strcmp(name, p->two->path) ? p->two->path : NULL);
1395 diff_fill_sha1_info(p->one);
1396 diff_fill_sha1_info(p->two);
1398 if (p->status == DIFF_STATUS_MODIFIED && p->score)
1399 complete_rewrite = 1;
1400 builtin_diffstat(name, other, p->one, p->two, diffstat, o, complete_rewrite);
1403 static void run_checkdiff(struct diff_filepair *p, struct diff_options *o)
1405 const char *name;
1406 const char *other;
1408 if (DIFF_PAIR_UNMERGED(p)) {
1409 /* unmerged */
1410 return;
1413 name = p->one->path;
1414 other = (strcmp(name, p->two->path) ? p->two->path : NULL);
1416 diff_fill_sha1_info(p->one);
1417 diff_fill_sha1_info(p->two);
1419 builtin_checkdiff(name, other, p->one, p->two);
1422 void diff_setup(struct diff_options *options)
1424 memset(options, 0, sizeof(*options));
1425 options->line_termination = '\n';
1426 options->break_opt = -1;
1427 options->rename_limit = -1;
1428 options->context = 3;
1429 options->msg_sep = "";
1431 options->change = diff_change;
1432 options->add_remove = diff_addremove;
1433 options->color_diff = diff_use_color_default;
1436 int diff_setup_done(struct diff_options *options)
1438 if ((options->find_copies_harder &&
1439 options->detect_rename != DIFF_DETECT_COPY) ||
1440 (0 <= options->rename_limit && !options->detect_rename))
1441 return -1;
1443 if (options->output_format & (DIFF_FORMAT_NAME |
1444 DIFF_FORMAT_NAME_STATUS |
1445 DIFF_FORMAT_CHECKDIFF |
1446 DIFF_FORMAT_NO_OUTPUT))
1447 options->output_format &= ~(DIFF_FORMAT_RAW |
1448 DIFF_FORMAT_DIFFSTAT |
1449 DIFF_FORMAT_SUMMARY |
1450 DIFF_FORMAT_PATCH);
1453 * These cases always need recursive; we do not drop caller-supplied
1454 * recursive bits for other formats here.
1456 if (options->output_format & (DIFF_FORMAT_PATCH |
1457 DIFF_FORMAT_DIFFSTAT |
1458 DIFF_FORMAT_CHECKDIFF))
1459 options->recursive = 1;
1461 * Also pickaxe would not work very well if you do not say recursive
1463 if (options->pickaxe)
1464 options->recursive = 1;
1466 if (options->detect_rename && options->rename_limit < 0)
1467 options->rename_limit = diff_rename_limit_default;
1468 if (options->setup & DIFF_SETUP_USE_CACHE) {
1469 if (!active_cache)
1470 /* read-cache does not die even when it fails
1471 * so it is safe for us to do this here. Also
1472 * it does not smudge active_cache or active_nr
1473 * when it fails, so we do not have to worry about
1474 * cleaning it up ourselves either.
1476 read_cache();
1478 if (options->setup & DIFF_SETUP_USE_SIZE_CACHE)
1479 use_size_cache = 1;
1480 if (options->abbrev <= 0 || 40 < options->abbrev)
1481 options->abbrev = 40; /* full */
1483 return 0;
1486 static int opt_arg(const char *arg, int arg_short, const char *arg_long, int *val)
1488 char c, *eq;
1489 int len;
1491 if (*arg != '-')
1492 return 0;
1493 c = *++arg;
1494 if (!c)
1495 return 0;
1496 if (c == arg_short) {
1497 c = *++arg;
1498 if (!c)
1499 return 1;
1500 if (val && isdigit(c)) {
1501 char *end;
1502 int n = strtoul(arg, &end, 10);
1503 if (*end)
1504 return 0;
1505 *val = n;
1506 return 1;
1508 return 0;
1510 if (c != '-')
1511 return 0;
1512 arg++;
1513 eq = strchr(arg, '=');
1514 if (eq)
1515 len = eq - arg;
1516 else
1517 len = strlen(arg);
1518 if (!len || strncmp(arg, arg_long, len))
1519 return 0;
1520 if (eq) {
1521 int n;
1522 char *end;
1523 if (!isdigit(*++eq))
1524 return 0;
1525 n = strtoul(eq, &end, 10);
1526 if (*end)
1527 return 0;
1528 *val = n;
1530 return 1;
1533 int diff_opt_parse(struct diff_options *options, const char **av, int ac)
1535 const char *arg = av[0];
1536 if (!strcmp(arg, "-p") || !strcmp(arg, "-u"))
1537 options->output_format |= DIFF_FORMAT_PATCH;
1538 else if (opt_arg(arg, 'U', "unified", &options->context))
1539 options->output_format |= DIFF_FORMAT_PATCH;
1540 else if (!strcmp(arg, "--raw"))
1541 options->output_format |= DIFF_FORMAT_RAW;
1542 else if (!strcmp(arg, "--patch-with-raw")) {
1543 options->output_format |= DIFF_FORMAT_PATCH | DIFF_FORMAT_RAW;
1545 else if (!strcmp(arg, "--stat"))
1546 options->output_format |= DIFF_FORMAT_DIFFSTAT;
1547 else if (!strcmp(arg, "--check"))
1548 options->output_format |= DIFF_FORMAT_CHECKDIFF;
1549 else if (!strcmp(arg, "--summary"))
1550 options->output_format |= DIFF_FORMAT_SUMMARY;
1551 else if (!strcmp(arg, "--patch-with-stat")) {
1552 options->output_format |= DIFF_FORMAT_PATCH | DIFF_FORMAT_DIFFSTAT;
1554 else if (!strcmp(arg, "-z"))
1555 options->line_termination = 0;
1556 else if (!strncmp(arg, "-l", 2))
1557 options->rename_limit = strtoul(arg+2, NULL, 10);
1558 else if (!strcmp(arg, "--full-index"))
1559 options->full_index = 1;
1560 else if (!strcmp(arg, "--binary")) {
1561 options->output_format |= DIFF_FORMAT_PATCH;
1562 options->full_index = options->binary = 1;
1564 else if (!strcmp(arg, "-a") || !strcmp(arg, "--text")) {
1565 options->text = 1;
1567 else if (!strcmp(arg, "--name-only"))
1568 options->output_format |= DIFF_FORMAT_NAME;
1569 else if (!strcmp(arg, "--name-status"))
1570 options->output_format |= DIFF_FORMAT_NAME_STATUS;
1571 else if (!strcmp(arg, "-R"))
1572 options->reverse_diff = 1;
1573 else if (!strncmp(arg, "-S", 2))
1574 options->pickaxe = arg + 2;
1575 else if (!strcmp(arg, "-s")) {
1576 options->output_format |= DIFF_FORMAT_NO_OUTPUT;
1578 else if (!strncmp(arg, "-O", 2))
1579 options->orderfile = arg + 2;
1580 else if (!strncmp(arg, "--diff-filter=", 14))
1581 options->filter = arg + 14;
1582 else if (!strcmp(arg, "--pickaxe-all"))
1583 options->pickaxe_opts = DIFF_PICKAXE_ALL;
1584 else if (!strcmp(arg, "--pickaxe-regex"))
1585 options->pickaxe_opts = DIFF_PICKAXE_REGEX;
1586 else if (!strncmp(arg, "-B", 2)) {
1587 if ((options->break_opt =
1588 diff_scoreopt_parse(arg)) == -1)
1589 return -1;
1591 else if (!strncmp(arg, "-M", 2)) {
1592 if ((options->rename_score =
1593 diff_scoreopt_parse(arg)) == -1)
1594 return -1;
1595 options->detect_rename = DIFF_DETECT_RENAME;
1597 else if (!strncmp(arg, "-C", 2)) {
1598 if ((options->rename_score =
1599 diff_scoreopt_parse(arg)) == -1)
1600 return -1;
1601 options->detect_rename = DIFF_DETECT_COPY;
1603 else if (!strcmp(arg, "--find-copies-harder"))
1604 options->find_copies_harder = 1;
1605 else if (!strcmp(arg, "--abbrev"))
1606 options->abbrev = DEFAULT_ABBREV;
1607 else if (!strncmp(arg, "--abbrev=", 9)) {
1608 options->abbrev = strtoul(arg + 9, NULL, 10);
1609 if (options->abbrev < MINIMUM_ABBREV)
1610 options->abbrev = MINIMUM_ABBREV;
1611 else if (40 < options->abbrev)
1612 options->abbrev = 40;
1614 else if (!strcmp(arg, "--color"))
1615 options->color_diff = 1;
1616 else if (!strcmp(arg, "-w") || !strcmp(arg, "--ignore-all-space"))
1617 options->xdl_opts |= XDF_IGNORE_WHITESPACE;
1618 else if (!strcmp(arg, "-b") || !strcmp(arg, "--ignore-space-change"))
1619 options->xdl_opts |= XDF_IGNORE_WHITESPACE_CHANGE;
1620 else
1621 return 0;
1622 return 1;
1625 static int parse_num(const char **cp_p)
1627 unsigned long num, scale;
1628 int ch, dot;
1629 const char *cp = *cp_p;
1631 num = 0;
1632 scale = 1;
1633 dot = 0;
1634 for(;;) {
1635 ch = *cp;
1636 if ( !dot && ch == '.' ) {
1637 scale = 1;
1638 dot = 1;
1639 } else if ( ch == '%' ) {
1640 scale = dot ? scale*100 : 100;
1641 cp++; /* % is always at the end */
1642 break;
1643 } else if ( ch >= '0' && ch <= '9' ) {
1644 if ( scale < 100000 ) {
1645 scale *= 10;
1646 num = (num*10) + (ch-'0');
1648 } else {
1649 break;
1651 cp++;
1653 *cp_p = cp;
1655 /* user says num divided by scale and we say internally that
1656 * is MAX_SCORE * num / scale.
1658 return (num >= scale) ? MAX_SCORE : (MAX_SCORE * num / scale);
1661 int diff_scoreopt_parse(const char *opt)
1663 int opt1, opt2, cmd;
1665 if (*opt++ != '-')
1666 return -1;
1667 cmd = *opt++;
1668 if (cmd != 'M' && cmd != 'C' && cmd != 'B')
1669 return -1; /* that is not a -M, -C nor -B option */
1671 opt1 = parse_num(&opt);
1672 if (cmd != 'B')
1673 opt2 = 0;
1674 else {
1675 if (*opt == 0)
1676 opt2 = 0;
1677 else if (*opt != '/')
1678 return -1; /* we expect -B80/99 or -B80 */
1679 else {
1680 opt++;
1681 opt2 = parse_num(&opt);
1684 if (*opt != 0)
1685 return -1;
1686 return opt1 | (opt2 << 16);
1689 struct diff_queue_struct diff_queued_diff;
1691 void diff_q(struct diff_queue_struct *queue, struct diff_filepair *dp)
1693 if (queue->alloc <= queue->nr) {
1694 queue->alloc = alloc_nr(queue->alloc);
1695 queue->queue = xrealloc(queue->queue,
1696 sizeof(dp) * queue->alloc);
1698 queue->queue[queue->nr++] = dp;
1701 struct diff_filepair *diff_queue(struct diff_queue_struct *queue,
1702 struct diff_filespec *one,
1703 struct diff_filespec *two)
1705 struct diff_filepair *dp = xmalloc(sizeof(*dp));
1706 dp->one = one;
1707 dp->two = two;
1708 dp->score = 0;
1709 dp->status = 0;
1710 dp->source_stays = 0;
1711 dp->broken_pair = 0;
1712 if (queue)
1713 diff_q(queue, dp);
1714 return dp;
1717 void diff_free_filepair(struct diff_filepair *p)
1719 diff_free_filespec_data(p->one);
1720 diff_free_filespec_data(p->two);
1721 free(p->one);
1722 free(p->two);
1723 free(p);
1726 /* This is different from find_unique_abbrev() in that
1727 * it stuffs the result with dots for alignment.
1729 const char *diff_unique_abbrev(const unsigned char *sha1, int len)
1731 int abblen;
1732 const char *abbrev;
1733 if (len == 40)
1734 return sha1_to_hex(sha1);
1736 abbrev = find_unique_abbrev(sha1, len);
1737 if (!abbrev)
1738 return sha1_to_hex(sha1);
1739 abblen = strlen(abbrev);
1740 if (abblen < 37) {
1741 static char hex[41];
1742 if (len < abblen && abblen <= len + 2)
1743 sprintf(hex, "%s%.*s", abbrev, len+3-abblen, "..");
1744 else
1745 sprintf(hex, "%s...", abbrev);
1746 return hex;
1748 return sha1_to_hex(sha1);
1751 static void diff_flush_raw(struct diff_filepair *p,
1752 struct diff_options *options)
1754 int two_paths;
1755 char status[10];
1756 int abbrev = options->abbrev;
1757 const char *path_one, *path_two;
1758 int inter_name_termination = '\t';
1759 int line_termination = options->line_termination;
1761 if (!line_termination)
1762 inter_name_termination = 0;
1764 path_one = p->one->path;
1765 path_two = p->two->path;
1766 if (line_termination) {
1767 path_one = quote_one(path_one);
1768 path_two = quote_one(path_two);
1771 if (p->score)
1772 sprintf(status, "%c%03d", p->status,
1773 (int)(0.5 + p->score * 100.0/MAX_SCORE));
1774 else {
1775 status[0] = p->status;
1776 status[1] = 0;
1778 switch (p->status) {
1779 case DIFF_STATUS_COPIED:
1780 case DIFF_STATUS_RENAMED:
1781 two_paths = 1;
1782 break;
1783 case DIFF_STATUS_ADDED:
1784 case DIFF_STATUS_DELETED:
1785 two_paths = 0;
1786 break;
1787 default:
1788 two_paths = 0;
1789 break;
1791 if (!(options->output_format & DIFF_FORMAT_NAME_STATUS)) {
1792 printf(":%06o %06o %s ",
1793 p->one->mode, p->two->mode,
1794 diff_unique_abbrev(p->one->sha1, abbrev));
1795 printf("%s ",
1796 diff_unique_abbrev(p->two->sha1, abbrev));
1798 printf("%s%c%s", status, inter_name_termination, path_one);
1799 if (two_paths)
1800 printf("%c%s", inter_name_termination, path_two);
1801 putchar(line_termination);
1802 if (path_one != p->one->path)
1803 free((void*)path_one);
1804 if (path_two != p->two->path)
1805 free((void*)path_two);
1808 static void diff_flush_name(struct diff_filepair *p, int line_termination)
1810 char *path = p->two->path;
1812 if (line_termination)
1813 path = quote_one(p->two->path);
1814 printf("%s%c", path, line_termination);
1815 if (p->two->path != path)
1816 free(path);
1819 int diff_unmodified_pair(struct diff_filepair *p)
1821 /* This function is written stricter than necessary to support
1822 * the currently implemented transformers, but the idea is to
1823 * let transformers to produce diff_filepairs any way they want,
1824 * and filter and clean them up here before producing the output.
1826 struct diff_filespec *one, *two;
1828 if (DIFF_PAIR_UNMERGED(p))
1829 return 0; /* unmerged is interesting */
1831 one = p->one;
1832 two = p->two;
1834 /* deletion, addition, mode or type change
1835 * and rename are all interesting.
1837 if (DIFF_FILE_VALID(one) != DIFF_FILE_VALID(two) ||
1838 DIFF_PAIR_MODE_CHANGED(p) ||
1839 strcmp(one->path, two->path))
1840 return 0;
1842 /* both are valid and point at the same path. that is, we are
1843 * dealing with a change.
1845 if (one->sha1_valid && two->sha1_valid &&
1846 !memcmp(one->sha1, two->sha1, sizeof(one->sha1)))
1847 return 1; /* no change */
1848 if (!one->sha1_valid && !two->sha1_valid)
1849 return 1; /* both look at the same file on the filesystem. */
1850 return 0;
1853 static void diff_flush_patch(struct diff_filepair *p, struct diff_options *o)
1855 if (diff_unmodified_pair(p))
1856 return;
1858 if ((DIFF_FILE_VALID(p->one) && S_ISDIR(p->one->mode)) ||
1859 (DIFF_FILE_VALID(p->two) && S_ISDIR(p->two->mode)))
1860 return; /* no tree diffs in patch format */
1862 run_diff(p, o);
1865 static void diff_flush_stat(struct diff_filepair *p, struct diff_options *o,
1866 struct diffstat_t *diffstat)
1868 if (diff_unmodified_pair(p))
1869 return;
1871 if ((DIFF_FILE_VALID(p->one) && S_ISDIR(p->one->mode)) ||
1872 (DIFF_FILE_VALID(p->two) && S_ISDIR(p->two->mode)))
1873 return; /* no tree diffs in patch format */
1875 run_diffstat(p, o, diffstat);
1878 static void diff_flush_checkdiff(struct diff_filepair *p,
1879 struct diff_options *o)
1881 if (diff_unmodified_pair(p))
1882 return;
1884 if ((DIFF_FILE_VALID(p->one) && S_ISDIR(p->one->mode)) ||
1885 (DIFF_FILE_VALID(p->two) && S_ISDIR(p->two->mode)))
1886 return; /* no tree diffs in patch format */
1888 run_checkdiff(p, o);
1891 int diff_queue_is_empty(void)
1893 struct diff_queue_struct *q = &diff_queued_diff;
1894 int i;
1895 for (i = 0; i < q->nr; i++)
1896 if (!diff_unmodified_pair(q->queue[i]))
1897 return 0;
1898 return 1;
1901 #if DIFF_DEBUG
1902 void diff_debug_filespec(struct diff_filespec *s, int x, const char *one)
1904 fprintf(stderr, "queue[%d] %s (%s) %s %06o %s\n",
1905 x, one ? one : "",
1906 s->path,
1907 DIFF_FILE_VALID(s) ? "valid" : "invalid",
1908 s->mode,
1909 s->sha1_valid ? sha1_to_hex(s->sha1) : "");
1910 fprintf(stderr, "queue[%d] %s size %lu flags %d\n",
1911 x, one ? one : "",
1912 s->size, s->xfrm_flags);
1915 void diff_debug_filepair(const struct diff_filepair *p, int i)
1917 diff_debug_filespec(p->one, i, "one");
1918 diff_debug_filespec(p->two, i, "two");
1919 fprintf(stderr, "score %d, status %c stays %d broken %d\n",
1920 p->score, p->status ? p->status : '?',
1921 p->source_stays, p->broken_pair);
1924 void diff_debug_queue(const char *msg, struct diff_queue_struct *q)
1926 int i;
1927 if (msg)
1928 fprintf(stderr, "%s\n", msg);
1929 fprintf(stderr, "q->nr = %d\n", q->nr);
1930 for (i = 0; i < q->nr; i++) {
1931 struct diff_filepair *p = q->queue[i];
1932 diff_debug_filepair(p, i);
1935 #endif
1937 static void diff_resolve_rename_copy(void)
1939 int i, j;
1940 struct diff_filepair *p, *pp;
1941 struct diff_queue_struct *q = &diff_queued_diff;
1943 diff_debug_queue("resolve-rename-copy", q);
1945 for (i = 0; i < q->nr; i++) {
1946 p = q->queue[i];
1947 p->status = 0; /* undecided */
1948 if (DIFF_PAIR_UNMERGED(p))
1949 p->status = DIFF_STATUS_UNMERGED;
1950 else if (!DIFF_FILE_VALID(p->one))
1951 p->status = DIFF_STATUS_ADDED;
1952 else if (!DIFF_FILE_VALID(p->two))
1953 p->status = DIFF_STATUS_DELETED;
1954 else if (DIFF_PAIR_TYPE_CHANGED(p))
1955 p->status = DIFF_STATUS_TYPE_CHANGED;
1957 /* from this point on, we are dealing with a pair
1958 * whose both sides are valid and of the same type, i.e.
1959 * either in-place edit or rename/copy edit.
1961 else if (DIFF_PAIR_RENAME(p)) {
1962 if (p->source_stays) {
1963 p->status = DIFF_STATUS_COPIED;
1964 continue;
1966 /* See if there is some other filepair that
1967 * copies from the same source as us. If so
1968 * we are a copy. Otherwise we are either a
1969 * copy if the path stays, or a rename if it
1970 * does not, but we already handled "stays" case.
1972 for (j = i + 1; j < q->nr; j++) {
1973 pp = q->queue[j];
1974 if (strcmp(pp->one->path, p->one->path))
1975 continue; /* not us */
1976 if (!DIFF_PAIR_RENAME(pp))
1977 continue; /* not a rename/copy */
1978 /* pp is a rename/copy from the same source */
1979 p->status = DIFF_STATUS_COPIED;
1980 break;
1982 if (!p->status)
1983 p->status = DIFF_STATUS_RENAMED;
1985 else if (memcmp(p->one->sha1, p->two->sha1, 20) ||
1986 p->one->mode != p->two->mode)
1987 p->status = DIFF_STATUS_MODIFIED;
1988 else {
1989 /* This is a "no-change" entry and should not
1990 * happen anymore, but prepare for broken callers.
1992 error("feeding unmodified %s to diffcore",
1993 p->one->path);
1994 p->status = DIFF_STATUS_UNKNOWN;
1997 diff_debug_queue("resolve-rename-copy done", q);
2000 static int check_pair_status(struct diff_filepair *p)
2002 switch (p->status) {
2003 case DIFF_STATUS_UNKNOWN:
2004 return 0;
2005 case 0:
2006 die("internal error in diff-resolve-rename-copy");
2007 default:
2008 return 1;
2012 static void flush_one_pair(struct diff_filepair *p, struct diff_options *opt)
2014 int fmt = opt->output_format;
2016 if (fmt & DIFF_FORMAT_CHECKDIFF)
2017 diff_flush_checkdiff(p, opt);
2018 else if (fmt & (DIFF_FORMAT_RAW | DIFF_FORMAT_NAME_STATUS))
2019 diff_flush_raw(p, opt);
2020 else if (fmt & DIFF_FORMAT_NAME)
2021 diff_flush_name(p, opt->line_termination);
2024 static void show_file_mode_name(const char *newdelete, struct diff_filespec *fs)
2026 if (fs->mode)
2027 printf(" %s mode %06o %s\n", newdelete, fs->mode, fs->path);
2028 else
2029 printf(" %s %s\n", newdelete, fs->path);
2033 static void show_mode_change(struct diff_filepair *p, int show_name)
2035 if (p->one->mode && p->two->mode && p->one->mode != p->two->mode) {
2036 if (show_name)
2037 printf(" mode change %06o => %06o %s\n",
2038 p->one->mode, p->two->mode, p->two->path);
2039 else
2040 printf(" mode change %06o => %06o\n",
2041 p->one->mode, p->two->mode);
2045 static void show_rename_copy(const char *renamecopy, struct diff_filepair *p)
2047 const char *old, *new;
2049 /* Find common prefix */
2050 old = p->one->path;
2051 new = p->two->path;
2052 while (1) {
2053 const char *slash_old, *slash_new;
2054 slash_old = strchr(old, '/');
2055 slash_new = strchr(new, '/');
2056 if (!slash_old ||
2057 !slash_new ||
2058 slash_old - old != slash_new - new ||
2059 memcmp(old, new, slash_new - new))
2060 break;
2061 old = slash_old + 1;
2062 new = slash_new + 1;
2064 /* p->one->path thru old is the common prefix, and old and new
2065 * through the end of names are renames
2067 if (old != p->one->path)
2068 printf(" %s %.*s{%s => %s} (%d%%)\n", renamecopy,
2069 (int)(old - p->one->path), p->one->path,
2070 old, new, (int)(0.5 + p->score * 100.0/MAX_SCORE));
2071 else
2072 printf(" %s %s => %s (%d%%)\n", renamecopy,
2073 p->one->path, p->two->path,
2074 (int)(0.5 + p->score * 100.0/MAX_SCORE));
2075 show_mode_change(p, 0);
2078 static void diff_summary(struct diff_filepair *p)
2080 switch(p->status) {
2081 case DIFF_STATUS_DELETED:
2082 show_file_mode_name("delete", p->one);
2083 break;
2084 case DIFF_STATUS_ADDED:
2085 show_file_mode_name("create", p->two);
2086 break;
2087 case DIFF_STATUS_COPIED:
2088 show_rename_copy("copy", p);
2089 break;
2090 case DIFF_STATUS_RENAMED:
2091 show_rename_copy("rename", p);
2092 break;
2093 default:
2094 if (p->score) {
2095 printf(" rewrite %s (%d%%)\n", p->two->path,
2096 (int)(0.5 + p->score * 100.0/MAX_SCORE));
2097 show_mode_change(p, 0);
2098 } else show_mode_change(p, 1);
2099 break;
2103 struct patch_id_t {
2104 struct xdiff_emit_state xm;
2105 SHA_CTX *ctx;
2106 int patchlen;
2109 static int remove_space(char *line, int len)
2111 int i;
2112 char *dst = line;
2113 unsigned char c;
2115 for (i = 0; i < len; i++)
2116 if (!isspace((c = line[i])))
2117 *dst++ = c;
2119 return dst - line;
2122 static void patch_id_consume(void *priv, char *line, unsigned long len)
2124 struct patch_id_t *data = priv;
2125 int new_len;
2127 /* Ignore line numbers when computing the SHA1 of the patch */
2128 if (!strncmp(line, "@@ -", 4))
2129 return;
2131 new_len = remove_space(line, len);
2133 SHA1_Update(data->ctx, line, new_len);
2134 data->patchlen += new_len;
2137 /* returns 0 upon success, and writes result into sha1 */
2138 static int diff_get_patch_id(struct diff_options *options, unsigned char *sha1)
2140 struct diff_queue_struct *q = &diff_queued_diff;
2141 int i;
2142 SHA_CTX ctx;
2143 struct patch_id_t data;
2144 char buffer[PATH_MAX * 4 + 20];
2146 SHA1_Init(&ctx);
2147 memset(&data, 0, sizeof(struct patch_id_t));
2148 data.ctx = &ctx;
2149 data.xm.consume = patch_id_consume;
2151 for (i = 0; i < q->nr; i++) {
2152 xpparam_t xpp;
2153 xdemitconf_t xecfg;
2154 xdemitcb_t ecb;
2155 mmfile_t mf1, mf2;
2156 struct diff_filepair *p = q->queue[i];
2157 int len1, len2;
2159 if (p->status == 0)
2160 return error("internal diff status error");
2161 if (p->status == DIFF_STATUS_UNKNOWN)
2162 continue;
2163 if (diff_unmodified_pair(p))
2164 continue;
2165 if ((DIFF_FILE_VALID(p->one) && S_ISDIR(p->one->mode)) ||
2166 (DIFF_FILE_VALID(p->two) && S_ISDIR(p->two->mode)))
2167 continue;
2168 if (DIFF_PAIR_UNMERGED(p))
2169 continue;
2171 diff_fill_sha1_info(p->one);
2172 diff_fill_sha1_info(p->two);
2173 if (fill_mmfile(&mf1, p->one) < 0 ||
2174 fill_mmfile(&mf2, p->two) < 0)
2175 return error("unable to read files to diff");
2177 /* Maybe hash p->two? into the patch id? */
2178 if (mmfile_is_binary(&mf2))
2179 continue;
2181 len1 = remove_space(p->one->path, strlen(p->one->path));
2182 len2 = remove_space(p->two->path, strlen(p->two->path));
2183 if (p->one->mode == 0)
2184 len1 = snprintf(buffer, sizeof(buffer),
2185 "diff--gita/%.*sb/%.*s"
2186 "newfilemode%06o"
2187 "---/dev/null"
2188 "+++b/%.*s",
2189 len1, p->one->path,
2190 len2, p->two->path,
2191 p->two->mode,
2192 len2, p->two->path);
2193 else if (p->two->mode == 0)
2194 len1 = snprintf(buffer, sizeof(buffer),
2195 "diff--gita/%.*sb/%.*s"
2196 "deletedfilemode%06o"
2197 "---a/%.*s"
2198 "+++/dev/null",
2199 len1, p->one->path,
2200 len2, p->two->path,
2201 p->one->mode,
2202 len1, p->one->path);
2203 else
2204 len1 = snprintf(buffer, sizeof(buffer),
2205 "diff--gita/%.*sb/%.*s"
2206 "---a/%.*s"
2207 "+++b/%.*s",
2208 len1, p->one->path,
2209 len2, p->two->path,
2210 len1, p->one->path,
2211 len2, p->two->path);
2212 SHA1_Update(&ctx, buffer, len1);
2214 xpp.flags = XDF_NEED_MINIMAL;
2215 xecfg.ctxlen = 3;
2216 xecfg.flags = XDL_EMIT_FUNCNAMES;
2217 ecb.outf = xdiff_outf;
2218 ecb.priv = &data;
2219 xdl_diff(&mf1, &mf2, &xpp, &xecfg, &ecb);
2222 SHA1_Final(sha1, &ctx);
2223 return 0;
2226 int diff_flush_patch_id(struct diff_options *options, unsigned char *sha1)
2228 struct diff_queue_struct *q = &diff_queued_diff;
2229 int i;
2230 int result = diff_get_patch_id(options, sha1);
2232 for (i = 0; i < q->nr; i++)
2233 diff_free_filepair(q->queue[i]);
2235 free(q->queue);
2236 q->queue = NULL;
2237 q->nr = q->alloc = 0;
2239 return result;
2242 static int is_summary_empty(const struct diff_queue_struct *q)
2244 int i;
2246 for (i = 0; i < q->nr; i++) {
2247 const struct diff_filepair *p = q->queue[i];
2249 switch (p->status) {
2250 case DIFF_STATUS_DELETED:
2251 case DIFF_STATUS_ADDED:
2252 case DIFF_STATUS_COPIED:
2253 case DIFF_STATUS_RENAMED:
2254 return 0;
2255 default:
2256 if (p->score)
2257 return 0;
2258 if (p->one->mode && p->two->mode &&
2259 p->one->mode != p->two->mode)
2260 return 0;
2261 break;
2264 return 1;
2267 void diff_flush(struct diff_options *options)
2269 struct diff_queue_struct *q = &diff_queued_diff;
2270 int i, output_format = options->output_format;
2271 int separator = 0;
2274 * Order: raw, stat, summary, patch
2275 * or: name/name-status/checkdiff (other bits clear)
2277 if (!q->nr)
2278 goto free_queue;
2280 if (output_format & (DIFF_FORMAT_RAW |
2281 DIFF_FORMAT_NAME |
2282 DIFF_FORMAT_NAME_STATUS |
2283 DIFF_FORMAT_CHECKDIFF)) {
2284 for (i = 0; i < q->nr; i++) {
2285 struct diff_filepair *p = q->queue[i];
2286 if (check_pair_status(p))
2287 flush_one_pair(p, options);
2289 separator++;
2292 if (output_format & DIFF_FORMAT_DIFFSTAT) {
2293 struct diffstat_t diffstat;
2295 memset(&diffstat, 0, sizeof(struct diffstat_t));
2296 diffstat.xm.consume = diffstat_consume;
2297 for (i = 0; i < q->nr; i++) {
2298 struct diff_filepair *p = q->queue[i];
2299 if (check_pair_status(p))
2300 diff_flush_stat(p, options, &diffstat);
2302 show_stats(&diffstat);
2303 separator++;
2306 if (output_format & DIFF_FORMAT_SUMMARY && !is_summary_empty(q)) {
2307 for (i = 0; i < q->nr; i++)
2308 diff_summary(q->queue[i]);
2309 separator++;
2312 if (output_format & DIFF_FORMAT_PATCH) {
2313 if (separator) {
2314 if (options->stat_sep) {
2315 /* attach patch instead of inline */
2316 fputs(options->stat_sep, stdout);
2317 } else {
2318 putchar(options->line_termination);
2322 for (i = 0; i < q->nr; i++) {
2323 struct diff_filepair *p = q->queue[i];
2324 if (check_pair_status(p))
2325 diff_flush_patch(p, options);
2329 for (i = 0; i < q->nr; i++)
2330 diff_free_filepair(q->queue[i]);
2331 free_queue:
2332 free(q->queue);
2333 q->queue = NULL;
2334 q->nr = q->alloc = 0;
2337 static void diffcore_apply_filter(const char *filter)
2339 int i;
2340 struct diff_queue_struct *q = &diff_queued_diff;
2341 struct diff_queue_struct outq;
2342 outq.queue = NULL;
2343 outq.nr = outq.alloc = 0;
2345 if (!filter)
2346 return;
2348 if (strchr(filter, DIFF_STATUS_FILTER_AON)) {
2349 int found;
2350 for (i = found = 0; !found && i < q->nr; i++) {
2351 struct diff_filepair *p = q->queue[i];
2352 if (((p->status == DIFF_STATUS_MODIFIED) &&
2353 ((p->score &&
2354 strchr(filter, DIFF_STATUS_FILTER_BROKEN)) ||
2355 (!p->score &&
2356 strchr(filter, DIFF_STATUS_MODIFIED)))) ||
2357 ((p->status != DIFF_STATUS_MODIFIED) &&
2358 strchr(filter, p->status)))
2359 found++;
2361 if (found)
2362 return;
2364 /* otherwise we will clear the whole queue
2365 * by copying the empty outq at the end of this
2366 * function, but first clear the current entries
2367 * in the queue.
2369 for (i = 0; i < q->nr; i++)
2370 diff_free_filepair(q->queue[i]);
2372 else {
2373 /* Only the matching ones */
2374 for (i = 0; i < q->nr; i++) {
2375 struct diff_filepair *p = q->queue[i];
2377 if (((p->status == DIFF_STATUS_MODIFIED) &&
2378 ((p->score &&
2379 strchr(filter, DIFF_STATUS_FILTER_BROKEN)) ||
2380 (!p->score &&
2381 strchr(filter, DIFF_STATUS_MODIFIED)))) ||
2382 ((p->status != DIFF_STATUS_MODIFIED) &&
2383 strchr(filter, p->status)))
2384 diff_q(&outq, p);
2385 else
2386 diff_free_filepair(p);
2389 free(q->queue);
2390 *q = outq;
2393 void diffcore_std(struct diff_options *options)
2395 if (options->break_opt != -1)
2396 diffcore_break(options->break_opt);
2397 if (options->detect_rename)
2398 diffcore_rename(options);
2399 if (options->break_opt != -1)
2400 diffcore_merge_broken();
2401 if (options->pickaxe)
2402 diffcore_pickaxe(options->pickaxe, options->pickaxe_opts);
2403 if (options->orderfile)
2404 diffcore_order(options->orderfile);
2405 diff_resolve_rename_copy();
2406 diffcore_apply_filter(options->filter);
2410 void diffcore_std_no_resolve(struct diff_options *options)
2412 if (options->pickaxe)
2413 diffcore_pickaxe(options->pickaxe, options->pickaxe_opts);
2414 if (options->orderfile)
2415 diffcore_order(options->orderfile);
2416 diffcore_apply_filter(options->filter);
2419 void diff_addremove(struct diff_options *options,
2420 int addremove, unsigned mode,
2421 const unsigned char *sha1,
2422 const char *base, const char *path)
2424 char concatpath[PATH_MAX];
2425 struct diff_filespec *one, *two;
2427 /* This may look odd, but it is a preparation for
2428 * feeding "there are unchanged files which should
2429 * not produce diffs, but when you are doing copy
2430 * detection you would need them, so here they are"
2431 * entries to the diff-core. They will be prefixed
2432 * with something like '=' or '*' (I haven't decided
2433 * which but should not make any difference).
2434 * Feeding the same new and old to diff_change()
2435 * also has the same effect.
2436 * Before the final output happens, they are pruned after
2437 * merged into rename/copy pairs as appropriate.
2439 if (options->reverse_diff)
2440 addremove = (addremove == '+' ? '-' :
2441 addremove == '-' ? '+' : addremove);
2443 if (!path) path = "";
2444 sprintf(concatpath, "%s%s", base, path);
2445 one = alloc_filespec(concatpath);
2446 two = alloc_filespec(concatpath);
2448 if (addremove != '+')
2449 fill_filespec(one, sha1, mode);
2450 if (addremove != '-')
2451 fill_filespec(two, sha1, mode);
2453 diff_queue(&diff_queued_diff, one, two);
2456 void diff_change(struct diff_options *options,
2457 unsigned old_mode, unsigned new_mode,
2458 const unsigned char *old_sha1,
2459 const unsigned char *new_sha1,
2460 const char *base, const char *path)
2462 char concatpath[PATH_MAX];
2463 struct diff_filespec *one, *two;
2465 if (options->reverse_diff) {
2466 unsigned tmp;
2467 const unsigned char *tmp_c;
2468 tmp = old_mode; old_mode = new_mode; new_mode = tmp;
2469 tmp_c = old_sha1; old_sha1 = new_sha1; new_sha1 = tmp_c;
2471 if (!path) path = "";
2472 sprintf(concatpath, "%s%s", base, path);
2473 one = alloc_filespec(concatpath);
2474 two = alloc_filespec(concatpath);
2475 fill_filespec(one, old_sha1, old_mode);
2476 fill_filespec(two, new_sha1, new_mode);
2478 diff_queue(&diff_queued_diff, one, two);
2481 void diff_unmerge(struct diff_options *options,
2482 const char *path)
2484 struct diff_filespec *one, *two;
2485 one = alloc_filespec(path);
2486 two = alloc_filespec(path);
2487 diff_queue(&diff_queued_diff, one, two);