gitweb: Make git_get_hash_by_path check type if provided
[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                 if (o->binary) {
1464                         mmfile_t mf;
1465                         if ((!fill_mmfile(&mf, one) && mmfile_is_binary(&mf)) ||
1466                             (!fill_mmfile(&mf, two) && mmfile_is_binary(&mf)))
1467                                 abbrev = 40;
1468                 }
1469                 len += snprintf(msg + len, sizeof(msg) - len,
1470                                 "index %.*s..%.*s",
1471                                 abbrev, sha1_to_hex(one->sha1),
1472                                 abbrev, sha1_to_hex(two->sha1));
1473                 if (one->mode == two->mode)
1474                         len += snprintf(msg + len, sizeof(msg) - len,
1475                                         " %06o", one->mode);
1476                 len += snprintf(msg + len, sizeof(msg) - len, "\n");
1477         }
1478
1479         if (len)
1480                 msg[--len] = 0;
1481         xfrm_msg = len ? msg : NULL;
1482
1483         if (!pgm &&
1484             DIFF_FILE_VALID(one) && DIFF_FILE_VALID(two) &&
1485             (S_IFMT & one->mode) != (S_IFMT & two->mode)) {
1486                 /* a filepair that changes between file and symlink
1487                  * needs to be split into deletion and creation.
1488                  */
1489                 struct diff_filespec *null = alloc_filespec(two->path);
1490                 run_diff_cmd(NULL, name, other, one, null, xfrm_msg, o, 0);
1491                 free(null);
1492                 null = alloc_filespec(one->path);
1493                 run_diff_cmd(NULL, name, other, null, two, xfrm_msg, o, 0);
1494                 free(null);
1495         }
1496         else
1497                 run_diff_cmd(pgm, name, other, one, two, xfrm_msg, o,
1498                              complete_rewrite);
1499
1500         free(name_munged);
1501         free(other_munged);
1502 }
1503
1504 static void run_diffstat(struct diff_filepair *p, struct diff_options *o,
1505                          struct diffstat_t *diffstat)
1506 {
1507         const char *name;
1508         const char *other;
1509         int complete_rewrite = 0;
1510
1511         if (DIFF_PAIR_UNMERGED(p)) {
1512                 /* unmerged */
1513                 builtin_diffstat(p->one->path, NULL, NULL, NULL, diffstat, o, 0);
1514                 return;
1515         }
1516
1517         name = p->one->path;
1518         other = (strcmp(name, p->two->path) ? p->two->path : NULL);
1519
1520         diff_fill_sha1_info(p->one);
1521         diff_fill_sha1_info(p->two);
1522
1523         if (p->status == DIFF_STATUS_MODIFIED && p->score)
1524                 complete_rewrite = 1;
1525         builtin_diffstat(name, other, p->one, p->two, diffstat, o, complete_rewrite);
1526 }
1527
1528 static void run_checkdiff(struct diff_filepair *p, struct diff_options *o)
1529 {
1530         const char *name;
1531         const char *other;
1532
1533         if (DIFF_PAIR_UNMERGED(p)) {
1534                 /* unmerged */
1535                 return;
1536         }
1537
1538         name = p->one->path;
1539         other = (strcmp(name, p->two->path) ? p->two->path : NULL);
1540
1541         diff_fill_sha1_info(p->one);
1542         diff_fill_sha1_info(p->two);
1543
1544         builtin_checkdiff(name, other, p->one, p->two);
1545 }
1546
1547 void diff_setup(struct diff_options *options)
1548 {
1549         memset(options, 0, sizeof(*options));
1550         options->line_termination = '\n';
1551         options->break_opt = -1;
1552         options->rename_limit = -1;
1553         options->context = 3;
1554         options->msg_sep = "";
1555
1556         options->change = diff_change;
1557         options->add_remove = diff_addremove;
1558         options->color_diff = diff_use_color_default;
1559         options->detect_rename = diff_detect_rename_default;
1560 }
1561
1562 int diff_setup_done(struct diff_options *options)
1563 {
1564         int count = 0;
1565
1566         if (options->output_format & DIFF_FORMAT_NAME)
1567                 count++;
1568         if (options->output_format & DIFF_FORMAT_NAME_STATUS)
1569                 count++;
1570         if (options->output_format & DIFF_FORMAT_CHECKDIFF)
1571                 count++;
1572         if (options->output_format & DIFF_FORMAT_NO_OUTPUT)
1573                 count++;
1574         if (count > 1)
1575                 die("--name-only, --name-status, --check and -s are mutually exclusive");
1576
1577         if (options->find_copies_harder)
1578                 options->detect_rename = DIFF_DETECT_COPY;
1579
1580         if (options->output_format & (DIFF_FORMAT_NAME |
1581                                       DIFF_FORMAT_NAME_STATUS |
1582                                       DIFF_FORMAT_CHECKDIFF |
1583                                       DIFF_FORMAT_NO_OUTPUT))
1584                 options->output_format &= ~(DIFF_FORMAT_RAW |
1585                                             DIFF_FORMAT_DIFFSTAT |
1586                                             DIFF_FORMAT_SUMMARY |
1587                                             DIFF_FORMAT_PATCH);
1588
1589         /*
1590          * These cases always need recursive; we do not drop caller-supplied
1591          * recursive bits for other formats here.
1592          */
1593         if (options->output_format & (DIFF_FORMAT_PATCH |
1594                                       DIFF_FORMAT_DIFFSTAT |
1595                                       DIFF_FORMAT_CHECKDIFF))
1596                 options->recursive = 1;
1597         /*
1598          * Also pickaxe would not work very well if you do not say recursive
1599          */
1600         if (options->pickaxe)
1601                 options->recursive = 1;
1602
1603         if (options->detect_rename && options->rename_limit < 0)
1604                 options->rename_limit = diff_rename_limit_default;
1605         if (options->setup & DIFF_SETUP_USE_CACHE) {
1606                 if (!active_cache)
1607                         /* read-cache does not die even when it fails
1608                          * so it is safe for us to do this here.  Also
1609                          * it does not smudge active_cache or active_nr
1610                          * when it fails, so we do not have to worry about
1611                          * cleaning it up ourselves either.
1612                          */
1613                         read_cache();
1614         }
1615         if (options->setup & DIFF_SETUP_USE_SIZE_CACHE)
1616                 use_size_cache = 1;
1617         if (options->abbrev <= 0 || 40 < options->abbrev)
1618                 options->abbrev = 40; /* full */
1619
1620         return 0;
1621 }
1622
1623 static int opt_arg(const char *arg, int arg_short, const char *arg_long, int *val)
1624 {
1625         char c, *eq;
1626         int len;
1627
1628         if (*arg != '-')
1629                 return 0;
1630         c = *++arg;
1631         if (!c)
1632                 return 0;
1633         if (c == arg_short) {
1634                 c = *++arg;
1635                 if (!c)
1636                         return 1;
1637                 if (val && isdigit(c)) {
1638                         char *end;
1639                         int n = strtoul(arg, &end, 10);
1640                         if (*end)
1641                                 return 0;
1642                         *val = n;
1643                         return 1;
1644                 }
1645                 return 0;
1646         }
1647         if (c != '-')
1648                 return 0;
1649         arg++;
1650         eq = strchr(arg, '=');
1651         if (eq)
1652                 len = eq - arg;
1653         else
1654                 len = strlen(arg);
1655         if (!len || strncmp(arg, arg_long, len))
1656                 return 0;
1657         if (eq) {
1658                 int n;
1659                 char *end;
1660                 if (!isdigit(*++eq))
1661                         return 0;
1662                 n = strtoul(eq, &end, 10);
1663                 if (*end)
1664                         return 0;
1665                 *val = n;
1666         }
1667         return 1;
1668 }
1669
1670 int diff_opt_parse(struct diff_options *options, const char **av, int ac)
1671 {
1672         const char *arg = av[0];
1673         if (!strcmp(arg, "-p") || !strcmp(arg, "-u"))
1674                 options->output_format |= DIFF_FORMAT_PATCH;
1675         else if (opt_arg(arg, 'U', "unified", &options->context))
1676                 options->output_format |= DIFF_FORMAT_PATCH;
1677         else if (!strcmp(arg, "--raw"))
1678                 options->output_format |= DIFF_FORMAT_RAW;
1679         else if (!strcmp(arg, "--patch-with-raw")) {
1680                 options->output_format |= DIFF_FORMAT_PATCH | DIFF_FORMAT_RAW;
1681         }
1682         else if (!strcmp(arg, "--stat"))
1683                 options->output_format |= DIFF_FORMAT_DIFFSTAT;
1684         else if (!strcmp(arg, "--check"))
1685                 options->output_format |= DIFF_FORMAT_CHECKDIFF;
1686         else if (!strcmp(arg, "--summary"))
1687                 options->output_format |= DIFF_FORMAT_SUMMARY;
1688         else if (!strcmp(arg, "--patch-with-stat")) {
1689                 options->output_format |= DIFF_FORMAT_PATCH | DIFF_FORMAT_DIFFSTAT;
1690         }
1691         else if (!strcmp(arg, "-z"))
1692                 options->line_termination = 0;
1693         else if (!strncmp(arg, "-l", 2))
1694                 options->rename_limit = strtoul(arg+2, NULL, 10);
1695         else if (!strcmp(arg, "--full-index"))
1696                 options->full_index = 1;
1697         else if (!strcmp(arg, "--binary")) {
1698                 options->output_format |= DIFF_FORMAT_PATCH;
1699                 options->binary = 1;
1700         }
1701         else if (!strcmp(arg, "-a") || !strcmp(arg, "--text")) {
1702                 options->text = 1;
1703         }
1704         else if (!strcmp(arg, "--name-only"))
1705                 options->output_format |= DIFF_FORMAT_NAME;
1706         else if (!strcmp(arg, "--name-status"))
1707                 options->output_format |= DIFF_FORMAT_NAME_STATUS;
1708         else if (!strcmp(arg, "-R"))
1709                 options->reverse_diff = 1;
1710         else if (!strncmp(arg, "-S", 2))
1711                 options->pickaxe = arg + 2;
1712         else if (!strcmp(arg, "-s")) {
1713                 options->output_format |= DIFF_FORMAT_NO_OUTPUT;
1714         }
1715         else if (!strncmp(arg, "-O", 2))
1716                 options->orderfile = arg + 2;
1717         else if (!strncmp(arg, "--diff-filter=", 14))
1718                 options->filter = arg + 14;
1719         else if (!strcmp(arg, "--pickaxe-all"))
1720                 options->pickaxe_opts = DIFF_PICKAXE_ALL;
1721         else if (!strcmp(arg, "--pickaxe-regex"))
1722                 options->pickaxe_opts = DIFF_PICKAXE_REGEX;
1723         else if (!strncmp(arg, "-B", 2)) {
1724                 if ((options->break_opt =
1725                      diff_scoreopt_parse(arg)) == -1)
1726                         return -1;
1727         }
1728         else if (!strncmp(arg, "-M", 2)) {
1729                 if ((options->rename_score =
1730                      diff_scoreopt_parse(arg)) == -1)
1731                         return -1;
1732                 options->detect_rename = DIFF_DETECT_RENAME;
1733         }
1734         else if (!strncmp(arg, "-C", 2)) {
1735                 if ((options->rename_score =
1736                      diff_scoreopt_parse(arg)) == -1)
1737                         return -1;
1738                 options->detect_rename = DIFF_DETECT_COPY;
1739         }
1740         else if (!strcmp(arg, "--find-copies-harder"))
1741                 options->find_copies_harder = 1;
1742         else if (!strcmp(arg, "--abbrev"))
1743                 options->abbrev = DEFAULT_ABBREV;
1744         else if (!strncmp(arg, "--abbrev=", 9)) {
1745                 options->abbrev = strtoul(arg + 9, NULL, 10);
1746                 if (options->abbrev < MINIMUM_ABBREV)
1747                         options->abbrev = MINIMUM_ABBREV;
1748                 else if (40 < options->abbrev)
1749                         options->abbrev = 40;
1750         }
1751         else if (!strcmp(arg, "--color"))
1752                 options->color_diff = 1;
1753         else if (!strcmp(arg, "--no-color"))
1754                 options->color_diff = 0;
1755         else if (!strcmp(arg, "-w") || !strcmp(arg, "--ignore-all-space"))
1756                 options->xdl_opts |= XDF_IGNORE_WHITESPACE;
1757         else if (!strcmp(arg, "-b") || !strcmp(arg, "--ignore-space-change"))
1758                 options->xdl_opts |= XDF_IGNORE_WHITESPACE_CHANGE;
1759         else if (!strcmp(arg, "--color-words"))
1760                 options->color_diff = options->color_diff_words = 1;
1761         else if (!strcmp(arg, "--no-renames"))
1762                 options->detect_rename = 0;
1763         else
1764                 return 0;
1765         return 1;
1766 }
1767
1768 static int parse_num(const char **cp_p)
1769 {
1770         unsigned long num, scale;
1771         int ch, dot;
1772         const char *cp = *cp_p;
1773
1774         num = 0;
1775         scale = 1;
1776         dot = 0;
1777         for(;;) {
1778                 ch = *cp;
1779                 if ( !dot && ch == '.' ) {
1780                         scale = 1;
1781                         dot = 1;
1782                 } else if ( ch == '%' ) {
1783                         scale = dot ? scale*100 : 100;
1784                         cp++;   /* % is always at the end */
1785                         break;
1786                 } else if ( ch >= '0' && ch <= '9' ) {
1787                         if ( scale < 100000 ) {
1788                                 scale *= 10;
1789                                 num = (num*10) + (ch-'0');
1790                         }
1791                 } else {
1792                         break;
1793                 }
1794                 cp++;
1795         }
1796         *cp_p = cp;
1797
1798         /* user says num divided by scale and we say internally that
1799          * is MAX_SCORE * num / scale.
1800          */
1801         return (num >= scale) ? MAX_SCORE : (MAX_SCORE * num / scale);
1802 }
1803
1804 int diff_scoreopt_parse(const char *opt)
1805 {
1806         int opt1, opt2, cmd;
1807
1808         if (*opt++ != '-')
1809                 return -1;
1810         cmd = *opt++;
1811         if (cmd != 'M' && cmd != 'C' && cmd != 'B')
1812                 return -1; /* that is not a -M, -C nor -B option */
1813
1814         opt1 = parse_num(&opt);
1815         if (cmd != 'B')
1816                 opt2 = 0;
1817         else {
1818                 if (*opt == 0)
1819                         opt2 = 0;
1820                 else if (*opt != '/')
1821                         return -1; /* we expect -B80/99 or -B80 */
1822                 else {
1823                         opt++;
1824                         opt2 = parse_num(&opt);
1825                 }
1826         }
1827         if (*opt != 0)
1828                 return -1;
1829         return opt1 | (opt2 << 16);
1830 }
1831
1832 struct diff_queue_struct diff_queued_diff;
1833
1834 void diff_q(struct diff_queue_struct *queue, struct diff_filepair *dp)
1835 {
1836         if (queue->alloc <= queue->nr) {
1837                 queue->alloc = alloc_nr(queue->alloc);
1838                 queue->queue = xrealloc(queue->queue,
1839                                         sizeof(dp) * queue->alloc);
1840         }
1841         queue->queue[queue->nr++] = dp;
1842 }
1843
1844 struct diff_filepair *diff_queue(struct diff_queue_struct *queue,
1845                                  struct diff_filespec *one,
1846                                  struct diff_filespec *two)
1847 {
1848         struct diff_filepair *dp = xcalloc(1, sizeof(*dp));
1849         dp->one = one;
1850         dp->two = two;
1851         if (queue)
1852                 diff_q(queue, dp);
1853         return dp;
1854 }
1855
1856 void diff_free_filepair(struct diff_filepair *p)
1857 {
1858         diff_free_filespec_data(p->one);
1859         diff_free_filespec_data(p->two);
1860         free(p->one);
1861         free(p->two);
1862         free(p);
1863 }
1864
1865 /* This is different from find_unique_abbrev() in that
1866  * it stuffs the result with dots for alignment.
1867  */
1868 const char *diff_unique_abbrev(const unsigned char *sha1, int len)
1869 {
1870         int abblen;
1871         const char *abbrev;
1872         if (len == 40)
1873                 return sha1_to_hex(sha1);
1874
1875         abbrev = find_unique_abbrev(sha1, len);
1876         if (!abbrev)
1877                 return sha1_to_hex(sha1);
1878         abblen = strlen(abbrev);
1879         if (abblen < 37) {
1880                 static char hex[41];
1881                 if (len < abblen && abblen <= len + 2)
1882                         sprintf(hex, "%s%.*s", abbrev, len+3-abblen, "..");
1883                 else
1884                         sprintf(hex, "%s...", abbrev);
1885                 return hex;
1886         }
1887         return sha1_to_hex(sha1);
1888 }
1889
1890 static void diff_flush_raw(struct diff_filepair *p,
1891                            struct diff_options *options)
1892 {
1893         int two_paths;
1894         char status[10];
1895         int abbrev = options->abbrev;
1896         const char *path_one, *path_two;
1897         int inter_name_termination = '\t';
1898         int line_termination = options->line_termination;
1899
1900         if (!line_termination)
1901                 inter_name_termination = 0;
1902
1903         path_one = p->one->path;
1904         path_two = p->two->path;
1905         if (line_termination) {
1906                 path_one = quote_one(path_one);
1907                 path_two = quote_one(path_two);
1908         }
1909
1910         if (p->score)
1911                 sprintf(status, "%c%03d", p->status,
1912                         (int)(0.5 + p->score * 100.0/MAX_SCORE));
1913         else {
1914                 status[0] = p->status;
1915                 status[1] = 0;
1916         }
1917         switch (p->status) {
1918         case DIFF_STATUS_COPIED:
1919         case DIFF_STATUS_RENAMED:
1920                 two_paths = 1;
1921                 break;
1922         case DIFF_STATUS_ADDED:
1923         case DIFF_STATUS_DELETED:
1924                 two_paths = 0;
1925                 break;
1926         default:
1927                 two_paths = 0;
1928                 break;
1929         }
1930         if (!(options->output_format & DIFF_FORMAT_NAME_STATUS)) {
1931                 printf(":%06o %06o %s ",
1932                        p->one->mode, p->two->mode,
1933                        diff_unique_abbrev(p->one->sha1, abbrev));
1934                 printf("%s ",
1935                        diff_unique_abbrev(p->two->sha1, abbrev));
1936         }
1937         printf("%s%c%s", status, inter_name_termination, path_one);
1938         if (two_paths)
1939                 printf("%c%s", inter_name_termination, path_two);
1940         putchar(line_termination);
1941         if (path_one != p->one->path)
1942                 free((void*)path_one);
1943         if (path_two != p->two->path)
1944                 free((void*)path_two);
1945 }
1946
1947 static void diff_flush_name(struct diff_filepair *p, int line_termination)
1948 {
1949         char *path = p->two->path;
1950
1951         if (line_termination)
1952                 path = quote_one(p->two->path);
1953         printf("%s%c", path, line_termination);
1954         if (p->two->path != path)
1955                 free(path);
1956 }
1957
1958 int diff_unmodified_pair(struct diff_filepair *p)
1959 {
1960         /* This function is written stricter than necessary to support
1961          * the currently implemented transformers, but the idea is to
1962          * let transformers to produce diff_filepairs any way they want,
1963          * and filter and clean them up here before producing the output.
1964          */
1965         struct diff_filespec *one, *two;
1966
1967         if (DIFF_PAIR_UNMERGED(p))
1968                 return 0; /* unmerged is interesting */
1969
1970         one = p->one;
1971         two = p->two;
1972
1973         /* deletion, addition, mode or type change
1974          * and rename are all interesting.
1975          */
1976         if (DIFF_FILE_VALID(one) != DIFF_FILE_VALID(two) ||
1977             DIFF_PAIR_MODE_CHANGED(p) ||
1978             strcmp(one->path, two->path))
1979                 return 0;
1980
1981         /* both are valid and point at the same path.  that is, we are
1982          * dealing with a change.
1983          */
1984         if (one->sha1_valid && two->sha1_valid &&
1985             !hashcmp(one->sha1, two->sha1))
1986                 return 1; /* no change */
1987         if (!one->sha1_valid && !two->sha1_valid)
1988                 return 1; /* both look at the same file on the filesystem. */
1989         return 0;
1990 }
1991
1992 static void diff_flush_patch(struct diff_filepair *p, struct diff_options *o)
1993 {
1994         if (diff_unmodified_pair(p))
1995                 return;
1996
1997         if ((DIFF_FILE_VALID(p->one) && S_ISDIR(p->one->mode)) ||
1998             (DIFF_FILE_VALID(p->two) && S_ISDIR(p->two->mode)))
1999                 return; /* no tree diffs in patch format */
2000
2001         run_diff(p, o);
2002 }
2003
2004 static void diff_flush_stat(struct diff_filepair *p, struct diff_options *o,
2005                             struct diffstat_t *diffstat)
2006 {
2007         if (diff_unmodified_pair(p))
2008                 return;
2009
2010         if ((DIFF_FILE_VALID(p->one) && S_ISDIR(p->one->mode)) ||
2011             (DIFF_FILE_VALID(p->two) && S_ISDIR(p->two->mode)))
2012                 return; /* no tree diffs in patch format */
2013
2014         run_diffstat(p, o, diffstat);
2015 }
2016
2017 static void diff_flush_checkdiff(struct diff_filepair *p,
2018                 struct diff_options *o)
2019 {
2020         if (diff_unmodified_pair(p))
2021                 return;
2022
2023         if ((DIFF_FILE_VALID(p->one) && S_ISDIR(p->one->mode)) ||
2024             (DIFF_FILE_VALID(p->two) && S_ISDIR(p->two->mode)))
2025                 return; /* no tree diffs in patch format */
2026
2027         run_checkdiff(p, o);
2028 }
2029
2030 int diff_queue_is_empty(void)
2031 {
2032         struct diff_queue_struct *q = &diff_queued_diff;
2033         int i;
2034         for (i = 0; i < q->nr; i++)
2035                 if (!diff_unmodified_pair(q->queue[i]))
2036                         return 0;
2037         return 1;
2038 }
2039
2040 #if DIFF_DEBUG
2041 void diff_debug_filespec(struct diff_filespec *s, int x, const char *one)
2042 {
2043         fprintf(stderr, "queue[%d] %s (%s) %s %06o %s\n",
2044                 x, one ? one : "",
2045                 s->path,
2046                 DIFF_FILE_VALID(s) ? "valid" : "invalid",
2047                 s->mode,
2048                 s->sha1_valid ? sha1_to_hex(s->sha1) : "");
2049         fprintf(stderr, "queue[%d] %s size %lu flags %d\n",
2050                 x, one ? one : "",
2051                 s->size, s->xfrm_flags);
2052 }
2053
2054 void diff_debug_filepair(const struct diff_filepair *p, int i)
2055 {
2056         diff_debug_filespec(p->one, i, "one");
2057         diff_debug_filespec(p->two, i, "two");
2058         fprintf(stderr, "score %d, status %c stays %d broken %d\n",
2059                 p->score, p->status ? p->status : '?',
2060                 p->source_stays, p->broken_pair);
2061 }
2062
2063 void diff_debug_queue(const char *msg, struct diff_queue_struct *q)
2064 {
2065         int i;
2066         if (msg)
2067                 fprintf(stderr, "%s\n", msg);
2068         fprintf(stderr, "q->nr = %d\n", q->nr);
2069         for (i = 0; i < q->nr; i++) {
2070                 struct diff_filepair *p = q->queue[i];
2071                 diff_debug_filepair(p, i);
2072         }
2073 }
2074 #endif
2075
2076 static void diff_resolve_rename_copy(void)
2077 {
2078         int i, j;
2079         struct diff_filepair *p, *pp;
2080         struct diff_queue_struct *q = &diff_queued_diff;
2081
2082         diff_debug_queue("resolve-rename-copy", q);
2083
2084         for (i = 0; i < q->nr; i++) {
2085                 p = q->queue[i];
2086                 p->status = 0; /* undecided */
2087                 if (DIFF_PAIR_UNMERGED(p))
2088                         p->status = DIFF_STATUS_UNMERGED;
2089                 else if (!DIFF_FILE_VALID(p->one))
2090                         p->status = DIFF_STATUS_ADDED;
2091                 else if (!DIFF_FILE_VALID(p->two))
2092                         p->status = DIFF_STATUS_DELETED;
2093                 else if (DIFF_PAIR_TYPE_CHANGED(p))
2094                         p->status = DIFF_STATUS_TYPE_CHANGED;
2095
2096                 /* from this point on, we are dealing with a pair
2097                  * whose both sides are valid and of the same type, i.e.
2098                  * either in-place edit or rename/copy edit.
2099                  */
2100                 else if (DIFF_PAIR_RENAME(p)) {
2101                         if (p->source_stays) {
2102                                 p->status = DIFF_STATUS_COPIED;
2103                                 continue;
2104                         }
2105                         /* See if there is some other filepair that
2106                          * copies from the same source as us.  If so
2107                          * we are a copy.  Otherwise we are either a
2108                          * copy if the path stays, or a rename if it
2109                          * does not, but we already handled "stays" case.
2110                          */
2111                         for (j = i + 1; j < q->nr; j++) {
2112                                 pp = q->queue[j];
2113                                 if (strcmp(pp->one->path, p->one->path))
2114                                         continue; /* not us */
2115                                 if (!DIFF_PAIR_RENAME(pp))
2116                                         continue; /* not a rename/copy */
2117                                 /* pp is a rename/copy from the same source */
2118                                 p->status = DIFF_STATUS_COPIED;
2119                                 break;
2120                         }
2121                         if (!p->status)
2122                                 p->status = DIFF_STATUS_RENAMED;
2123                 }
2124                 else if (hashcmp(p->one->sha1, p->two->sha1) ||
2125                          p->one->mode != p->two->mode)
2126                         p->status = DIFF_STATUS_MODIFIED;
2127                 else {
2128                         /* This is a "no-change" entry and should not
2129                          * happen anymore, but prepare for broken callers.
2130                          */
2131                         error("feeding unmodified %s to diffcore",
2132                               p->one->path);
2133                         p->status = DIFF_STATUS_UNKNOWN;
2134                 }
2135         }
2136         diff_debug_queue("resolve-rename-copy done", q);
2137 }
2138
2139 static int check_pair_status(struct diff_filepair *p)
2140 {
2141         switch (p->status) {
2142         case DIFF_STATUS_UNKNOWN:
2143                 return 0;
2144         case 0:
2145                 die("internal error in diff-resolve-rename-copy");
2146         default:
2147                 return 1;
2148         }
2149 }
2150
2151 static void flush_one_pair(struct diff_filepair *p, struct diff_options *opt)
2152 {
2153         int fmt = opt->output_format;
2154
2155         if (fmt & DIFF_FORMAT_CHECKDIFF)
2156                 diff_flush_checkdiff(p, opt);
2157         else if (fmt & (DIFF_FORMAT_RAW | DIFF_FORMAT_NAME_STATUS))
2158                 diff_flush_raw(p, opt);
2159         else if (fmt & DIFF_FORMAT_NAME)
2160                 diff_flush_name(p, opt->line_termination);
2161 }
2162
2163 static void show_file_mode_name(const char *newdelete, struct diff_filespec *fs)
2164 {
2165         if (fs->mode)
2166                 printf(" %s mode %06o %s\n", newdelete, fs->mode, fs->path);
2167         else
2168                 printf(" %s %s\n", newdelete, fs->path);
2169 }
2170
2171
2172 static void show_mode_change(struct diff_filepair *p, int show_name)
2173 {
2174         if (p->one->mode && p->two->mode && p->one->mode != p->two->mode) {
2175                 if (show_name)
2176                         printf(" mode change %06o => %06o %s\n",
2177                                p->one->mode, p->two->mode, p->two->path);
2178                 else
2179                         printf(" mode change %06o => %06o\n",
2180                                p->one->mode, p->two->mode);
2181         }
2182 }
2183
2184 static void show_rename_copy(const char *renamecopy, struct diff_filepair *p)
2185 {
2186         const char *old, *new;
2187
2188         /* Find common prefix */
2189         old = p->one->path;
2190         new = p->two->path;
2191         while (1) {
2192                 const char *slash_old, *slash_new;
2193                 slash_old = strchr(old, '/');
2194                 slash_new = strchr(new, '/');
2195                 if (!slash_old ||
2196                     !slash_new ||
2197                     slash_old - old != slash_new - new ||
2198                     memcmp(old, new, slash_new - new))
2199                         break;
2200                 old = slash_old + 1;
2201                 new = slash_new + 1;
2202         }
2203         /* p->one->path thru old is the common prefix, and old and new
2204          * through the end of names are renames
2205          */
2206         if (old != p->one->path)
2207                 printf(" %s %.*s{%s => %s} (%d%%)\n", renamecopy,
2208                        (int)(old - p->one->path), p->one->path,
2209                        old, new, (int)(0.5 + p->score * 100.0/MAX_SCORE));
2210         else
2211                 printf(" %s %s => %s (%d%%)\n", renamecopy,
2212                        p->one->path, p->two->path,
2213                        (int)(0.5 + p->score * 100.0/MAX_SCORE));
2214         show_mode_change(p, 0);
2215 }
2216
2217 static void diff_summary(struct diff_filepair *p)
2218 {
2219         switch(p->status) {
2220         case DIFF_STATUS_DELETED:
2221                 show_file_mode_name("delete", p->one);
2222                 break;
2223         case DIFF_STATUS_ADDED:
2224                 show_file_mode_name("create", p->two);
2225                 break;
2226         case DIFF_STATUS_COPIED:
2227                 show_rename_copy("copy", p);
2228                 break;
2229         case DIFF_STATUS_RENAMED:
2230                 show_rename_copy("rename", p);
2231                 break;
2232         default:
2233                 if (p->score) {
2234                         printf(" rewrite %s (%d%%)\n", p->two->path,
2235                                 (int)(0.5 + p->score * 100.0/MAX_SCORE));
2236                         show_mode_change(p, 0);
2237                 } else  show_mode_change(p, 1);
2238                 break;
2239         }
2240 }
2241
2242 struct patch_id_t {
2243         struct xdiff_emit_state xm;
2244         SHA_CTX *ctx;
2245         int patchlen;
2246 };
2247
2248 static int remove_space(char *line, int len)
2249 {
2250         int i;
2251         char *dst = line;
2252         unsigned char c;
2253
2254         for (i = 0; i < len; i++)
2255                 if (!isspace((c = line[i])))
2256                         *dst++ = c;
2257
2258         return dst - line;
2259 }
2260
2261 static void patch_id_consume(void *priv, char *line, unsigned long len)
2262 {
2263         struct patch_id_t *data = priv;
2264         int new_len;
2265
2266         /* Ignore line numbers when computing the SHA1 of the patch */
2267         if (!strncmp(line, "@@ -", 4))
2268                 return;
2269
2270         new_len = remove_space(line, len);
2271
2272         SHA1_Update(data->ctx, line, new_len);
2273         data->patchlen += new_len;
2274 }
2275
2276 /* returns 0 upon success, and writes result into sha1 */
2277 static int diff_get_patch_id(struct diff_options *options, unsigned char *sha1)
2278 {
2279         struct diff_queue_struct *q = &diff_queued_diff;
2280         int i;
2281         SHA_CTX ctx;
2282         struct patch_id_t data;
2283         char buffer[PATH_MAX * 4 + 20];
2284
2285         SHA1_Init(&ctx);
2286         memset(&data, 0, sizeof(struct patch_id_t));
2287         data.ctx = &ctx;
2288         data.xm.consume = patch_id_consume;
2289
2290         for (i = 0; i < q->nr; i++) {
2291                 xpparam_t xpp;
2292                 xdemitconf_t xecfg;
2293                 xdemitcb_t ecb;
2294                 mmfile_t mf1, mf2;
2295                 struct diff_filepair *p = q->queue[i];
2296                 int len1, len2;
2297
2298                 if (p->status == 0)
2299                         return error("internal diff status error");
2300                 if (p->status == DIFF_STATUS_UNKNOWN)
2301                         continue;
2302                 if (diff_unmodified_pair(p))
2303                         continue;
2304                 if ((DIFF_FILE_VALID(p->one) && S_ISDIR(p->one->mode)) ||
2305                     (DIFF_FILE_VALID(p->two) && S_ISDIR(p->two->mode)))
2306                         continue;
2307                 if (DIFF_PAIR_UNMERGED(p))
2308                         continue;
2309
2310                 diff_fill_sha1_info(p->one);
2311                 diff_fill_sha1_info(p->two);
2312                 if (fill_mmfile(&mf1, p->one) < 0 ||
2313                                 fill_mmfile(&mf2, p->two) < 0)
2314                         return error("unable to read files to diff");
2315
2316                 /* Maybe hash p->two? into the patch id? */
2317                 if (mmfile_is_binary(&mf2))
2318                         continue;
2319
2320                 len1 = remove_space(p->one->path, strlen(p->one->path));
2321                 len2 = remove_space(p->two->path, strlen(p->two->path));
2322                 if (p->one->mode == 0)
2323                         len1 = snprintf(buffer, sizeof(buffer),
2324                                         "diff--gita/%.*sb/%.*s"
2325                                         "newfilemode%06o"
2326                                         "---/dev/null"
2327                                         "+++b/%.*s",
2328                                         len1, p->one->path,
2329                                         len2, p->two->path,
2330                                         p->two->mode,
2331                                         len2, p->two->path);
2332                 else if (p->two->mode == 0)
2333                         len1 = snprintf(buffer, sizeof(buffer),
2334                                         "diff--gita/%.*sb/%.*s"
2335                                         "deletedfilemode%06o"
2336                                         "---a/%.*s"
2337                                         "+++/dev/null",
2338                                         len1, p->one->path,
2339                                         len2, p->two->path,
2340                                         p->one->mode,
2341                                         len1, p->one->path);
2342                 else
2343                         len1 = snprintf(buffer, sizeof(buffer),
2344                                         "diff--gita/%.*sb/%.*s"
2345                                         "---a/%.*s"
2346                                         "+++b/%.*s",
2347                                         len1, p->one->path,
2348                                         len2, p->two->path,
2349                                         len1, p->one->path,
2350                                         len2, p->two->path);
2351                 SHA1_Update(&ctx, buffer, len1);
2352
2353                 xpp.flags = XDF_NEED_MINIMAL;
2354                 xecfg.ctxlen = 3;
2355                 xecfg.flags = XDL_EMIT_FUNCNAMES;
2356                 ecb.outf = xdiff_outf;
2357                 ecb.priv = &data;
2358                 xdl_diff(&mf1, &mf2, &xpp, &xecfg, &ecb);
2359         }
2360
2361         SHA1_Final(sha1, &ctx);
2362         return 0;
2363 }
2364
2365 int diff_flush_patch_id(struct diff_options *options, unsigned char *sha1)
2366 {
2367         struct diff_queue_struct *q = &diff_queued_diff;
2368         int i;
2369         int result = diff_get_patch_id(options, sha1);
2370
2371         for (i = 0; i < q->nr; i++)
2372                 diff_free_filepair(q->queue[i]);
2373
2374         free(q->queue);
2375         q->queue = NULL;
2376         q->nr = q->alloc = 0;
2377
2378         return result;
2379 }
2380
2381 static int is_summary_empty(const struct diff_queue_struct *q)
2382 {
2383         int i;
2384
2385         for (i = 0; i < q->nr; i++) {
2386                 const struct diff_filepair *p = q->queue[i];
2387
2388                 switch (p->status) {
2389                 case DIFF_STATUS_DELETED:
2390                 case DIFF_STATUS_ADDED:
2391                 case DIFF_STATUS_COPIED:
2392                 case DIFF_STATUS_RENAMED:
2393                         return 0;
2394                 default:
2395                         if (p->score)
2396                                 return 0;
2397                         if (p->one->mode && p->two->mode &&
2398                             p->one->mode != p->two->mode)
2399                                 return 0;
2400                         break;
2401                 }
2402         }
2403         return 1;
2404 }
2405
2406 void diff_flush(struct diff_options *options)
2407 {
2408         struct diff_queue_struct *q = &diff_queued_diff;
2409         int i, output_format = options->output_format;
2410         int separator = 0;
2411
2412         /*
2413          * Order: raw, stat, summary, patch
2414          * or:    name/name-status/checkdiff (other bits clear)
2415          */
2416         if (!q->nr)
2417                 goto free_queue;
2418
2419         if (output_format & (DIFF_FORMAT_RAW |
2420                              DIFF_FORMAT_NAME |
2421                              DIFF_FORMAT_NAME_STATUS |
2422                              DIFF_FORMAT_CHECKDIFF)) {
2423                 for (i = 0; i < q->nr; i++) {
2424                         struct diff_filepair *p = q->queue[i];
2425                         if (check_pair_status(p))
2426                                 flush_one_pair(p, options);
2427                 }
2428                 separator++;
2429         }
2430
2431         if (output_format & DIFF_FORMAT_DIFFSTAT) {
2432                 struct diffstat_t diffstat;
2433
2434                 memset(&diffstat, 0, sizeof(struct diffstat_t));
2435                 diffstat.xm.consume = diffstat_consume;
2436                 for (i = 0; i < q->nr; i++) {
2437                         struct diff_filepair *p = q->queue[i];
2438                         if (check_pair_status(p))
2439                                 diff_flush_stat(p, options, &diffstat);
2440                 }
2441                 show_stats(&diffstat);
2442                 separator++;
2443         }
2444
2445         if (output_format & DIFF_FORMAT_SUMMARY && !is_summary_empty(q)) {
2446                 for (i = 0; i < q->nr; i++)
2447                         diff_summary(q->queue[i]);
2448                 separator++;
2449         }
2450
2451         if (output_format & DIFF_FORMAT_PATCH) {
2452                 if (separator) {
2453                         if (options->stat_sep) {
2454                                 /* attach patch instead of inline */
2455                                 fputs(options->stat_sep, stdout);
2456                         } else {
2457                                 putchar(options->line_termination);
2458                         }
2459                 }
2460
2461                 for (i = 0; i < q->nr; i++) {
2462                         struct diff_filepair *p = q->queue[i];
2463                         if (check_pair_status(p))
2464                                 diff_flush_patch(p, options);
2465                 }
2466         }
2467
2468         if (output_format & DIFF_FORMAT_CALLBACK)
2469                 options->format_callback(q, options, options->format_callback_data);
2470
2471         for (i = 0; i < q->nr; i++)
2472                 diff_free_filepair(q->queue[i]);
2473 free_queue:
2474         free(q->queue);
2475         q->queue = NULL;
2476         q->nr = q->alloc = 0;
2477 }
2478
2479 static void diffcore_apply_filter(const char *filter)
2480 {
2481         int i;
2482         struct diff_queue_struct *q = &diff_queued_diff;
2483         struct diff_queue_struct outq;
2484         outq.queue = NULL;
2485         outq.nr = outq.alloc = 0;
2486
2487         if (!filter)
2488                 return;
2489
2490         if (strchr(filter, DIFF_STATUS_FILTER_AON)) {
2491                 int found;
2492                 for (i = found = 0; !found && i < q->nr; i++) {
2493                         struct diff_filepair *p = q->queue[i];
2494                         if (((p->status == DIFF_STATUS_MODIFIED) &&
2495                              ((p->score &&
2496                                strchr(filter, DIFF_STATUS_FILTER_BROKEN)) ||
2497                               (!p->score &&
2498                                strchr(filter, DIFF_STATUS_MODIFIED)))) ||
2499                             ((p->status != DIFF_STATUS_MODIFIED) &&
2500                              strchr(filter, p->status)))
2501                                 found++;
2502                 }
2503                 if (found)
2504                         return;
2505
2506                 /* otherwise we will clear the whole queue
2507                  * by copying the empty outq at the end of this
2508                  * function, but first clear the current entries
2509                  * in the queue.
2510                  */
2511                 for (i = 0; i < q->nr; i++)
2512                         diff_free_filepair(q->queue[i]);
2513         }
2514         else {
2515                 /* Only the matching ones */
2516                 for (i = 0; i < q->nr; i++) {
2517                         struct diff_filepair *p = q->queue[i];
2518
2519                         if (((p->status == DIFF_STATUS_MODIFIED) &&
2520                              ((p->score &&
2521                                strchr(filter, DIFF_STATUS_FILTER_BROKEN)) ||
2522                               (!p->score &&
2523                                strchr(filter, DIFF_STATUS_MODIFIED)))) ||
2524                             ((p->status != DIFF_STATUS_MODIFIED) &&
2525                              strchr(filter, p->status)))
2526                                 diff_q(&outq, p);
2527                         else
2528                                 diff_free_filepair(p);
2529                 }
2530         }
2531         free(q->queue);
2532         *q = outq;
2533 }
2534
2535 void diffcore_std(struct diff_options *options)
2536 {
2537         if (options->break_opt != -1)
2538                 diffcore_break(options->break_opt);
2539         if (options->detect_rename)
2540                 diffcore_rename(options);
2541         if (options->break_opt != -1)
2542                 diffcore_merge_broken();
2543         if (options->pickaxe)
2544                 diffcore_pickaxe(options->pickaxe, options->pickaxe_opts);
2545         if (options->orderfile)
2546                 diffcore_order(options->orderfile);
2547         diff_resolve_rename_copy();
2548         diffcore_apply_filter(options->filter);
2549 }
2550
2551
2552 void diffcore_std_no_resolve(struct diff_options *options)
2553 {
2554         if (options->pickaxe)
2555                 diffcore_pickaxe(options->pickaxe, options->pickaxe_opts);
2556         if (options->orderfile)
2557                 diffcore_order(options->orderfile);
2558         diffcore_apply_filter(options->filter);
2559 }
2560
2561 void diff_addremove(struct diff_options *options,
2562                     int addremove, unsigned mode,
2563                     const unsigned char *sha1,
2564                     const char *base, const char *path)
2565 {
2566         char concatpath[PATH_MAX];
2567         struct diff_filespec *one, *two;
2568
2569         /* This may look odd, but it is a preparation for
2570          * feeding "there are unchanged files which should
2571          * not produce diffs, but when you are doing copy
2572          * detection you would need them, so here they are"
2573          * entries to the diff-core.  They will be prefixed
2574          * with something like '=' or '*' (I haven't decided
2575          * which but should not make any difference).
2576          * Feeding the same new and old to diff_change() 
2577          * also has the same effect.
2578          * Before the final output happens, they are pruned after
2579          * merged into rename/copy pairs as appropriate.
2580          */
2581         if (options->reverse_diff)
2582                 addremove = (addremove == '+' ? '-' :
2583                              addremove == '-' ? '+' : addremove);
2584
2585         if (!path) path = "";
2586         sprintf(concatpath, "%s%s", base, path);
2587         one = alloc_filespec(concatpath);
2588         two = alloc_filespec(concatpath);
2589
2590         if (addremove != '+')
2591                 fill_filespec(one, sha1, mode);
2592         if (addremove != '-')
2593                 fill_filespec(two, sha1, mode);
2594
2595         diff_queue(&diff_queued_diff, one, two);
2596 }
2597
2598 void diff_change(struct diff_options *options,
2599                  unsigned old_mode, unsigned new_mode,
2600                  const unsigned char *old_sha1,
2601                  const unsigned char *new_sha1,
2602                  const char *base, const char *path) 
2603 {
2604         char concatpath[PATH_MAX];
2605         struct diff_filespec *one, *two;
2606
2607         if (options->reverse_diff) {
2608                 unsigned tmp;
2609                 const unsigned char *tmp_c;
2610                 tmp = old_mode; old_mode = new_mode; new_mode = tmp;
2611                 tmp_c = old_sha1; old_sha1 = new_sha1; new_sha1 = tmp_c;
2612         }
2613         if (!path) path = "";
2614         sprintf(concatpath, "%s%s", base, path);
2615         one = alloc_filespec(concatpath);
2616         two = alloc_filespec(concatpath);
2617         fill_filespec(one, old_sha1, old_mode);
2618         fill_filespec(two, new_sha1, new_mode);
2619
2620         diff_queue(&diff_queued_diff, one, two);
2621 }
2622
2623 void diff_unmerge(struct diff_options *options,
2624                   const char *path)
2625 {
2626         struct diff_filespec *one, *two;
2627         one = alloc_filespec(path);
2628         two = alloc_filespec(path);
2629         diff_queue(&diff_queued_diff, one, two);
2630 }