binary patch.
[git] / diff.c
1 /*
2  * Copyright (C) 2005 Junio C Hamano
3  */
4 #include <sys/types.h>
5 #include <sys/wait.h>
6 #include <signal.h>
7 #include "cache.h"
8 #include "quote.h"
9 #include "diff.h"
10 #include "diffcore.h"
11 #include "delta.h"
12 #include "xdiff-interface.h"
13
14 static int use_size_cache;
15
16 int diff_rename_limit_default = -1;
17
18 int git_diff_config(const char *var, const char *value)
19 {
20         if (!strcmp(var, "diff.renamelimit")) {
21                 diff_rename_limit_default = git_config_int(var, value);
22                 return 0;
23         }
24
25         return git_default_config(var, value);
26 }
27
28 static char *quote_one(const char *str)
29 {
30         int needlen;
31         char *xp;
32
33         if (!str)
34                 return NULL;
35         needlen = quote_c_style(str, NULL, NULL, 0);
36         if (!needlen)
37                 return strdup(str);
38         xp = xmalloc(needlen + 1);
39         quote_c_style(str, xp, NULL, 0);
40         return xp;
41 }
42
43 static char *quote_two(const char *one, const char *two)
44 {
45         int need_one = quote_c_style(one, NULL, NULL, 1);
46         int need_two = quote_c_style(two, NULL, NULL, 1);
47         char *xp;
48
49         if (need_one + need_two) {
50                 if (!need_one) need_one = strlen(one);
51                 if (!need_two) need_one = strlen(two);
52
53                 xp = xmalloc(need_one + need_two + 3);
54                 xp[0] = '"';
55                 quote_c_style(one, xp + 1, NULL, 1);
56                 quote_c_style(two, xp + need_one + 1, NULL, 1);
57                 strcpy(xp + need_one + need_two + 1, "\"");
58                 return xp;
59         }
60         need_one = strlen(one);
61         need_two = strlen(two);
62         xp = xmalloc(need_one + need_two + 1);
63         strcpy(xp, one);
64         strcpy(xp + need_one, two);
65         return xp;
66 }
67
68 static const char *external_diff(void)
69 {
70         static const char *external_diff_cmd = NULL;
71         static int done_preparing = 0;
72
73         if (done_preparing)
74                 return external_diff_cmd;
75         external_diff_cmd = getenv("GIT_EXTERNAL_DIFF");
76         done_preparing = 1;
77         return external_diff_cmd;
78 }
79
80 #define TEMPFILE_PATH_LEN               50
81
82 static struct diff_tempfile {
83         const char *name; /* filename external diff should read from */
84         char hex[41];
85         char mode[10];
86         char tmp_path[TEMPFILE_PATH_LEN];
87 } diff_temp[2];
88
89 static int count_lines(const char *data, int size)
90 {
91         int count, ch, completely_empty = 1, nl_just_seen = 0;
92         count = 0;
93         while (0 < size--) {
94                 ch = *data++;
95                 if (ch == '\n') {
96                         count++;
97                         nl_just_seen = 1;
98                         completely_empty = 0;
99                 }
100                 else {
101                         nl_just_seen = 0;
102                         completely_empty = 0;
103                 }
104         }
105         if (completely_empty)
106                 return 0;
107         if (!nl_just_seen)
108                 count++; /* no trailing newline */
109         return count;
110 }
111
112 static void print_line_count(int count)
113 {
114         switch (count) {
115         case 0:
116                 printf("0,0");
117                 break;
118         case 1:
119                 printf("1");
120                 break;
121         default:
122                 printf("1,%d", count);
123                 break;
124         }
125 }
126
127 static void copy_file(int prefix, const char *data, int size)
128 {
129         int ch, nl_just_seen = 1;
130         while (0 < size--) {
131                 ch = *data++;
132                 if (nl_just_seen)
133                         putchar(prefix);
134                 putchar(ch);
135                 if (ch == '\n')
136                         nl_just_seen = 1;
137                 else
138                         nl_just_seen = 0;
139         }
140         if (!nl_just_seen)
141                 printf("\n\\ No newline at end of file\n");
142 }
143
144 static void emit_rewrite_diff(const char *name_a,
145                               const char *name_b,
146                               struct diff_filespec *one,
147                               struct diff_filespec *two)
148 {
149         int lc_a, lc_b;
150         diff_populate_filespec(one, 0);
151         diff_populate_filespec(two, 0);
152         lc_a = count_lines(one->data, one->size);
153         lc_b = count_lines(two->data, two->size);
154         printf("--- %s\n+++ %s\n@@ -", name_a, name_b);
155         print_line_count(lc_a);
156         printf(" +");
157         print_line_count(lc_b);
158         printf(" @@\n");
159         if (lc_a)
160                 copy_file('-', one->data, one->size);
161         if (lc_b)
162                 copy_file('+', two->data, two->size);
163 }
164
165 static int fill_mmfile(mmfile_t *mf, struct diff_filespec *one)
166 {
167         if (!DIFF_FILE_VALID(one)) {
168                 mf->ptr = ""; /* does not matter */
169                 mf->size = 0;
170                 return 0;
171         }
172         else if (diff_populate_filespec(one, 0))
173                 return -1;
174         mf->ptr = one->data;
175         mf->size = one->size;
176         return 0;
177 }
178
179 struct emit_callback {
180         const char **label_path;
181 };
182
183 static int fn_out(void *priv, mmbuffer_t *mb, int nbuf)
184 {
185         int i;
186         struct emit_callback *ecbdata = priv;
187
188         if (ecbdata->label_path[0]) {
189                 printf("--- %s\n", ecbdata->label_path[0]);
190                 printf("+++ %s\n", ecbdata->label_path[1]);
191                 ecbdata->label_path[0] = ecbdata->label_path[1] = NULL;
192         }
193         for (i = 0; i < nbuf; i++)
194                 if (!fwrite(mb[i].ptr, mb[i].size, 1, stdout))
195                         return -1;
196         return 0;
197 }
198
199 static char *pprint_rename(const char *a, const char *b)
200 {
201         const char *old = a;
202         const char *new = b;
203         char *name = NULL;
204         int pfx_length, sfx_length;
205         int len_a = strlen(a);
206         int len_b = strlen(b);
207
208         /* Find common prefix */
209         pfx_length = 0;
210         while (*old && *new && *old == *new) {
211                 if (*old == '/')
212                         pfx_length = old - a + 1;
213                 old++;
214                 new++;
215         }
216
217         /* Find common suffix */
218         old = a + len_a;
219         new = b + len_b;
220         sfx_length = 0;
221         while (a <= old && b <= new && *old == *new) {
222                 if (*old == '/')
223                         sfx_length = len_a - (old - a);
224                 old--;
225                 new--;
226         }
227
228         /*
229          * pfx{mid-a => mid-b}sfx
230          * {pfx-a => pfx-b}sfx
231          * pfx{sfx-a => sfx-b}
232          * name-a => name-b
233          */
234         if (pfx_length + sfx_length) {
235                 name = xmalloc(len_a + len_b - pfx_length - sfx_length + 7);
236                 sprintf(name, "%.*s{%.*s => %.*s}%s",
237                         pfx_length, a,
238                         len_a - pfx_length - sfx_length, a + pfx_length,
239                         len_b - pfx_length - sfx_length, b + pfx_length,
240                         a + len_a - sfx_length);
241         }
242         else {
243                 name = xmalloc(len_a + len_b + 5);
244                 sprintf(name, "%s => %s", a, b);
245         }
246         return name;
247 }
248
249 struct diffstat_t {
250         struct xdiff_emit_state xm;
251
252         int nr;
253         int alloc;
254         struct diffstat_file {
255                 char *name;
256                 unsigned is_unmerged:1;
257                 unsigned is_binary:1;
258                 unsigned is_renamed:1;
259                 unsigned int added, deleted;
260         } **files;
261 };
262
263 static struct diffstat_file *diffstat_add(struct diffstat_t *diffstat,
264                                           const char *name_a,
265                                           const char *name_b)
266 {
267         struct diffstat_file *x;
268         x = xcalloc(sizeof (*x), 1);
269         if (diffstat->nr == diffstat->alloc) {
270                 diffstat->alloc = alloc_nr(diffstat->alloc);
271                 diffstat->files = xrealloc(diffstat->files,
272                                 diffstat->alloc * sizeof(x));
273         }
274         diffstat->files[diffstat->nr++] = x;
275         if (name_b) {
276                 x->name = pprint_rename(name_a, name_b);
277                 x->is_renamed = 1;
278         }
279         else
280                 x->name = strdup(name_a);
281         return x;
282 }
283
284 static void diffstat_consume(void *priv, char *line, unsigned long len)
285 {
286         struct diffstat_t *diffstat = priv;
287         struct diffstat_file *x = diffstat->files[diffstat->nr - 1];
288
289         if (line[0] == '+')
290                 x->added++;
291         else if (line[0] == '-')
292                 x->deleted++;
293 }
294
295 static const char pluses[] = "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++";
296 static const char minuses[]= "----------------------------------------------------------------------";
297
298 static void show_stats(struct diffstat_t* data)
299 {
300         char *prefix = "";
301         int i, len, add, del, total, adds = 0, dels = 0;
302         int max, max_change = 0, max_len = 0;
303         int total_files = data->nr;
304
305         if (data->nr == 0)
306                 return;
307
308         for (i = 0; i < data->nr; i++) {
309                 struct diffstat_file *file = data->files[i];
310
311                 len = strlen(file->name);
312                 if (max_len < len)
313                         max_len = len;
314
315                 if (file->is_binary || file->is_unmerged)
316                         continue;
317                 if (max_change < file->added + file->deleted)
318                         max_change = file->added + file->deleted;
319         }
320
321         for (i = 0; i < data->nr; i++) {
322                 char *name = data->files[i]->name;
323                 int added = data->files[i]->added;
324                 int deleted = data->files[i]->deleted;
325
326                 if (0 < (len = quote_c_style(name, NULL, NULL, 0))) {
327                         char *qname = xmalloc(len + 1);
328                         quote_c_style(name, qname, NULL, 0);
329                         free(name);
330                         data->files[i]->name = name = qname;
331                 }
332
333                 /*
334                  * "scale" the filename
335                  */
336                 len = strlen(name);
337                 max = max_len;
338                 if (max > 50)
339                         max = 50;
340                 if (len > max) {
341                         char *slash;
342                         prefix = "...";
343                         max -= 3;
344                         name += len - max;
345                         slash = strchr(name, '/');
346                         if (slash)
347                                 name = slash;
348                 }
349                 len = max;
350
351                 /*
352                  * scale the add/delete
353                  */
354                 max = max_change;
355                 if (max + len > 70)
356                         max = 70 - len;
357
358                 if (data->files[i]->is_binary) {
359                         printf(" %s%-*s |  Bin\n", prefix, len, name);
360                         goto free_diffstat_file;
361                 }
362                 else if (data->files[i]->is_unmerged) {
363                         printf(" %s%-*s |  Unmerged\n", prefix, len, name);
364                         goto free_diffstat_file;
365                 }
366                 else if (!data->files[i]->is_renamed &&
367                          (added + deleted == 0)) {
368                         total_files--;
369                         goto free_diffstat_file;
370                 }
371
372                 add = added;
373                 del = deleted;
374                 total = add + del;
375                 adds += add;
376                 dels += del;
377
378                 if (max_change > 0) {
379                         total = (total * max + max_change / 2) / max_change;
380                         add = (add * max + max_change / 2) / max_change;
381                         del = total - add;
382                 }
383                 printf(" %s%-*s |%5d %.*s%.*s\n", prefix,
384                                 len, name, added + deleted,
385                                 add, pluses, del, minuses);
386         free_diffstat_file:
387                 free(data->files[i]->name);
388                 free(data->files[i]);
389         }
390         free(data->files);
391         printf(" %d files changed, %d insertions(+), %d deletions(-)\n",
392                         total_files, adds, dels);
393 }
394
395 static void *encode_delta_size(void *data, unsigned long size)
396 {
397         unsigned char *cp = data;
398         *cp++ = size;
399         size >>= 7;
400         while (size) {
401                 cp[-1] |= 0x80;
402                 *cp++ = size;
403                 size >>= 7;
404         }
405         return cp;
406 }
407
408 static void *safe_diff_delta(const unsigned char *src, unsigned long src_size,
409                              const unsigned char *dst, unsigned long dst_size,
410                              unsigned long *delta_size)
411 {
412         unsigned long bufsize;
413         unsigned char *data;
414         unsigned char *cp;
415
416         if (src_size && dst_size)
417                 return diff_delta(src, src_size, dst, dst_size, delta_size, 0);
418
419         /* diff-delta does not like to do delta with empty, so
420          * we do that by hand here.  Sigh...
421          */
422
423         if (!src_size)
424                 /* literal copy can be done only 127-byte at a time.
425                  */
426                 bufsize = dst_size + (dst_size / 127) + 40;
427         else
428                 bufsize = 40;
429         data = xmalloc(bufsize);
430         cp = encode_delta_size(data, src_size);
431         cp = encode_delta_size(cp, dst_size);
432
433         if (dst_size) {
434                 /* copy out literally */
435                 while (dst_size) {
436                         int sz = (127 < dst_size) ? 127 : dst_size;
437                         *cp++ = sz;
438                         dst_size -= sz;
439                         while (sz) {
440                                 *cp++ = *dst++;
441                                 sz--;
442                         }
443                 }
444         }
445         *delta_size = (cp - data);
446         return data;
447 }
448
449 static void emit_binary_diff(mmfile_t *one, mmfile_t *two)
450 {
451         void *delta, *cp;
452         unsigned long delta_size;
453
454         printf("GIT binary patch\n");
455         delta = safe_diff_delta(one->ptr, one->size,
456                                 two->ptr, two->size,
457                                 &delta_size);
458         if (!delta)
459                 die("unable to generate binary diff");
460
461         /* emit delta encoded in base85 */
462         cp = delta;
463         while (delta_size) {
464                 int bytes = (52 < delta_size) ? 52 : delta_size;
465                 char line[70];
466                 delta_size -= bytes;
467                 if (bytes <= 26)
468                         line[0] = bytes + 'A' - 1;
469                 else
470                         line[0] = bytes - 26 + 'a' - 1;
471                 encode_85(line + 1, cp, bytes);
472                 cp += bytes;
473                 puts(line);
474         }
475         printf("\n");
476         free(delta);
477 }
478
479 #define FIRST_FEW_BYTES 8000
480 static int mmfile_is_binary(mmfile_t *mf)
481 {
482         long sz = mf->size;
483         if (FIRST_FEW_BYTES < sz)
484                 sz = FIRST_FEW_BYTES;
485         if (memchr(mf->ptr, 0, sz))
486                 return 1;
487         return 0;
488 }
489
490 static void builtin_diff(const char *name_a,
491                          const char *name_b,
492                          struct diff_filespec *one,
493                          struct diff_filespec *two,
494                          const char *xfrm_msg,
495                          struct diff_options *o,
496                          int complete_rewrite)
497 {
498         mmfile_t mf1, mf2;
499         const char *lbl[2];
500         char *a_one, *b_two;
501
502         a_one = quote_two("a/", name_a);
503         b_two = quote_two("b/", name_b);
504         lbl[0] = DIFF_FILE_VALID(one) ? a_one : "/dev/null";
505         lbl[1] = DIFF_FILE_VALID(two) ? b_two : "/dev/null";
506         printf("diff --git %s %s\n", a_one, b_two);
507         if (lbl[0][0] == '/') {
508                 /* /dev/null */
509                 printf("new file mode %06o\n", two->mode);
510                 if (xfrm_msg && xfrm_msg[0])
511                         puts(xfrm_msg);
512         }
513         else if (lbl[1][0] == '/') {
514                 printf("deleted file mode %06o\n", one->mode);
515                 if (xfrm_msg && xfrm_msg[0])
516                         puts(xfrm_msg);
517         }
518         else {
519                 if (one->mode != two->mode) {
520                         printf("old mode %06o\n", one->mode);
521                         printf("new mode %06o\n", two->mode);
522                 }
523                 if (xfrm_msg && xfrm_msg[0])
524                         puts(xfrm_msg);
525                 /*
526                  * we do not run diff between different kind
527                  * of objects.
528                  */
529                 if ((one->mode ^ two->mode) & S_IFMT)
530                         goto free_ab_and_return;
531                 if (complete_rewrite) {
532                         emit_rewrite_diff(name_a, name_b, one, two);
533                         goto free_ab_and_return;
534                 }
535         }
536
537         if (fill_mmfile(&mf1, one) < 0 || fill_mmfile(&mf2, two) < 0)
538                 die("unable to read files to diff");
539
540         if (mmfile_is_binary(&mf1) || mmfile_is_binary(&mf2)) {
541                 if (o->full_index)
542                         emit_binary_diff(&mf1, &mf2);
543                 else
544                         printf("Binary files %s and %s differ\n",
545                                lbl[0], lbl[1]);
546         }
547         else {
548                 /* Crazy xdl interfaces.. */
549                 const char *diffopts = getenv("GIT_DIFF_OPTS");
550                 xpparam_t xpp;
551                 xdemitconf_t xecfg;
552                 xdemitcb_t ecb;
553                 struct emit_callback ecbdata;
554
555                 ecbdata.label_path = lbl;
556                 xpp.flags = XDF_NEED_MINIMAL;
557                 xecfg.ctxlen = 3;
558                 xecfg.flags = XDL_EMIT_FUNCNAMES;
559                 if (!diffopts)
560                         ;
561                 else if (!strncmp(diffopts, "--unified=", 10))
562                         xecfg.ctxlen = strtoul(diffopts + 10, NULL, 10);
563                 else if (!strncmp(diffopts, "-u", 2))
564                         xecfg.ctxlen = strtoul(diffopts + 2, NULL, 10);
565                 ecb.outf = fn_out;
566                 ecb.priv = &ecbdata;
567                 xdl_diff(&mf1, &mf2, &xpp, &xecfg, &ecb);
568         }
569
570  free_ab_and_return:
571         free(a_one);
572         free(b_two);
573         return;
574 }
575
576 static void builtin_diffstat(const char *name_a, const char *name_b,
577                              struct diff_filespec *one,
578                              struct diff_filespec *two,
579                              struct diffstat_t *diffstat,
580                              int complete_rewrite)
581 {
582         mmfile_t mf1, mf2;
583         struct diffstat_file *data;
584
585         data = diffstat_add(diffstat, name_a, name_b);
586
587         if (!one || !two) {
588                 data->is_unmerged = 1;
589                 return;
590         }
591         if (complete_rewrite) {
592                 diff_populate_filespec(one, 0);
593                 diff_populate_filespec(two, 0);
594                 data->deleted = count_lines(one->data, one->size);
595                 data->added = count_lines(two->data, two->size);
596                 return;
597         }
598         if (fill_mmfile(&mf1, one) < 0 || fill_mmfile(&mf2, two) < 0)
599                 die("unable to read files to diff");
600
601         if (mmfile_is_binary(&mf1) || mmfile_is_binary(&mf2))
602                 data->is_binary = 1;
603         else {
604                 /* Crazy xdl interfaces.. */
605                 xpparam_t xpp;
606                 xdemitconf_t xecfg;
607                 xdemitcb_t ecb;
608
609                 xpp.flags = XDF_NEED_MINIMAL;
610                 xecfg.ctxlen = 0;
611                 xecfg.flags = 0;
612                 ecb.outf = xdiff_outf;
613                 ecb.priv = diffstat;
614                 xdl_diff(&mf1, &mf2, &xpp, &xecfg, &ecb);
615         }
616 }
617
618 struct diff_filespec *alloc_filespec(const char *path)
619 {
620         int namelen = strlen(path);
621         struct diff_filespec *spec = xmalloc(sizeof(*spec) + namelen + 1);
622
623         memset(spec, 0, sizeof(*spec));
624         spec->path = (char *)(spec + 1);
625         memcpy(spec->path, path, namelen+1);
626         return spec;
627 }
628
629 void fill_filespec(struct diff_filespec *spec, const unsigned char *sha1,
630                    unsigned short mode)
631 {
632         if (mode) {
633                 spec->mode = canon_mode(mode);
634                 memcpy(spec->sha1, sha1, 20);
635                 spec->sha1_valid = !!memcmp(sha1, null_sha1, 20);
636         }
637 }
638
639 /*
640  * Given a name and sha1 pair, if the dircache tells us the file in
641  * the work tree has that object contents, return true, so that
642  * prepare_temp_file() does not have to inflate and extract.
643  */
644 static int work_tree_matches(const char *name, const unsigned char *sha1)
645 {
646         struct cache_entry *ce;
647         struct stat st;
648         int pos, len;
649
650         /* We do not read the cache ourselves here, because the
651          * benchmark with my previous version that always reads cache
652          * shows that it makes things worse for diff-tree comparing
653          * two linux-2.6 kernel trees in an already checked out work
654          * tree.  This is because most diff-tree comparisons deal with
655          * only a small number of files, while reading the cache is
656          * expensive for a large project, and its cost outweighs the
657          * savings we get by not inflating the object to a temporary
658          * file.  Practically, this code only helps when we are used
659          * by diff-cache --cached, which does read the cache before
660          * calling us.
661          */
662         if (!active_cache)
663                 return 0;
664
665         len = strlen(name);
666         pos = cache_name_pos(name, len);
667         if (pos < 0)
668                 return 0;
669         ce = active_cache[pos];
670         if ((lstat(name, &st) < 0) ||
671             !S_ISREG(st.st_mode) || /* careful! */
672             ce_match_stat(ce, &st, 0) ||
673             memcmp(sha1, ce->sha1, 20))
674                 return 0;
675         /* we return 1 only when we can stat, it is a regular file,
676          * stat information matches, and sha1 recorded in the cache
677          * matches.  I.e. we know the file in the work tree really is
678          * the same as the <name, sha1> pair.
679          */
680         return 1;
681 }
682
683 static struct sha1_size_cache {
684         unsigned char sha1[20];
685         unsigned long size;
686 } **sha1_size_cache;
687 static int sha1_size_cache_nr, sha1_size_cache_alloc;
688
689 static struct sha1_size_cache *locate_size_cache(unsigned char *sha1,
690                                                  int find_only,
691                                                  unsigned long size)
692 {
693         int first, last;
694         struct sha1_size_cache *e;
695
696         first = 0;
697         last = sha1_size_cache_nr;
698         while (last > first) {
699                 int cmp, next = (last + first) >> 1;
700                 e = sha1_size_cache[next];
701                 cmp = memcmp(e->sha1, sha1, 20);
702                 if (!cmp)
703                         return e;
704                 if (cmp < 0) {
705                         last = next;
706                         continue;
707                 }
708                 first = next+1;
709         }
710         /* not found */
711         if (find_only)
712                 return NULL;
713         /* insert to make it at "first" */
714         if (sha1_size_cache_alloc <= sha1_size_cache_nr) {
715                 sha1_size_cache_alloc = alloc_nr(sha1_size_cache_alloc);
716                 sha1_size_cache = xrealloc(sha1_size_cache,
717                                            sha1_size_cache_alloc *
718                                            sizeof(*sha1_size_cache));
719         }
720         sha1_size_cache_nr++;
721         if (first < sha1_size_cache_nr)
722                 memmove(sha1_size_cache + first + 1, sha1_size_cache + first,
723                         (sha1_size_cache_nr - first - 1) *
724                         sizeof(*sha1_size_cache));
725         e = xmalloc(sizeof(struct sha1_size_cache));
726         sha1_size_cache[first] = e;
727         memcpy(e->sha1, sha1, 20);
728         e->size = size;
729         return e;
730 }
731
732 /*
733  * While doing rename detection and pickaxe operation, we may need to
734  * grab the data for the blob (or file) for our own in-core comparison.
735  * diff_filespec has data and size fields for this purpose.
736  */
737 int diff_populate_filespec(struct diff_filespec *s, int size_only)
738 {
739         int err = 0;
740         if (!DIFF_FILE_VALID(s))
741                 die("internal error: asking to populate invalid file.");
742         if (S_ISDIR(s->mode))
743                 return -1;
744
745         if (!use_size_cache)
746                 size_only = 0;
747
748         if (s->data)
749                 return err;
750         if (!s->sha1_valid ||
751             work_tree_matches(s->path, s->sha1)) {
752                 struct stat st;
753                 int fd;
754                 if (lstat(s->path, &st) < 0) {
755                         if (errno == ENOENT) {
756                         err_empty:
757                                 err = -1;
758                         empty:
759                                 s->data = "";
760                                 s->size = 0;
761                                 return err;
762                         }
763                 }
764                 s->size = st.st_size;
765                 if (!s->size)
766                         goto empty;
767                 if (size_only)
768                         return 0;
769                 if (S_ISLNK(st.st_mode)) {
770                         int ret;
771                         s->data = xmalloc(s->size);
772                         s->should_free = 1;
773                         ret = readlink(s->path, s->data, s->size);
774                         if (ret < 0) {
775                                 free(s->data);
776                                 goto err_empty;
777                         }
778                         return 0;
779                 }
780                 fd = open(s->path, O_RDONLY);
781                 if (fd < 0)
782                         goto err_empty;
783                 s->data = mmap(NULL, s->size, PROT_READ, MAP_PRIVATE, fd, 0);
784                 close(fd);
785                 if (s->data == MAP_FAILED)
786                         goto err_empty;
787                 s->should_munmap = 1;
788         }
789         else {
790                 char type[20];
791                 struct sha1_size_cache *e;
792
793                 if (size_only) {
794                         e = locate_size_cache(s->sha1, 1, 0);
795                         if (e) {
796                                 s->size = e->size;
797                                 return 0;
798                         }
799                         if (!sha1_object_info(s->sha1, type, &s->size))
800                                 locate_size_cache(s->sha1, 0, s->size);
801                 }
802                 else {
803                         s->data = read_sha1_file(s->sha1, type, &s->size);
804                         s->should_free = 1;
805                 }
806         }
807         return 0;
808 }
809
810 void diff_free_filespec_data(struct diff_filespec *s)
811 {
812         if (s->should_free)
813                 free(s->data);
814         else if (s->should_munmap)
815                 munmap(s->data, s->size);
816         s->should_free = s->should_munmap = 0;
817         s->data = NULL;
818         free(s->cnt_data);
819         s->cnt_data = NULL;
820 }
821
822 static void prep_temp_blob(struct diff_tempfile *temp,
823                            void *blob,
824                            unsigned long size,
825                            const unsigned char *sha1,
826                            int mode)
827 {
828         int fd;
829
830         fd = git_mkstemp(temp->tmp_path, TEMPFILE_PATH_LEN, ".diff_XXXXXX");
831         if (fd < 0)
832                 die("unable to create temp-file");
833         if (write(fd, blob, size) != size)
834                 die("unable to write temp-file");
835         close(fd);
836         temp->name = temp->tmp_path;
837         strcpy(temp->hex, sha1_to_hex(sha1));
838         temp->hex[40] = 0;
839         sprintf(temp->mode, "%06o", mode);
840 }
841
842 static void prepare_temp_file(const char *name,
843                               struct diff_tempfile *temp,
844                               struct diff_filespec *one)
845 {
846         if (!DIFF_FILE_VALID(one)) {
847         not_a_valid_file:
848                 /* A '-' entry produces this for file-2, and
849                  * a '+' entry produces this for file-1.
850                  */
851                 temp->name = "/dev/null";
852                 strcpy(temp->hex, ".");
853                 strcpy(temp->mode, ".");
854                 return;
855         }
856
857         if (!one->sha1_valid ||
858             work_tree_matches(name, one->sha1)) {
859                 struct stat st;
860                 if (lstat(name, &st) < 0) {
861                         if (errno == ENOENT)
862                                 goto not_a_valid_file;
863                         die("stat(%s): %s", name, strerror(errno));
864                 }
865                 if (S_ISLNK(st.st_mode)) {
866                         int ret;
867                         char buf[PATH_MAX + 1]; /* ought to be SYMLINK_MAX */
868                         if (sizeof(buf) <= st.st_size)
869                                 die("symlink too long: %s", name);
870                         ret = readlink(name, buf, st.st_size);
871                         if (ret < 0)
872                                 die("readlink(%s)", name);
873                         prep_temp_blob(temp, buf, st.st_size,
874                                        (one->sha1_valid ?
875                                         one->sha1 : null_sha1),
876                                        (one->sha1_valid ?
877                                         one->mode : S_IFLNK));
878                 }
879                 else {
880                         /* we can borrow from the file in the work tree */
881                         temp->name = name;
882                         if (!one->sha1_valid)
883                                 strcpy(temp->hex, sha1_to_hex(null_sha1));
884                         else
885                                 strcpy(temp->hex, sha1_to_hex(one->sha1));
886                         /* Even though we may sometimes borrow the
887                          * contents from the work tree, we always want
888                          * one->mode.  mode is trustworthy even when
889                          * !(one->sha1_valid), as long as
890                          * DIFF_FILE_VALID(one).
891                          */
892                         sprintf(temp->mode, "%06o", one->mode);
893                 }
894                 return;
895         }
896         else {
897                 if (diff_populate_filespec(one, 0))
898                         die("cannot read data blob for %s", one->path);
899                 prep_temp_blob(temp, one->data, one->size,
900                                one->sha1, one->mode);
901         }
902 }
903
904 static void remove_tempfile(void)
905 {
906         int i;
907
908         for (i = 0; i < 2; i++)
909                 if (diff_temp[i].name == diff_temp[i].tmp_path) {
910                         unlink(diff_temp[i].name);
911                         diff_temp[i].name = NULL;
912                 }
913 }
914
915 static void remove_tempfile_on_signal(int signo)
916 {
917         remove_tempfile();
918         signal(SIGINT, SIG_DFL);
919         raise(signo);
920 }
921
922 static int spawn_prog(const char *pgm, const char **arg)
923 {
924         pid_t pid;
925         int status;
926
927         fflush(NULL);
928         pid = fork();
929         if (pid < 0)
930                 die("unable to fork");
931         if (!pid) {
932                 execvp(pgm, (char *const*) arg);
933                 exit(255);
934         }
935
936         while (waitpid(pid, &status, 0) < 0) {
937                 if (errno == EINTR)
938                         continue;
939                 return -1;
940         }
941
942         /* Earlier we did not check the exit status because
943          * diff exits non-zero if files are different, and
944          * we are not interested in knowing that.  It was a
945          * mistake which made it harder to quit a diff-*
946          * session that uses the git-apply-patch-script as
947          * the GIT_EXTERNAL_DIFF.  A custom GIT_EXTERNAL_DIFF
948          * should also exit non-zero only when it wants to
949          * abort the entire diff-* session.
950          */
951         if (WIFEXITED(status) && !WEXITSTATUS(status))
952                 return 0;
953         return -1;
954 }
955
956 /* An external diff command takes:
957  *
958  * diff-cmd name infile1 infile1-sha1 infile1-mode \
959  *               infile2 infile2-sha1 infile2-mode [ rename-to ]
960  *
961  */
962 static void run_external_diff(const char *pgm,
963                               const char *name,
964                               const char *other,
965                               struct diff_filespec *one,
966                               struct diff_filespec *two,
967                               const char *xfrm_msg,
968                               int complete_rewrite)
969 {
970         const char *spawn_arg[10];
971         struct diff_tempfile *temp = diff_temp;
972         int retval;
973         static int atexit_asked = 0;
974         const char *othername;
975         const char **arg = &spawn_arg[0];
976
977         othername = (other? other : name);
978         if (one && two) {
979                 prepare_temp_file(name, &temp[0], one);
980                 prepare_temp_file(othername, &temp[1], two);
981                 if (! atexit_asked &&
982                     (temp[0].name == temp[0].tmp_path ||
983                      temp[1].name == temp[1].tmp_path)) {
984                         atexit_asked = 1;
985                         atexit(remove_tempfile);
986                 }
987                 signal(SIGINT, remove_tempfile_on_signal);
988         }
989
990         if (one && two) {
991                 *arg++ = pgm;
992                 *arg++ = name;
993                 *arg++ = temp[0].name;
994                 *arg++ = temp[0].hex;
995                 *arg++ = temp[0].mode;
996                 *arg++ = temp[1].name;
997                 *arg++ = temp[1].hex;
998                 *arg++ = temp[1].mode;
999                 if (other) {
1000                         *arg++ = other;
1001                         *arg++ = xfrm_msg;
1002                 }
1003         } else {
1004                 *arg++ = pgm;
1005                 *arg++ = name;
1006         }
1007         *arg = NULL;
1008         retval = spawn_prog(pgm, spawn_arg);
1009         remove_tempfile();
1010         if (retval) {
1011                 fprintf(stderr, "external diff died, stopping at %s.\n", name);
1012                 exit(1);
1013         }
1014 }
1015
1016 static void run_diff_cmd(const char *pgm,
1017                          const char *name,
1018                          const char *other,
1019                          struct diff_filespec *one,
1020                          struct diff_filespec *two,
1021                          const char *xfrm_msg,
1022                          struct diff_options *o,
1023                          int complete_rewrite)
1024 {
1025         if (pgm) {
1026                 run_external_diff(pgm, name, other, one, two, xfrm_msg,
1027                                   complete_rewrite);
1028                 return;
1029         }
1030         if (one && two)
1031                 builtin_diff(name, other ? other : name,
1032                              one, two, xfrm_msg, o, complete_rewrite);
1033         else
1034                 printf("* Unmerged path %s\n", name);
1035 }
1036
1037 static void diff_fill_sha1_info(struct diff_filespec *one)
1038 {
1039         if (DIFF_FILE_VALID(one)) {
1040                 if (!one->sha1_valid) {
1041                         struct stat st;
1042                         if (lstat(one->path, &st) < 0)
1043                                 die("stat %s", one->path);
1044                         if (index_path(one->sha1, one->path, &st, 0))
1045                                 die("cannot hash %s\n", one->path);
1046                 }
1047         }
1048         else
1049                 memset(one->sha1, 0, 20);
1050 }
1051
1052 static void run_diff(struct diff_filepair *p, struct diff_options *o)
1053 {
1054         const char *pgm = external_diff();
1055         char msg[PATH_MAX*2+300], *xfrm_msg;
1056         struct diff_filespec *one;
1057         struct diff_filespec *two;
1058         const char *name;
1059         const char *other;
1060         char *name_munged, *other_munged;
1061         int complete_rewrite = 0;
1062         int len;
1063
1064         if (DIFF_PAIR_UNMERGED(p)) {
1065                 /* unmerged */
1066                 run_diff_cmd(pgm, p->one->path, NULL, NULL, NULL, NULL, o, 0);
1067                 return;
1068         }
1069
1070         name = p->one->path;
1071         other = (strcmp(name, p->two->path) ? p->two->path : NULL);
1072         name_munged = quote_one(name);
1073         other_munged = quote_one(other);
1074         one = p->one; two = p->two;
1075
1076         diff_fill_sha1_info(one);
1077         diff_fill_sha1_info(two);
1078
1079         len = 0;
1080         switch (p->status) {
1081         case DIFF_STATUS_COPIED:
1082                 len += snprintf(msg + len, sizeof(msg) - len,
1083                                 "similarity index %d%%\n"
1084                                 "copy from %s\n"
1085                                 "copy to %s\n",
1086                                 (int)(0.5 + p->score * 100.0/MAX_SCORE),
1087                                 name_munged, other_munged);
1088                 break;
1089         case DIFF_STATUS_RENAMED:
1090                 len += snprintf(msg + len, sizeof(msg) - len,
1091                                 "similarity index %d%%\n"
1092                                 "rename from %s\n"
1093                                 "rename to %s\n",
1094                                 (int)(0.5 + p->score * 100.0/MAX_SCORE),
1095                                 name_munged, other_munged);
1096                 break;
1097         case DIFF_STATUS_MODIFIED:
1098                 if (p->score) {
1099                         len += snprintf(msg + len, sizeof(msg) - len,
1100                                         "dissimilarity index %d%%\n",
1101                                         (int)(0.5 + p->score *
1102                                               100.0/MAX_SCORE));
1103                         complete_rewrite = 1;
1104                         break;
1105                 }
1106                 /* fallthru */
1107         default:
1108                 /* nothing */
1109                 ;
1110         }
1111
1112         if (memcmp(one->sha1, two->sha1, 20)) {
1113                 int abbrev = o->full_index ? 40 : DEFAULT_ABBREV;
1114
1115                 len += snprintf(msg + len, sizeof(msg) - len,
1116                                 "index %.*s..%.*s",
1117                                 abbrev, sha1_to_hex(one->sha1),
1118                                 abbrev, sha1_to_hex(two->sha1));
1119                 if (one->mode == two->mode)
1120                         len += snprintf(msg + len, sizeof(msg) - len,
1121                                         " %06o", one->mode);
1122                 len += snprintf(msg + len, sizeof(msg) - len, "\n");
1123         }
1124
1125         if (len)
1126                 msg[--len] = 0;
1127         xfrm_msg = len ? msg : NULL;
1128
1129         if (!pgm &&
1130             DIFF_FILE_VALID(one) && DIFF_FILE_VALID(two) &&
1131             (S_IFMT & one->mode) != (S_IFMT & two->mode)) {
1132                 /* a filepair that changes between file and symlink
1133                  * needs to be split into deletion and creation.
1134                  */
1135                 struct diff_filespec *null = alloc_filespec(two->path);
1136                 run_diff_cmd(NULL, name, other, one, null, xfrm_msg, o, 0);
1137                 free(null);
1138                 null = alloc_filespec(one->path);
1139                 run_diff_cmd(NULL, name, other, null, two, xfrm_msg, o, 0);
1140                 free(null);
1141         }
1142         else
1143                 run_diff_cmd(pgm, name, other, one, two, xfrm_msg, o,
1144                              complete_rewrite);
1145
1146         free(name_munged);
1147         free(other_munged);
1148 }
1149
1150 static void run_diffstat(struct diff_filepair *p, struct diff_options *o,
1151                          struct diffstat_t *diffstat)
1152 {
1153         const char *name;
1154         const char *other;
1155         int complete_rewrite = 0;
1156
1157         if (DIFF_PAIR_UNMERGED(p)) {
1158                 /* unmerged */
1159                 builtin_diffstat(p->one->path, NULL, NULL, NULL, diffstat, 0);
1160                 return;
1161         }
1162
1163         name = p->one->path;
1164         other = (strcmp(name, p->two->path) ? p->two->path : NULL);
1165
1166         diff_fill_sha1_info(p->one);
1167         diff_fill_sha1_info(p->two);
1168
1169         if (p->status == DIFF_STATUS_MODIFIED && p->score)
1170                 complete_rewrite = 1;
1171         builtin_diffstat(name, other, p->one, p->two, diffstat, complete_rewrite);
1172 }
1173
1174 void diff_setup(struct diff_options *options)
1175 {
1176         memset(options, 0, sizeof(*options));
1177         options->output_format = DIFF_FORMAT_RAW;
1178         options->line_termination = '\n';
1179         options->break_opt = -1;
1180         options->rename_limit = -1;
1181
1182         options->change = diff_change;
1183         options->add_remove = diff_addremove;
1184 }
1185
1186 int diff_setup_done(struct diff_options *options)
1187 {
1188         if ((options->find_copies_harder &&
1189              options->detect_rename != DIFF_DETECT_COPY) ||
1190             (0 <= options->rename_limit && !options->detect_rename))
1191                 return -1;
1192
1193         /*
1194          * These cases always need recursive; we do not drop caller-supplied
1195          * recursive bits for other formats here.
1196          */
1197         if ((options->output_format == DIFF_FORMAT_PATCH) ||
1198             (options->output_format == DIFF_FORMAT_DIFFSTAT))
1199                 options->recursive = 1;
1200
1201         if (options->detect_rename && options->rename_limit < 0)
1202                 options->rename_limit = diff_rename_limit_default;
1203         if (options->setup & DIFF_SETUP_USE_CACHE) {
1204                 if (!active_cache)
1205                         /* read-cache does not die even when it fails
1206                          * so it is safe for us to do this here.  Also
1207                          * it does not smudge active_cache or active_nr
1208                          * when it fails, so we do not have to worry about
1209                          * cleaning it up ourselves either.
1210                          */
1211                         read_cache();
1212         }
1213         if (options->setup & DIFF_SETUP_USE_SIZE_CACHE)
1214                 use_size_cache = 1;
1215         if (options->abbrev <= 0 || 40 < options->abbrev)
1216                 options->abbrev = 40; /* full */
1217
1218         return 0;
1219 }
1220
1221 int diff_opt_parse(struct diff_options *options, const char **av, int ac)
1222 {
1223         const char *arg = av[0];
1224         if (!strcmp(arg, "-p") || !strcmp(arg, "-u"))
1225                 options->output_format = DIFF_FORMAT_PATCH;
1226         else if (!strcmp(arg, "--patch-with-raw")) {
1227                 options->output_format = DIFF_FORMAT_PATCH;
1228                 options->with_raw = 1;
1229         }
1230         else if (!strcmp(arg, "--stat"))
1231                 options->output_format = DIFF_FORMAT_DIFFSTAT;
1232         else if (!strcmp(arg, "--patch-with-stat")) {
1233                 options->output_format = DIFF_FORMAT_PATCH;
1234                 options->with_stat = 1;
1235         }
1236         else if (!strcmp(arg, "-z"))
1237                 options->line_termination = 0;
1238         else if (!strncmp(arg, "-l", 2))
1239                 options->rename_limit = strtoul(arg+2, NULL, 10);
1240         else if (!strcmp(arg, "--full-index"))
1241                 options->full_index = 1;
1242         else if (!strcmp(arg, "--name-only"))
1243                 options->output_format = DIFF_FORMAT_NAME;
1244         else if (!strcmp(arg, "--name-status"))
1245                 options->output_format = DIFF_FORMAT_NAME_STATUS;
1246         else if (!strcmp(arg, "-R"))
1247                 options->reverse_diff = 1;
1248         else if (!strncmp(arg, "-S", 2))
1249                 options->pickaxe = arg + 2;
1250         else if (!strcmp(arg, "-s"))
1251                 options->output_format = DIFF_FORMAT_NO_OUTPUT;
1252         else if (!strncmp(arg, "-O", 2))
1253                 options->orderfile = arg + 2;
1254         else if (!strncmp(arg, "--diff-filter=", 14))
1255                 options->filter = arg + 14;
1256         else if (!strcmp(arg, "--pickaxe-all"))
1257                 options->pickaxe_opts = DIFF_PICKAXE_ALL;
1258         else if (!strcmp(arg, "--pickaxe-regex"))
1259                 options->pickaxe_opts = DIFF_PICKAXE_REGEX;
1260         else if (!strncmp(arg, "-B", 2)) {
1261                 if ((options->break_opt =
1262                      diff_scoreopt_parse(arg)) == -1)
1263                         return -1;
1264         }
1265         else if (!strncmp(arg, "-M", 2)) {
1266                 if ((options->rename_score =
1267                      diff_scoreopt_parse(arg)) == -1)
1268                         return -1;
1269                 options->detect_rename = DIFF_DETECT_RENAME;
1270         }
1271         else if (!strncmp(arg, "-C", 2)) {
1272                 if ((options->rename_score =
1273                      diff_scoreopt_parse(arg)) == -1)
1274                         return -1;
1275                 options->detect_rename = DIFF_DETECT_COPY;
1276         }
1277         else if (!strcmp(arg, "--find-copies-harder"))
1278                 options->find_copies_harder = 1;
1279         else if (!strcmp(arg, "--abbrev"))
1280                 options->abbrev = DEFAULT_ABBREV;
1281         else if (!strncmp(arg, "--abbrev=", 9)) {
1282                 options->abbrev = strtoul(arg + 9, NULL, 10);
1283                 if (options->abbrev < MINIMUM_ABBREV)
1284                         options->abbrev = MINIMUM_ABBREV;
1285                 else if (40 < options->abbrev)
1286                         options->abbrev = 40;
1287         }
1288         else
1289                 return 0;
1290         return 1;
1291 }
1292
1293 static int parse_num(const char **cp_p)
1294 {
1295         unsigned long num, scale;
1296         int ch, dot;
1297         const char *cp = *cp_p;
1298
1299         num = 0;
1300         scale = 1;
1301         dot = 0;
1302         for(;;) {
1303                 ch = *cp;
1304                 if ( !dot && ch == '.' ) {
1305                         scale = 1;
1306                         dot = 1;
1307                 } else if ( ch == '%' ) {
1308                         scale = dot ? scale*100 : 100;
1309                         cp++;   /* % is always at the end */
1310                         break;
1311                 } else if ( ch >= '0' && ch <= '9' ) {
1312                         if ( scale < 100000 ) {
1313                                 scale *= 10;
1314                                 num = (num*10) + (ch-'0');
1315                         }
1316                 } else {
1317                         break;
1318                 }
1319                 cp++;
1320         }
1321         *cp_p = cp;
1322
1323         /* user says num divided by scale and we say internally that
1324          * is MAX_SCORE * num / scale.
1325          */
1326         return (num >= scale) ? MAX_SCORE : (MAX_SCORE * num / scale);
1327 }
1328
1329 int diff_scoreopt_parse(const char *opt)
1330 {
1331         int opt1, opt2, cmd;
1332
1333         if (*opt++ != '-')
1334                 return -1;
1335         cmd = *opt++;
1336         if (cmd != 'M' && cmd != 'C' && cmd != 'B')
1337                 return -1; /* that is not a -M, -C nor -B option */
1338
1339         opt1 = parse_num(&opt);
1340         if (cmd != 'B')
1341                 opt2 = 0;
1342         else {
1343                 if (*opt == 0)
1344                         opt2 = 0;
1345                 else if (*opt != '/')
1346                         return -1; /* we expect -B80/99 or -B80 */
1347                 else {
1348                         opt++;
1349                         opt2 = parse_num(&opt);
1350                 }
1351         }
1352         if (*opt != 0)
1353                 return -1;
1354         return opt1 | (opt2 << 16);
1355 }
1356
1357 struct diff_queue_struct diff_queued_diff;
1358
1359 void diff_q(struct diff_queue_struct *queue, struct diff_filepair *dp)
1360 {
1361         if (queue->alloc <= queue->nr) {
1362                 queue->alloc = alloc_nr(queue->alloc);
1363                 queue->queue = xrealloc(queue->queue,
1364                                         sizeof(dp) * queue->alloc);
1365         }
1366         queue->queue[queue->nr++] = dp;
1367 }
1368
1369 struct diff_filepair *diff_queue(struct diff_queue_struct *queue,
1370                                  struct diff_filespec *one,
1371                                  struct diff_filespec *two)
1372 {
1373         struct diff_filepair *dp = xmalloc(sizeof(*dp));
1374         dp->one = one;
1375         dp->two = two;
1376         dp->score = 0;
1377         dp->status = 0;
1378         dp->source_stays = 0;
1379         dp->broken_pair = 0;
1380         if (queue)
1381                 diff_q(queue, dp);
1382         return dp;
1383 }
1384
1385 void diff_free_filepair(struct diff_filepair *p)
1386 {
1387         diff_free_filespec_data(p->one);
1388         diff_free_filespec_data(p->two);
1389         free(p->one);
1390         free(p->two);
1391         free(p);
1392 }
1393
1394 /* This is different from find_unique_abbrev() in that
1395  * it stuffs the result with dots for alignment.
1396  */
1397 const char *diff_unique_abbrev(const unsigned char *sha1, int len)
1398 {
1399         int abblen;
1400         const char *abbrev;
1401         if (len == 40)
1402                 return sha1_to_hex(sha1);
1403
1404         abbrev = find_unique_abbrev(sha1, len);
1405         if (!abbrev)
1406                 return sha1_to_hex(sha1);
1407         abblen = strlen(abbrev);
1408         if (abblen < 37) {
1409                 static char hex[41];
1410                 if (len < abblen && abblen <= len + 2)
1411                         sprintf(hex, "%s%.*s", abbrev, len+3-abblen, "..");
1412                 else
1413                         sprintf(hex, "%s...", abbrev);
1414                 return hex;
1415         }
1416         return sha1_to_hex(sha1);
1417 }
1418
1419 static void diff_flush_raw(struct diff_filepair *p,
1420                            int line_termination,
1421                            int inter_name_termination,
1422                            struct diff_options *options,
1423                            int output_format)
1424 {
1425         int two_paths;
1426         char status[10];
1427         int abbrev = options->abbrev;
1428         const char *path_one, *path_two;
1429
1430         path_one = p->one->path;
1431         path_two = p->two->path;
1432         if (line_termination) {
1433                 path_one = quote_one(path_one);
1434                 path_two = quote_one(path_two);
1435         }
1436
1437         if (p->score)
1438                 sprintf(status, "%c%03d", p->status,
1439                         (int)(0.5 + p->score * 100.0/MAX_SCORE));
1440         else {
1441                 status[0] = p->status;
1442                 status[1] = 0;
1443         }
1444         switch (p->status) {
1445         case DIFF_STATUS_COPIED:
1446         case DIFF_STATUS_RENAMED:
1447                 two_paths = 1;
1448                 break;
1449         case DIFF_STATUS_ADDED:
1450         case DIFF_STATUS_DELETED:
1451                 two_paths = 0;
1452                 break;
1453         default:
1454                 two_paths = 0;
1455                 break;
1456         }
1457         if (output_format != DIFF_FORMAT_NAME_STATUS) {
1458                 printf(":%06o %06o %s ",
1459                        p->one->mode, p->two->mode,
1460                        diff_unique_abbrev(p->one->sha1, abbrev));
1461                 printf("%s ",
1462                        diff_unique_abbrev(p->two->sha1, abbrev));
1463         }
1464         printf("%s%c%s", status, inter_name_termination, path_one);
1465         if (two_paths)
1466                 printf("%c%s", inter_name_termination, path_two);
1467         putchar(line_termination);
1468         if (path_one != p->one->path)
1469                 free((void*)path_one);
1470         if (path_two != p->two->path)
1471                 free((void*)path_two);
1472 }
1473
1474 static void diff_flush_name(struct diff_filepair *p,
1475                             int inter_name_termination,
1476                             int line_termination)
1477 {
1478         char *path = p->two->path;
1479
1480         if (line_termination)
1481                 path = quote_one(p->two->path);
1482         else
1483                 path = p->two->path;
1484         printf("%s%c", path, line_termination);
1485         if (p->two->path != path)
1486                 free(path);
1487 }
1488
1489 int diff_unmodified_pair(struct diff_filepair *p)
1490 {
1491         /* This function is written stricter than necessary to support
1492          * the currently implemented transformers, but the idea is to
1493          * let transformers to produce diff_filepairs any way they want,
1494          * and filter and clean them up here before producing the output.
1495          */
1496         struct diff_filespec *one, *two;
1497
1498         if (DIFF_PAIR_UNMERGED(p))
1499                 return 0; /* unmerged is interesting */
1500
1501         one = p->one;
1502         two = p->two;
1503
1504         /* deletion, addition, mode or type change
1505          * and rename are all interesting.
1506          */
1507         if (DIFF_FILE_VALID(one) != DIFF_FILE_VALID(two) ||
1508             DIFF_PAIR_MODE_CHANGED(p) ||
1509             strcmp(one->path, two->path))
1510                 return 0;
1511
1512         /* both are valid and point at the same path.  that is, we are
1513          * dealing with a change.
1514          */
1515         if (one->sha1_valid && two->sha1_valid &&
1516             !memcmp(one->sha1, two->sha1, sizeof(one->sha1)))
1517                 return 1; /* no change */
1518         if (!one->sha1_valid && !two->sha1_valid)
1519                 return 1; /* both look at the same file on the filesystem. */
1520         return 0;
1521 }
1522
1523 static void diff_flush_patch(struct diff_filepair *p, struct diff_options *o)
1524 {
1525         if (diff_unmodified_pair(p))
1526                 return;
1527
1528         if ((DIFF_FILE_VALID(p->one) && S_ISDIR(p->one->mode)) ||
1529             (DIFF_FILE_VALID(p->two) && S_ISDIR(p->two->mode)))
1530                 return; /* no tree diffs in patch format */
1531
1532         run_diff(p, o);
1533 }
1534
1535 static void diff_flush_stat(struct diff_filepair *p, struct diff_options *o,
1536                             struct diffstat_t *diffstat)
1537 {
1538         if (diff_unmodified_pair(p))
1539                 return;
1540
1541         if ((DIFF_FILE_VALID(p->one) && S_ISDIR(p->one->mode)) ||
1542             (DIFF_FILE_VALID(p->two) && S_ISDIR(p->two->mode)))
1543                 return; /* no tree diffs in patch format */
1544
1545         run_diffstat(p, o, diffstat);
1546 }
1547
1548 int diff_queue_is_empty(void)
1549 {
1550         struct diff_queue_struct *q = &diff_queued_diff;
1551         int i;
1552         for (i = 0; i < q->nr; i++)
1553                 if (!diff_unmodified_pair(q->queue[i]))
1554                         return 0;
1555         return 1;
1556 }
1557
1558 #if DIFF_DEBUG
1559 void diff_debug_filespec(struct diff_filespec *s, int x, const char *one)
1560 {
1561         fprintf(stderr, "queue[%d] %s (%s) %s %06o %s\n",
1562                 x, one ? one : "",
1563                 s->path,
1564                 DIFF_FILE_VALID(s) ? "valid" : "invalid",
1565                 s->mode,
1566                 s->sha1_valid ? sha1_to_hex(s->sha1) : "");
1567         fprintf(stderr, "queue[%d] %s size %lu flags %d\n",
1568                 x, one ? one : "",
1569                 s->size, s->xfrm_flags);
1570 }
1571
1572 void diff_debug_filepair(const struct diff_filepair *p, int i)
1573 {
1574         diff_debug_filespec(p->one, i, "one");
1575         diff_debug_filespec(p->two, i, "two");
1576         fprintf(stderr, "score %d, status %c stays %d broken %d\n",
1577                 p->score, p->status ? p->status : '?',
1578                 p->source_stays, p->broken_pair);
1579 }
1580
1581 void diff_debug_queue(const char *msg, struct diff_queue_struct *q)
1582 {
1583         int i;
1584         if (msg)
1585                 fprintf(stderr, "%s\n", msg);
1586         fprintf(stderr, "q->nr = %d\n", q->nr);
1587         for (i = 0; i < q->nr; i++) {
1588                 struct diff_filepair *p = q->queue[i];
1589                 diff_debug_filepair(p, i);
1590         }
1591 }
1592 #endif
1593
1594 static void diff_resolve_rename_copy(void)
1595 {
1596         int i, j;
1597         struct diff_filepair *p, *pp;
1598         struct diff_queue_struct *q = &diff_queued_diff;
1599
1600         diff_debug_queue("resolve-rename-copy", q);
1601
1602         for (i = 0; i < q->nr; i++) {
1603                 p = q->queue[i];
1604                 p->status = 0; /* undecided */
1605                 if (DIFF_PAIR_UNMERGED(p))
1606                         p->status = DIFF_STATUS_UNMERGED;
1607                 else if (!DIFF_FILE_VALID(p->one))
1608                         p->status = DIFF_STATUS_ADDED;
1609                 else if (!DIFF_FILE_VALID(p->two))
1610                         p->status = DIFF_STATUS_DELETED;
1611                 else if (DIFF_PAIR_TYPE_CHANGED(p))
1612                         p->status = DIFF_STATUS_TYPE_CHANGED;
1613
1614                 /* from this point on, we are dealing with a pair
1615                  * whose both sides are valid and of the same type, i.e.
1616                  * either in-place edit or rename/copy edit.
1617                  */
1618                 else if (DIFF_PAIR_RENAME(p)) {
1619                         if (p->source_stays) {
1620                                 p->status = DIFF_STATUS_COPIED;
1621                                 continue;
1622                         }
1623                         /* See if there is some other filepair that
1624                          * copies from the same source as us.  If so
1625                          * we are a copy.  Otherwise we are either a
1626                          * copy if the path stays, or a rename if it
1627                          * does not, but we already handled "stays" case.
1628                          */
1629                         for (j = i + 1; j < q->nr; j++) {
1630                                 pp = q->queue[j];
1631                                 if (strcmp(pp->one->path, p->one->path))
1632                                         continue; /* not us */
1633                                 if (!DIFF_PAIR_RENAME(pp))
1634                                         continue; /* not a rename/copy */
1635                                 /* pp is a rename/copy from the same source */
1636                                 p->status = DIFF_STATUS_COPIED;
1637                                 break;
1638                         }
1639                         if (!p->status)
1640                                 p->status = DIFF_STATUS_RENAMED;
1641                 }
1642                 else if (memcmp(p->one->sha1, p->two->sha1, 20) ||
1643                          p->one->mode != p->two->mode)
1644                         p->status = DIFF_STATUS_MODIFIED;
1645                 else {
1646                         /* This is a "no-change" entry and should not
1647                          * happen anymore, but prepare for broken callers.
1648                          */
1649                         error("feeding unmodified %s to diffcore",
1650                               p->one->path);
1651                         p->status = DIFF_STATUS_UNKNOWN;
1652                 }
1653         }
1654         diff_debug_queue("resolve-rename-copy done", q);
1655 }
1656
1657 static void flush_one_pair(struct diff_filepair *p,
1658                            int diff_output_format,
1659                            struct diff_options *options,
1660                            struct diffstat_t *diffstat)
1661 {
1662         int inter_name_termination = '\t';
1663         int line_termination = options->line_termination;
1664         if (!line_termination)
1665                 inter_name_termination = 0;
1666
1667         switch (p->status) {
1668         case DIFF_STATUS_UNKNOWN:
1669                 break;
1670         case 0:
1671                 die("internal error in diff-resolve-rename-copy");
1672                 break;
1673         default:
1674                 switch (diff_output_format) {
1675                 case DIFF_FORMAT_DIFFSTAT:
1676                         diff_flush_stat(p, options, diffstat);
1677                         break;
1678                 case DIFF_FORMAT_PATCH:
1679                         diff_flush_patch(p, options);
1680                         break;
1681                 case DIFF_FORMAT_RAW:
1682                 case DIFF_FORMAT_NAME_STATUS:
1683                         diff_flush_raw(p, line_termination,
1684                                        inter_name_termination,
1685                                        options, diff_output_format);
1686                         break;
1687                 case DIFF_FORMAT_NAME:
1688                         diff_flush_name(p,
1689                                         inter_name_termination,
1690                                         line_termination);
1691                         break;
1692                 case DIFF_FORMAT_NO_OUTPUT:
1693                         break;
1694                 }
1695         }
1696 }
1697
1698 void diff_flush(struct diff_options *options)
1699 {
1700         struct diff_queue_struct *q = &diff_queued_diff;
1701         int i;
1702         int diff_output_format = options->output_format;
1703         struct diffstat_t *diffstat = NULL;
1704
1705         if (diff_output_format == DIFF_FORMAT_DIFFSTAT || options->with_stat) {
1706                 diffstat = xcalloc(sizeof (struct diffstat_t), 1);
1707                 diffstat->xm.consume = diffstat_consume;
1708         }
1709
1710         if (options->with_raw) {
1711                 for (i = 0; i < q->nr; i++) {
1712                         struct diff_filepair *p = q->queue[i];
1713                         flush_one_pair(p, DIFF_FORMAT_RAW, options, NULL);
1714                 }
1715                 putchar(options->line_termination);
1716         }
1717         if (options->with_stat) {
1718                 for (i = 0; i < q->nr; i++) {
1719                         struct diff_filepair *p = q->queue[i];
1720                         flush_one_pair(p, DIFF_FORMAT_DIFFSTAT, options,
1721                                        diffstat);
1722                 }
1723                 show_stats(diffstat);
1724                 free(diffstat);
1725                 diffstat = NULL;
1726                 putchar(options->line_termination);
1727         }
1728         for (i = 0; i < q->nr; i++) {
1729                 struct diff_filepair *p = q->queue[i];
1730                 flush_one_pair(p, diff_output_format, options, diffstat);
1731                 diff_free_filepair(p);
1732         }
1733
1734         if (diffstat) {
1735                 show_stats(diffstat);
1736                 free(diffstat);
1737         }
1738
1739         free(q->queue);
1740         q->queue = NULL;
1741         q->nr = q->alloc = 0;
1742 }
1743
1744 static void diffcore_apply_filter(const char *filter)
1745 {
1746         int i;
1747         struct diff_queue_struct *q = &diff_queued_diff;
1748         struct diff_queue_struct outq;
1749         outq.queue = NULL;
1750         outq.nr = outq.alloc = 0;
1751
1752         if (!filter)
1753                 return;
1754
1755         if (strchr(filter, DIFF_STATUS_FILTER_AON)) {
1756                 int found;
1757                 for (i = found = 0; !found && i < q->nr; i++) {
1758                         struct diff_filepair *p = q->queue[i];
1759                         if (((p->status == DIFF_STATUS_MODIFIED) &&
1760                              ((p->score &&
1761                                strchr(filter, DIFF_STATUS_FILTER_BROKEN)) ||
1762                               (!p->score &&
1763                                strchr(filter, DIFF_STATUS_MODIFIED)))) ||
1764                             ((p->status != DIFF_STATUS_MODIFIED) &&
1765                              strchr(filter, p->status)))
1766                                 found++;
1767                 }
1768                 if (found)
1769                         return;
1770
1771                 /* otherwise we will clear the whole queue
1772                  * by copying the empty outq at the end of this
1773                  * function, but first clear the current entries
1774                  * in the queue.
1775                  */
1776                 for (i = 0; i < q->nr; i++)
1777                         diff_free_filepair(q->queue[i]);
1778         }
1779         else {
1780                 /* Only the matching ones */
1781                 for (i = 0; i < q->nr; i++) {
1782                         struct diff_filepair *p = q->queue[i];
1783
1784                         if (((p->status == DIFF_STATUS_MODIFIED) &&
1785                              ((p->score &&
1786                                strchr(filter, DIFF_STATUS_FILTER_BROKEN)) ||
1787                               (!p->score &&
1788                                strchr(filter, DIFF_STATUS_MODIFIED)))) ||
1789                             ((p->status != DIFF_STATUS_MODIFIED) &&
1790                              strchr(filter, p->status)))
1791                                 diff_q(&outq, p);
1792                         else
1793                                 diff_free_filepair(p);
1794                 }
1795         }
1796         free(q->queue);
1797         *q = outq;
1798 }
1799
1800 void diffcore_std(struct diff_options *options)
1801 {
1802         if (options->break_opt != -1)
1803                 diffcore_break(options->break_opt);
1804         if (options->detect_rename)
1805                 diffcore_rename(options);
1806         if (options->break_opt != -1)
1807                 diffcore_merge_broken();
1808         if (options->pickaxe)
1809                 diffcore_pickaxe(options->pickaxe, options->pickaxe_opts);
1810         if (options->orderfile)
1811                 diffcore_order(options->orderfile);
1812         diff_resolve_rename_copy();
1813         diffcore_apply_filter(options->filter);
1814 }
1815
1816
1817 void diffcore_std_no_resolve(struct diff_options *options)
1818 {
1819         if (options->pickaxe)
1820                 diffcore_pickaxe(options->pickaxe, options->pickaxe_opts);
1821         if (options->orderfile)
1822                 diffcore_order(options->orderfile);
1823         diffcore_apply_filter(options->filter);
1824 }
1825
1826 void diff_addremove(struct diff_options *options,
1827                     int addremove, unsigned mode,
1828                     const unsigned char *sha1,
1829                     const char *base, const char *path)
1830 {
1831         char concatpath[PATH_MAX];
1832         struct diff_filespec *one, *two;
1833
1834         /* This may look odd, but it is a preparation for
1835          * feeding "there are unchanged files which should
1836          * not produce diffs, but when you are doing copy
1837          * detection you would need them, so here they are"
1838          * entries to the diff-core.  They will be prefixed
1839          * with something like '=' or '*' (I haven't decided
1840          * which but should not make any difference).
1841          * Feeding the same new and old to diff_change() 
1842          * also has the same effect.
1843          * Before the final output happens, they are pruned after
1844          * merged into rename/copy pairs as appropriate.
1845          */
1846         if (options->reverse_diff)
1847                 addremove = (addremove == '+' ? '-' :
1848                              addremove == '-' ? '+' : addremove);
1849
1850         if (!path) path = "";
1851         sprintf(concatpath, "%s%s", base, path);
1852         one = alloc_filespec(concatpath);
1853         two = alloc_filespec(concatpath);
1854
1855         if (addremove != '+')
1856                 fill_filespec(one, sha1, mode);
1857         if (addremove != '-')
1858                 fill_filespec(two, sha1, mode);
1859
1860         diff_queue(&diff_queued_diff, one, two);
1861 }
1862
1863 void diff_change(struct diff_options *options,
1864                  unsigned old_mode, unsigned new_mode,
1865                  const unsigned char *old_sha1,
1866                  const unsigned char *new_sha1,
1867                  const char *base, const char *path) 
1868 {
1869         char concatpath[PATH_MAX];
1870         struct diff_filespec *one, *two;
1871
1872         if (options->reverse_diff) {
1873                 unsigned tmp;
1874                 const unsigned char *tmp_c;
1875                 tmp = old_mode; old_mode = new_mode; new_mode = tmp;
1876                 tmp_c = old_sha1; old_sha1 = new_sha1; new_sha1 = tmp_c;
1877         }
1878         if (!path) path = "";
1879         sprintf(concatpath, "%s%s", base, path);
1880         one = alloc_filespec(concatpath);
1881         two = alloc_filespec(concatpath);
1882         fill_filespec(one, old_sha1, old_mode);
1883         fill_filespec(two, new_sha1, new_mode);
1884
1885         diff_queue(&diff_queued_diff, one, two);
1886 }
1887
1888 void diff_unmerge(struct diff_options *options,
1889                   const char *path)
1890 {
1891         struct diff_filespec *one, *two;
1892         one = alloc_filespec(path);
1893         two = alloc_filespec(path);
1894         diff_queue(&diff_queued_diff, one, two);
1895 }