Merge git://git.kernel.org/pub/scm/gitk/gitk
[git.git] / diff-lib.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 {
490         mmfile_t mf1, mf2;
491         struct diffstat_file *data;
492
493         data = diffstat_add(diffstat, name_a, name_b);
494
495         if (!one || !two) {
496                 data->is_unmerged = 1;
497                 return;
498         }
499
500         if (fill_mmfile(&mf1, one) < 0 || fill_mmfile(&mf2, two) < 0)
501                 die("unable to read files to diff");
502
503         if (mmfile_is_binary(&mf1) || mmfile_is_binary(&mf2))
504                 data->is_binary = 1;
505         else {
506                 /* Crazy xdl interfaces.. */
507                 xpparam_t xpp;
508                 xdemitconf_t xecfg;
509                 xdemitcb_t ecb;
510
511                 xpp.flags = XDF_NEED_MINIMAL;
512                 xecfg.ctxlen = 0;
513                 xecfg.flags = 0;
514                 ecb.outf = xdiff_outf;
515                 ecb.priv = diffstat;
516                 xdl_diff(&mf1, &mf2, &xpp, &xecfg, &ecb);
517         }
518 }
519
520 struct diff_filespec *alloc_filespec(const char *path)
521 {
522         int namelen = strlen(path);
523         struct diff_filespec *spec = xmalloc(sizeof(*spec) + namelen + 1);
524
525         memset(spec, 0, sizeof(*spec));
526         spec->path = (char *)(spec + 1);
527         memcpy(spec->path, path, namelen+1);
528         return spec;
529 }
530
531 void fill_filespec(struct diff_filespec *spec, const unsigned char *sha1,
532                    unsigned short mode)
533 {
534         if (mode) {
535                 spec->mode = canon_mode(mode);
536                 memcpy(spec->sha1, sha1, 20);
537                 spec->sha1_valid = !!memcmp(sha1, null_sha1, 20);
538         }
539 }
540
541 /*
542  * Given a name and sha1 pair, if the dircache tells us the file in
543  * the work tree has that object contents, return true, so that
544  * prepare_temp_file() does not have to inflate and extract.
545  */
546 static int work_tree_matches(const char *name, const unsigned char *sha1)
547 {
548         struct cache_entry *ce;
549         struct stat st;
550         int pos, len;
551
552         /* We do not read the cache ourselves here, because the
553          * benchmark with my previous version that always reads cache
554          * shows that it makes things worse for diff-tree comparing
555          * two linux-2.6 kernel trees in an already checked out work
556          * tree.  This is because most diff-tree comparisons deal with
557          * only a small number of files, while reading the cache is
558          * expensive for a large project, and its cost outweighs the
559          * savings we get by not inflating the object to a temporary
560          * file.  Practically, this code only helps when we are used
561          * by diff-cache --cached, which does read the cache before
562          * calling us.
563          */
564         if (!active_cache)
565                 return 0;
566
567         len = strlen(name);
568         pos = cache_name_pos(name, len);
569         if (pos < 0)
570                 return 0;
571         ce = active_cache[pos];
572         if ((lstat(name, &st) < 0) ||
573             !S_ISREG(st.st_mode) || /* careful! */
574             ce_match_stat(ce, &st, 0) ||
575             memcmp(sha1, ce->sha1, 20))
576                 return 0;
577         /* we return 1 only when we can stat, it is a regular file,
578          * stat information matches, and sha1 recorded in the cache
579          * matches.  I.e. we know the file in the work tree really is
580          * the same as the <name, sha1> pair.
581          */
582         return 1;
583 }
584
585 static struct sha1_size_cache {
586         unsigned char sha1[20];
587         unsigned long size;
588 } **sha1_size_cache;
589 static int sha1_size_cache_nr, sha1_size_cache_alloc;
590
591 static struct sha1_size_cache *locate_size_cache(unsigned char *sha1,
592                                                  int find_only,
593                                                  unsigned long size)
594 {
595         int first, last;
596         struct sha1_size_cache *e;
597
598         first = 0;
599         last = sha1_size_cache_nr;
600         while (last > first) {
601                 int cmp, next = (last + first) >> 1;
602                 e = sha1_size_cache[next];
603                 cmp = memcmp(e->sha1, sha1, 20);
604                 if (!cmp)
605                         return e;
606                 if (cmp < 0) {
607                         last = next;
608                         continue;
609                 }
610                 first = next+1;
611         }
612         /* not found */
613         if (find_only)
614                 return NULL;
615         /* insert to make it at "first" */
616         if (sha1_size_cache_alloc <= sha1_size_cache_nr) {
617                 sha1_size_cache_alloc = alloc_nr(sha1_size_cache_alloc);
618                 sha1_size_cache = xrealloc(sha1_size_cache,
619                                            sha1_size_cache_alloc *
620                                            sizeof(*sha1_size_cache));
621         }
622         sha1_size_cache_nr++;
623         if (first < sha1_size_cache_nr)
624                 memmove(sha1_size_cache + first + 1, sha1_size_cache + first,
625                         (sha1_size_cache_nr - first - 1) *
626                         sizeof(*sha1_size_cache));
627         e = xmalloc(sizeof(struct sha1_size_cache));
628         sha1_size_cache[first] = e;
629         memcpy(e->sha1, sha1, 20);
630         e->size = size;
631         return e;
632 }
633
634 /*
635  * While doing rename detection and pickaxe operation, we may need to
636  * grab the data for the blob (or file) for our own in-core comparison.
637  * diff_filespec has data and size fields for this purpose.
638  */
639 int diff_populate_filespec(struct diff_filespec *s, int size_only)
640 {
641         int err = 0;
642         if (!DIFF_FILE_VALID(s))
643                 die("internal error: asking to populate invalid file.");
644         if (S_ISDIR(s->mode))
645                 return -1;
646
647         if (!use_size_cache)
648                 size_only = 0;
649
650         if (s->data)
651                 return err;
652         if (!s->sha1_valid ||
653             work_tree_matches(s->path, s->sha1)) {
654                 struct stat st;
655                 int fd;
656                 if (lstat(s->path, &st) < 0) {
657                         if (errno == ENOENT) {
658                         err_empty:
659                                 err = -1;
660                         empty:
661                                 s->data = "";
662                                 s->size = 0;
663                                 return err;
664                         }
665                 }
666                 s->size = st.st_size;
667                 if (!s->size)
668                         goto empty;
669                 if (size_only)
670                         return 0;
671                 if (S_ISLNK(st.st_mode)) {
672                         int ret;
673                         s->data = xmalloc(s->size);
674                         s->should_free = 1;
675                         ret = readlink(s->path, s->data, s->size);
676                         if (ret < 0) {
677                                 free(s->data);
678                                 goto err_empty;
679                         }
680                         return 0;
681                 }
682                 fd = open(s->path, O_RDONLY);
683                 if (fd < 0)
684                         goto err_empty;
685                 s->data = mmap(NULL, s->size, PROT_READ, MAP_PRIVATE, fd, 0);
686                 close(fd);
687                 if (s->data == MAP_FAILED)
688                         goto err_empty;
689                 s->should_munmap = 1;
690         }
691         else {
692                 char type[20];
693                 struct sha1_size_cache *e;
694
695                 if (size_only) {
696                         e = locate_size_cache(s->sha1, 1, 0);
697                         if (e) {
698                                 s->size = e->size;
699                                 return 0;
700                         }
701                         if (!sha1_object_info(s->sha1, type, &s->size))
702                                 locate_size_cache(s->sha1, 0, s->size);
703                 }
704                 else {
705                         s->data = read_sha1_file(s->sha1, type, &s->size);
706                         s->should_free = 1;
707                 }
708         }
709         return 0;
710 }
711
712 void diff_free_filespec_data(struct diff_filespec *s)
713 {
714         if (s->should_free)
715                 free(s->data);
716         else if (s->should_munmap)
717                 munmap(s->data, s->size);
718         s->should_free = s->should_munmap = 0;
719         s->data = NULL;
720         free(s->cnt_data);
721         s->cnt_data = NULL;
722 }
723
724 static void prep_temp_blob(struct diff_tempfile *temp,
725                            void *blob,
726                            unsigned long size,
727                            const unsigned char *sha1,
728                            int mode)
729 {
730         int fd;
731
732         fd = git_mkstemp(temp->tmp_path, TEMPFILE_PATH_LEN, ".diff_XXXXXX");
733         if (fd < 0)
734                 die("unable to create temp-file");
735         if (write(fd, blob, size) != size)
736                 die("unable to write temp-file");
737         close(fd);
738         temp->name = temp->tmp_path;
739         strcpy(temp->hex, sha1_to_hex(sha1));
740         temp->hex[40] = 0;
741         sprintf(temp->mode, "%06o", mode);
742 }
743
744 static void prepare_temp_file(const char *name,
745                               struct diff_tempfile *temp,
746                               struct diff_filespec *one)
747 {
748         if (!DIFF_FILE_VALID(one)) {
749         not_a_valid_file:
750                 /* A '-' entry produces this for file-2, and
751                  * a '+' entry produces this for file-1.
752                  */
753                 temp->name = "/dev/null";
754                 strcpy(temp->hex, ".");
755                 strcpy(temp->mode, ".");
756                 return;
757         }
758
759         if (!one->sha1_valid ||
760             work_tree_matches(name, one->sha1)) {
761                 struct stat st;
762                 if (lstat(name, &st) < 0) {
763                         if (errno == ENOENT)
764                                 goto not_a_valid_file;
765                         die("stat(%s): %s", name, strerror(errno));
766                 }
767                 if (S_ISLNK(st.st_mode)) {
768                         int ret;
769                         char buf[PATH_MAX + 1]; /* ought to be SYMLINK_MAX */
770                         if (sizeof(buf) <= st.st_size)
771                                 die("symlink too long: %s", name);
772                         ret = readlink(name, buf, st.st_size);
773                         if (ret < 0)
774                                 die("readlink(%s)", name);
775                         prep_temp_blob(temp, buf, st.st_size,
776                                        (one->sha1_valid ?
777                                         one->sha1 : null_sha1),
778                                        (one->sha1_valid ?
779                                         one->mode : S_IFLNK));
780                 }
781                 else {
782                         /* we can borrow from the file in the work tree */
783                         temp->name = name;
784                         if (!one->sha1_valid)
785                                 strcpy(temp->hex, sha1_to_hex(null_sha1));
786                         else
787                                 strcpy(temp->hex, sha1_to_hex(one->sha1));
788                         /* Even though we may sometimes borrow the
789                          * contents from the work tree, we always want
790                          * one->mode.  mode is trustworthy even when
791                          * !(one->sha1_valid), as long as
792                          * DIFF_FILE_VALID(one).
793                          */
794                         sprintf(temp->mode, "%06o", one->mode);
795                 }
796                 return;
797         }
798         else {
799                 if (diff_populate_filespec(one, 0))
800                         die("cannot read data blob for %s", one->path);
801                 prep_temp_blob(temp, one->data, one->size,
802                                one->sha1, one->mode);
803         }
804 }
805
806 static void remove_tempfile(void)
807 {
808         int i;
809
810         for (i = 0; i < 2; i++)
811                 if (diff_temp[i].name == diff_temp[i].tmp_path) {
812                         unlink(diff_temp[i].name);
813                         diff_temp[i].name = NULL;
814                 }
815 }
816
817 static void remove_tempfile_on_signal(int signo)
818 {
819         remove_tempfile();
820         signal(SIGINT, SIG_DFL);
821         raise(signo);
822 }
823
824 static int spawn_prog(const char *pgm, const char **arg)
825 {
826         pid_t pid;
827         int status;
828
829         fflush(NULL);
830         pid = fork();
831         if (pid < 0)
832                 die("unable to fork");
833         if (!pid) {
834                 execvp(pgm, (char *const*) arg);
835                 exit(255);
836         }
837
838         while (waitpid(pid, &status, 0) < 0) {
839                 if (errno == EINTR)
840                         continue;
841                 return -1;
842         }
843
844         /* Earlier we did not check the exit status because
845          * diff exits non-zero if files are different, and
846          * we are not interested in knowing that.  It was a
847          * mistake which made it harder to quit a diff-*
848          * session that uses the git-apply-patch-script as
849          * the GIT_EXTERNAL_DIFF.  A custom GIT_EXTERNAL_DIFF
850          * should also exit non-zero only when it wants to
851          * abort the entire diff-* session.
852          */
853         if (WIFEXITED(status) && !WEXITSTATUS(status))
854                 return 0;
855         return -1;
856 }
857
858 /* An external diff command takes:
859  *
860  * diff-cmd name infile1 infile1-sha1 infile1-mode \
861  *               infile2 infile2-sha1 infile2-mode [ rename-to ]
862  *
863  */
864 static void run_external_diff(const char *pgm,
865                               const char *name,
866                               const char *other,
867                               struct diff_filespec *one,
868                               struct diff_filespec *two,
869                               const char *xfrm_msg,
870                               int complete_rewrite)
871 {
872         const char *spawn_arg[10];
873         struct diff_tempfile *temp = diff_temp;
874         int retval;
875         static int atexit_asked = 0;
876         const char *othername;
877         const char **arg = &spawn_arg[0];
878
879         othername = (other? other : name);
880         if (one && two) {
881                 prepare_temp_file(name, &temp[0], one);
882                 prepare_temp_file(othername, &temp[1], two);
883                 if (! atexit_asked &&
884                     (temp[0].name == temp[0].tmp_path ||
885                      temp[1].name == temp[1].tmp_path)) {
886                         atexit_asked = 1;
887                         atexit(remove_tempfile);
888                 }
889                 signal(SIGINT, remove_tempfile_on_signal);
890         }
891
892         if (one && two) {
893                 *arg++ = pgm;
894                 *arg++ = name;
895                 *arg++ = temp[0].name;
896                 *arg++ = temp[0].hex;
897                 *arg++ = temp[0].mode;
898                 *arg++ = temp[1].name;
899                 *arg++ = temp[1].hex;
900                 *arg++ = temp[1].mode;
901                 if (other) {
902                         *arg++ = other;
903                         *arg++ = xfrm_msg;
904                 }
905         } else {
906                 *arg++ = pgm;
907                 *arg++ = name;
908         }
909         *arg = NULL;
910         retval = spawn_prog(pgm, spawn_arg);
911         remove_tempfile();
912         if (retval) {
913                 fprintf(stderr, "external diff died, stopping at %s.\n", name);
914                 exit(1);
915         }
916 }
917
918 static void run_diff_cmd(const char *pgm,
919                          const char *name,
920                          const char *other,
921                          struct diff_filespec *one,
922                          struct diff_filespec *two,
923                          const char *xfrm_msg,
924                          int complete_rewrite)
925 {
926         if (pgm) {
927                 run_external_diff(pgm, name, other, one, two, xfrm_msg,
928                                   complete_rewrite);
929                 return;
930         }
931         if (one && two)
932                 builtin_diff(name, other ? other : name,
933                              one, two, xfrm_msg, complete_rewrite);
934         else
935                 printf("* Unmerged path %s\n", name);
936 }
937
938 static void diff_fill_sha1_info(struct diff_filespec *one)
939 {
940         if (DIFF_FILE_VALID(one)) {
941                 if (!one->sha1_valid) {
942                         struct stat st;
943                         if (lstat(one->path, &st) < 0)
944                                 die("stat %s", one->path);
945                         if (index_path(one->sha1, one->path, &st, 0))
946                                 die("cannot hash %s\n", one->path);
947                 }
948         }
949         else
950                 memset(one->sha1, 0, 20);
951 }
952
953 static void run_diff(struct diff_filepair *p, struct diff_options *o)
954 {
955         const char *pgm = external_diff();
956         char msg[PATH_MAX*2+300], *xfrm_msg;
957         struct diff_filespec *one;
958         struct diff_filespec *two;
959         const char *name;
960         const char *other;
961         char *name_munged, *other_munged;
962         int complete_rewrite = 0;
963         int len;
964
965         if (DIFF_PAIR_UNMERGED(p)) {
966                 /* unmerged */
967                 run_diff_cmd(pgm, p->one->path, NULL, NULL, NULL, NULL, 0);
968                 return;
969         }
970
971         name = p->one->path;
972         other = (strcmp(name, p->two->path) ? p->two->path : NULL);
973         name_munged = quote_one(name);
974         other_munged = quote_one(other);
975         one = p->one; two = p->two;
976
977         diff_fill_sha1_info(one);
978         diff_fill_sha1_info(two);
979
980         len = 0;
981         switch (p->status) {
982         case DIFF_STATUS_COPIED:
983                 len += snprintf(msg + len, sizeof(msg) - len,
984                                 "similarity index %d%%\n"
985                                 "copy from %s\n"
986                                 "copy to %s\n",
987                                 (int)(0.5 + p->score * 100.0/MAX_SCORE),
988                                 name_munged, other_munged);
989                 break;
990         case DIFF_STATUS_RENAMED:
991                 len += snprintf(msg + len, sizeof(msg) - len,
992                                 "similarity index %d%%\n"
993                                 "rename from %s\n"
994                                 "rename to %s\n",
995                                 (int)(0.5 + p->score * 100.0/MAX_SCORE),
996                                 name_munged, other_munged);
997                 break;
998         case DIFF_STATUS_MODIFIED:
999                 if (p->score) {
1000                         len += snprintf(msg + len, sizeof(msg) - len,
1001                                         "dissimilarity index %d%%\n",
1002                                         (int)(0.5 + p->score *
1003                                               100.0/MAX_SCORE));
1004                         complete_rewrite = 1;
1005                         break;
1006                 }
1007                 /* fallthru */
1008         default:
1009                 /* nothing */
1010                 ;
1011         }
1012
1013         if (memcmp(one->sha1, two->sha1, 20)) {
1014                 char one_sha1[41];
1015                 int abbrev = o->full_index ? 40 : DEFAULT_ABBREV;
1016                 memcpy(one_sha1, sha1_to_hex(one->sha1), 41);
1017
1018                 len += snprintf(msg + len, sizeof(msg) - len,
1019                                 "index %.*s..%.*s",
1020                                 abbrev, one_sha1, abbrev,
1021                                 sha1_to_hex(two->sha1));
1022                 if (one->mode == two->mode)
1023                         len += snprintf(msg + len, sizeof(msg) - len,
1024                                         " %06o", one->mode);
1025                 len += snprintf(msg + len, sizeof(msg) - len, "\n");
1026         }
1027
1028         if (len)
1029                 msg[--len] = 0;
1030         xfrm_msg = len ? msg : NULL;
1031
1032         if (!pgm &&
1033             DIFF_FILE_VALID(one) && DIFF_FILE_VALID(two) &&
1034             (S_IFMT & one->mode) != (S_IFMT & two->mode)) {
1035                 /* a filepair that changes between file and symlink
1036                  * needs to be split into deletion and creation.
1037                  */
1038                 struct diff_filespec *null = alloc_filespec(two->path);
1039                 run_diff_cmd(NULL, name, other, one, null, xfrm_msg, 0);
1040                 free(null);
1041                 null = alloc_filespec(one->path);
1042                 run_diff_cmd(NULL, name, other, null, two, xfrm_msg, 0);
1043                 free(null);
1044         }
1045         else
1046                 run_diff_cmd(pgm, name, other, one, two, xfrm_msg,
1047                              complete_rewrite);
1048
1049         free(name_munged);
1050         free(other_munged);
1051 }
1052
1053 static void run_diffstat(struct diff_filepair *p, struct diff_options *o,
1054                          struct diffstat_t *diffstat)
1055 {
1056         const char *name;
1057         const char *other;
1058
1059         if (DIFF_PAIR_UNMERGED(p)) {
1060                 /* unmerged */
1061                 builtin_diffstat(p->one->path, NULL, NULL, NULL, diffstat);
1062                 return;
1063         }
1064
1065         name = p->one->path;
1066         other = (strcmp(name, p->two->path) ? p->two->path : NULL);
1067
1068         diff_fill_sha1_info(p->one);
1069         diff_fill_sha1_info(p->two);
1070
1071         builtin_diffstat(name, other, p->one, p->two, diffstat);
1072 }
1073
1074 void diff_setup(struct diff_options *options)
1075 {
1076         memset(options, 0, sizeof(*options));
1077         options->output_format = DIFF_FORMAT_RAW;
1078         options->line_termination = '\n';
1079         options->break_opt = -1;
1080         options->rename_limit = -1;
1081
1082         options->change = diff_change;
1083         options->add_remove = diff_addremove;
1084 }
1085
1086 int diff_setup_done(struct diff_options *options)
1087 {
1088         if ((options->find_copies_harder &&
1089              options->detect_rename != DIFF_DETECT_COPY) ||
1090             (0 <= options->rename_limit && !options->detect_rename))
1091                 return -1;
1092
1093         /*
1094          * These cases always need recursive; we do not drop caller-supplied
1095          * recursive bits for other formats here.
1096          */
1097         if ((options->output_format == DIFF_FORMAT_PATCH) ||
1098             (options->output_format == DIFF_FORMAT_DIFFSTAT))
1099                 options->recursive = 1;
1100
1101         if (options->detect_rename && options->rename_limit < 0)
1102                 options->rename_limit = diff_rename_limit_default;
1103         if (options->setup & DIFF_SETUP_USE_CACHE) {
1104                 if (!active_cache)
1105                         /* read-cache does not die even when it fails
1106                          * so it is safe for us to do this here.  Also
1107                          * it does not smudge active_cache or active_nr
1108                          * when it fails, so we do not have to worry about
1109                          * cleaning it up ourselves either.
1110                          */
1111                         read_cache();
1112         }
1113         if (options->setup & DIFF_SETUP_USE_SIZE_CACHE)
1114                 use_size_cache = 1;
1115         if (options->abbrev <= 0 || 40 < options->abbrev)
1116                 options->abbrev = 40; /* full */
1117
1118         return 0;
1119 }
1120
1121 int diff_opt_parse(struct diff_options *options, const char **av, int ac)
1122 {
1123         const char *arg = av[0];
1124         if (!strcmp(arg, "-p") || !strcmp(arg, "-u"))
1125                 options->output_format = DIFF_FORMAT_PATCH;
1126         else if (!strcmp(arg, "--patch-with-raw")) {
1127                 options->output_format = DIFF_FORMAT_PATCH;
1128                 options->with_raw = 1;
1129         }
1130         else if (!strcmp(arg, "--stat"))
1131                 options->output_format = DIFF_FORMAT_DIFFSTAT;
1132         else if (!strcmp(arg, "--patch-with-stat")) {
1133                 options->output_format = DIFF_FORMAT_PATCH;
1134                 options->with_stat = 1;
1135         }
1136         else if (!strcmp(arg, "-z"))
1137                 options->line_termination = 0;
1138         else if (!strncmp(arg, "-l", 2))
1139                 options->rename_limit = strtoul(arg+2, NULL, 10);
1140         else if (!strcmp(arg, "--full-index"))
1141                 options->full_index = 1;
1142         else if (!strcmp(arg, "--name-only"))
1143                 options->output_format = DIFF_FORMAT_NAME;
1144         else if (!strcmp(arg, "--name-status"))
1145                 options->output_format = DIFF_FORMAT_NAME_STATUS;
1146         else if (!strcmp(arg, "-R"))
1147                 options->reverse_diff = 1;
1148         else if (!strncmp(arg, "-S", 2))
1149                 options->pickaxe = arg + 2;
1150         else if (!strcmp(arg, "-s"))
1151                 options->output_format = DIFF_FORMAT_NO_OUTPUT;
1152         else if (!strncmp(arg, "-O", 2))
1153                 options->orderfile = arg + 2;
1154         else if (!strncmp(arg, "--diff-filter=", 14))
1155                 options->filter = arg + 14;
1156         else if (!strcmp(arg, "--pickaxe-all"))
1157                 options->pickaxe_opts = DIFF_PICKAXE_ALL;
1158         else if (!strcmp(arg, "--pickaxe-regex"))
1159                 options->pickaxe_opts = DIFF_PICKAXE_REGEX;
1160         else if (!strncmp(arg, "-B", 2)) {
1161                 if ((options->break_opt =
1162                      diff_scoreopt_parse(arg)) == -1)
1163                         return -1;
1164         }
1165         else if (!strncmp(arg, "-M", 2)) {
1166                 if ((options->rename_score =
1167                      diff_scoreopt_parse(arg)) == -1)
1168                         return -1;
1169                 options->detect_rename = DIFF_DETECT_RENAME;
1170         }
1171         else if (!strncmp(arg, "-C", 2)) {
1172                 if ((options->rename_score =
1173                      diff_scoreopt_parse(arg)) == -1)
1174                         return -1;
1175                 options->detect_rename = DIFF_DETECT_COPY;
1176         }
1177         else if (!strcmp(arg, "--find-copies-harder"))
1178                 options->find_copies_harder = 1;
1179         else if (!strcmp(arg, "--abbrev"))
1180                 options->abbrev = DEFAULT_ABBREV;
1181         else if (!strncmp(arg, "--abbrev=", 9)) {
1182                 options->abbrev = strtoul(arg + 9, NULL, 10);
1183                 if (options->abbrev < MINIMUM_ABBREV)
1184                         options->abbrev = MINIMUM_ABBREV;
1185                 else if (40 < options->abbrev)
1186                         options->abbrev = 40;
1187         }
1188         else
1189                 return 0;
1190         return 1;
1191 }
1192
1193 static int parse_num(const char **cp_p)
1194 {
1195         unsigned long num, scale;
1196         int ch, dot;
1197         const char *cp = *cp_p;
1198
1199         num = 0;
1200         scale = 1;
1201         dot = 0;
1202         for(;;) {
1203                 ch = *cp;
1204                 if ( !dot && ch == '.' ) {
1205                         scale = 1;
1206                         dot = 1;
1207                 } else if ( ch == '%' ) {
1208                         scale = dot ? scale*100 : 100;
1209                         cp++;   /* % is always at the end */
1210                         break;
1211                 } else if ( ch >= '0' && ch <= '9' ) {
1212                         if ( scale < 100000 ) {
1213                                 scale *= 10;
1214                                 num = (num*10) + (ch-'0');
1215                         }
1216                 } else {
1217                         break;
1218                 }
1219                 cp++;
1220         }
1221         *cp_p = cp;
1222
1223         /* user says num divided by scale and we say internally that
1224          * is MAX_SCORE * num / scale.
1225          */
1226         return (num >= scale) ? MAX_SCORE : (MAX_SCORE * num / scale);
1227 }
1228
1229 int diff_scoreopt_parse(const char *opt)
1230 {
1231         int opt1, opt2, cmd;
1232
1233         if (*opt++ != '-')
1234                 return -1;
1235         cmd = *opt++;
1236         if (cmd != 'M' && cmd != 'C' && cmd != 'B')
1237                 return -1; /* that is not a -M, -C nor -B option */
1238
1239         opt1 = parse_num(&opt);
1240         if (cmd != 'B')
1241                 opt2 = 0;
1242         else {
1243                 if (*opt == 0)
1244                         opt2 = 0;
1245                 else if (*opt != '/')
1246                         return -1; /* we expect -B80/99 or -B80 */
1247                 else {
1248                         opt++;
1249                         opt2 = parse_num(&opt);
1250                 }
1251         }
1252         if (*opt != 0)
1253                 return -1;
1254         return opt1 | (opt2 << 16);
1255 }
1256
1257 struct diff_queue_struct diff_queued_diff;
1258
1259 void diff_q(struct diff_queue_struct *queue, struct diff_filepair *dp)
1260 {
1261         if (queue->alloc <= queue->nr) {
1262                 queue->alloc = alloc_nr(queue->alloc);
1263                 queue->queue = xrealloc(queue->queue,
1264                                         sizeof(dp) * queue->alloc);
1265         }
1266         queue->queue[queue->nr++] = dp;
1267 }
1268
1269 struct diff_filepair *diff_queue(struct diff_queue_struct *queue,
1270                                  struct diff_filespec *one,
1271                                  struct diff_filespec *two)
1272 {
1273         struct diff_filepair *dp = xmalloc(sizeof(*dp));
1274         dp->one = one;
1275         dp->two = two;
1276         dp->score = 0;
1277         dp->status = 0;
1278         dp->source_stays = 0;
1279         dp->broken_pair = 0;
1280         if (queue)
1281                 diff_q(queue, dp);
1282         return dp;
1283 }
1284
1285 void diff_free_filepair(struct diff_filepair *p)
1286 {
1287         diff_free_filespec_data(p->one);
1288         diff_free_filespec_data(p->two);
1289         free(p->one);
1290         free(p->two);
1291         free(p);
1292 }
1293
1294 /* This is different from find_unique_abbrev() in that
1295  * it stuffs the result with dots for alignment.
1296  */
1297 const char *diff_unique_abbrev(const unsigned char *sha1, int len)
1298 {
1299         int abblen;
1300         const char *abbrev;
1301         if (len == 40)
1302                 return sha1_to_hex(sha1);
1303
1304         abbrev = find_unique_abbrev(sha1, len);
1305         if (!abbrev)
1306                 return sha1_to_hex(sha1);
1307         abblen = strlen(abbrev);
1308         if (abblen < 37) {
1309                 static char hex[41];
1310                 if (len < abblen && abblen <= len + 2)
1311                         sprintf(hex, "%s%.*s", abbrev, len+3-abblen, "..");
1312                 else
1313                         sprintf(hex, "%s...", abbrev);
1314                 return hex;
1315         }
1316         return sha1_to_hex(sha1);
1317 }
1318
1319 static void diff_flush_raw(struct diff_filepair *p,
1320                            int line_termination,
1321                            int inter_name_termination,
1322                            struct diff_options *options,
1323                            int output_format)
1324 {
1325         int two_paths;
1326         char status[10];
1327         int abbrev = options->abbrev;
1328         const char *path_one, *path_two;
1329
1330         path_one = p->one->path;
1331         path_two = p->two->path;
1332         if (line_termination) {
1333                 path_one = quote_one(path_one);
1334                 path_two = quote_one(path_two);
1335         }
1336
1337         if (p->score)
1338                 sprintf(status, "%c%03d", p->status,
1339                         (int)(0.5 + p->score * 100.0/MAX_SCORE));
1340         else {
1341                 status[0] = p->status;
1342                 status[1] = 0;
1343         }
1344         switch (p->status) {
1345         case DIFF_STATUS_COPIED:
1346         case DIFF_STATUS_RENAMED:
1347                 two_paths = 1;
1348                 break;
1349         case DIFF_STATUS_ADDED:
1350         case DIFF_STATUS_DELETED:
1351                 two_paths = 0;
1352                 break;
1353         default:
1354                 two_paths = 0;
1355                 break;
1356         }
1357         if (output_format != DIFF_FORMAT_NAME_STATUS) {
1358                 printf(":%06o %06o %s ",
1359                        p->one->mode, p->two->mode,
1360                        diff_unique_abbrev(p->one->sha1, abbrev));
1361                 printf("%s ",
1362                        diff_unique_abbrev(p->two->sha1, abbrev));
1363         }
1364         printf("%s%c%s", status, inter_name_termination, path_one);
1365         if (two_paths)
1366                 printf("%c%s", inter_name_termination, path_two);
1367         putchar(line_termination);
1368         if (path_one != p->one->path)
1369                 free((void*)path_one);
1370         if (path_two != p->two->path)
1371                 free((void*)path_two);
1372 }
1373
1374 static void diff_flush_name(struct diff_filepair *p,
1375                             int inter_name_termination,
1376                             int line_termination)
1377 {
1378         char *path = p->two->path;
1379
1380         if (line_termination)
1381                 path = quote_one(p->two->path);
1382         else
1383                 path = p->two->path;
1384         printf("%s%c", path, line_termination);
1385         if (p->two->path != path)
1386                 free(path);
1387 }
1388
1389 int diff_unmodified_pair(struct diff_filepair *p)
1390 {
1391         /* This function is written stricter than necessary to support
1392          * the currently implemented transformers, but the idea is to
1393          * let transformers to produce diff_filepairs any way they want,
1394          * and filter and clean them up here before producing the output.
1395          */
1396         struct diff_filespec *one, *two;
1397
1398         if (DIFF_PAIR_UNMERGED(p))
1399                 return 0; /* unmerged is interesting */
1400
1401         one = p->one;
1402         two = p->two;
1403
1404         /* deletion, addition, mode or type change
1405          * and rename are all interesting.
1406          */
1407         if (DIFF_FILE_VALID(one) != DIFF_FILE_VALID(two) ||
1408             DIFF_PAIR_MODE_CHANGED(p) ||
1409             strcmp(one->path, two->path))
1410                 return 0;
1411
1412         /* both are valid and point at the same path.  that is, we are
1413          * dealing with a change.
1414          */
1415         if (one->sha1_valid && two->sha1_valid &&
1416             !memcmp(one->sha1, two->sha1, sizeof(one->sha1)))
1417                 return 1; /* no change */
1418         if (!one->sha1_valid && !two->sha1_valid)
1419                 return 1; /* both look at the same file on the filesystem. */
1420         return 0;
1421 }
1422
1423 static void diff_flush_patch(struct diff_filepair *p, struct diff_options *o)
1424 {
1425         if (diff_unmodified_pair(p))
1426                 return;
1427
1428         if ((DIFF_FILE_VALID(p->one) && S_ISDIR(p->one->mode)) ||
1429             (DIFF_FILE_VALID(p->two) && S_ISDIR(p->two->mode)))
1430                 return; /* no tree diffs in patch format */
1431
1432         run_diff(p, o);
1433 }
1434
1435 static void diff_flush_stat(struct diff_filepair *p, struct diff_options *o,
1436                             struct diffstat_t *diffstat)
1437 {
1438         if (diff_unmodified_pair(p))
1439                 return;
1440
1441         if ((DIFF_FILE_VALID(p->one) && S_ISDIR(p->one->mode)) ||
1442             (DIFF_FILE_VALID(p->two) && S_ISDIR(p->two->mode)))
1443                 return; /* no tree diffs in patch format */
1444
1445         run_diffstat(p, o, diffstat);
1446 }
1447
1448 int diff_queue_is_empty(void)
1449 {
1450         struct diff_queue_struct *q = &diff_queued_diff;
1451         int i;
1452         for (i = 0; i < q->nr; i++)
1453                 if (!diff_unmodified_pair(q->queue[i]))
1454                         return 0;
1455         return 1;
1456 }
1457
1458 #if DIFF_DEBUG
1459 void diff_debug_filespec(struct diff_filespec *s, int x, const char *one)
1460 {
1461         fprintf(stderr, "queue[%d] %s (%s) %s %06o %s\n",
1462                 x, one ? one : "",
1463                 s->path,
1464                 DIFF_FILE_VALID(s) ? "valid" : "invalid",
1465                 s->mode,
1466                 s->sha1_valid ? sha1_to_hex(s->sha1) : "");
1467         fprintf(stderr, "queue[%d] %s size %lu flags %d\n",
1468                 x, one ? one : "",
1469                 s->size, s->xfrm_flags);
1470 }
1471
1472 void diff_debug_filepair(const struct diff_filepair *p, int i)
1473 {
1474         diff_debug_filespec(p->one, i, "one");
1475         diff_debug_filespec(p->two, i, "two");
1476         fprintf(stderr, "score %d, status %c stays %d broken %d\n",
1477                 p->score, p->status ? p->status : '?',
1478                 p->source_stays, p->broken_pair);
1479 }
1480
1481 void diff_debug_queue(const char *msg, struct diff_queue_struct *q)
1482 {
1483         int i;
1484         if (msg)
1485                 fprintf(stderr, "%s\n", msg);
1486         fprintf(stderr, "q->nr = %d\n", q->nr);
1487         for (i = 0; i < q->nr; i++) {
1488                 struct diff_filepair *p = q->queue[i];
1489                 diff_debug_filepair(p, i);
1490         }
1491 }
1492 #endif
1493
1494 static void diff_resolve_rename_copy(void)
1495 {
1496         int i, j;
1497         struct diff_filepair *p, *pp;
1498         struct diff_queue_struct *q = &diff_queued_diff;
1499
1500         diff_debug_queue("resolve-rename-copy", q);
1501
1502         for (i = 0; i < q->nr; i++) {
1503                 p = q->queue[i];
1504                 p->status = 0; /* undecided */
1505                 if (DIFF_PAIR_UNMERGED(p))
1506                         p->status = DIFF_STATUS_UNMERGED;
1507                 else if (!DIFF_FILE_VALID(p->one))
1508                         p->status = DIFF_STATUS_ADDED;
1509                 else if (!DIFF_FILE_VALID(p->two))
1510                         p->status = DIFF_STATUS_DELETED;
1511                 else if (DIFF_PAIR_TYPE_CHANGED(p))
1512                         p->status = DIFF_STATUS_TYPE_CHANGED;
1513
1514                 /* from this point on, we are dealing with a pair
1515                  * whose both sides are valid and of the same type, i.e.
1516                  * either in-place edit or rename/copy edit.
1517                  */
1518                 else if (DIFF_PAIR_RENAME(p)) {
1519                         if (p->source_stays) {
1520                                 p->status = DIFF_STATUS_COPIED;
1521                                 continue;
1522                         }
1523                         /* See if there is some other filepair that
1524                          * copies from the same source as us.  If so
1525                          * we are a copy.  Otherwise we are either a
1526                          * copy if the path stays, or a rename if it
1527                          * does not, but we already handled "stays" case.
1528                          */
1529                         for (j = i + 1; j < q->nr; j++) {
1530                                 pp = q->queue[j];
1531                                 if (strcmp(pp->one->path, p->one->path))
1532                                         continue; /* not us */
1533                                 if (!DIFF_PAIR_RENAME(pp))
1534                                         continue; /* not a rename/copy */
1535                                 /* pp is a rename/copy from the same source */
1536                                 p->status = DIFF_STATUS_COPIED;
1537                                 break;
1538                         }
1539                         if (!p->status)
1540                                 p->status = DIFF_STATUS_RENAMED;
1541                 }
1542                 else if (memcmp(p->one->sha1, p->two->sha1, 20) ||
1543                          p->one->mode != p->two->mode)
1544                         p->status = DIFF_STATUS_MODIFIED;
1545                 else {
1546                         /* This is a "no-change" entry and should not
1547                          * happen anymore, but prepare for broken callers.
1548                          */
1549                         error("feeding unmodified %s to diffcore",
1550                               p->one->path);
1551                         p->status = DIFF_STATUS_UNKNOWN;
1552                 }
1553         }
1554         diff_debug_queue("resolve-rename-copy done", q);
1555 }
1556
1557 static void flush_one_pair(struct diff_filepair *p,
1558                            int diff_output_format,
1559                            struct diff_options *options,
1560                            struct diffstat_t *diffstat)
1561 {
1562         int inter_name_termination = '\t';
1563         int line_termination = options->line_termination;
1564         if (!line_termination)
1565                 inter_name_termination = 0;
1566
1567         switch (p->status) {
1568         case DIFF_STATUS_UNKNOWN:
1569                 break;
1570         case 0:
1571                 die("internal error in diff-resolve-rename-copy");
1572                 break;
1573         default:
1574                 switch (diff_output_format) {
1575                 case DIFF_FORMAT_DIFFSTAT:
1576                         diff_flush_stat(p, options, diffstat);
1577                         break;
1578                 case DIFF_FORMAT_PATCH:
1579                         diff_flush_patch(p, options);
1580                         break;
1581                 case DIFF_FORMAT_RAW:
1582                 case DIFF_FORMAT_NAME_STATUS:
1583                         diff_flush_raw(p, line_termination,
1584                                        inter_name_termination,
1585                                        options, diff_output_format);
1586                         break;
1587                 case DIFF_FORMAT_NAME:
1588                         diff_flush_name(p,
1589                                         inter_name_termination,
1590                                         line_termination);
1591                         break;
1592                 case DIFF_FORMAT_NO_OUTPUT:
1593                         break;
1594                 }
1595         }
1596 }
1597
1598 void diff_flush(struct diff_options *options)
1599 {
1600         struct diff_queue_struct *q = &diff_queued_diff;
1601         int i;
1602         int diff_output_format = options->output_format;
1603         struct diffstat_t *diffstat = NULL;
1604
1605         if (diff_output_format == DIFF_FORMAT_DIFFSTAT || options->with_stat) {
1606                 diffstat = xcalloc(sizeof (struct diffstat_t), 1);
1607                 diffstat->xm.consume = diffstat_consume;
1608         }
1609
1610         if (options->with_raw) {
1611                 for (i = 0; i < q->nr; i++) {
1612                         struct diff_filepair *p = q->queue[i];
1613                         flush_one_pair(p, DIFF_FORMAT_RAW, options, NULL);
1614                 }
1615                 putchar(options->line_termination);
1616         }
1617         if (options->with_stat) {
1618                 for (i = 0; i < q->nr; i++) {
1619                         struct diff_filepair *p = q->queue[i];
1620                         flush_one_pair(p, DIFF_FORMAT_DIFFSTAT, options,
1621                                        diffstat);
1622                 }
1623                 show_stats(diffstat);
1624                 free(diffstat);
1625                 diffstat = NULL;
1626                 putchar(options->line_termination);
1627         }
1628         for (i = 0; i < q->nr; i++) {
1629                 struct diff_filepair *p = q->queue[i];
1630                 flush_one_pair(p, diff_output_format, options, diffstat);
1631                 diff_free_filepair(p);
1632         }
1633
1634         if (diffstat) {
1635                 show_stats(diffstat);
1636                 free(diffstat);
1637         }
1638
1639         free(q->queue);
1640         q->queue = NULL;
1641         q->nr = q->alloc = 0;
1642 }
1643
1644 static void diffcore_apply_filter(const char *filter)
1645 {
1646         int i;
1647         struct diff_queue_struct *q = &diff_queued_diff;
1648         struct diff_queue_struct outq;
1649         outq.queue = NULL;
1650         outq.nr = outq.alloc = 0;
1651
1652         if (!filter)
1653                 return;
1654
1655         if (strchr(filter, DIFF_STATUS_FILTER_AON)) {
1656                 int found;
1657                 for (i = found = 0; !found && i < q->nr; i++) {
1658                         struct diff_filepair *p = q->queue[i];
1659                         if (((p->status == DIFF_STATUS_MODIFIED) &&
1660                              ((p->score &&
1661                                strchr(filter, DIFF_STATUS_FILTER_BROKEN)) ||
1662                               (!p->score &&
1663                                strchr(filter, DIFF_STATUS_MODIFIED)))) ||
1664                             ((p->status != DIFF_STATUS_MODIFIED) &&
1665                              strchr(filter, p->status)))
1666                                 found++;
1667                 }
1668                 if (found)
1669                         return;
1670
1671                 /* otherwise we will clear the whole queue
1672                  * by copying the empty outq at the end of this
1673                  * function, but first clear the current entries
1674                  * in the queue.
1675                  */
1676                 for (i = 0; i < q->nr; i++)
1677                         diff_free_filepair(q->queue[i]);
1678         }
1679         else {
1680                 /* Only the matching ones */
1681                 for (i = 0; i < q->nr; i++) {
1682                         struct diff_filepair *p = q->queue[i];
1683
1684                         if (((p->status == DIFF_STATUS_MODIFIED) &&
1685                              ((p->score &&
1686                                strchr(filter, DIFF_STATUS_FILTER_BROKEN)) ||
1687                               (!p->score &&
1688                                strchr(filter, DIFF_STATUS_MODIFIED)))) ||
1689                             ((p->status != DIFF_STATUS_MODIFIED) &&
1690                              strchr(filter, p->status)))
1691                                 diff_q(&outq, p);
1692                         else
1693                                 diff_free_filepair(p);
1694                 }
1695         }
1696         free(q->queue);
1697         *q = outq;
1698 }
1699
1700 void diffcore_std(struct diff_options *options)
1701 {
1702         if (options->break_opt != -1)
1703                 diffcore_break(options->break_opt);
1704         if (options->detect_rename)
1705                 diffcore_rename(options);
1706         if (options->break_opt != -1)
1707                 diffcore_merge_broken();
1708         if (options->pickaxe)
1709                 diffcore_pickaxe(options->pickaxe, options->pickaxe_opts);
1710         if (options->orderfile)
1711                 diffcore_order(options->orderfile);
1712         diff_resolve_rename_copy();
1713         diffcore_apply_filter(options->filter);
1714 }
1715
1716
1717 void diffcore_std_no_resolve(struct diff_options *options)
1718 {
1719         if (options->pickaxe)
1720                 diffcore_pickaxe(options->pickaxe, options->pickaxe_opts);
1721         if (options->orderfile)
1722                 diffcore_order(options->orderfile);
1723         diffcore_apply_filter(options->filter);
1724 }
1725
1726 void diff_addremove(struct diff_options *options,
1727                     int addremove, unsigned mode,
1728                     const unsigned char *sha1,
1729                     const char *base, const char *path)
1730 {
1731         char concatpath[PATH_MAX];
1732         struct diff_filespec *one, *two;
1733
1734         /* This may look odd, but it is a preparation for
1735          * feeding "there are unchanged files which should
1736          * not produce diffs, but when you are doing copy
1737          * detection you would need them, so here they are"
1738          * entries to the diff-core.  They will be prefixed
1739          * with something like '=' or '*' (I haven't decided
1740          * which but should not make any difference).
1741          * Feeding the same new and old to diff_change() 
1742          * also has the same effect.
1743          * Before the final output happens, they are pruned after
1744          * merged into rename/copy pairs as appropriate.
1745          */
1746         if (options->reverse_diff)
1747                 addremove = (addremove == '+' ? '-' :
1748                              addremove == '-' ? '+' : addremove);
1749
1750         if (!path) path = "";
1751         sprintf(concatpath, "%s%s", base, path);
1752         one = alloc_filespec(concatpath);
1753         two = alloc_filespec(concatpath);
1754
1755         if (addremove != '+')
1756                 fill_filespec(one, sha1, mode);
1757         if (addremove != '-')
1758                 fill_filespec(two, sha1, mode);
1759
1760         diff_queue(&diff_queued_diff, one, two);
1761 }
1762
1763 void diff_change(struct diff_options *options,
1764                  unsigned old_mode, unsigned new_mode,
1765                  const unsigned char *old_sha1,
1766                  const unsigned char *new_sha1,
1767                  const char *base, const char *path) 
1768 {
1769         char concatpath[PATH_MAX];
1770         struct diff_filespec *one, *two;
1771
1772         if (options->reverse_diff) {
1773                 unsigned tmp;
1774                 const unsigned char *tmp_c;
1775                 tmp = old_mode; old_mode = new_mode; new_mode = tmp;
1776                 tmp_c = old_sha1; old_sha1 = new_sha1; new_sha1 = tmp_c;
1777         }
1778         if (!path) path = "";
1779         sprintf(concatpath, "%s%s", base, path);
1780         one = alloc_filespec(concatpath);
1781         two = alloc_filespec(concatpath);
1782         fill_filespec(one, old_sha1, old_mode);
1783         fill_filespec(two, new_sha1, new_mode);
1784
1785         diff_queue(&diff_queued_diff, one, two);
1786 }
1787
1788 void diff_unmerge(struct diff_options *options,
1789                   const char *path)
1790 {
1791         struct diff_filespec *one, *two;
1792         one = alloc_filespec(path);
1793         two = alloc_filespec(path);
1794         diff_queue(&diff_queued_diff, one, two);
1795 }