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