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