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