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