Close the index file between writing and committing
[git/debian.git] / diff.c
blobaab246c97eb1660acfa231f7008180a12f6deb8d
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 = 0;
115 if (isatty(1)) {
116 char *term = getenv("TERM");
117 if (term && strcmp(term, "dumb"))
118 diff_use_color_default = 1;
121 else if (!strcasecmp(value, "never"))
122 diff_use_color_default = 0;
123 else if (!strcasecmp(value, "always"))
124 diff_use_color_default = 1;
125 else
126 diff_use_color_default = git_config_bool(var, value);
127 return 0;
129 if (!strncmp(var, "diff.color.", 11)) {
130 int slot = parse_diff_color_slot(var, 11);
131 diff_colors[slot] = parse_diff_color_value(value, var);
132 return 0;
134 return git_default_config(var, value);
137 static char *quote_one(const char *str)
139 int needlen;
140 char *xp;
142 if (!str)
143 return NULL;
144 needlen = quote_c_style(str, NULL, NULL, 0);
145 if (!needlen)
146 return strdup(str);
147 xp = xmalloc(needlen + 1);
148 quote_c_style(str, xp, NULL, 0);
149 return xp;
152 static char *quote_two(const char *one, const char *two)
154 int need_one = quote_c_style(one, NULL, NULL, 1);
155 int need_two = quote_c_style(two, NULL, NULL, 1);
156 char *xp;
158 if (need_one + need_two) {
159 if (!need_one) need_one = strlen(one);
160 if (!need_two) need_one = strlen(two);
162 xp = xmalloc(need_one + need_two + 3);
163 xp[0] = '"';
164 quote_c_style(one, xp + 1, NULL, 1);
165 quote_c_style(two, xp + need_one + 1, NULL, 1);
166 strcpy(xp + need_one + need_two + 1, "\"");
167 return xp;
169 need_one = strlen(one);
170 need_two = strlen(two);
171 xp = xmalloc(need_one + need_two + 1);
172 strcpy(xp, one);
173 strcpy(xp + need_one, two);
174 return xp;
177 static const char *external_diff(void)
179 static const char *external_diff_cmd = NULL;
180 static int done_preparing = 0;
182 if (done_preparing)
183 return external_diff_cmd;
184 external_diff_cmd = getenv("GIT_EXTERNAL_DIFF");
185 done_preparing = 1;
186 return external_diff_cmd;
189 #define TEMPFILE_PATH_LEN 50
191 static struct diff_tempfile {
192 const char *name; /* filename external diff should read from */
193 char hex[41];
194 char mode[10];
195 char tmp_path[TEMPFILE_PATH_LEN];
196 } diff_temp[2];
198 static int count_lines(const char *data, int size)
200 int count, ch, completely_empty = 1, nl_just_seen = 0;
201 count = 0;
202 while (0 < size--) {
203 ch = *data++;
204 if (ch == '\n') {
205 count++;
206 nl_just_seen = 1;
207 completely_empty = 0;
209 else {
210 nl_just_seen = 0;
211 completely_empty = 0;
214 if (completely_empty)
215 return 0;
216 if (!nl_just_seen)
217 count++; /* no trailing newline */
218 return count;
221 static void print_line_count(int count)
223 switch (count) {
224 case 0:
225 printf("0,0");
226 break;
227 case 1:
228 printf("1");
229 break;
230 default:
231 printf("1,%d", count);
232 break;
236 static void copy_file(int prefix, const char *data, int size)
238 int ch, nl_just_seen = 1;
239 while (0 < size--) {
240 ch = *data++;
241 if (nl_just_seen)
242 putchar(prefix);
243 putchar(ch);
244 if (ch == '\n')
245 nl_just_seen = 1;
246 else
247 nl_just_seen = 0;
249 if (!nl_just_seen)
250 printf("\n\\ No newline at end of file\n");
253 static void emit_rewrite_diff(const char *name_a,
254 const char *name_b,
255 struct diff_filespec *one,
256 struct diff_filespec *two)
258 int lc_a, lc_b;
259 diff_populate_filespec(one, 0);
260 diff_populate_filespec(two, 0);
261 lc_a = count_lines(one->data, one->size);
262 lc_b = count_lines(two->data, two->size);
263 printf("--- %s\n+++ %s\n@@ -", name_a, name_b);
264 print_line_count(lc_a);
265 printf(" +");
266 print_line_count(lc_b);
267 printf(" @@\n");
268 if (lc_a)
269 copy_file('-', one->data, one->size);
270 if (lc_b)
271 copy_file('+', two->data, two->size);
274 static int fill_mmfile(mmfile_t *mf, struct diff_filespec *one)
276 if (!DIFF_FILE_VALID(one)) {
277 mf->ptr = (char *)""; /* does not matter */
278 mf->size = 0;
279 return 0;
281 else if (diff_populate_filespec(one, 0))
282 return -1;
283 mf->ptr = one->data;
284 mf->size = one->size;
285 return 0;
288 struct emit_callback {
289 struct xdiff_emit_state xm;
290 int nparents, color_diff;
291 const char **label_path;
294 static inline const char *get_color(int diff_use_color, enum color_diff ix)
296 if (diff_use_color)
297 return diff_colors[ix];
298 return "";
301 static void fn_out_consume(void *priv, char *line, unsigned long len)
303 int i;
304 struct emit_callback *ecbdata = priv;
305 const char *set = get_color(ecbdata->color_diff, DIFF_METAINFO);
306 const char *reset = get_color(ecbdata->color_diff, DIFF_RESET);
308 if (ecbdata->label_path[0]) {
309 printf("%s--- %s%s\n", set, ecbdata->label_path[0], reset);
310 printf("%s+++ %s%s\n", set, ecbdata->label_path[1], reset);
311 ecbdata->label_path[0] = ecbdata->label_path[1] = NULL;
314 /* This is not really necessary for now because
315 * this codepath only deals with two-way diffs.
317 for (i = 0; i < len && line[i] == '@'; i++)
319 if (2 <= i && i < len && line[i] == ' ') {
320 ecbdata->nparents = i - 1;
321 set = get_color(ecbdata->color_diff, DIFF_FRAGINFO);
323 else if (len < ecbdata->nparents)
324 set = reset;
325 else {
326 int nparents = ecbdata->nparents;
327 int color = DIFF_PLAIN;
328 for (i = 0; i < nparents && len; i++) {
329 if (line[i] == '-')
330 color = DIFF_FILE_OLD;
331 else if (line[i] == '+')
332 color = DIFF_FILE_NEW;
334 set = get_color(ecbdata->color_diff, color);
336 if (len > 0 && line[len-1] == '\n')
337 len--;
338 fputs (set, stdout);
339 fwrite (line, len, 1, stdout);
340 puts (reset);
343 static char *pprint_rename(const char *a, const char *b)
345 const char *old = a;
346 const char *new = b;
347 char *name = NULL;
348 int pfx_length, sfx_length;
349 int len_a = strlen(a);
350 int len_b = strlen(b);
352 /* Find common prefix */
353 pfx_length = 0;
354 while (*old && *new && *old == *new) {
355 if (*old == '/')
356 pfx_length = old - a + 1;
357 old++;
358 new++;
361 /* Find common suffix */
362 old = a + len_a;
363 new = b + len_b;
364 sfx_length = 0;
365 while (a <= old && b <= new && *old == *new) {
366 if (*old == '/')
367 sfx_length = len_a - (old - a);
368 old--;
369 new--;
373 * pfx{mid-a => mid-b}sfx
374 * {pfx-a => pfx-b}sfx
375 * pfx{sfx-a => sfx-b}
376 * name-a => name-b
378 if (pfx_length + sfx_length) {
379 int a_midlen = len_a - pfx_length - sfx_length;
380 int b_midlen = len_b - pfx_length - sfx_length;
381 if (a_midlen < 0) a_midlen = 0;
382 if (b_midlen < 0) b_midlen = 0;
384 name = xmalloc(pfx_length + a_midlen + b_midlen + sfx_length + 7);
385 sprintf(name, "%.*s{%.*s => %.*s}%s",
386 pfx_length, a,
387 a_midlen, a + pfx_length,
388 b_midlen, b + pfx_length,
389 a + len_a - sfx_length);
391 else {
392 name = xmalloc(len_a + len_b + 5);
393 sprintf(name, "%s => %s", a, b);
395 return name;
398 struct diffstat_t {
399 struct xdiff_emit_state xm;
401 int nr;
402 int alloc;
403 struct diffstat_file {
404 char *name;
405 unsigned is_unmerged:1;
406 unsigned is_binary:1;
407 unsigned is_renamed:1;
408 unsigned int added, deleted;
409 } **files;
412 static struct diffstat_file *diffstat_add(struct diffstat_t *diffstat,
413 const char *name_a,
414 const char *name_b)
416 struct diffstat_file *x;
417 x = xcalloc(sizeof (*x), 1);
418 if (diffstat->nr == diffstat->alloc) {
419 diffstat->alloc = alloc_nr(diffstat->alloc);
420 diffstat->files = xrealloc(diffstat->files,
421 diffstat->alloc * sizeof(x));
423 diffstat->files[diffstat->nr++] = x;
424 if (name_b) {
425 x->name = pprint_rename(name_a, name_b);
426 x->is_renamed = 1;
428 else
429 x->name = strdup(name_a);
430 return x;
433 static void diffstat_consume(void *priv, char *line, unsigned long len)
435 struct diffstat_t *diffstat = priv;
436 struct diffstat_file *x = diffstat->files[diffstat->nr - 1];
438 if (line[0] == '+')
439 x->added++;
440 else if (line[0] == '-')
441 x->deleted++;
444 static const char pluses[] = "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++";
445 static const char minuses[]= "----------------------------------------------------------------------";
446 const char mime_boundary_leader[] = "------------";
448 static void show_stats(struct diffstat_t* data)
450 int i, len, add, del, total, adds = 0, dels = 0;
451 int max, max_change = 0, max_len = 0;
452 int total_files = data->nr;
454 if (data->nr == 0)
455 return;
457 for (i = 0; i < data->nr; i++) {
458 struct diffstat_file *file = data->files[i];
460 len = strlen(file->name);
461 if (max_len < len)
462 max_len = len;
464 if (file->is_binary || file->is_unmerged)
465 continue;
466 if (max_change < file->added + file->deleted)
467 max_change = file->added + file->deleted;
470 for (i = 0; i < data->nr; i++) {
471 const char *prefix = "";
472 char *name = data->files[i]->name;
473 int added = data->files[i]->added;
474 int deleted = data->files[i]->deleted;
476 if (0 < (len = quote_c_style(name, NULL, NULL, 0))) {
477 char *qname = xmalloc(len + 1);
478 quote_c_style(name, qname, NULL, 0);
479 free(name);
480 data->files[i]->name = name = qname;
484 * "scale" the filename
486 len = strlen(name);
487 max = max_len;
488 if (max > 50)
489 max = 50;
490 if (len > max) {
491 char *slash;
492 prefix = "...";
493 max -= 3;
494 name += len - max;
495 slash = strchr(name, '/');
496 if (slash)
497 name = slash;
499 len = max;
502 * scale the add/delete
504 max = max_change;
505 if (max + len > 70)
506 max = 70 - len;
508 if (data->files[i]->is_binary) {
509 printf(" %s%-*s | Bin\n", prefix, len, name);
510 goto free_diffstat_file;
512 else if (data->files[i]->is_unmerged) {
513 printf(" %s%-*s | Unmerged\n", prefix, len, name);
514 goto free_diffstat_file;
516 else if (!data->files[i]->is_renamed &&
517 (added + deleted == 0)) {
518 total_files--;
519 goto free_diffstat_file;
522 add = added;
523 del = deleted;
524 total = add + del;
525 adds += add;
526 dels += del;
528 if (max_change > 0) {
529 total = (total * max + max_change / 2) / max_change;
530 add = (add * max + max_change / 2) / max_change;
531 del = total - add;
533 printf(" %s%-*s |%5d %.*s%.*s\n", prefix,
534 len, name, added + deleted,
535 add, pluses, del, minuses);
536 free_diffstat_file:
537 free(data->files[i]->name);
538 free(data->files[i]);
540 free(data->files);
541 printf(" %d files changed, %d insertions(+), %d deletions(-)\n",
542 total_files, adds, dels);
545 struct checkdiff_t {
546 struct xdiff_emit_state xm;
547 const char *filename;
548 int lineno;
551 static void checkdiff_consume(void *priv, char *line, unsigned long len)
553 struct checkdiff_t *data = priv;
555 if (line[0] == '+') {
556 int i, spaces = 0;
558 data->lineno++;
560 /* check space before tab */
561 for (i = 1; i < len && (line[i] == ' ' || line[i] == '\t'); i++)
562 if (line[i] == ' ')
563 spaces++;
564 if (line[i - 1] == '\t' && spaces)
565 printf("%s:%d: space before tab:%.*s\n",
566 data->filename, data->lineno, (int)len, line);
568 /* check white space at line end */
569 if (line[len - 1] == '\n')
570 len--;
571 if (isspace(line[len - 1]))
572 printf("%s:%d: white space at end: %.*s\n",
573 data->filename, data->lineno, (int)len, line);
574 } else if (line[0] == ' ')
575 data->lineno++;
576 else if (line[0] == '@') {
577 char *plus = strchr(line, '+');
578 if (plus)
579 data->lineno = strtol(plus, NULL, 10);
580 else
581 die("invalid diff");
585 static unsigned char *deflate_it(char *data,
586 unsigned long size,
587 unsigned long *result_size)
589 int bound;
590 unsigned char *deflated;
591 z_stream stream;
593 memset(&stream, 0, sizeof(stream));
594 deflateInit(&stream, zlib_compression_level);
595 bound = deflateBound(&stream, size);
596 deflated = xmalloc(bound);
597 stream.next_out = deflated;
598 stream.avail_out = bound;
600 stream.next_in = (unsigned char *)data;
601 stream.avail_in = size;
602 while (deflate(&stream, Z_FINISH) == Z_OK)
603 ; /* nothing */
604 deflateEnd(&stream);
605 *result_size = stream.total_out;
606 return deflated;
609 static void emit_binary_diff(mmfile_t *one, mmfile_t *two)
611 void *cp;
612 void *delta;
613 void *deflated;
614 void *data;
615 unsigned long orig_size;
616 unsigned long delta_size;
617 unsigned long deflate_size;
618 unsigned long data_size;
620 printf("GIT binary patch\n");
621 /* We could do deflated delta, or we could do just deflated two,
622 * whichever is smaller.
624 delta = NULL;
625 deflated = deflate_it(two->ptr, two->size, &deflate_size);
626 if (one->size && two->size) {
627 delta = diff_delta(one->ptr, one->size,
628 two->ptr, two->size,
629 &delta_size, deflate_size);
630 if (delta) {
631 void *to_free = delta;
632 orig_size = delta_size;
633 delta = deflate_it(delta, delta_size, &delta_size);
634 free(to_free);
638 if (delta && delta_size < deflate_size) {
639 printf("delta %lu\n", orig_size);
640 free(deflated);
641 data = delta;
642 data_size = delta_size;
644 else {
645 printf("literal %lu\n", two->size);
646 free(delta);
647 data = deflated;
648 data_size = deflate_size;
651 /* emit data encoded in base85 */
652 cp = data;
653 while (data_size) {
654 int bytes = (52 < data_size) ? 52 : data_size;
655 char line[70];
656 data_size -= bytes;
657 if (bytes <= 26)
658 line[0] = bytes + 'A' - 1;
659 else
660 line[0] = bytes - 26 + 'a' - 1;
661 encode_85(line + 1, cp, bytes);
662 cp = (char *) cp + bytes;
663 puts(line);
665 printf("\n");
666 free(data);
669 #define FIRST_FEW_BYTES 8000
670 static int mmfile_is_binary(mmfile_t *mf)
672 long sz = mf->size;
673 if (FIRST_FEW_BYTES < sz)
674 sz = FIRST_FEW_BYTES;
675 if (memchr(mf->ptr, 0, sz))
676 return 1;
677 return 0;
680 static void builtin_diff(const char *name_a,
681 const char *name_b,
682 struct diff_filespec *one,
683 struct diff_filespec *two,
684 const char *xfrm_msg,
685 struct diff_options *o,
686 int complete_rewrite)
688 mmfile_t mf1, mf2;
689 const char *lbl[2];
690 char *a_one, *b_two;
691 const char *set = get_color(o->color_diff, DIFF_METAINFO);
692 const char *reset = get_color(o->color_diff, DIFF_RESET);
694 a_one = quote_two("a/", name_a);
695 b_two = quote_two("b/", name_b);
696 lbl[0] = DIFF_FILE_VALID(one) ? a_one : "/dev/null";
697 lbl[1] = DIFF_FILE_VALID(two) ? b_two : "/dev/null";
698 printf("%sdiff --git %s %s%s\n", set, a_one, b_two, reset);
699 if (lbl[0][0] == '/') {
700 /* /dev/null */
701 printf("%snew file mode %06o%s\n", set, two->mode, reset);
702 if (xfrm_msg && xfrm_msg[0])
703 printf("%s%s%s\n", set, xfrm_msg, reset);
705 else if (lbl[1][0] == '/') {
706 printf("%sdeleted file mode %06o%s\n", set, one->mode, reset);
707 if (xfrm_msg && xfrm_msg[0])
708 printf("%s%s%s\n", set, xfrm_msg, reset);
710 else {
711 if (one->mode != two->mode) {
712 printf("%sold mode %06o%s\n", set, one->mode, reset);
713 printf("%snew mode %06o%s\n", set, two->mode, reset);
715 if (xfrm_msg && xfrm_msg[0])
716 printf("%s%s%s\n", set, xfrm_msg, reset);
718 * we do not run diff between different kind
719 * of objects.
721 if ((one->mode ^ two->mode) & S_IFMT)
722 goto free_ab_and_return;
723 if (complete_rewrite) {
724 emit_rewrite_diff(name_a, name_b, one, two);
725 goto free_ab_and_return;
729 if (fill_mmfile(&mf1, one) < 0 || fill_mmfile(&mf2, two) < 0)
730 die("unable to read files to diff");
732 if (mmfile_is_binary(&mf1) || mmfile_is_binary(&mf2)) {
733 /* Quite common confusing case */
734 if (mf1.size == mf2.size &&
735 !memcmp(mf1.ptr, mf2.ptr, mf1.size))
736 goto free_ab_and_return;
737 if (o->binary)
738 emit_binary_diff(&mf1, &mf2);
739 else
740 printf("Binary files %s and %s differ\n",
741 lbl[0], lbl[1]);
743 else {
744 /* Crazy xdl interfaces.. */
745 const char *diffopts = getenv("GIT_DIFF_OPTS");
746 xpparam_t xpp;
747 xdemitconf_t xecfg;
748 xdemitcb_t ecb;
749 struct emit_callback ecbdata;
751 memset(&ecbdata, 0, sizeof(ecbdata));
752 ecbdata.label_path = lbl;
753 ecbdata.color_diff = o->color_diff;
754 xpp.flags = XDF_NEED_MINIMAL | o->xdl_opts;
755 xecfg.ctxlen = o->context;
756 xecfg.flags = XDL_EMIT_FUNCNAMES;
757 if (!diffopts)
759 else if (!strncmp(diffopts, "--unified=", 10))
760 xecfg.ctxlen = strtoul(diffopts + 10, NULL, 10);
761 else if (!strncmp(diffopts, "-u", 2))
762 xecfg.ctxlen = strtoul(diffopts + 2, NULL, 10);
763 ecb.outf = xdiff_outf;
764 ecb.priv = &ecbdata;
765 ecbdata.xm.consume = fn_out_consume;
766 xdl_diff(&mf1, &mf2, &xpp, &xecfg, &ecb);
769 free_ab_and_return:
770 free(a_one);
771 free(b_two);
772 return;
775 static void builtin_diffstat(const char *name_a, const char *name_b,
776 struct diff_filespec *one,
777 struct diff_filespec *two,
778 struct diffstat_t *diffstat,
779 struct diff_options *o,
780 int complete_rewrite)
782 mmfile_t mf1, mf2;
783 struct diffstat_file *data;
785 data = diffstat_add(diffstat, name_a, name_b);
787 if (!one || !two) {
788 data->is_unmerged = 1;
789 return;
791 if (complete_rewrite) {
792 diff_populate_filespec(one, 0);
793 diff_populate_filespec(two, 0);
794 data->deleted = count_lines(one->data, one->size);
795 data->added = count_lines(two->data, two->size);
796 return;
798 if (fill_mmfile(&mf1, one) < 0 || fill_mmfile(&mf2, two) < 0)
799 die("unable to read files to diff");
801 if (mmfile_is_binary(&mf1) || mmfile_is_binary(&mf2))
802 data->is_binary = 1;
803 else {
804 /* Crazy xdl interfaces.. */
805 xpparam_t xpp;
806 xdemitconf_t xecfg;
807 xdemitcb_t ecb;
809 xpp.flags = XDF_NEED_MINIMAL | o->xdl_opts;
810 xecfg.ctxlen = 0;
811 xecfg.flags = 0;
812 ecb.outf = xdiff_outf;
813 ecb.priv = diffstat;
814 xdl_diff(&mf1, &mf2, &xpp, &xecfg, &ecb);
818 static void builtin_checkdiff(const char *name_a, const char *name_b,
819 struct diff_filespec *one,
820 struct diff_filespec *two)
822 mmfile_t mf1, mf2;
823 struct checkdiff_t data;
825 if (!two)
826 return;
828 memset(&data, 0, sizeof(data));
829 data.xm.consume = checkdiff_consume;
830 data.filename = name_b ? name_b : name_a;
831 data.lineno = 0;
833 if (fill_mmfile(&mf1, one) < 0 || fill_mmfile(&mf2, two) < 0)
834 die("unable to read files to diff");
836 if (mmfile_is_binary(&mf2))
837 return;
838 else {
839 /* Crazy xdl interfaces.. */
840 xpparam_t xpp;
841 xdemitconf_t xecfg;
842 xdemitcb_t ecb;
844 xpp.flags = XDF_NEED_MINIMAL;
845 xecfg.ctxlen = 0;
846 xecfg.flags = 0;
847 ecb.outf = xdiff_outf;
848 ecb.priv = &data;
849 xdl_diff(&mf1, &mf2, &xpp, &xecfg, &ecb);
853 struct diff_filespec *alloc_filespec(const char *path)
855 int namelen = strlen(path);
856 struct diff_filespec *spec = xmalloc(sizeof(*spec) + namelen + 1);
858 memset(spec, 0, sizeof(*spec));
859 spec->path = (char *)(spec + 1);
860 memcpy(spec->path, path, namelen+1);
861 return spec;
864 void fill_filespec(struct diff_filespec *spec, const unsigned char *sha1,
865 unsigned short mode)
867 if (mode) {
868 spec->mode = canon_mode(mode);
869 memcpy(spec->sha1, sha1, 20);
870 spec->sha1_valid = !!memcmp(sha1, null_sha1, 20);
875 * Given a name and sha1 pair, if the dircache tells us the file in
876 * the work tree has that object contents, return true, so that
877 * prepare_temp_file() does not have to inflate and extract.
879 static int work_tree_matches(const char *name, const unsigned char *sha1)
881 struct cache_entry *ce;
882 struct stat st;
883 int pos, len;
885 /* We do not read the cache ourselves here, because the
886 * benchmark with my previous version that always reads cache
887 * shows that it makes things worse for diff-tree comparing
888 * two linux-2.6 kernel trees in an already checked out work
889 * tree. This is because most diff-tree comparisons deal with
890 * only a small number of files, while reading the cache is
891 * expensive for a large project, and its cost outweighs the
892 * savings we get by not inflating the object to a temporary
893 * file. Practically, this code only helps when we are used
894 * by diff-cache --cached, which does read the cache before
895 * calling us.
897 if (!active_cache)
898 return 0;
900 len = strlen(name);
901 pos = cache_name_pos(name, len);
902 if (pos < 0)
903 return 0;
904 ce = active_cache[pos];
905 if ((lstat(name, &st) < 0) ||
906 !S_ISREG(st.st_mode) || /* careful! */
907 ce_match_stat(ce, &st, 0) ||
908 memcmp(sha1, ce->sha1, 20))
909 return 0;
910 /* we return 1 only when we can stat, it is a regular file,
911 * stat information matches, and sha1 recorded in the cache
912 * matches. I.e. we know the file in the work tree really is
913 * the same as the <name, sha1> pair.
915 return 1;
918 static struct sha1_size_cache {
919 unsigned char sha1[20];
920 unsigned long size;
921 } **sha1_size_cache;
922 static int sha1_size_cache_nr, sha1_size_cache_alloc;
924 static struct sha1_size_cache *locate_size_cache(unsigned char *sha1,
925 int find_only,
926 unsigned long size)
928 int first, last;
929 struct sha1_size_cache *e;
931 first = 0;
932 last = sha1_size_cache_nr;
933 while (last > first) {
934 int cmp, next = (last + first) >> 1;
935 e = sha1_size_cache[next];
936 cmp = memcmp(e->sha1, sha1, 20);
937 if (!cmp)
938 return e;
939 if (cmp < 0) {
940 last = next;
941 continue;
943 first = next+1;
945 /* not found */
946 if (find_only)
947 return NULL;
948 /* insert to make it at "first" */
949 if (sha1_size_cache_alloc <= sha1_size_cache_nr) {
950 sha1_size_cache_alloc = alloc_nr(sha1_size_cache_alloc);
951 sha1_size_cache = xrealloc(sha1_size_cache,
952 sha1_size_cache_alloc *
953 sizeof(*sha1_size_cache));
955 sha1_size_cache_nr++;
956 if (first < sha1_size_cache_nr)
957 memmove(sha1_size_cache + first + 1, sha1_size_cache + first,
958 (sha1_size_cache_nr - first - 1) *
959 sizeof(*sha1_size_cache));
960 e = xmalloc(sizeof(struct sha1_size_cache));
961 sha1_size_cache[first] = e;
962 memcpy(e->sha1, sha1, 20);
963 e->size = size;
964 return e;
968 * While doing rename detection and pickaxe operation, we may need to
969 * grab the data for the blob (or file) for our own in-core comparison.
970 * diff_filespec has data and size fields for this purpose.
972 int diff_populate_filespec(struct diff_filespec *s, int size_only)
974 int err = 0;
975 if (!DIFF_FILE_VALID(s))
976 die("internal error: asking to populate invalid file.");
977 if (S_ISDIR(s->mode))
978 return -1;
980 if (!use_size_cache)
981 size_only = 0;
983 if (s->data)
984 return err;
985 if (!s->sha1_valid ||
986 work_tree_matches(s->path, s->sha1)) {
987 struct stat st;
988 int fd;
989 if (lstat(s->path, &st) < 0) {
990 if (errno == ENOENT) {
991 err_empty:
992 err = -1;
993 empty:
994 s->data = (char *)"";
995 s->size = 0;
996 return err;
999 s->size = st.st_size;
1000 if (!s->size)
1001 goto empty;
1002 if (size_only)
1003 return 0;
1004 if (S_ISLNK(st.st_mode)) {
1005 int ret;
1006 s->data = xmalloc(s->size);
1007 s->should_free = 1;
1008 ret = readlink(s->path, s->data, s->size);
1009 if (ret < 0) {
1010 free(s->data);
1011 goto err_empty;
1013 return 0;
1015 fd = open(s->path, O_RDONLY);
1016 if (fd < 0)
1017 goto err_empty;
1018 s->data = mmap(NULL, s->size, PROT_READ, MAP_PRIVATE, fd, 0);
1019 close(fd);
1020 if (s->data == MAP_FAILED)
1021 goto err_empty;
1022 s->should_munmap = 1;
1024 else {
1025 char type[20];
1026 struct sha1_size_cache *e;
1028 if (size_only) {
1029 e = locate_size_cache(s->sha1, 1, 0);
1030 if (e) {
1031 s->size = e->size;
1032 return 0;
1034 if (!sha1_object_info(s->sha1, type, &s->size))
1035 locate_size_cache(s->sha1, 0, s->size);
1037 else {
1038 s->data = read_sha1_file(s->sha1, type, &s->size);
1039 s->should_free = 1;
1042 return 0;
1045 void diff_free_filespec_data(struct diff_filespec *s)
1047 if (s->should_free)
1048 free(s->data);
1049 else if (s->should_munmap)
1050 munmap(s->data, s->size);
1051 s->should_free = s->should_munmap = 0;
1052 s->data = NULL;
1053 free(s->cnt_data);
1054 s->cnt_data = NULL;
1057 static void prep_temp_blob(struct diff_tempfile *temp,
1058 void *blob,
1059 unsigned long size,
1060 const unsigned char *sha1,
1061 int mode)
1063 int fd;
1065 fd = git_mkstemp(temp->tmp_path, TEMPFILE_PATH_LEN, ".diff_XXXXXX");
1066 if (fd < 0)
1067 die("unable to create temp-file");
1068 if (write(fd, blob, size) != size)
1069 die("unable to write temp-file");
1070 close(fd);
1071 temp->name = temp->tmp_path;
1072 strcpy(temp->hex, sha1_to_hex(sha1));
1073 temp->hex[40] = 0;
1074 sprintf(temp->mode, "%06o", mode);
1077 static void prepare_temp_file(const char *name,
1078 struct diff_tempfile *temp,
1079 struct diff_filespec *one)
1081 if (!DIFF_FILE_VALID(one)) {
1082 not_a_valid_file:
1083 /* A '-' entry produces this for file-2, and
1084 * a '+' entry produces this for file-1.
1086 temp->name = "/dev/null";
1087 strcpy(temp->hex, ".");
1088 strcpy(temp->mode, ".");
1089 return;
1092 if (!one->sha1_valid ||
1093 work_tree_matches(name, one->sha1)) {
1094 struct stat st;
1095 if (lstat(name, &st) < 0) {
1096 if (errno == ENOENT)
1097 goto not_a_valid_file;
1098 die("stat(%s): %s", name, strerror(errno));
1100 if (S_ISLNK(st.st_mode)) {
1101 int ret;
1102 char buf[PATH_MAX + 1]; /* ought to be SYMLINK_MAX */
1103 if (sizeof(buf) <= st.st_size)
1104 die("symlink too long: %s", name);
1105 ret = readlink(name, buf, st.st_size);
1106 if (ret < 0)
1107 die("readlink(%s)", name);
1108 prep_temp_blob(temp, buf, st.st_size,
1109 (one->sha1_valid ?
1110 one->sha1 : null_sha1),
1111 (one->sha1_valid ?
1112 one->mode : S_IFLNK));
1114 else {
1115 /* we can borrow from the file in the work tree */
1116 temp->name = name;
1117 if (!one->sha1_valid)
1118 strcpy(temp->hex, sha1_to_hex(null_sha1));
1119 else
1120 strcpy(temp->hex, sha1_to_hex(one->sha1));
1121 /* Even though we may sometimes borrow the
1122 * contents from the work tree, we always want
1123 * one->mode. mode is trustworthy even when
1124 * !(one->sha1_valid), as long as
1125 * DIFF_FILE_VALID(one).
1127 sprintf(temp->mode, "%06o", one->mode);
1129 return;
1131 else {
1132 if (diff_populate_filespec(one, 0))
1133 die("cannot read data blob for %s", one->path);
1134 prep_temp_blob(temp, one->data, one->size,
1135 one->sha1, one->mode);
1139 static void remove_tempfile(void)
1141 int i;
1143 for (i = 0; i < 2; i++)
1144 if (diff_temp[i].name == diff_temp[i].tmp_path) {
1145 unlink(diff_temp[i].name);
1146 diff_temp[i].name = NULL;
1150 static void remove_tempfile_on_signal(int signo)
1152 remove_tempfile();
1153 signal(SIGINT, SIG_DFL);
1154 raise(signo);
1157 static int spawn_prog(const char *pgm, const char **arg)
1159 pid_t pid;
1160 int status;
1162 fflush(NULL);
1163 pid = fork();
1164 if (pid < 0)
1165 die("unable to fork");
1166 if (!pid) {
1167 execvp(pgm, (char *const*) arg);
1168 exit(255);
1171 while (waitpid(pid, &status, 0) < 0) {
1172 if (errno == EINTR)
1173 continue;
1174 return -1;
1177 /* Earlier we did not check the exit status because
1178 * diff exits non-zero if files are different, and
1179 * we are not interested in knowing that. It was a
1180 * mistake which made it harder to quit a diff-*
1181 * session that uses the git-apply-patch-script as
1182 * the GIT_EXTERNAL_DIFF. A custom GIT_EXTERNAL_DIFF
1183 * should also exit non-zero only when it wants to
1184 * abort the entire diff-* session.
1186 if (WIFEXITED(status) && !WEXITSTATUS(status))
1187 return 0;
1188 return -1;
1191 /* An external diff command takes:
1193 * diff-cmd name infile1 infile1-sha1 infile1-mode \
1194 * infile2 infile2-sha1 infile2-mode [ rename-to ]
1197 static void run_external_diff(const char *pgm,
1198 const char *name,
1199 const char *other,
1200 struct diff_filespec *one,
1201 struct diff_filespec *two,
1202 const char *xfrm_msg,
1203 int complete_rewrite)
1205 const char *spawn_arg[10];
1206 struct diff_tempfile *temp = diff_temp;
1207 int retval;
1208 static int atexit_asked = 0;
1209 const char *othername;
1210 const char **arg = &spawn_arg[0];
1212 othername = (other? other : name);
1213 if (one && two) {
1214 prepare_temp_file(name, &temp[0], one);
1215 prepare_temp_file(othername, &temp[1], two);
1216 if (! atexit_asked &&
1217 (temp[0].name == temp[0].tmp_path ||
1218 temp[1].name == temp[1].tmp_path)) {
1219 atexit_asked = 1;
1220 atexit(remove_tempfile);
1222 signal(SIGINT, remove_tempfile_on_signal);
1225 if (one && two) {
1226 *arg++ = pgm;
1227 *arg++ = name;
1228 *arg++ = temp[0].name;
1229 *arg++ = temp[0].hex;
1230 *arg++ = temp[0].mode;
1231 *arg++ = temp[1].name;
1232 *arg++ = temp[1].hex;
1233 *arg++ = temp[1].mode;
1234 if (other) {
1235 *arg++ = other;
1236 *arg++ = xfrm_msg;
1238 } else {
1239 *arg++ = pgm;
1240 *arg++ = name;
1242 *arg = NULL;
1243 retval = spawn_prog(pgm, spawn_arg);
1244 remove_tempfile();
1245 if (retval) {
1246 fprintf(stderr, "external diff died, stopping at %s.\n", name);
1247 exit(1);
1251 static void run_diff_cmd(const char *pgm,
1252 const char *name,
1253 const char *other,
1254 struct diff_filespec *one,
1255 struct diff_filespec *two,
1256 const char *xfrm_msg,
1257 struct diff_options *o,
1258 int complete_rewrite)
1260 if (pgm) {
1261 run_external_diff(pgm, name, other, one, two, xfrm_msg,
1262 complete_rewrite);
1263 return;
1265 if (one && two)
1266 builtin_diff(name, other ? other : name,
1267 one, two, xfrm_msg, o, complete_rewrite);
1268 else
1269 printf("* Unmerged path %s\n", name);
1272 static void diff_fill_sha1_info(struct diff_filespec *one)
1274 if (DIFF_FILE_VALID(one)) {
1275 if (!one->sha1_valid) {
1276 struct stat st;
1277 if (lstat(one->path, &st) < 0)
1278 die("stat %s", one->path);
1279 if (index_path(one->sha1, one->path, &st, 0))
1280 die("cannot hash %s\n", one->path);
1283 else
1284 memset(one->sha1, 0, 20);
1287 static void run_diff(struct diff_filepair *p, struct diff_options *o)
1289 const char *pgm = external_diff();
1290 char msg[PATH_MAX*2+300], *xfrm_msg;
1291 struct diff_filespec *one;
1292 struct diff_filespec *two;
1293 const char *name;
1294 const char *other;
1295 char *name_munged, *other_munged;
1296 int complete_rewrite = 0;
1297 int len;
1299 if (DIFF_PAIR_UNMERGED(p)) {
1300 /* unmerged */
1301 run_diff_cmd(pgm, p->one->path, NULL, NULL, NULL, NULL, o, 0);
1302 return;
1305 name = p->one->path;
1306 other = (strcmp(name, p->two->path) ? p->two->path : NULL);
1307 name_munged = quote_one(name);
1308 other_munged = quote_one(other);
1309 one = p->one; two = p->two;
1311 diff_fill_sha1_info(one);
1312 diff_fill_sha1_info(two);
1314 len = 0;
1315 switch (p->status) {
1316 case DIFF_STATUS_COPIED:
1317 len += snprintf(msg + len, sizeof(msg) - len,
1318 "similarity index %d%%\n"
1319 "copy from %s\n"
1320 "copy to %s\n",
1321 (int)(0.5 + p->score * 100.0/MAX_SCORE),
1322 name_munged, other_munged);
1323 break;
1324 case DIFF_STATUS_RENAMED:
1325 len += snprintf(msg + len, sizeof(msg) - len,
1326 "similarity index %d%%\n"
1327 "rename from %s\n"
1328 "rename to %s\n",
1329 (int)(0.5 + p->score * 100.0/MAX_SCORE),
1330 name_munged, other_munged);
1331 break;
1332 case DIFF_STATUS_MODIFIED:
1333 if (p->score) {
1334 len += snprintf(msg + len, sizeof(msg) - len,
1335 "dissimilarity index %d%%\n",
1336 (int)(0.5 + p->score *
1337 100.0/MAX_SCORE));
1338 complete_rewrite = 1;
1339 break;
1341 /* fallthru */
1342 default:
1343 /* nothing */
1347 if (memcmp(one->sha1, two->sha1, 20)) {
1348 int abbrev = o->full_index ? 40 : DEFAULT_ABBREV;
1350 len += snprintf(msg + len, sizeof(msg) - len,
1351 "index %.*s..%.*s",
1352 abbrev, sha1_to_hex(one->sha1),
1353 abbrev, sha1_to_hex(two->sha1));
1354 if (one->mode == two->mode)
1355 len += snprintf(msg + len, sizeof(msg) - len,
1356 " %06o", one->mode);
1357 len += snprintf(msg + len, sizeof(msg) - len, "\n");
1360 if (len)
1361 msg[--len] = 0;
1362 xfrm_msg = len ? msg : NULL;
1364 if (!pgm &&
1365 DIFF_FILE_VALID(one) && DIFF_FILE_VALID(two) &&
1366 (S_IFMT & one->mode) != (S_IFMT & two->mode)) {
1367 /* a filepair that changes between file and symlink
1368 * needs to be split into deletion and creation.
1370 struct diff_filespec *null = alloc_filespec(two->path);
1371 run_diff_cmd(NULL, name, other, one, null, xfrm_msg, o, 0);
1372 free(null);
1373 null = alloc_filespec(one->path);
1374 run_diff_cmd(NULL, name, other, null, two, xfrm_msg, o, 0);
1375 free(null);
1377 else
1378 run_diff_cmd(pgm, name, other, one, two, xfrm_msg, o,
1379 complete_rewrite);
1381 free(name_munged);
1382 free(other_munged);
1385 static void run_diffstat(struct diff_filepair *p, struct diff_options *o,
1386 struct diffstat_t *diffstat)
1388 const char *name;
1389 const char *other;
1390 int complete_rewrite = 0;
1392 if (DIFF_PAIR_UNMERGED(p)) {
1393 /* unmerged */
1394 builtin_diffstat(p->one->path, NULL, NULL, NULL, diffstat, o, 0);
1395 return;
1398 name = p->one->path;
1399 other = (strcmp(name, p->two->path) ? p->two->path : NULL);
1401 diff_fill_sha1_info(p->one);
1402 diff_fill_sha1_info(p->two);
1404 if (p->status == DIFF_STATUS_MODIFIED && p->score)
1405 complete_rewrite = 1;
1406 builtin_diffstat(name, other, p->one, p->two, diffstat, o, complete_rewrite);
1409 static void run_checkdiff(struct diff_filepair *p, struct diff_options *o)
1411 const char *name;
1412 const char *other;
1414 if (DIFF_PAIR_UNMERGED(p)) {
1415 /* unmerged */
1416 return;
1419 name = p->one->path;
1420 other = (strcmp(name, p->two->path) ? p->two->path : NULL);
1422 diff_fill_sha1_info(p->one);
1423 diff_fill_sha1_info(p->two);
1425 builtin_checkdiff(name, other, p->one, p->two);
1428 void diff_setup(struct diff_options *options)
1430 memset(options, 0, sizeof(*options));
1431 options->line_termination = '\n';
1432 options->break_opt = -1;
1433 options->rename_limit = -1;
1434 options->context = 3;
1435 options->msg_sep = "";
1437 options->change = diff_change;
1438 options->add_remove = diff_addremove;
1439 options->color_diff = diff_use_color_default;
1442 int diff_setup_done(struct diff_options *options)
1444 if ((options->find_copies_harder &&
1445 options->detect_rename != DIFF_DETECT_COPY) ||
1446 (0 <= options->rename_limit && !options->detect_rename))
1447 return -1;
1449 if (options->output_format & (DIFF_FORMAT_NAME |
1450 DIFF_FORMAT_NAME_STATUS |
1451 DIFF_FORMAT_CHECKDIFF |
1452 DIFF_FORMAT_NO_OUTPUT))
1453 options->output_format &= ~(DIFF_FORMAT_RAW |
1454 DIFF_FORMAT_DIFFSTAT |
1455 DIFF_FORMAT_SUMMARY |
1456 DIFF_FORMAT_PATCH);
1459 * These cases always need recursive; we do not drop caller-supplied
1460 * recursive bits for other formats here.
1462 if (options->output_format & (DIFF_FORMAT_PATCH |
1463 DIFF_FORMAT_DIFFSTAT |
1464 DIFF_FORMAT_CHECKDIFF))
1465 options->recursive = 1;
1467 * Also pickaxe would not work very well if you do not say recursive
1469 if (options->pickaxe)
1470 options->recursive = 1;
1472 if (options->detect_rename && options->rename_limit < 0)
1473 options->rename_limit = diff_rename_limit_default;
1474 if (options->setup & DIFF_SETUP_USE_CACHE) {
1475 if (!active_cache)
1476 /* read-cache does not die even when it fails
1477 * so it is safe for us to do this here. Also
1478 * it does not smudge active_cache or active_nr
1479 * when it fails, so we do not have to worry about
1480 * cleaning it up ourselves either.
1482 read_cache();
1484 if (options->setup & DIFF_SETUP_USE_SIZE_CACHE)
1485 use_size_cache = 1;
1486 if (options->abbrev <= 0 || 40 < options->abbrev)
1487 options->abbrev = 40; /* full */
1489 return 0;
1492 static int opt_arg(const char *arg, int arg_short, const char *arg_long, int *val)
1494 char c, *eq;
1495 int len;
1497 if (*arg != '-')
1498 return 0;
1499 c = *++arg;
1500 if (!c)
1501 return 0;
1502 if (c == arg_short) {
1503 c = *++arg;
1504 if (!c)
1505 return 1;
1506 if (val && isdigit(c)) {
1507 char *end;
1508 int n = strtoul(arg, &end, 10);
1509 if (*end)
1510 return 0;
1511 *val = n;
1512 return 1;
1514 return 0;
1516 if (c != '-')
1517 return 0;
1518 arg++;
1519 eq = strchr(arg, '=');
1520 if (eq)
1521 len = eq - arg;
1522 else
1523 len = strlen(arg);
1524 if (!len || strncmp(arg, arg_long, len))
1525 return 0;
1526 if (eq) {
1527 int n;
1528 char *end;
1529 if (!isdigit(*++eq))
1530 return 0;
1531 n = strtoul(eq, &end, 10);
1532 if (*end)
1533 return 0;
1534 *val = n;
1536 return 1;
1539 int diff_opt_parse(struct diff_options *options, const char **av, int ac)
1541 const char *arg = av[0];
1542 if (!strcmp(arg, "-p") || !strcmp(arg, "-u"))
1543 options->output_format |= DIFF_FORMAT_PATCH;
1544 else if (opt_arg(arg, 'U', "unified", &options->context))
1545 options->output_format |= DIFF_FORMAT_PATCH;
1546 else if (!strcmp(arg, "--raw"))
1547 options->output_format |= DIFF_FORMAT_RAW;
1548 else if (!strcmp(arg, "--patch-with-raw")) {
1549 options->output_format |= DIFF_FORMAT_PATCH | DIFF_FORMAT_RAW;
1551 else if (!strcmp(arg, "--stat"))
1552 options->output_format |= DIFF_FORMAT_DIFFSTAT;
1553 else if (!strcmp(arg, "--check"))
1554 options->output_format |= DIFF_FORMAT_CHECKDIFF;
1555 else if (!strcmp(arg, "--summary"))
1556 options->output_format |= DIFF_FORMAT_SUMMARY;
1557 else if (!strcmp(arg, "--patch-with-stat")) {
1558 options->output_format |= DIFF_FORMAT_PATCH | DIFF_FORMAT_DIFFSTAT;
1560 else if (!strcmp(arg, "-z"))
1561 options->line_termination = 0;
1562 else if (!strncmp(arg, "-l", 2))
1563 options->rename_limit = strtoul(arg+2, NULL, 10);
1564 else if (!strcmp(arg, "--full-index"))
1565 options->full_index = 1;
1566 else if (!strcmp(arg, "--binary")) {
1567 options->output_format |= DIFF_FORMAT_PATCH;
1568 options->full_index = options->binary = 1;
1570 else if (!strcmp(arg, "--name-only"))
1571 options->output_format |= DIFF_FORMAT_NAME;
1572 else if (!strcmp(arg, "--name-status"))
1573 options->output_format |= DIFF_FORMAT_NAME_STATUS;
1574 else if (!strcmp(arg, "-R"))
1575 options->reverse_diff = 1;
1576 else if (!strncmp(arg, "-S", 2))
1577 options->pickaxe = arg + 2;
1578 else if (!strcmp(arg, "-s")) {
1579 options->output_format |= DIFF_FORMAT_NO_OUTPUT;
1581 else if (!strncmp(arg, "-O", 2))
1582 options->orderfile = arg + 2;
1583 else if (!strncmp(arg, "--diff-filter=", 14))
1584 options->filter = arg + 14;
1585 else if (!strcmp(arg, "--pickaxe-all"))
1586 options->pickaxe_opts = DIFF_PICKAXE_ALL;
1587 else if (!strcmp(arg, "--pickaxe-regex"))
1588 options->pickaxe_opts = DIFF_PICKAXE_REGEX;
1589 else if (!strncmp(arg, "-B", 2)) {
1590 if ((options->break_opt =
1591 diff_scoreopt_parse(arg)) == -1)
1592 return -1;
1594 else if (!strncmp(arg, "-M", 2)) {
1595 if ((options->rename_score =
1596 diff_scoreopt_parse(arg)) == -1)
1597 return -1;
1598 options->detect_rename = DIFF_DETECT_RENAME;
1600 else if (!strncmp(arg, "-C", 2)) {
1601 if ((options->rename_score =
1602 diff_scoreopt_parse(arg)) == -1)
1603 return -1;
1604 options->detect_rename = DIFF_DETECT_COPY;
1606 else if (!strcmp(arg, "--find-copies-harder"))
1607 options->find_copies_harder = 1;
1608 else if (!strcmp(arg, "--abbrev"))
1609 options->abbrev = DEFAULT_ABBREV;
1610 else if (!strncmp(arg, "--abbrev=", 9)) {
1611 options->abbrev = strtoul(arg + 9, NULL, 10);
1612 if (options->abbrev < MINIMUM_ABBREV)
1613 options->abbrev = MINIMUM_ABBREV;
1614 else if (40 < options->abbrev)
1615 options->abbrev = 40;
1617 else if (!strcmp(arg, "--color"))
1618 options->color_diff = 1;
1619 else if (!strcmp(arg, "-w") || !strcmp(arg, "--ignore-all-space"))
1620 options->xdl_opts |= XDF_IGNORE_WHITESPACE;
1621 else if (!strcmp(arg, "-b") || !strcmp(arg, "--ignore-space-change"))
1622 options->xdl_opts |= XDF_IGNORE_WHITESPACE_CHANGE;
1623 else
1624 return 0;
1625 return 1;
1628 static int parse_num(const char **cp_p)
1630 unsigned long num, scale;
1631 int ch, dot;
1632 const char *cp = *cp_p;
1634 num = 0;
1635 scale = 1;
1636 dot = 0;
1637 for(;;) {
1638 ch = *cp;
1639 if ( !dot && ch == '.' ) {
1640 scale = 1;
1641 dot = 1;
1642 } else if ( ch == '%' ) {
1643 scale = dot ? scale*100 : 100;
1644 cp++; /* % is always at the end */
1645 break;
1646 } else if ( ch >= '0' && ch <= '9' ) {
1647 if ( scale < 100000 ) {
1648 scale *= 10;
1649 num = (num*10) + (ch-'0');
1651 } else {
1652 break;
1654 cp++;
1656 *cp_p = cp;
1658 /* user says num divided by scale and we say internally that
1659 * is MAX_SCORE * num / scale.
1661 return (num >= scale) ? MAX_SCORE : (MAX_SCORE * num / scale);
1664 int diff_scoreopt_parse(const char *opt)
1666 int opt1, opt2, cmd;
1668 if (*opt++ != '-')
1669 return -1;
1670 cmd = *opt++;
1671 if (cmd != 'M' && cmd != 'C' && cmd != 'B')
1672 return -1; /* that is not a -M, -C nor -B option */
1674 opt1 = parse_num(&opt);
1675 if (cmd != 'B')
1676 opt2 = 0;
1677 else {
1678 if (*opt == 0)
1679 opt2 = 0;
1680 else if (*opt != '/')
1681 return -1; /* we expect -B80/99 or -B80 */
1682 else {
1683 opt++;
1684 opt2 = parse_num(&opt);
1687 if (*opt != 0)
1688 return -1;
1689 return opt1 | (opt2 << 16);
1692 struct diff_queue_struct diff_queued_diff;
1694 void diff_q(struct diff_queue_struct *queue, struct diff_filepair *dp)
1696 if (queue->alloc <= queue->nr) {
1697 queue->alloc = alloc_nr(queue->alloc);
1698 queue->queue = xrealloc(queue->queue,
1699 sizeof(dp) * queue->alloc);
1701 queue->queue[queue->nr++] = dp;
1704 struct diff_filepair *diff_queue(struct diff_queue_struct *queue,
1705 struct diff_filespec *one,
1706 struct diff_filespec *two)
1708 struct diff_filepair *dp = xmalloc(sizeof(*dp));
1709 dp->one = one;
1710 dp->two = two;
1711 dp->score = 0;
1712 dp->status = 0;
1713 dp->source_stays = 0;
1714 dp->broken_pair = 0;
1715 if (queue)
1716 diff_q(queue, dp);
1717 return dp;
1720 void diff_free_filepair(struct diff_filepair *p)
1722 diff_free_filespec_data(p->one);
1723 diff_free_filespec_data(p->two);
1724 free(p->one);
1725 free(p->two);
1726 free(p);
1729 /* This is different from find_unique_abbrev() in that
1730 * it stuffs the result with dots for alignment.
1732 const char *diff_unique_abbrev(const unsigned char *sha1, int len)
1734 int abblen;
1735 const char *abbrev;
1736 if (len == 40)
1737 return sha1_to_hex(sha1);
1739 abbrev = find_unique_abbrev(sha1, len);
1740 if (!abbrev)
1741 return sha1_to_hex(sha1);
1742 abblen = strlen(abbrev);
1743 if (abblen < 37) {
1744 static char hex[41];
1745 if (len < abblen && abblen <= len + 2)
1746 sprintf(hex, "%s%.*s", abbrev, len+3-abblen, "..");
1747 else
1748 sprintf(hex, "%s...", abbrev);
1749 return hex;
1751 return sha1_to_hex(sha1);
1754 static void diff_flush_raw(struct diff_filepair *p,
1755 struct diff_options *options)
1757 int two_paths;
1758 char status[10];
1759 int abbrev = options->abbrev;
1760 const char *path_one, *path_two;
1761 int inter_name_termination = '\t';
1762 int line_termination = options->line_termination;
1764 if (!line_termination)
1765 inter_name_termination = 0;
1767 path_one = p->one->path;
1768 path_two = p->two->path;
1769 if (line_termination) {
1770 path_one = quote_one(path_one);
1771 path_two = quote_one(path_two);
1774 if (p->score)
1775 sprintf(status, "%c%03d", p->status,
1776 (int)(0.5 + p->score * 100.0/MAX_SCORE));
1777 else {
1778 status[0] = p->status;
1779 status[1] = 0;
1781 switch (p->status) {
1782 case DIFF_STATUS_COPIED:
1783 case DIFF_STATUS_RENAMED:
1784 two_paths = 1;
1785 break;
1786 case DIFF_STATUS_ADDED:
1787 case DIFF_STATUS_DELETED:
1788 two_paths = 0;
1789 break;
1790 default:
1791 two_paths = 0;
1792 break;
1794 if (!(options->output_format & DIFF_FORMAT_NAME_STATUS)) {
1795 printf(":%06o %06o %s ",
1796 p->one->mode, p->two->mode,
1797 diff_unique_abbrev(p->one->sha1, abbrev));
1798 printf("%s ",
1799 diff_unique_abbrev(p->two->sha1, abbrev));
1801 printf("%s%c%s", status, inter_name_termination, path_one);
1802 if (two_paths)
1803 printf("%c%s", inter_name_termination, path_two);
1804 putchar(line_termination);
1805 if (path_one != p->one->path)
1806 free((void*)path_one);
1807 if (path_two != p->two->path)
1808 free((void*)path_two);
1811 static void diff_flush_name(struct diff_filepair *p, int line_termination)
1813 char *path = p->two->path;
1815 if (line_termination)
1816 path = quote_one(p->two->path);
1817 printf("%s%c", path, line_termination);
1818 if (p->two->path != path)
1819 free(path);
1822 int diff_unmodified_pair(struct diff_filepair *p)
1824 /* This function is written stricter than necessary to support
1825 * the currently implemented transformers, but the idea is to
1826 * let transformers to produce diff_filepairs any way they want,
1827 * and filter and clean them up here before producing the output.
1829 struct diff_filespec *one, *two;
1831 if (DIFF_PAIR_UNMERGED(p))
1832 return 0; /* unmerged is interesting */
1834 one = p->one;
1835 two = p->two;
1837 /* deletion, addition, mode or type change
1838 * and rename are all interesting.
1840 if (DIFF_FILE_VALID(one) != DIFF_FILE_VALID(two) ||
1841 DIFF_PAIR_MODE_CHANGED(p) ||
1842 strcmp(one->path, two->path))
1843 return 0;
1845 /* both are valid and point at the same path. that is, we are
1846 * dealing with a change.
1848 if (one->sha1_valid && two->sha1_valid &&
1849 !memcmp(one->sha1, two->sha1, sizeof(one->sha1)))
1850 return 1; /* no change */
1851 if (!one->sha1_valid && !two->sha1_valid)
1852 return 1; /* both look at the same file on the filesystem. */
1853 return 0;
1856 static void diff_flush_patch(struct diff_filepair *p, struct diff_options *o)
1858 if (diff_unmodified_pair(p))
1859 return;
1861 if ((DIFF_FILE_VALID(p->one) && S_ISDIR(p->one->mode)) ||
1862 (DIFF_FILE_VALID(p->two) && S_ISDIR(p->two->mode)))
1863 return; /* no tree diffs in patch format */
1865 run_diff(p, o);
1868 static void diff_flush_stat(struct diff_filepair *p, struct diff_options *o,
1869 struct diffstat_t *diffstat)
1871 if (diff_unmodified_pair(p))
1872 return;
1874 if ((DIFF_FILE_VALID(p->one) && S_ISDIR(p->one->mode)) ||
1875 (DIFF_FILE_VALID(p->two) && S_ISDIR(p->two->mode)))
1876 return; /* no tree diffs in patch format */
1878 run_diffstat(p, o, diffstat);
1881 static void diff_flush_checkdiff(struct diff_filepair *p,
1882 struct diff_options *o)
1884 if (diff_unmodified_pair(p))
1885 return;
1887 if ((DIFF_FILE_VALID(p->one) && S_ISDIR(p->one->mode)) ||
1888 (DIFF_FILE_VALID(p->two) && S_ISDIR(p->two->mode)))
1889 return; /* no tree diffs in patch format */
1891 run_checkdiff(p, o);
1894 int diff_queue_is_empty(void)
1896 struct diff_queue_struct *q = &diff_queued_diff;
1897 int i;
1898 for (i = 0; i < q->nr; i++)
1899 if (!diff_unmodified_pair(q->queue[i]))
1900 return 0;
1901 return 1;
1904 #if DIFF_DEBUG
1905 void diff_debug_filespec(struct diff_filespec *s, int x, const char *one)
1907 fprintf(stderr, "queue[%d] %s (%s) %s %06o %s\n",
1908 x, one ? one : "",
1909 s->path,
1910 DIFF_FILE_VALID(s) ? "valid" : "invalid",
1911 s->mode,
1912 s->sha1_valid ? sha1_to_hex(s->sha1) : "");
1913 fprintf(stderr, "queue[%d] %s size %lu flags %d\n",
1914 x, one ? one : "",
1915 s->size, s->xfrm_flags);
1918 void diff_debug_filepair(const struct diff_filepair *p, int i)
1920 diff_debug_filespec(p->one, i, "one");
1921 diff_debug_filespec(p->two, i, "two");
1922 fprintf(stderr, "score %d, status %c stays %d broken %d\n",
1923 p->score, p->status ? p->status : '?',
1924 p->source_stays, p->broken_pair);
1927 void diff_debug_queue(const char *msg, struct diff_queue_struct *q)
1929 int i;
1930 if (msg)
1931 fprintf(stderr, "%s\n", msg);
1932 fprintf(stderr, "q->nr = %d\n", q->nr);
1933 for (i = 0; i < q->nr; i++) {
1934 struct diff_filepair *p = q->queue[i];
1935 diff_debug_filepair(p, i);
1938 #endif
1940 static void diff_resolve_rename_copy(void)
1942 int i, j;
1943 struct diff_filepair *p, *pp;
1944 struct diff_queue_struct *q = &diff_queued_diff;
1946 diff_debug_queue("resolve-rename-copy", q);
1948 for (i = 0; i < q->nr; i++) {
1949 p = q->queue[i];
1950 p->status = 0; /* undecided */
1951 if (DIFF_PAIR_UNMERGED(p))
1952 p->status = DIFF_STATUS_UNMERGED;
1953 else if (!DIFF_FILE_VALID(p->one))
1954 p->status = DIFF_STATUS_ADDED;
1955 else if (!DIFF_FILE_VALID(p->two))
1956 p->status = DIFF_STATUS_DELETED;
1957 else if (DIFF_PAIR_TYPE_CHANGED(p))
1958 p->status = DIFF_STATUS_TYPE_CHANGED;
1960 /* from this point on, we are dealing with a pair
1961 * whose both sides are valid and of the same type, i.e.
1962 * either in-place edit or rename/copy edit.
1964 else if (DIFF_PAIR_RENAME(p)) {
1965 if (p->source_stays) {
1966 p->status = DIFF_STATUS_COPIED;
1967 continue;
1969 /* See if there is some other filepair that
1970 * copies from the same source as us. If so
1971 * we are a copy. Otherwise we are either a
1972 * copy if the path stays, or a rename if it
1973 * does not, but we already handled "stays" case.
1975 for (j = i + 1; j < q->nr; j++) {
1976 pp = q->queue[j];
1977 if (strcmp(pp->one->path, p->one->path))
1978 continue; /* not us */
1979 if (!DIFF_PAIR_RENAME(pp))
1980 continue; /* not a rename/copy */
1981 /* pp is a rename/copy from the same source */
1982 p->status = DIFF_STATUS_COPIED;
1983 break;
1985 if (!p->status)
1986 p->status = DIFF_STATUS_RENAMED;
1988 else if (memcmp(p->one->sha1, p->two->sha1, 20) ||
1989 p->one->mode != p->two->mode)
1990 p->status = DIFF_STATUS_MODIFIED;
1991 else {
1992 /* This is a "no-change" entry and should not
1993 * happen anymore, but prepare for broken callers.
1995 error("feeding unmodified %s to diffcore",
1996 p->one->path);
1997 p->status = DIFF_STATUS_UNKNOWN;
2000 diff_debug_queue("resolve-rename-copy done", q);
2003 static int check_pair_status(struct diff_filepair *p)
2005 switch (p->status) {
2006 case DIFF_STATUS_UNKNOWN:
2007 return 0;
2008 case 0:
2009 die("internal error in diff-resolve-rename-copy");
2010 default:
2011 return 1;
2015 static void flush_one_pair(struct diff_filepair *p, struct diff_options *opt)
2017 int fmt = opt->output_format;
2019 if (fmt & DIFF_FORMAT_CHECKDIFF)
2020 diff_flush_checkdiff(p, opt);
2021 else if (fmt & (DIFF_FORMAT_RAW | DIFF_FORMAT_NAME_STATUS))
2022 diff_flush_raw(p, opt);
2023 else if (fmt & DIFF_FORMAT_NAME)
2024 diff_flush_name(p, opt->line_termination);
2027 static void show_file_mode_name(const char *newdelete, struct diff_filespec *fs)
2029 if (fs->mode)
2030 printf(" %s mode %06o %s\n", newdelete, fs->mode, fs->path);
2031 else
2032 printf(" %s %s\n", newdelete, fs->path);
2036 static void show_mode_change(struct diff_filepair *p, int show_name)
2038 if (p->one->mode && p->two->mode && p->one->mode != p->two->mode) {
2039 if (show_name)
2040 printf(" mode change %06o => %06o %s\n",
2041 p->one->mode, p->two->mode, p->two->path);
2042 else
2043 printf(" mode change %06o => %06o\n",
2044 p->one->mode, p->two->mode);
2048 static void show_rename_copy(const char *renamecopy, struct diff_filepair *p)
2050 const char *old, *new;
2052 /* Find common prefix */
2053 old = p->one->path;
2054 new = p->two->path;
2055 while (1) {
2056 const char *slash_old, *slash_new;
2057 slash_old = strchr(old, '/');
2058 slash_new = strchr(new, '/');
2059 if (!slash_old ||
2060 !slash_new ||
2061 slash_old - old != slash_new - new ||
2062 memcmp(old, new, slash_new - new))
2063 break;
2064 old = slash_old + 1;
2065 new = slash_new + 1;
2067 /* p->one->path thru old is the common prefix, and old and new
2068 * through the end of names are renames
2070 if (old != p->one->path)
2071 printf(" %s %.*s{%s => %s} (%d%%)\n", renamecopy,
2072 (int)(old - p->one->path), p->one->path,
2073 old, new, (int)(0.5 + p->score * 100.0/MAX_SCORE));
2074 else
2075 printf(" %s %s => %s (%d%%)\n", renamecopy,
2076 p->one->path, p->two->path,
2077 (int)(0.5 + p->score * 100.0/MAX_SCORE));
2078 show_mode_change(p, 0);
2081 static void diff_summary(struct diff_filepair *p)
2083 switch(p->status) {
2084 case DIFF_STATUS_DELETED:
2085 show_file_mode_name("delete", p->one);
2086 break;
2087 case DIFF_STATUS_ADDED:
2088 show_file_mode_name("create", p->two);
2089 break;
2090 case DIFF_STATUS_COPIED:
2091 show_rename_copy("copy", p);
2092 break;
2093 case DIFF_STATUS_RENAMED:
2094 show_rename_copy("rename", p);
2095 break;
2096 default:
2097 if (p->score) {
2098 printf(" rewrite %s (%d%%)\n", p->two->path,
2099 (int)(0.5 + p->score * 100.0/MAX_SCORE));
2100 show_mode_change(p, 0);
2101 } else show_mode_change(p, 1);
2102 break;
2106 struct patch_id_t {
2107 struct xdiff_emit_state xm;
2108 SHA_CTX *ctx;
2109 int patchlen;
2112 static int remove_space(char *line, int len)
2114 int i;
2115 char *dst = line;
2116 unsigned char c;
2118 for (i = 0; i < len; i++)
2119 if (!isspace((c = line[i])))
2120 *dst++ = c;
2122 return dst - line;
2125 static void patch_id_consume(void *priv, char *line, unsigned long len)
2127 struct patch_id_t *data = priv;
2128 int new_len;
2130 /* Ignore line numbers when computing the SHA1 of the patch */
2131 if (!strncmp(line, "@@ -", 4))
2132 return;
2134 new_len = remove_space(line, len);
2136 SHA1_Update(data->ctx, line, new_len);
2137 data->patchlen += new_len;
2140 /* returns 0 upon success, and writes result into sha1 */
2141 static int diff_get_patch_id(struct diff_options *options, unsigned char *sha1)
2143 struct diff_queue_struct *q = &diff_queued_diff;
2144 int i;
2145 SHA_CTX ctx;
2146 struct patch_id_t data;
2147 char buffer[PATH_MAX * 4 + 20];
2149 SHA1_Init(&ctx);
2150 memset(&data, 0, sizeof(struct patch_id_t));
2151 data.ctx = &ctx;
2152 data.xm.consume = patch_id_consume;
2154 for (i = 0; i < q->nr; i++) {
2155 xpparam_t xpp;
2156 xdemitconf_t xecfg;
2157 xdemitcb_t ecb;
2158 mmfile_t mf1, mf2;
2159 struct diff_filepair *p = q->queue[i];
2160 int len1, len2;
2162 if (p->status == 0)
2163 return error("internal diff status error");
2164 if (p->status == DIFF_STATUS_UNKNOWN)
2165 continue;
2166 if (diff_unmodified_pair(p))
2167 continue;
2168 if ((DIFF_FILE_VALID(p->one) && S_ISDIR(p->one->mode)) ||
2169 (DIFF_FILE_VALID(p->two) && S_ISDIR(p->two->mode)))
2170 continue;
2171 if (DIFF_PAIR_UNMERGED(p))
2172 continue;
2174 diff_fill_sha1_info(p->one);
2175 diff_fill_sha1_info(p->two);
2176 if (fill_mmfile(&mf1, p->one) < 0 ||
2177 fill_mmfile(&mf2, p->two) < 0)
2178 return error("unable to read files to diff");
2180 /* Maybe hash p->two? into the patch id? */
2181 if (mmfile_is_binary(&mf2))
2182 continue;
2184 len1 = remove_space(p->one->path, strlen(p->one->path));
2185 len2 = remove_space(p->two->path, strlen(p->two->path));
2186 if (p->one->mode == 0)
2187 len1 = snprintf(buffer, sizeof(buffer),
2188 "diff--gita/%.*sb/%.*s"
2189 "newfilemode%06o"
2190 "---/dev/null"
2191 "+++b/%.*s",
2192 len1, p->one->path,
2193 len2, p->two->path,
2194 p->two->mode,
2195 len2, p->two->path);
2196 else if (p->two->mode == 0)
2197 len1 = snprintf(buffer, sizeof(buffer),
2198 "diff--gita/%.*sb/%.*s"
2199 "deletedfilemode%06o"
2200 "---a/%.*s"
2201 "+++/dev/null",
2202 len1, p->one->path,
2203 len2, p->two->path,
2204 p->one->mode,
2205 len1, p->one->path);
2206 else
2207 len1 = snprintf(buffer, sizeof(buffer),
2208 "diff--gita/%.*sb/%.*s"
2209 "---a/%.*s"
2210 "+++b/%.*s",
2211 len1, p->one->path,
2212 len2, p->two->path,
2213 len1, p->one->path,
2214 len2, p->two->path);
2215 SHA1_Update(&ctx, buffer, len1);
2217 xpp.flags = XDF_NEED_MINIMAL;
2218 xecfg.ctxlen = 3;
2219 xecfg.flags = XDL_EMIT_FUNCNAMES;
2220 ecb.outf = xdiff_outf;
2221 ecb.priv = &data;
2222 xdl_diff(&mf1, &mf2, &xpp, &xecfg, &ecb);
2225 SHA1_Final(sha1, &ctx);
2226 return 0;
2229 int diff_flush_patch_id(struct diff_options *options, unsigned char *sha1)
2231 struct diff_queue_struct *q = &diff_queued_diff;
2232 int i;
2233 int result = diff_get_patch_id(options, sha1);
2235 for (i = 0; i < q->nr; i++)
2236 diff_free_filepair(q->queue[i]);
2238 free(q->queue);
2239 q->queue = NULL;
2240 q->nr = q->alloc = 0;
2242 return result;
2245 static int is_summary_empty(const struct diff_queue_struct *q)
2247 int i;
2249 for (i = 0; i < q->nr; i++) {
2250 const struct diff_filepair *p = q->queue[i];
2252 switch (p->status) {
2253 case DIFF_STATUS_DELETED:
2254 case DIFF_STATUS_ADDED:
2255 case DIFF_STATUS_COPIED:
2256 case DIFF_STATUS_RENAMED:
2257 return 0;
2258 default:
2259 if (p->score)
2260 return 0;
2261 if (p->one->mode && p->two->mode &&
2262 p->one->mode != p->two->mode)
2263 return 0;
2264 break;
2267 return 1;
2270 void diff_flush(struct diff_options *options)
2272 struct diff_queue_struct *q = &diff_queued_diff;
2273 int i, output_format = options->output_format;
2274 int separator = 0;
2277 * Order: raw, stat, summary, patch
2278 * or: name/name-status/checkdiff (other bits clear)
2280 if (!q->nr)
2281 goto free_queue;
2283 if (output_format & (DIFF_FORMAT_RAW |
2284 DIFF_FORMAT_NAME |
2285 DIFF_FORMAT_NAME_STATUS |
2286 DIFF_FORMAT_CHECKDIFF)) {
2287 for (i = 0; i < q->nr; i++) {
2288 struct diff_filepair *p = q->queue[i];
2289 if (check_pair_status(p))
2290 flush_one_pair(p, options);
2292 separator++;
2295 if (output_format & DIFF_FORMAT_DIFFSTAT) {
2296 struct diffstat_t diffstat;
2298 memset(&diffstat, 0, sizeof(struct diffstat_t));
2299 diffstat.xm.consume = diffstat_consume;
2300 for (i = 0; i < q->nr; i++) {
2301 struct diff_filepair *p = q->queue[i];
2302 if (check_pair_status(p))
2303 diff_flush_stat(p, options, &diffstat);
2305 show_stats(&diffstat);
2306 separator++;
2309 if (output_format & DIFF_FORMAT_SUMMARY && !is_summary_empty(q)) {
2310 for (i = 0; i < q->nr; i++)
2311 diff_summary(q->queue[i]);
2312 separator++;
2315 if (output_format & DIFF_FORMAT_PATCH) {
2316 if (separator) {
2317 if (options->stat_sep) {
2318 /* attach patch instead of inline */
2319 fputs(options->stat_sep, stdout);
2320 } else {
2321 putchar(options->line_termination);
2325 for (i = 0; i < q->nr; i++) {
2326 struct diff_filepair *p = q->queue[i];
2327 if (check_pair_status(p))
2328 diff_flush_patch(p, options);
2332 for (i = 0; i < q->nr; i++)
2333 diff_free_filepair(q->queue[i]);
2334 free_queue:
2335 free(q->queue);
2336 q->queue = NULL;
2337 q->nr = q->alloc = 0;
2340 static void diffcore_apply_filter(const char *filter)
2342 int i;
2343 struct diff_queue_struct *q = &diff_queued_diff;
2344 struct diff_queue_struct outq;
2345 outq.queue = NULL;
2346 outq.nr = outq.alloc = 0;
2348 if (!filter)
2349 return;
2351 if (strchr(filter, DIFF_STATUS_FILTER_AON)) {
2352 int found;
2353 for (i = found = 0; !found && i < q->nr; i++) {
2354 struct diff_filepair *p = q->queue[i];
2355 if (((p->status == DIFF_STATUS_MODIFIED) &&
2356 ((p->score &&
2357 strchr(filter, DIFF_STATUS_FILTER_BROKEN)) ||
2358 (!p->score &&
2359 strchr(filter, DIFF_STATUS_MODIFIED)))) ||
2360 ((p->status != DIFF_STATUS_MODIFIED) &&
2361 strchr(filter, p->status)))
2362 found++;
2364 if (found)
2365 return;
2367 /* otherwise we will clear the whole queue
2368 * by copying the empty outq at the end of this
2369 * function, but first clear the current entries
2370 * in the queue.
2372 for (i = 0; i < q->nr; i++)
2373 diff_free_filepair(q->queue[i]);
2375 else {
2376 /* Only the matching ones */
2377 for (i = 0; i < q->nr; i++) {
2378 struct diff_filepair *p = q->queue[i];
2380 if (((p->status == DIFF_STATUS_MODIFIED) &&
2381 ((p->score &&
2382 strchr(filter, DIFF_STATUS_FILTER_BROKEN)) ||
2383 (!p->score &&
2384 strchr(filter, DIFF_STATUS_MODIFIED)))) ||
2385 ((p->status != DIFF_STATUS_MODIFIED) &&
2386 strchr(filter, p->status)))
2387 diff_q(&outq, p);
2388 else
2389 diff_free_filepair(p);
2392 free(q->queue);
2393 *q = outq;
2396 void diffcore_std(struct diff_options *options)
2398 if (options->break_opt != -1)
2399 diffcore_break(options->break_opt);
2400 if (options->detect_rename)
2401 diffcore_rename(options);
2402 if (options->break_opt != -1)
2403 diffcore_merge_broken();
2404 if (options->pickaxe)
2405 diffcore_pickaxe(options->pickaxe, options->pickaxe_opts);
2406 if (options->orderfile)
2407 diffcore_order(options->orderfile);
2408 diff_resolve_rename_copy();
2409 diffcore_apply_filter(options->filter);
2413 void diffcore_std_no_resolve(struct diff_options *options)
2415 if (options->pickaxe)
2416 diffcore_pickaxe(options->pickaxe, options->pickaxe_opts);
2417 if (options->orderfile)
2418 diffcore_order(options->orderfile);
2419 diffcore_apply_filter(options->filter);
2422 void diff_addremove(struct diff_options *options,
2423 int addremove, unsigned mode,
2424 const unsigned char *sha1,
2425 const char *base, const char *path)
2427 char concatpath[PATH_MAX];
2428 struct diff_filespec *one, *two;
2430 /* This may look odd, but it is a preparation for
2431 * feeding "there are unchanged files which should
2432 * not produce diffs, but when you are doing copy
2433 * detection you would need them, so here they are"
2434 * entries to the diff-core. They will be prefixed
2435 * with something like '=' or '*' (I haven't decided
2436 * which but should not make any difference).
2437 * Feeding the same new and old to diff_change()
2438 * also has the same effect.
2439 * Before the final output happens, they are pruned after
2440 * merged into rename/copy pairs as appropriate.
2442 if (options->reverse_diff)
2443 addremove = (addremove == '+' ? '-' :
2444 addremove == '-' ? '+' : addremove);
2446 if (!path) path = "";
2447 sprintf(concatpath, "%s%s", base, path);
2448 one = alloc_filespec(concatpath);
2449 two = alloc_filespec(concatpath);
2451 if (addremove != '+')
2452 fill_filespec(one, sha1, mode);
2453 if (addremove != '-')
2454 fill_filespec(two, sha1, mode);
2456 diff_queue(&diff_queued_diff, one, two);
2459 void diff_change(struct diff_options *options,
2460 unsigned old_mode, unsigned new_mode,
2461 const unsigned char *old_sha1,
2462 const unsigned char *new_sha1,
2463 const char *base, const char *path)
2465 char concatpath[PATH_MAX];
2466 struct diff_filespec *one, *two;
2468 if (options->reverse_diff) {
2469 unsigned tmp;
2470 const unsigned char *tmp_c;
2471 tmp = old_mode; old_mode = new_mode; new_mode = tmp;
2472 tmp_c = old_sha1; old_sha1 = new_sha1; new_sha1 = tmp_c;
2474 if (!path) path = "";
2475 sprintf(concatpath, "%s%s", base, path);
2476 one = alloc_filespec(concatpath);
2477 two = alloc_filespec(concatpath);
2478 fill_filespec(one, old_sha1, old_mode);
2479 fill_filespec(two, new_sha1, new_mode);
2481 diff_queue(&diff_queued_diff, one, two);
2484 void diff_unmerge(struct diff_options *options,
2485 const char *path)
2487 struct diff_filespec *one, *two;
2488 one = alloc_filespec(path);
2489 two = alloc_filespec(path);
2490 diff_queue(&diff_queued_diff, one, two);