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