]> asedeno.scripts.mit.edu Git - git.git/blob - diff.c
Merge branch 'jc/archive'
[git.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;
17 static int diff_rename_limit_default = -1;
18 static int diff_use_color_default;
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 xstrdup(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 = xstrdup(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_body(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         /* We could do deflated delta, or we could do just deflated two,
853          * whichever is smaller.
854          */
855         delta = NULL;
856         deflated = deflate_it(two->ptr, two->size, &deflate_size);
857         if (one->size && two->size) {
858                 delta = diff_delta(one->ptr, one->size,
859                                    two->ptr, two->size,
860                                    &delta_size, deflate_size);
861                 if (delta) {
862                         void *to_free = delta;
863                         orig_size = delta_size;
864                         delta = deflate_it(delta, delta_size, &delta_size);
865                         free(to_free);
866                 }
867         }
868
869         if (delta && delta_size < deflate_size) {
870                 printf("delta %lu\n", orig_size);
871                 free(deflated);
872                 data = delta;
873                 data_size = delta_size;
874         }
875         else {
876                 printf("literal %lu\n", two->size);
877                 free(delta);
878                 data = deflated;
879                 data_size = deflate_size;
880         }
881
882         /* emit data encoded in base85 */
883         cp = data;
884         while (data_size) {
885                 int bytes = (52 < data_size) ? 52 : data_size;
886                 char line[70];
887                 data_size -= bytes;
888                 if (bytes <= 26)
889                         line[0] = bytes + 'A' - 1;
890                 else
891                         line[0] = bytes - 26 + 'a' - 1;
892                 encode_85(line + 1, cp, bytes);
893                 cp = (char *) cp + bytes;
894                 puts(line);
895         }
896         printf("\n");
897         free(data);
898 }
899
900 static void emit_binary_diff(mmfile_t *one, mmfile_t *two)
901 {
902         printf("GIT binary patch\n");
903         emit_binary_diff_body(one, two);
904         emit_binary_diff_body(two, one);
905 }
906
907 #define FIRST_FEW_BYTES 8000
908 static int mmfile_is_binary(mmfile_t *mf)
909 {
910         long sz = mf->size;
911         if (FIRST_FEW_BYTES < sz)
912                 sz = FIRST_FEW_BYTES;
913         return !!memchr(mf->ptr, 0, sz);
914 }
915
916 static void builtin_diff(const char *name_a,
917                          const char *name_b,
918                          struct diff_filespec *one,
919                          struct diff_filespec *two,
920                          const char *xfrm_msg,
921                          struct diff_options *o,
922                          int complete_rewrite)
923 {
924         mmfile_t mf1, mf2;
925         const char *lbl[2];
926         char *a_one, *b_two;
927         const char *set = diff_get_color(o->color_diff, DIFF_METAINFO);
928         const char *reset = diff_get_color(o->color_diff, DIFF_RESET);
929
930         a_one = quote_two("a/", name_a);
931         b_two = quote_two("b/", name_b);
932         lbl[0] = DIFF_FILE_VALID(one) ? a_one : "/dev/null";
933         lbl[1] = DIFF_FILE_VALID(two) ? b_two : "/dev/null";
934         printf("%sdiff --git %s %s%s\n", set, a_one, b_two, reset);
935         if (lbl[0][0] == '/') {
936                 /* /dev/null */
937                 printf("%snew file mode %06o%s\n", set, two->mode, reset);
938                 if (xfrm_msg && xfrm_msg[0])
939                         printf("%s%s%s\n", set, xfrm_msg, reset);
940         }
941         else if (lbl[1][0] == '/') {
942                 printf("%sdeleted file mode %06o%s\n", set, one->mode, reset);
943                 if (xfrm_msg && xfrm_msg[0])
944                         printf("%s%s%s\n", set, xfrm_msg, reset);
945         }
946         else {
947                 if (one->mode != two->mode) {
948                         printf("%sold mode %06o%s\n", set, one->mode, reset);
949                         printf("%snew mode %06o%s\n", set, two->mode, reset);
950                 }
951                 if (xfrm_msg && xfrm_msg[0])
952                         printf("%s%s%s\n", set, xfrm_msg, reset);
953                 /*
954                  * we do not run diff between different kind
955                  * of objects.
956                  */
957                 if ((one->mode ^ two->mode) & S_IFMT)
958                         goto free_ab_and_return;
959                 if (complete_rewrite) {
960                         emit_rewrite_diff(name_a, name_b, one, two);
961                         goto free_ab_and_return;
962                 }
963         }
964
965         if (fill_mmfile(&mf1, one) < 0 || fill_mmfile(&mf2, two) < 0)
966                 die("unable to read files to diff");
967
968         if (!o->text && (mmfile_is_binary(&mf1) || mmfile_is_binary(&mf2))) {
969                 /* Quite common confusing case */
970                 if (mf1.size == mf2.size &&
971                     !memcmp(mf1.ptr, mf2.ptr, mf1.size))
972                         goto free_ab_and_return;
973                 if (o->binary)
974                         emit_binary_diff(&mf1, &mf2);
975                 else
976                         printf("Binary files %s and %s differ\n",
977                                lbl[0], lbl[1]);
978         }
979         else {
980                 /* Crazy xdl interfaces.. */
981                 const char *diffopts = getenv("GIT_DIFF_OPTS");
982                 xpparam_t xpp;
983                 xdemitconf_t xecfg;
984                 xdemitcb_t ecb;
985                 struct emit_callback ecbdata;
986
987                 memset(&ecbdata, 0, sizeof(ecbdata));
988                 ecbdata.label_path = lbl;
989                 ecbdata.color_diff = o->color_diff;
990                 xpp.flags = XDF_NEED_MINIMAL | o->xdl_opts;
991                 xecfg.ctxlen = o->context;
992                 xecfg.flags = XDL_EMIT_FUNCNAMES;
993                 if (!diffopts)
994                         ;
995                 else if (!strncmp(diffopts, "--unified=", 10))
996                         xecfg.ctxlen = strtoul(diffopts + 10, NULL, 10);
997                 else if (!strncmp(diffopts, "-u", 2))
998                         xecfg.ctxlen = strtoul(diffopts + 2, NULL, 10);
999                 ecb.outf = xdiff_outf;
1000                 ecb.priv = &ecbdata;
1001                 ecbdata.xm.consume = fn_out_consume;
1002                 if (o->color_diff_words)
1003                         ecbdata.diff_words =
1004                                 xcalloc(1, sizeof(struct diff_words_data));
1005                 xdl_diff(&mf1, &mf2, &xpp, &xecfg, &ecb);
1006                 if (o->color_diff_words)
1007                         free_diff_words_data(&ecbdata);
1008         }
1009
1010  free_ab_and_return:
1011         free(a_one);
1012         free(b_two);
1013         return;
1014 }
1015
1016 static void builtin_diffstat(const char *name_a, const char *name_b,
1017                              struct diff_filespec *one,
1018                              struct diff_filespec *two,
1019                              struct diffstat_t *diffstat,
1020                              struct diff_options *o,
1021                              int complete_rewrite)
1022 {
1023         mmfile_t mf1, mf2;
1024         struct diffstat_file *data;
1025
1026         data = diffstat_add(diffstat, name_a, name_b);
1027
1028         if (!one || !two) {
1029                 data->is_unmerged = 1;
1030                 return;
1031         }
1032         if (complete_rewrite) {
1033                 diff_populate_filespec(one, 0);
1034                 diff_populate_filespec(two, 0);
1035                 data->deleted = count_lines(one->data, one->size);
1036                 data->added = count_lines(two->data, two->size);
1037                 return;
1038         }
1039         if (fill_mmfile(&mf1, one) < 0 || fill_mmfile(&mf2, two) < 0)
1040                 die("unable to read files to diff");
1041
1042         if (mmfile_is_binary(&mf1) || mmfile_is_binary(&mf2))
1043                 data->is_binary = 1;
1044         else {
1045                 /* Crazy xdl interfaces.. */
1046                 xpparam_t xpp;
1047                 xdemitconf_t xecfg;
1048                 xdemitcb_t ecb;
1049
1050                 xpp.flags = XDF_NEED_MINIMAL | o->xdl_opts;
1051                 xecfg.ctxlen = 0;
1052                 xecfg.flags = 0;
1053                 ecb.outf = xdiff_outf;
1054                 ecb.priv = diffstat;
1055                 xdl_diff(&mf1, &mf2, &xpp, &xecfg, &ecb);
1056         }
1057 }
1058
1059 static void builtin_checkdiff(const char *name_a, const char *name_b,
1060                              struct diff_filespec *one,
1061                              struct diff_filespec *two)
1062 {
1063         mmfile_t mf1, mf2;
1064         struct checkdiff_t data;
1065
1066         if (!two)
1067                 return;
1068
1069         memset(&data, 0, sizeof(data));
1070         data.xm.consume = checkdiff_consume;
1071         data.filename = name_b ? name_b : name_a;
1072         data.lineno = 0;
1073
1074         if (fill_mmfile(&mf1, one) < 0 || fill_mmfile(&mf2, two) < 0)
1075                 die("unable to read files to diff");
1076
1077         if (mmfile_is_binary(&mf2))
1078                 return;
1079         else {
1080                 /* Crazy xdl interfaces.. */
1081                 xpparam_t xpp;
1082                 xdemitconf_t xecfg;
1083                 xdemitcb_t ecb;
1084
1085                 xpp.flags = XDF_NEED_MINIMAL;
1086                 xecfg.ctxlen = 0;
1087                 xecfg.flags = 0;
1088                 ecb.outf = xdiff_outf;
1089                 ecb.priv = &data;
1090                 xdl_diff(&mf1, &mf2, &xpp, &xecfg, &ecb);
1091         }
1092 }
1093
1094 struct diff_filespec *alloc_filespec(const char *path)
1095 {
1096         int namelen = strlen(path);
1097         struct diff_filespec *spec = xmalloc(sizeof(*spec) + namelen + 1);
1098
1099         memset(spec, 0, sizeof(*spec));
1100         spec->path = (char *)(spec + 1);
1101         memcpy(spec->path, path, namelen+1);
1102         return spec;
1103 }
1104
1105 void fill_filespec(struct diff_filespec *spec, const unsigned char *sha1,
1106                    unsigned short mode)
1107 {
1108         if (mode) {
1109                 spec->mode = canon_mode(mode);
1110                 hashcpy(spec->sha1, sha1);
1111                 spec->sha1_valid = !is_null_sha1(sha1);
1112         }
1113 }
1114
1115 /*
1116  * Given a name and sha1 pair, if the dircache tells us the file in
1117  * the work tree has that object contents, return true, so that
1118  * prepare_temp_file() does not have to inflate and extract.
1119  */
1120 static int work_tree_matches(const char *name, const unsigned char *sha1)
1121 {
1122         struct cache_entry *ce;
1123         struct stat st;
1124         int pos, len;
1125
1126         /* We do not read the cache ourselves here, because the
1127          * benchmark with my previous version that always reads cache
1128          * shows that it makes things worse for diff-tree comparing
1129          * two linux-2.6 kernel trees in an already checked out work
1130          * tree.  This is because most diff-tree comparisons deal with
1131          * only a small number of files, while reading the cache is
1132          * expensive for a large project, and its cost outweighs the
1133          * savings we get by not inflating the object to a temporary
1134          * file.  Practically, this code only helps when we are used
1135          * by diff-cache --cached, which does read the cache before
1136          * calling us.
1137          */
1138         if (!active_cache)
1139                 return 0;
1140
1141         len = strlen(name);
1142         pos = cache_name_pos(name, len);
1143         if (pos < 0)
1144                 return 0;
1145         ce = active_cache[pos];
1146         if ((lstat(name, &st) < 0) ||
1147             !S_ISREG(st.st_mode) || /* careful! */
1148             ce_match_stat(ce, &st, 0) ||
1149             hashcmp(sha1, ce->sha1))
1150                 return 0;
1151         /* we return 1 only when we can stat, it is a regular file,
1152          * stat information matches, and sha1 recorded in the cache
1153          * matches.  I.e. we know the file in the work tree really is
1154          * the same as the <name, sha1> pair.
1155          */
1156         return 1;
1157 }
1158
1159 static struct sha1_size_cache {
1160         unsigned char sha1[20];
1161         unsigned long size;
1162 } **sha1_size_cache;
1163 static int sha1_size_cache_nr, sha1_size_cache_alloc;
1164
1165 static struct sha1_size_cache *locate_size_cache(unsigned char *sha1,
1166                                                  int find_only,
1167                                                  unsigned long size)
1168 {
1169         int first, last;
1170         struct sha1_size_cache *e;
1171
1172         first = 0;
1173         last = sha1_size_cache_nr;
1174         while (last > first) {
1175                 int cmp, next = (last + first) >> 1;
1176                 e = sha1_size_cache[next];
1177                 cmp = hashcmp(e->sha1, sha1);
1178                 if (!cmp)
1179                         return e;
1180                 if (cmp < 0) {
1181                         last = next;
1182                         continue;
1183                 }
1184                 first = next+1;
1185         }
1186         /* not found */
1187         if (find_only)
1188                 return NULL;
1189         /* insert to make it at "first" */
1190         if (sha1_size_cache_alloc <= sha1_size_cache_nr) {
1191                 sha1_size_cache_alloc = alloc_nr(sha1_size_cache_alloc);
1192                 sha1_size_cache = xrealloc(sha1_size_cache,
1193                                            sha1_size_cache_alloc *
1194                                            sizeof(*sha1_size_cache));
1195         }
1196         sha1_size_cache_nr++;
1197         if (first < sha1_size_cache_nr)
1198                 memmove(sha1_size_cache + first + 1, sha1_size_cache + first,
1199                         (sha1_size_cache_nr - first - 1) *
1200                         sizeof(*sha1_size_cache));
1201         e = xmalloc(sizeof(struct sha1_size_cache));
1202         sha1_size_cache[first] = e;
1203         hashcpy(e->sha1, sha1);
1204         e->size = size;
1205         return e;
1206 }
1207
1208 /*
1209  * While doing rename detection and pickaxe operation, we may need to
1210  * grab the data for the blob (or file) for our own in-core comparison.
1211  * diff_filespec has data and size fields for this purpose.
1212  */
1213 int diff_populate_filespec(struct diff_filespec *s, int size_only)
1214 {
1215         int err = 0;
1216         if (!DIFF_FILE_VALID(s))
1217                 die("internal error: asking to populate invalid file.");
1218         if (S_ISDIR(s->mode))
1219                 return -1;
1220
1221         if (!use_size_cache)
1222                 size_only = 0;
1223
1224         if (s->data)
1225                 return err;
1226         if (!s->sha1_valid ||
1227             work_tree_matches(s->path, s->sha1)) {
1228                 struct stat st;
1229                 int fd;
1230                 if (lstat(s->path, &st) < 0) {
1231                         if (errno == ENOENT) {
1232                         err_empty:
1233                                 err = -1;
1234                         empty:
1235                                 s->data = (char *)"";
1236                                 s->size = 0;
1237                                 return err;
1238                         }
1239                 }
1240                 s->size = st.st_size;
1241                 if (!s->size)
1242                         goto empty;
1243                 if (size_only)
1244                         return 0;
1245                 if (S_ISLNK(st.st_mode)) {
1246                         int ret;
1247                         s->data = xmalloc(s->size);
1248                         s->should_free = 1;
1249                         ret = readlink(s->path, s->data, s->size);
1250                         if (ret < 0) {
1251                                 free(s->data);
1252                                 goto err_empty;
1253                         }
1254                         return 0;
1255                 }
1256                 fd = open(s->path, O_RDONLY);
1257                 if (fd < 0)
1258                         goto err_empty;
1259                 s->data = mmap(NULL, s->size, PROT_READ, MAP_PRIVATE, fd, 0);
1260                 close(fd);
1261                 if (s->data == MAP_FAILED)
1262                         goto err_empty;
1263                 s->should_munmap = 1;
1264         }
1265         else {
1266                 char type[20];
1267                 struct sha1_size_cache *e;
1268
1269                 if (size_only) {
1270                         e = locate_size_cache(s->sha1, 1, 0);
1271                         if (e) {
1272                                 s->size = e->size;
1273                                 return 0;
1274                         }
1275                         if (!sha1_object_info(s->sha1, type, &s->size))
1276                                 locate_size_cache(s->sha1, 0, s->size);
1277                 }
1278                 else {
1279                         s->data = read_sha1_file(s->sha1, type, &s->size);
1280                         s->should_free = 1;
1281                 }
1282         }
1283         return 0;
1284 }
1285
1286 void diff_free_filespec_data(struct diff_filespec *s)
1287 {
1288         if (s->should_free)
1289                 free(s->data);
1290         else if (s->should_munmap)
1291                 munmap(s->data, s->size);
1292         s->should_free = s->should_munmap = 0;
1293         s->data = NULL;
1294         free(s->cnt_data);
1295         s->cnt_data = NULL;
1296 }
1297
1298 static void prep_temp_blob(struct diff_tempfile *temp,
1299                            void *blob,
1300                            unsigned long size,
1301                            const unsigned char *sha1,
1302                            int mode)
1303 {
1304         int fd;
1305
1306         fd = git_mkstemp(temp->tmp_path, TEMPFILE_PATH_LEN, ".diff_XXXXXX");
1307         if (fd < 0)
1308                 die("unable to create temp-file");
1309         if (write(fd, blob, size) != size)
1310                 die("unable to write temp-file");
1311         close(fd);
1312         temp->name = temp->tmp_path;
1313         strcpy(temp->hex, sha1_to_hex(sha1));
1314         temp->hex[40] = 0;
1315         sprintf(temp->mode, "%06o", mode);
1316 }
1317
1318 static void prepare_temp_file(const char *name,
1319                               struct diff_tempfile *temp,
1320                               struct diff_filespec *one)
1321 {
1322         if (!DIFF_FILE_VALID(one)) {
1323         not_a_valid_file:
1324                 /* A '-' entry produces this for file-2, and
1325                  * a '+' entry produces this for file-1.
1326                  */
1327                 temp->name = "/dev/null";
1328                 strcpy(temp->hex, ".");
1329                 strcpy(temp->mode, ".");
1330                 return;
1331         }
1332
1333         if (!one->sha1_valid ||
1334             work_tree_matches(name, one->sha1)) {
1335                 struct stat st;
1336                 if (lstat(name, &st) < 0) {
1337                         if (errno == ENOENT)
1338                                 goto not_a_valid_file;
1339                         die("stat(%s): %s", name, strerror(errno));
1340                 }
1341                 if (S_ISLNK(st.st_mode)) {
1342                         int ret;
1343                         char buf[PATH_MAX + 1]; /* ought to be SYMLINK_MAX */
1344                         if (sizeof(buf) <= st.st_size)
1345                                 die("symlink too long: %s", name);
1346                         ret = readlink(name, buf, st.st_size);
1347                         if (ret < 0)
1348                                 die("readlink(%s)", name);
1349                         prep_temp_blob(temp, buf, st.st_size,
1350                                        (one->sha1_valid ?
1351                                         one->sha1 : null_sha1),
1352                                        (one->sha1_valid ?
1353                                         one->mode : S_IFLNK));
1354                 }
1355                 else {
1356                         /* we can borrow from the file in the work tree */
1357                         temp->name = name;
1358                         if (!one->sha1_valid)
1359                                 strcpy(temp->hex, sha1_to_hex(null_sha1));
1360                         else
1361                                 strcpy(temp->hex, sha1_to_hex(one->sha1));
1362                         /* Even though we may sometimes borrow the
1363                          * contents from the work tree, we always want
1364                          * one->mode.  mode is trustworthy even when
1365                          * !(one->sha1_valid), as long as
1366                          * DIFF_FILE_VALID(one).
1367                          */
1368                         sprintf(temp->mode, "%06o", one->mode);
1369                 }
1370                 return;
1371         }
1372         else {
1373                 if (diff_populate_filespec(one, 0))
1374                         die("cannot read data blob for %s", one->path);
1375                 prep_temp_blob(temp, one->data, one->size,
1376                                one->sha1, one->mode);
1377         }
1378 }
1379
1380 static void remove_tempfile(void)
1381 {
1382         int i;
1383
1384         for (i = 0; i < 2; i++)
1385                 if (diff_temp[i].name == diff_temp[i].tmp_path) {
1386                         unlink(diff_temp[i].name);
1387                         diff_temp[i].name = NULL;
1388                 }
1389 }
1390
1391 static void remove_tempfile_on_signal(int signo)
1392 {
1393         remove_tempfile();
1394         signal(SIGINT, SIG_DFL);
1395         raise(signo);
1396 }
1397
1398 static int spawn_prog(const char *pgm, const char **arg)
1399 {
1400         pid_t pid;
1401         int status;
1402
1403         fflush(NULL);
1404         pid = fork();
1405         if (pid < 0)
1406                 die("unable to fork");
1407         if (!pid) {
1408                 execvp(pgm, (char *const*) arg);
1409                 exit(255);
1410         }
1411
1412         while (waitpid(pid, &status, 0) < 0) {
1413                 if (errno == EINTR)
1414                         continue;
1415                 return -1;
1416         }
1417
1418         /* Earlier we did not check the exit status because
1419          * diff exits non-zero if files are different, and
1420          * we are not interested in knowing that.  It was a
1421          * mistake which made it harder to quit a diff-*
1422          * session that uses the git-apply-patch-script as
1423          * the GIT_EXTERNAL_DIFF.  A custom GIT_EXTERNAL_DIFF
1424          * should also exit non-zero only when it wants to
1425          * abort the entire diff-* session.
1426          */
1427         if (WIFEXITED(status) && !WEXITSTATUS(status))
1428                 return 0;
1429         return -1;
1430 }
1431
1432 /* An external diff command takes:
1433  *
1434  * diff-cmd name infile1 infile1-sha1 infile1-mode \
1435  *               infile2 infile2-sha1 infile2-mode [ rename-to ]
1436  *
1437  */
1438 static void run_external_diff(const char *pgm,
1439                               const char *name,
1440                               const char *other,
1441                               struct diff_filespec *one,
1442                               struct diff_filespec *two,
1443                               const char *xfrm_msg,
1444                               int complete_rewrite)
1445 {
1446         const char *spawn_arg[10];
1447         struct diff_tempfile *temp = diff_temp;
1448         int retval;
1449         static int atexit_asked = 0;
1450         const char *othername;
1451         const char **arg = &spawn_arg[0];
1452
1453         othername = (other? other : name);
1454         if (one && two) {
1455                 prepare_temp_file(name, &temp[0], one);
1456                 prepare_temp_file(othername, &temp[1], two);
1457                 if (! atexit_asked &&
1458                     (temp[0].name == temp[0].tmp_path ||
1459                      temp[1].name == temp[1].tmp_path)) {
1460                         atexit_asked = 1;
1461                         atexit(remove_tempfile);
1462                 }
1463                 signal(SIGINT, remove_tempfile_on_signal);
1464         }
1465
1466         if (one && two) {
1467                 *arg++ = pgm;
1468                 *arg++ = name;
1469                 *arg++ = temp[0].name;
1470                 *arg++ = temp[0].hex;
1471                 *arg++ = temp[0].mode;
1472                 *arg++ = temp[1].name;
1473                 *arg++ = temp[1].hex;
1474                 *arg++ = temp[1].mode;
1475                 if (other) {
1476                         *arg++ = other;
1477                         *arg++ = xfrm_msg;
1478                 }
1479         } else {
1480                 *arg++ = pgm;
1481                 *arg++ = name;
1482         }
1483         *arg = NULL;
1484         retval = spawn_prog(pgm, spawn_arg);
1485         remove_tempfile();
1486         if (retval) {
1487                 fprintf(stderr, "external diff died, stopping at %s.\n", name);
1488                 exit(1);
1489         }
1490 }
1491
1492 static void run_diff_cmd(const char *pgm,
1493                          const char *name,
1494                          const char *other,
1495                          struct diff_filespec *one,
1496                          struct diff_filespec *two,
1497                          const char *xfrm_msg,
1498                          struct diff_options *o,
1499                          int complete_rewrite)
1500 {
1501         if (pgm) {
1502                 run_external_diff(pgm, name, other, one, two, xfrm_msg,
1503                                   complete_rewrite);
1504                 return;
1505         }
1506         if (one && two)
1507                 builtin_diff(name, other ? other : name,
1508                              one, two, xfrm_msg, o, complete_rewrite);
1509         else
1510                 printf("* Unmerged path %s\n", name);
1511 }
1512
1513 static void diff_fill_sha1_info(struct diff_filespec *one)
1514 {
1515         if (DIFF_FILE_VALID(one)) {
1516                 if (!one->sha1_valid) {
1517                         struct stat st;
1518                         if (lstat(one->path, &st) < 0)
1519                                 die("stat %s", one->path);
1520                         if (index_path(one->sha1, one->path, &st, 0))
1521                                 die("cannot hash %s\n", one->path);
1522                 }
1523         }
1524         else
1525                 hashclr(one->sha1);
1526 }
1527
1528 static void run_diff(struct diff_filepair *p, struct diff_options *o)
1529 {
1530         const char *pgm = external_diff();
1531         char msg[PATH_MAX*2+300], *xfrm_msg;
1532         struct diff_filespec *one;
1533         struct diff_filespec *two;
1534         const char *name;
1535         const char *other;
1536         char *name_munged, *other_munged;
1537         int complete_rewrite = 0;
1538         int len;
1539
1540         if (DIFF_PAIR_UNMERGED(p)) {
1541                 /* unmerged */
1542                 run_diff_cmd(pgm, p->one->path, NULL, NULL, NULL, NULL, o, 0);
1543                 return;
1544         }
1545
1546         name = p->one->path;
1547         other = (strcmp(name, p->two->path) ? p->two->path : NULL);
1548         name_munged = quote_one(name);
1549         other_munged = quote_one(other);
1550         one = p->one; two = p->two;
1551
1552         diff_fill_sha1_info(one);
1553         diff_fill_sha1_info(two);
1554
1555         len = 0;
1556         switch (p->status) {
1557         case DIFF_STATUS_COPIED:
1558                 len += snprintf(msg + len, sizeof(msg) - len,
1559                                 "similarity index %d%%\n"
1560                                 "copy from %s\n"
1561                                 "copy to %s\n",
1562                                 (int)(0.5 + p->score * 100.0/MAX_SCORE),
1563                                 name_munged, other_munged);
1564                 break;
1565         case DIFF_STATUS_RENAMED:
1566                 len += snprintf(msg + len, sizeof(msg) - len,
1567                                 "similarity index %d%%\n"
1568                                 "rename from %s\n"
1569                                 "rename to %s\n",
1570                                 (int)(0.5 + p->score * 100.0/MAX_SCORE),
1571                                 name_munged, other_munged);
1572                 break;
1573         case DIFF_STATUS_MODIFIED:
1574                 if (p->score) {
1575                         len += snprintf(msg + len, sizeof(msg) - len,
1576                                         "dissimilarity index %d%%\n",
1577                                         (int)(0.5 + p->score *
1578                                               100.0/MAX_SCORE));
1579                         complete_rewrite = 1;
1580                         break;
1581                 }
1582                 /* fallthru */
1583         default:
1584                 /* nothing */
1585                 ;
1586         }
1587
1588         if (hashcmp(one->sha1, two->sha1)) {
1589                 int abbrev = o->full_index ? 40 : DEFAULT_ABBREV;
1590
1591                 if (o->binary) {
1592                         mmfile_t mf;
1593                         if ((!fill_mmfile(&mf, one) && mmfile_is_binary(&mf)) ||
1594                             (!fill_mmfile(&mf, two) && mmfile_is_binary(&mf)))
1595                                 abbrev = 40;
1596                 }
1597                 len += snprintf(msg + len, sizeof(msg) - len,
1598                                 "index %.*s..%.*s",
1599                                 abbrev, sha1_to_hex(one->sha1),
1600                                 abbrev, sha1_to_hex(two->sha1));
1601                 if (one->mode == two->mode)
1602                         len += snprintf(msg + len, sizeof(msg) - len,
1603                                         " %06o", one->mode);
1604                 len += snprintf(msg + len, sizeof(msg) - len, "\n");
1605         }
1606
1607         if (len)
1608                 msg[--len] = 0;
1609         xfrm_msg = len ? msg : NULL;
1610
1611         if (!pgm &&
1612             DIFF_FILE_VALID(one) && DIFF_FILE_VALID(two) &&
1613             (S_IFMT & one->mode) != (S_IFMT & two->mode)) {
1614                 /* a filepair that changes between file and symlink
1615                  * needs to be split into deletion and creation.
1616                  */
1617                 struct diff_filespec *null = alloc_filespec(two->path);
1618                 run_diff_cmd(NULL, name, other, one, null, xfrm_msg, o, 0);
1619                 free(null);
1620                 null = alloc_filespec(one->path);
1621                 run_diff_cmd(NULL, name, other, null, two, xfrm_msg, o, 0);
1622                 free(null);
1623         }
1624         else
1625                 run_diff_cmd(pgm, name, other, one, two, xfrm_msg, o,
1626                              complete_rewrite);
1627
1628         free(name_munged);
1629         free(other_munged);
1630 }
1631
1632 static void run_diffstat(struct diff_filepair *p, struct diff_options *o,
1633                          struct diffstat_t *diffstat)
1634 {
1635         const char *name;
1636         const char *other;
1637         int complete_rewrite = 0;
1638
1639         if (DIFF_PAIR_UNMERGED(p)) {
1640                 /* unmerged */
1641                 builtin_diffstat(p->one->path, NULL, NULL, NULL, diffstat, o, 0);
1642                 return;
1643         }
1644
1645         name = p->one->path;
1646         other = (strcmp(name, p->two->path) ? p->two->path : NULL);
1647
1648         diff_fill_sha1_info(p->one);
1649         diff_fill_sha1_info(p->two);
1650
1651         if (p->status == DIFF_STATUS_MODIFIED && p->score)
1652                 complete_rewrite = 1;
1653         builtin_diffstat(name, other, p->one, p->two, diffstat, o, complete_rewrite);
1654 }
1655
1656 static void run_checkdiff(struct diff_filepair *p, struct diff_options *o)
1657 {
1658         const char *name;
1659         const char *other;
1660
1661         if (DIFF_PAIR_UNMERGED(p)) {
1662                 /* unmerged */
1663                 return;
1664         }
1665
1666         name = p->one->path;
1667         other = (strcmp(name, p->two->path) ? p->two->path : NULL);
1668
1669         diff_fill_sha1_info(p->one);
1670         diff_fill_sha1_info(p->two);
1671
1672         builtin_checkdiff(name, other, p->one, p->two);
1673 }
1674
1675 void diff_setup(struct diff_options *options)
1676 {
1677         memset(options, 0, sizeof(*options));
1678         options->line_termination = '\n';
1679         options->break_opt = -1;
1680         options->rename_limit = -1;
1681         options->context = 3;
1682         options->msg_sep = "";
1683
1684         options->change = diff_change;
1685         options->add_remove = diff_addremove;
1686         options->color_diff = diff_use_color_default;
1687         options->detect_rename = diff_detect_rename_default;
1688 }
1689
1690 int diff_setup_done(struct diff_options *options)
1691 {
1692         int count = 0;
1693
1694         if (options->output_format & DIFF_FORMAT_NAME)
1695                 count++;
1696         if (options->output_format & DIFF_FORMAT_NAME_STATUS)
1697                 count++;
1698         if (options->output_format & DIFF_FORMAT_CHECKDIFF)
1699                 count++;
1700         if (options->output_format & DIFF_FORMAT_NO_OUTPUT)
1701                 count++;
1702         if (count > 1)
1703                 die("--name-only, --name-status, --check and -s are mutually exclusive");
1704
1705         if (options->find_copies_harder)
1706                 options->detect_rename = DIFF_DETECT_COPY;
1707
1708         if (options->output_format & (DIFF_FORMAT_NAME |
1709                                       DIFF_FORMAT_NAME_STATUS |
1710                                       DIFF_FORMAT_CHECKDIFF |
1711                                       DIFF_FORMAT_NO_OUTPUT))
1712                 options->output_format &= ~(DIFF_FORMAT_RAW |
1713                                             DIFF_FORMAT_DIFFSTAT |
1714                                             DIFF_FORMAT_SUMMARY |
1715                                             DIFF_FORMAT_PATCH);
1716
1717         /*
1718          * These cases always need recursive; we do not drop caller-supplied
1719          * recursive bits for other formats here.
1720          */
1721         if (options->output_format & (DIFF_FORMAT_PATCH |
1722                                       DIFF_FORMAT_DIFFSTAT |
1723                                       DIFF_FORMAT_CHECKDIFF))
1724                 options->recursive = 1;
1725         /*
1726          * Also pickaxe would not work very well if you do not say recursive
1727          */
1728         if (options->pickaxe)
1729                 options->recursive = 1;
1730
1731         if (options->detect_rename && options->rename_limit < 0)
1732                 options->rename_limit = diff_rename_limit_default;
1733         if (options->setup & DIFF_SETUP_USE_CACHE) {
1734                 if (!active_cache)
1735                         /* read-cache does not die even when it fails
1736                          * so it is safe for us to do this here.  Also
1737                          * it does not smudge active_cache or active_nr
1738                          * when it fails, so we do not have to worry about
1739                          * cleaning it up ourselves either.
1740                          */
1741                         read_cache();
1742         }
1743         if (options->setup & DIFF_SETUP_USE_SIZE_CACHE)
1744                 use_size_cache = 1;
1745         if (options->abbrev <= 0 || 40 < options->abbrev)
1746                 options->abbrev = 40; /* full */
1747
1748         return 0;
1749 }
1750
1751 static int opt_arg(const char *arg, int arg_short, const char *arg_long, int *val)
1752 {
1753         char c, *eq;
1754         int len;
1755
1756         if (*arg != '-')
1757                 return 0;
1758         c = *++arg;
1759         if (!c)
1760                 return 0;
1761         if (c == arg_short) {
1762                 c = *++arg;
1763                 if (!c)
1764                         return 1;
1765                 if (val && isdigit(c)) {
1766                         char *end;
1767                         int n = strtoul(arg, &end, 10);
1768                         if (*end)
1769                                 return 0;
1770                         *val = n;
1771                         return 1;
1772                 }
1773                 return 0;
1774         }
1775         if (c != '-')
1776                 return 0;
1777         arg++;
1778         eq = strchr(arg, '=');
1779         if (eq)
1780                 len = eq - arg;
1781         else
1782                 len = strlen(arg);
1783         if (!len || strncmp(arg, arg_long, len))
1784                 return 0;
1785         if (eq) {
1786                 int n;
1787                 char *end;
1788                 if (!isdigit(*++eq))
1789                         return 0;
1790                 n = strtoul(eq, &end, 10);
1791                 if (*end)
1792                         return 0;
1793                 *val = n;
1794         }
1795         return 1;
1796 }
1797
1798 int diff_opt_parse(struct diff_options *options, const char **av, int ac)
1799 {
1800         const char *arg = av[0];
1801         if (!strcmp(arg, "-p") || !strcmp(arg, "-u"))
1802                 options->output_format |= DIFF_FORMAT_PATCH;
1803         else if (opt_arg(arg, 'U', "unified", &options->context))
1804                 options->output_format |= DIFF_FORMAT_PATCH;
1805         else if (!strcmp(arg, "--raw"))
1806                 options->output_format |= DIFF_FORMAT_RAW;
1807         else if (!strcmp(arg, "--patch-with-raw")) {
1808                 options->output_format |= DIFF_FORMAT_PATCH | DIFF_FORMAT_RAW;
1809         }
1810         else if (!strcmp(arg, "--stat"))
1811                 options->output_format |= DIFF_FORMAT_DIFFSTAT;
1812         else if (!strcmp(arg, "--check"))
1813                 options->output_format |= DIFF_FORMAT_CHECKDIFF;
1814         else if (!strcmp(arg, "--summary"))
1815                 options->output_format |= DIFF_FORMAT_SUMMARY;
1816         else if (!strcmp(arg, "--patch-with-stat")) {
1817                 options->output_format |= DIFF_FORMAT_PATCH | DIFF_FORMAT_DIFFSTAT;
1818         }
1819         else if (!strcmp(arg, "-z"))
1820                 options->line_termination = 0;
1821         else if (!strncmp(arg, "-l", 2))
1822                 options->rename_limit = strtoul(arg+2, NULL, 10);
1823         else if (!strcmp(arg, "--full-index"))
1824                 options->full_index = 1;
1825         else if (!strcmp(arg, "--binary")) {
1826                 options->output_format |= DIFF_FORMAT_PATCH;
1827                 options->binary = 1;
1828         }
1829         else if (!strcmp(arg, "-a") || !strcmp(arg, "--text")) {
1830                 options->text = 1;
1831         }
1832         else if (!strcmp(arg, "--name-only"))
1833                 options->output_format |= DIFF_FORMAT_NAME;
1834         else if (!strcmp(arg, "--name-status"))
1835                 options->output_format |= DIFF_FORMAT_NAME_STATUS;
1836         else if (!strcmp(arg, "-R"))
1837                 options->reverse_diff = 1;
1838         else if (!strncmp(arg, "-S", 2))
1839                 options->pickaxe = arg + 2;
1840         else if (!strcmp(arg, "-s")) {
1841                 options->output_format |= DIFF_FORMAT_NO_OUTPUT;
1842         }
1843         else if (!strncmp(arg, "-O", 2))
1844                 options->orderfile = arg + 2;
1845         else if (!strncmp(arg, "--diff-filter=", 14))
1846                 options->filter = arg + 14;
1847         else if (!strcmp(arg, "--pickaxe-all"))
1848                 options->pickaxe_opts = DIFF_PICKAXE_ALL;
1849         else if (!strcmp(arg, "--pickaxe-regex"))
1850                 options->pickaxe_opts = DIFF_PICKAXE_REGEX;
1851         else if (!strncmp(arg, "-B", 2)) {
1852                 if ((options->break_opt =
1853                      diff_scoreopt_parse(arg)) == -1)
1854                         return -1;
1855         }
1856         else if (!strncmp(arg, "-M", 2)) {
1857                 if ((options->rename_score =
1858                      diff_scoreopt_parse(arg)) == -1)
1859                         return -1;
1860                 options->detect_rename = DIFF_DETECT_RENAME;
1861         }
1862         else if (!strncmp(arg, "-C", 2)) {
1863                 if ((options->rename_score =
1864                      diff_scoreopt_parse(arg)) == -1)
1865                         return -1;
1866                 options->detect_rename = DIFF_DETECT_COPY;
1867         }
1868         else if (!strcmp(arg, "--find-copies-harder"))
1869                 options->find_copies_harder = 1;
1870         else if (!strcmp(arg, "--abbrev"))
1871                 options->abbrev = DEFAULT_ABBREV;
1872         else if (!strncmp(arg, "--abbrev=", 9)) {
1873                 options->abbrev = strtoul(arg + 9, NULL, 10);
1874                 if (options->abbrev < MINIMUM_ABBREV)
1875                         options->abbrev = MINIMUM_ABBREV;
1876                 else if (40 < options->abbrev)
1877                         options->abbrev = 40;
1878         }
1879         else if (!strcmp(arg, "--color"))
1880                 options->color_diff = 1;
1881         else if (!strcmp(arg, "--no-color"))
1882                 options->color_diff = 0;
1883         else if (!strcmp(arg, "-w") || !strcmp(arg, "--ignore-all-space"))
1884                 options->xdl_opts |= XDF_IGNORE_WHITESPACE;
1885         else if (!strcmp(arg, "-b") || !strcmp(arg, "--ignore-space-change"))
1886                 options->xdl_opts |= XDF_IGNORE_WHITESPACE_CHANGE;
1887         else if (!strcmp(arg, "--color-words"))
1888                 options->color_diff = options->color_diff_words = 1;
1889         else if (!strcmp(arg, "--no-renames"))
1890                 options->detect_rename = 0;
1891         else
1892                 return 0;
1893         return 1;
1894 }
1895
1896 static int parse_num(const char **cp_p)
1897 {
1898         unsigned long num, scale;
1899         int ch, dot;
1900         const char *cp = *cp_p;
1901
1902         num = 0;
1903         scale = 1;
1904         dot = 0;
1905         for(;;) {
1906                 ch = *cp;
1907                 if ( !dot && ch == '.' ) {
1908                         scale = 1;
1909                         dot = 1;
1910                 } else if ( ch == '%' ) {
1911                         scale = dot ? scale*100 : 100;
1912                         cp++;   /* % is always at the end */
1913                         break;
1914                 } else if ( ch >= '0' && ch <= '9' ) {
1915                         if ( scale < 100000 ) {
1916                                 scale *= 10;
1917                                 num = (num*10) + (ch-'0');
1918                         }
1919                 } else {
1920                         break;
1921                 }
1922                 cp++;
1923         }
1924         *cp_p = cp;
1925
1926         /* user says num divided by scale and we say internally that
1927          * is MAX_SCORE * num / scale.
1928          */
1929         return (num >= scale) ? MAX_SCORE : (MAX_SCORE * num / scale);
1930 }
1931
1932 int diff_scoreopt_parse(const char *opt)
1933 {
1934         int opt1, opt2, cmd;
1935
1936         if (*opt++ != '-')
1937                 return -1;
1938         cmd = *opt++;
1939         if (cmd != 'M' && cmd != 'C' && cmd != 'B')
1940                 return -1; /* that is not a -M, -C nor -B option */
1941
1942         opt1 = parse_num(&opt);
1943         if (cmd != 'B')
1944                 opt2 = 0;
1945         else {
1946                 if (*opt == 0)
1947                         opt2 = 0;
1948                 else if (*opt != '/')
1949                         return -1; /* we expect -B80/99 or -B80 */
1950                 else {
1951                         opt++;
1952                         opt2 = parse_num(&opt);
1953                 }
1954         }
1955         if (*opt != 0)
1956                 return -1;
1957         return opt1 | (opt2 << 16);
1958 }
1959
1960 struct diff_queue_struct diff_queued_diff;
1961
1962 void diff_q(struct diff_queue_struct *queue, struct diff_filepair *dp)
1963 {
1964         if (queue->alloc <= queue->nr) {
1965                 queue->alloc = alloc_nr(queue->alloc);
1966                 queue->queue = xrealloc(queue->queue,
1967                                         sizeof(dp) * queue->alloc);
1968         }
1969         queue->queue[queue->nr++] = dp;
1970 }
1971
1972 struct diff_filepair *diff_queue(struct diff_queue_struct *queue,
1973                                  struct diff_filespec *one,
1974                                  struct diff_filespec *two)
1975 {
1976         struct diff_filepair *dp = xcalloc(1, sizeof(*dp));
1977         dp->one = one;
1978         dp->two = two;
1979         if (queue)
1980                 diff_q(queue, dp);
1981         return dp;
1982 }
1983
1984 void diff_free_filepair(struct diff_filepair *p)
1985 {
1986         diff_free_filespec_data(p->one);
1987         diff_free_filespec_data(p->two);
1988         free(p->one);
1989         free(p->two);
1990         free(p);
1991 }
1992
1993 /* This is different from find_unique_abbrev() in that
1994  * it stuffs the result with dots for alignment.
1995  */
1996 const char *diff_unique_abbrev(const unsigned char *sha1, int len)
1997 {
1998         int abblen;
1999         const char *abbrev;
2000         if (len == 40)
2001                 return sha1_to_hex(sha1);
2002
2003         abbrev = find_unique_abbrev(sha1, len);
2004         if (!abbrev)
2005                 return sha1_to_hex(sha1);
2006         abblen = strlen(abbrev);
2007         if (abblen < 37) {
2008                 static char hex[41];
2009                 if (len < abblen && abblen <= len + 2)
2010                         sprintf(hex, "%s%.*s", abbrev, len+3-abblen, "..");
2011                 else
2012                         sprintf(hex, "%s...", abbrev);
2013                 return hex;
2014         }
2015         return sha1_to_hex(sha1);
2016 }
2017
2018 static void diff_flush_raw(struct diff_filepair *p,
2019                            struct diff_options *options)
2020 {
2021         int two_paths;
2022         char status[10];
2023         int abbrev = options->abbrev;
2024         const char *path_one, *path_two;
2025         int inter_name_termination = '\t';
2026         int line_termination = options->line_termination;
2027
2028         if (!line_termination)
2029                 inter_name_termination = 0;
2030
2031         path_one = p->one->path;
2032         path_two = p->two->path;
2033         if (line_termination) {
2034                 path_one = quote_one(path_one);
2035                 path_two = quote_one(path_two);
2036         }
2037
2038         if (p->score)
2039                 sprintf(status, "%c%03d", p->status,
2040                         (int)(0.5 + p->score * 100.0/MAX_SCORE));
2041         else {
2042                 status[0] = p->status;
2043                 status[1] = 0;
2044         }
2045         switch (p->status) {
2046         case DIFF_STATUS_COPIED:
2047         case DIFF_STATUS_RENAMED:
2048                 two_paths = 1;
2049                 break;
2050         case DIFF_STATUS_ADDED:
2051         case DIFF_STATUS_DELETED:
2052                 two_paths = 0;
2053                 break;
2054         default:
2055                 two_paths = 0;
2056                 break;
2057         }
2058         if (!(options->output_format & DIFF_FORMAT_NAME_STATUS)) {
2059                 printf(":%06o %06o %s ",
2060                        p->one->mode, p->two->mode,
2061                        diff_unique_abbrev(p->one->sha1, abbrev));
2062                 printf("%s ",
2063                        diff_unique_abbrev(p->two->sha1, abbrev));
2064         }
2065         printf("%s%c%s", status, inter_name_termination, path_one);
2066         if (two_paths)
2067                 printf("%c%s", inter_name_termination, path_two);
2068         putchar(line_termination);
2069         if (path_one != p->one->path)
2070                 free((void*)path_one);
2071         if (path_two != p->two->path)
2072                 free((void*)path_two);
2073 }
2074
2075 static void diff_flush_name(struct diff_filepair *p, int line_termination)
2076 {
2077         char *path = p->two->path;
2078
2079         if (line_termination)
2080                 path = quote_one(p->two->path);
2081         printf("%s%c", path, line_termination);
2082         if (p->two->path != path)
2083                 free(path);
2084 }
2085
2086 int diff_unmodified_pair(struct diff_filepair *p)
2087 {
2088         /* This function is written stricter than necessary to support
2089          * the currently implemented transformers, but the idea is to
2090          * let transformers to produce diff_filepairs any way they want,
2091          * and filter and clean them up here before producing the output.
2092          */
2093         struct diff_filespec *one, *two;
2094
2095         if (DIFF_PAIR_UNMERGED(p))
2096                 return 0; /* unmerged is interesting */
2097
2098         one = p->one;
2099         two = p->two;
2100
2101         /* deletion, addition, mode or type change
2102          * and rename are all interesting.
2103          */
2104         if (DIFF_FILE_VALID(one) != DIFF_FILE_VALID(two) ||
2105             DIFF_PAIR_MODE_CHANGED(p) ||
2106             strcmp(one->path, two->path))
2107                 return 0;
2108
2109         /* both are valid and point at the same path.  that is, we are
2110          * dealing with a change.
2111          */
2112         if (one->sha1_valid && two->sha1_valid &&
2113             !hashcmp(one->sha1, two->sha1))
2114                 return 1; /* no change */
2115         if (!one->sha1_valid && !two->sha1_valid)
2116                 return 1; /* both look at the same file on the filesystem. */
2117         return 0;
2118 }
2119
2120 static void diff_flush_patch(struct diff_filepair *p, struct diff_options *o)
2121 {
2122         if (diff_unmodified_pair(p))
2123                 return;
2124
2125         if ((DIFF_FILE_VALID(p->one) && S_ISDIR(p->one->mode)) ||
2126             (DIFF_FILE_VALID(p->two) && S_ISDIR(p->two->mode)))
2127                 return; /* no tree diffs in patch format */
2128
2129         run_diff(p, o);
2130 }
2131
2132 static void diff_flush_stat(struct diff_filepair *p, struct diff_options *o,
2133                             struct diffstat_t *diffstat)
2134 {
2135         if (diff_unmodified_pair(p))
2136                 return;
2137
2138         if ((DIFF_FILE_VALID(p->one) && S_ISDIR(p->one->mode)) ||
2139             (DIFF_FILE_VALID(p->two) && S_ISDIR(p->two->mode)))
2140                 return; /* no tree diffs in patch format */
2141
2142         run_diffstat(p, o, diffstat);
2143 }
2144
2145 static void diff_flush_checkdiff(struct diff_filepair *p,
2146                 struct diff_options *o)
2147 {
2148         if (diff_unmodified_pair(p))
2149                 return;
2150
2151         if ((DIFF_FILE_VALID(p->one) && S_ISDIR(p->one->mode)) ||
2152             (DIFF_FILE_VALID(p->two) && S_ISDIR(p->two->mode)))
2153                 return; /* no tree diffs in patch format */
2154
2155         run_checkdiff(p, o);
2156 }
2157
2158 int diff_queue_is_empty(void)
2159 {
2160         struct diff_queue_struct *q = &diff_queued_diff;
2161         int i;
2162         for (i = 0; i < q->nr; i++)
2163                 if (!diff_unmodified_pair(q->queue[i]))
2164                         return 0;
2165         return 1;
2166 }
2167
2168 #if DIFF_DEBUG
2169 void diff_debug_filespec(struct diff_filespec *s, int x, const char *one)
2170 {
2171         fprintf(stderr, "queue[%d] %s (%s) %s %06o %s\n",
2172                 x, one ? one : "",
2173                 s->path,
2174                 DIFF_FILE_VALID(s) ? "valid" : "invalid",
2175                 s->mode,
2176                 s->sha1_valid ? sha1_to_hex(s->sha1) : "");
2177         fprintf(stderr, "queue[%d] %s size %lu flags %d\n",
2178                 x, one ? one : "",
2179                 s->size, s->xfrm_flags);
2180 }
2181
2182 void diff_debug_filepair(const struct diff_filepair *p, int i)
2183 {
2184         diff_debug_filespec(p->one, i, "one");
2185         diff_debug_filespec(p->two, i, "two");
2186         fprintf(stderr, "score %d, status %c stays %d broken %d\n",
2187                 p->score, p->status ? p->status : '?',
2188                 p->source_stays, p->broken_pair);
2189 }
2190
2191 void diff_debug_queue(const char *msg, struct diff_queue_struct *q)
2192 {
2193         int i;
2194         if (msg)
2195                 fprintf(stderr, "%s\n", msg);
2196         fprintf(stderr, "q->nr = %d\n", q->nr);
2197         for (i = 0; i < q->nr; i++) {
2198                 struct diff_filepair *p = q->queue[i];
2199                 diff_debug_filepair(p, i);
2200         }
2201 }
2202 #endif
2203
2204 static void diff_resolve_rename_copy(void)
2205 {
2206         int i, j;
2207         struct diff_filepair *p, *pp;
2208         struct diff_queue_struct *q = &diff_queued_diff;
2209
2210         diff_debug_queue("resolve-rename-copy", q);
2211
2212         for (i = 0; i < q->nr; i++) {
2213                 p = q->queue[i];
2214                 p->status = 0; /* undecided */
2215                 if (DIFF_PAIR_UNMERGED(p))
2216                         p->status = DIFF_STATUS_UNMERGED;
2217                 else if (!DIFF_FILE_VALID(p->one))
2218                         p->status = DIFF_STATUS_ADDED;
2219                 else if (!DIFF_FILE_VALID(p->two))
2220                         p->status = DIFF_STATUS_DELETED;
2221                 else if (DIFF_PAIR_TYPE_CHANGED(p))
2222                         p->status = DIFF_STATUS_TYPE_CHANGED;
2223
2224                 /* from this point on, we are dealing with a pair
2225                  * whose both sides are valid and of the same type, i.e.
2226                  * either in-place edit or rename/copy edit.
2227                  */
2228                 else if (DIFF_PAIR_RENAME(p)) {
2229                         if (p->source_stays) {
2230                                 p->status = DIFF_STATUS_COPIED;
2231                                 continue;
2232                         }
2233                         /* See if there is some other filepair that
2234                          * copies from the same source as us.  If so
2235                          * we are a copy.  Otherwise we are either a
2236                          * copy if the path stays, or a rename if it
2237                          * does not, but we already handled "stays" case.
2238                          */
2239                         for (j = i + 1; j < q->nr; j++) {
2240                                 pp = q->queue[j];
2241                                 if (strcmp(pp->one->path, p->one->path))
2242                                         continue; /* not us */
2243                                 if (!DIFF_PAIR_RENAME(pp))
2244                                         continue; /* not a rename/copy */
2245                                 /* pp is a rename/copy from the same source */
2246                                 p->status = DIFF_STATUS_COPIED;
2247                                 break;
2248                         }
2249                         if (!p->status)
2250                                 p->status = DIFF_STATUS_RENAMED;
2251                 }
2252                 else if (hashcmp(p->one->sha1, p->two->sha1) ||
2253                          p->one->mode != p->two->mode)
2254                         p->status = DIFF_STATUS_MODIFIED;
2255                 else {
2256                         /* This is a "no-change" entry and should not
2257                          * happen anymore, but prepare for broken callers.
2258                          */
2259                         error("feeding unmodified %s to diffcore",
2260                               p->one->path);
2261                         p->status = DIFF_STATUS_UNKNOWN;
2262                 }
2263         }
2264         diff_debug_queue("resolve-rename-copy done", q);
2265 }
2266
2267 static int check_pair_status(struct diff_filepair *p)
2268 {
2269         switch (p->status) {
2270         case DIFF_STATUS_UNKNOWN:
2271                 return 0;
2272         case 0:
2273                 die("internal error in diff-resolve-rename-copy");
2274         default:
2275                 return 1;
2276         }
2277 }
2278
2279 static void flush_one_pair(struct diff_filepair *p, struct diff_options *opt)
2280 {
2281         int fmt = opt->output_format;
2282
2283         if (fmt & DIFF_FORMAT_CHECKDIFF)
2284                 diff_flush_checkdiff(p, opt);
2285         else if (fmt & (DIFF_FORMAT_RAW | DIFF_FORMAT_NAME_STATUS))
2286                 diff_flush_raw(p, opt);
2287         else if (fmt & DIFF_FORMAT_NAME)
2288                 diff_flush_name(p, opt->line_termination);
2289 }
2290
2291 static void show_file_mode_name(const char *newdelete, struct diff_filespec *fs)
2292 {
2293         if (fs->mode)
2294                 printf(" %s mode %06o %s\n", newdelete, fs->mode, fs->path);
2295         else
2296                 printf(" %s %s\n", newdelete, fs->path);
2297 }
2298
2299
2300 static void show_mode_change(struct diff_filepair *p, int show_name)
2301 {
2302         if (p->one->mode && p->two->mode && p->one->mode != p->two->mode) {
2303                 if (show_name)
2304                         printf(" mode change %06o => %06o %s\n",
2305                                p->one->mode, p->two->mode, p->two->path);
2306                 else
2307                         printf(" mode change %06o => %06o\n",
2308                                p->one->mode, p->two->mode);
2309         }
2310 }
2311
2312 static void show_rename_copy(const char *renamecopy, struct diff_filepair *p)
2313 {
2314         const char *old, *new;
2315
2316         /* Find common prefix */
2317         old = p->one->path;
2318         new = p->two->path;
2319         while (1) {
2320                 const char *slash_old, *slash_new;
2321                 slash_old = strchr(old, '/');
2322                 slash_new = strchr(new, '/');
2323                 if (!slash_old ||
2324                     !slash_new ||
2325                     slash_old - old != slash_new - new ||
2326                     memcmp(old, new, slash_new - new))
2327                         break;
2328                 old = slash_old + 1;
2329                 new = slash_new + 1;
2330         }
2331         /* p->one->path thru old is the common prefix, and old and new
2332          * through the end of names are renames
2333          */
2334         if (old != p->one->path)
2335                 printf(" %s %.*s{%s => %s} (%d%%)\n", renamecopy,
2336                        (int)(old - p->one->path), p->one->path,
2337                        old, new, (int)(0.5 + p->score * 100.0/MAX_SCORE));
2338         else
2339                 printf(" %s %s => %s (%d%%)\n", renamecopy,
2340                        p->one->path, p->two->path,
2341                        (int)(0.5 + p->score * 100.0/MAX_SCORE));
2342         show_mode_change(p, 0);
2343 }
2344
2345 static void diff_summary(struct diff_filepair *p)
2346 {
2347         switch(p->status) {
2348         case DIFF_STATUS_DELETED:
2349                 show_file_mode_name("delete", p->one);
2350                 break;
2351         case DIFF_STATUS_ADDED:
2352                 show_file_mode_name("create", p->two);
2353                 break;
2354         case DIFF_STATUS_COPIED:
2355                 show_rename_copy("copy", p);
2356                 break;
2357         case DIFF_STATUS_RENAMED:
2358                 show_rename_copy("rename", p);
2359                 break;
2360         default:
2361                 if (p->score) {
2362                         printf(" rewrite %s (%d%%)\n", p->two->path,
2363                                 (int)(0.5 + p->score * 100.0/MAX_SCORE));
2364                         show_mode_change(p, 0);
2365                 } else  show_mode_change(p, 1);
2366                 break;
2367         }
2368 }
2369
2370 struct patch_id_t {
2371         struct xdiff_emit_state xm;
2372         SHA_CTX *ctx;
2373         int patchlen;
2374 };
2375
2376 static int remove_space(char *line, int len)
2377 {
2378         int i;
2379         char *dst = line;
2380         unsigned char c;
2381
2382         for (i = 0; i < len; i++)
2383                 if (!isspace((c = line[i])))
2384                         *dst++ = c;
2385
2386         return dst - line;
2387 }
2388
2389 static void patch_id_consume(void *priv, char *line, unsigned long len)
2390 {
2391         struct patch_id_t *data = priv;
2392         int new_len;
2393
2394         /* Ignore line numbers when computing the SHA1 of the patch */
2395         if (!strncmp(line, "@@ -", 4))
2396                 return;
2397
2398         new_len = remove_space(line, len);
2399
2400         SHA1_Update(data->ctx, line, new_len);
2401         data->patchlen += new_len;
2402 }
2403
2404 /* returns 0 upon success, and writes result into sha1 */
2405 static int diff_get_patch_id(struct diff_options *options, unsigned char *sha1)
2406 {
2407         struct diff_queue_struct *q = &diff_queued_diff;
2408         int i;
2409         SHA_CTX ctx;
2410         struct patch_id_t data;
2411         char buffer[PATH_MAX * 4 + 20];
2412
2413         SHA1_Init(&ctx);
2414         memset(&data, 0, sizeof(struct patch_id_t));
2415         data.ctx = &ctx;
2416         data.xm.consume = patch_id_consume;
2417
2418         for (i = 0; i < q->nr; i++) {
2419                 xpparam_t xpp;
2420                 xdemitconf_t xecfg;
2421                 xdemitcb_t ecb;
2422                 mmfile_t mf1, mf2;
2423                 struct diff_filepair *p = q->queue[i];
2424                 int len1, len2;
2425
2426                 if (p->status == 0)
2427                         return error("internal diff status error");
2428                 if (p->status == DIFF_STATUS_UNKNOWN)
2429                         continue;
2430                 if (diff_unmodified_pair(p))
2431                         continue;
2432                 if ((DIFF_FILE_VALID(p->one) && S_ISDIR(p->one->mode)) ||
2433                     (DIFF_FILE_VALID(p->two) && S_ISDIR(p->two->mode)))
2434                         continue;
2435                 if (DIFF_PAIR_UNMERGED(p))
2436                         continue;
2437
2438                 diff_fill_sha1_info(p->one);
2439                 diff_fill_sha1_info(p->two);
2440                 if (fill_mmfile(&mf1, p->one) < 0 ||
2441                                 fill_mmfile(&mf2, p->two) < 0)
2442                         return error("unable to read files to diff");
2443
2444                 /* Maybe hash p->two? into the patch id? */
2445                 if (mmfile_is_binary(&mf2))
2446                         continue;
2447
2448                 len1 = remove_space(p->one->path, strlen(p->one->path));
2449                 len2 = remove_space(p->two->path, strlen(p->two->path));
2450                 if (p->one->mode == 0)
2451                         len1 = snprintf(buffer, sizeof(buffer),
2452                                         "diff--gita/%.*sb/%.*s"
2453                                         "newfilemode%06o"
2454                                         "---/dev/null"
2455                                         "+++b/%.*s",
2456                                         len1, p->one->path,
2457                                         len2, p->two->path,
2458                                         p->two->mode,
2459                                         len2, p->two->path);
2460                 else if (p->two->mode == 0)
2461                         len1 = snprintf(buffer, sizeof(buffer),
2462                                         "diff--gita/%.*sb/%.*s"
2463                                         "deletedfilemode%06o"
2464                                         "---a/%.*s"
2465                                         "+++/dev/null",
2466                                         len1, p->one->path,
2467                                         len2, p->two->path,
2468                                         p->one->mode,
2469                                         len1, p->one->path);
2470                 else
2471                         len1 = snprintf(buffer, sizeof(buffer),
2472                                         "diff--gita/%.*sb/%.*s"
2473                                         "---a/%.*s"
2474                                         "+++b/%.*s",
2475                                         len1, p->one->path,
2476                                         len2, p->two->path,
2477                                         len1, p->one->path,
2478                                         len2, p->two->path);
2479                 SHA1_Update(&ctx, buffer, len1);
2480
2481                 xpp.flags = XDF_NEED_MINIMAL;
2482                 xecfg.ctxlen = 3;
2483                 xecfg.flags = XDL_EMIT_FUNCNAMES;
2484                 ecb.outf = xdiff_outf;
2485                 ecb.priv = &data;
2486                 xdl_diff(&mf1, &mf2, &xpp, &xecfg, &ecb);
2487         }
2488
2489         SHA1_Final(sha1, &ctx);
2490         return 0;
2491 }
2492
2493 int diff_flush_patch_id(struct diff_options *options, unsigned char *sha1)
2494 {
2495         struct diff_queue_struct *q = &diff_queued_diff;
2496         int i;
2497         int result = diff_get_patch_id(options, sha1);
2498
2499         for (i = 0; i < q->nr; i++)
2500                 diff_free_filepair(q->queue[i]);
2501
2502         free(q->queue);
2503         q->queue = NULL;
2504         q->nr = q->alloc = 0;
2505
2506         return result;
2507 }
2508
2509 static int is_summary_empty(const struct diff_queue_struct *q)
2510 {
2511         int i;
2512
2513         for (i = 0; i < q->nr; i++) {
2514                 const struct diff_filepair *p = q->queue[i];
2515
2516                 switch (p->status) {
2517                 case DIFF_STATUS_DELETED:
2518                 case DIFF_STATUS_ADDED:
2519                 case DIFF_STATUS_COPIED:
2520                 case DIFF_STATUS_RENAMED:
2521                         return 0;
2522                 default:
2523                         if (p->score)
2524                                 return 0;
2525                         if (p->one->mode && p->two->mode &&
2526                             p->one->mode != p->two->mode)
2527                                 return 0;
2528                         break;
2529                 }
2530         }
2531         return 1;
2532 }
2533
2534 void diff_flush(struct diff_options *options)
2535 {
2536         struct diff_queue_struct *q = &diff_queued_diff;
2537         int i, output_format = options->output_format;
2538         int separator = 0;
2539
2540         /*
2541          * Order: raw, stat, summary, patch
2542          * or:    name/name-status/checkdiff (other bits clear)
2543          */
2544         if (!q->nr)
2545                 goto free_queue;
2546
2547         if (output_format & (DIFF_FORMAT_RAW |
2548                              DIFF_FORMAT_NAME |
2549                              DIFF_FORMAT_NAME_STATUS |
2550                              DIFF_FORMAT_CHECKDIFF)) {
2551                 for (i = 0; i < q->nr; i++) {
2552                         struct diff_filepair *p = q->queue[i];
2553                         if (check_pair_status(p))
2554                                 flush_one_pair(p, options);
2555                 }
2556                 separator++;
2557         }
2558
2559         if (output_format & DIFF_FORMAT_DIFFSTAT) {
2560                 struct diffstat_t diffstat;
2561
2562                 memset(&diffstat, 0, sizeof(struct diffstat_t));
2563                 diffstat.xm.consume = diffstat_consume;
2564                 for (i = 0; i < q->nr; i++) {
2565                         struct diff_filepair *p = q->queue[i];
2566                         if (check_pair_status(p))
2567                                 diff_flush_stat(p, options, &diffstat);
2568                 }
2569                 show_stats(&diffstat);
2570                 separator++;
2571         }
2572
2573         if (output_format & DIFF_FORMAT_SUMMARY && !is_summary_empty(q)) {
2574                 for (i = 0; i < q->nr; i++)
2575                         diff_summary(q->queue[i]);
2576                 separator++;
2577         }
2578
2579         if (output_format & DIFF_FORMAT_PATCH) {
2580                 if (separator) {
2581                         if (options->stat_sep) {
2582                                 /* attach patch instead of inline */
2583                                 fputs(options->stat_sep, stdout);
2584                         } else {
2585                                 putchar(options->line_termination);
2586                         }
2587                 }
2588
2589                 for (i = 0; i < q->nr; i++) {
2590                         struct diff_filepair *p = q->queue[i];
2591                         if (check_pair_status(p))
2592                                 diff_flush_patch(p, options);
2593                 }
2594         }
2595
2596         for (i = 0; i < q->nr; i++)
2597                 diff_free_filepair(q->queue[i]);
2598 free_queue:
2599         free(q->queue);
2600         q->queue = NULL;
2601         q->nr = q->alloc = 0;
2602 }
2603
2604 static void diffcore_apply_filter(const char *filter)
2605 {
2606         int i;
2607         struct diff_queue_struct *q = &diff_queued_diff;
2608         struct diff_queue_struct outq;
2609         outq.queue = NULL;
2610         outq.nr = outq.alloc = 0;
2611
2612         if (!filter)
2613                 return;
2614
2615         if (strchr(filter, DIFF_STATUS_FILTER_AON)) {
2616                 int found;
2617                 for (i = found = 0; !found && i < q->nr; i++) {
2618                         struct diff_filepair *p = q->queue[i];
2619                         if (((p->status == DIFF_STATUS_MODIFIED) &&
2620                              ((p->score &&
2621                                strchr(filter, DIFF_STATUS_FILTER_BROKEN)) ||
2622                               (!p->score &&
2623                                strchr(filter, DIFF_STATUS_MODIFIED)))) ||
2624                             ((p->status != DIFF_STATUS_MODIFIED) &&
2625                              strchr(filter, p->status)))
2626                                 found++;
2627                 }
2628                 if (found)
2629                         return;
2630
2631                 /* otherwise we will clear the whole queue
2632                  * by copying the empty outq at the end of this
2633                  * function, but first clear the current entries
2634                  * in the queue.
2635                  */
2636                 for (i = 0; i < q->nr; i++)
2637                         diff_free_filepair(q->queue[i]);
2638         }
2639         else {
2640                 /* Only the matching ones */
2641                 for (i = 0; i < q->nr; i++) {
2642                         struct diff_filepair *p = q->queue[i];
2643
2644                         if (((p->status == DIFF_STATUS_MODIFIED) &&
2645                              ((p->score &&
2646                                strchr(filter, DIFF_STATUS_FILTER_BROKEN)) ||
2647                               (!p->score &&
2648                                strchr(filter, DIFF_STATUS_MODIFIED)))) ||
2649                             ((p->status != DIFF_STATUS_MODIFIED) &&
2650                              strchr(filter, p->status)))
2651                                 diff_q(&outq, p);
2652                         else
2653                                 diff_free_filepair(p);
2654                 }
2655         }
2656         free(q->queue);
2657         *q = outq;
2658 }
2659
2660 void diffcore_std(struct diff_options *options)
2661 {
2662         if (options->break_opt != -1)
2663                 diffcore_break(options->break_opt);
2664         if (options->detect_rename)
2665                 diffcore_rename(options);
2666         if (options->break_opt != -1)
2667                 diffcore_merge_broken();
2668         if (options->pickaxe)
2669                 diffcore_pickaxe(options->pickaxe, options->pickaxe_opts);
2670         if (options->orderfile)
2671                 diffcore_order(options->orderfile);
2672         diff_resolve_rename_copy();
2673         diffcore_apply_filter(options->filter);
2674 }
2675
2676
2677 void diffcore_std_no_resolve(struct diff_options *options)
2678 {
2679         if (options->pickaxe)
2680                 diffcore_pickaxe(options->pickaxe, options->pickaxe_opts);
2681         if (options->orderfile)
2682                 diffcore_order(options->orderfile);
2683         diffcore_apply_filter(options->filter);
2684 }
2685
2686 void diff_addremove(struct diff_options *options,
2687                     int addremove, unsigned mode,
2688                     const unsigned char *sha1,
2689                     const char *base, const char *path)
2690 {
2691         char concatpath[PATH_MAX];
2692         struct diff_filespec *one, *two;
2693
2694         /* This may look odd, but it is a preparation for
2695          * feeding "there are unchanged files which should
2696          * not produce diffs, but when you are doing copy
2697          * detection you would need them, so here they are"
2698          * entries to the diff-core.  They will be prefixed
2699          * with something like '=' or '*' (I haven't decided
2700          * which but should not make any difference).
2701          * Feeding the same new and old to diff_change() 
2702          * also has the same effect.
2703          * Before the final output happens, they are pruned after
2704          * merged into rename/copy pairs as appropriate.
2705          */
2706         if (options->reverse_diff)
2707                 addremove = (addremove == '+' ? '-' :
2708                              addremove == '-' ? '+' : addremove);
2709
2710         if (!path) path = "";
2711         sprintf(concatpath, "%s%s", base, path);
2712         one = alloc_filespec(concatpath);
2713         two = alloc_filespec(concatpath);
2714
2715         if (addremove != '+')
2716                 fill_filespec(one, sha1, mode);
2717         if (addremove != '-')
2718                 fill_filespec(two, sha1, mode);
2719
2720         diff_queue(&diff_queued_diff, one, two);
2721 }
2722
2723 void diff_change(struct diff_options *options,
2724                  unsigned old_mode, unsigned new_mode,
2725                  const unsigned char *old_sha1,
2726                  const unsigned char *new_sha1,
2727                  const char *base, const char *path) 
2728 {
2729         char concatpath[PATH_MAX];
2730         struct diff_filespec *one, *two;
2731
2732         if (options->reverse_diff) {
2733                 unsigned tmp;
2734                 const unsigned char *tmp_c;
2735                 tmp = old_mode; old_mode = new_mode; new_mode = tmp;
2736                 tmp_c = old_sha1; old_sha1 = new_sha1; new_sha1 = tmp_c;
2737         }
2738         if (!path) path = "";
2739         sprintf(concatpath, "%s%s", base, path);
2740         one = alloc_filespec(concatpath);
2741         two = alloc_filespec(concatpath);
2742         fill_filespec(one, old_sha1, old_mode);
2743         fill_filespec(two, new_sha1, new_mode);
2744
2745         diff_queue(&diff_queued_diff, one, two);
2746 }
2747
2748 void diff_unmerge(struct diff_options *options,
2749                   const char *path)
2750 {
2751         struct diff_filespec *one, *two;
2752         one = alloc_filespec(path);
2753         two = alloc_filespec(path);
2754         diff_queue(&diff_queued_diff, one, two);
2755 }