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