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