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