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