git-am: interactive should fail gracefully.
[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
12 static const char *diff_opts = "-pu";
13
14 static int use_size_cache;
15
16 static const char *external_diff(void)
17 {
18         static const char *external_diff_cmd = NULL;
19         static int done_preparing = 0;
20         const char *env_diff_opts;
21
22         if (done_preparing)
23                 return external_diff_cmd;
24
25         /*
26          * Default values above are meant to match the
27          * Linux kernel development style.  Examples of
28          * alternative styles you can specify via environment
29          * variables are:
30          *
31          * GIT_DIFF_OPTS="-c";
32          */
33         external_diff_cmd = getenv("GIT_EXTERNAL_DIFF");
34
35         /* In case external diff fails... */
36         env_diff_opts = getenv("GIT_DIFF_OPTS");
37         if (env_diff_opts) diff_opts = env_diff_opts;
38
39         done_preparing = 1;
40         return external_diff_cmd;
41 }
42
43 #define TEMPFILE_PATH_LEN               50
44
45 static struct diff_tempfile {
46         const char *name; /* filename external diff should read from */
47         char hex[41];
48         char mode[10];
49         char tmp_path[TEMPFILE_PATH_LEN];
50 } diff_temp[2];
51
52 static int count_lines(const char *filename)
53 {
54         FILE *in;
55         int count, ch, completely_empty = 1, nl_just_seen = 0;
56         in = fopen(filename, "r");
57         count = 0;
58         while ((ch = fgetc(in)) != EOF)
59                 if (ch == '\n') {
60                         count++;
61                         nl_just_seen = 1;
62                         completely_empty = 0;
63                 }
64                 else {
65                         nl_just_seen = 0;
66                         completely_empty = 0;
67                 }
68         fclose(in);
69         if (completely_empty)
70                 return 0;
71         if (!nl_just_seen)
72                 count++; /* no trailing newline */
73         return count;
74 }
75
76 static void print_line_count(int count)
77 {
78         switch (count) {
79         case 0:
80                 printf("0,0");
81                 break;
82         case 1:
83                 printf("1");
84                 break;
85         default:
86                 printf("1,%d", count);
87                 break;
88         }
89 }
90
91 static void copy_file(int prefix, const char *filename)
92 {
93         FILE *in;
94         int ch, nl_just_seen = 1;
95         in = fopen(filename, "r");
96         while ((ch = fgetc(in)) != EOF) {
97                 if (nl_just_seen)
98                         putchar(prefix);
99                 putchar(ch);
100                 if (ch == '\n')
101                         nl_just_seen = 1;
102                 else
103                         nl_just_seen = 0;
104         }
105         fclose(in);
106         if (!nl_just_seen)
107                 printf("\n\\ No newline at end of file\n");
108 }
109
110 static void emit_rewrite_diff(const char *name_a,
111                               const char *name_b,
112                               struct diff_tempfile *temp)
113 {
114         /* Use temp[i].name as input, name_a and name_b as labels */
115         int lc_a, lc_b;
116         lc_a = count_lines(temp[0].name);
117         lc_b = count_lines(temp[1].name);
118         printf("--- %s\n+++ %s\n@@ -", name_a, name_b);
119         print_line_count(lc_a);
120         printf(" +");
121         print_line_count(lc_b);
122         printf(" @@\n");
123         if (lc_a)
124                 copy_file('-', temp[0].name);
125         if (lc_b)
126                 copy_file('+', temp[1].name);
127 }
128
129 static void builtin_diff(const char *name_a,
130                          const char *name_b,
131                          struct diff_tempfile *temp,
132                          const char *xfrm_msg,
133                          int complete_rewrite)
134 {
135         int i, next_at, cmd_size;
136         const char *const diff_cmd = "diff -L%s%s -L%s%s";
137         const char *const diff_arg  = "%s %s||:"; /* "||:" is to return 0 */
138         const char *input_name_sq[2];
139         const char *path0[2];
140         const char *path1[2];
141         const char *name_sq[2];
142         char *cmd;
143
144         name_sq[0] = sq_quote(name_a);
145         name_sq[1] = sq_quote(name_b);
146
147         /* diff_cmd and diff_arg have 6 %s in total which makes
148          * the sum of these strings 12 bytes larger than required.
149          * we use 2 spaces around diff-opts, and we need to count
150          * terminating NUL, so we subtract 9 here.
151          */
152         cmd_size = (strlen(diff_cmd) + strlen(diff_opts) +
153                         strlen(diff_arg) - 9);
154         for (i = 0; i < 2; i++) {
155                 input_name_sq[i] = sq_quote(temp[i].name);
156                 if (!strcmp(temp[i].name, "/dev/null")) {
157                         path0[i] = "/dev/null";
158                         path1[i] = "";
159                 } else {
160                         path0[i] = i ? "b/" : "a/";
161                         path1[i] = name_sq[i];
162                 }
163                 cmd_size += (strlen(path0[i]) + strlen(path1[i]) +
164                              strlen(input_name_sq[i]));
165         }
166
167         cmd = xmalloc(cmd_size);
168
169         next_at = 0;
170         next_at += snprintf(cmd+next_at, cmd_size-next_at,
171                             diff_cmd,
172                             path0[0], path1[0], path0[1], path1[1]);
173         next_at += snprintf(cmd+next_at, cmd_size-next_at,
174                             " %s ", diff_opts);
175         next_at += snprintf(cmd+next_at, cmd_size-next_at,
176                             diff_arg, input_name_sq[0], input_name_sq[1]);
177
178         printf("diff --git a/%s b/%s\n", name_a, name_b);
179         if (!path1[0][0]) {
180                 printf("new file mode %s\n", temp[1].mode);
181                 if (xfrm_msg && xfrm_msg[0])
182                         puts(xfrm_msg);
183         }
184         else if (!path1[1][0]) {
185                 printf("deleted file mode %s\n", temp[0].mode);
186                 if (xfrm_msg && xfrm_msg[0])
187                         puts(xfrm_msg);
188         }
189         else {
190                 if (strcmp(temp[0].mode, temp[1].mode)) {
191                         printf("old mode %s\n", temp[0].mode);
192                         printf("new mode %s\n", temp[1].mode);
193                 }
194                 if (xfrm_msg && xfrm_msg[0])
195                         puts(xfrm_msg);
196                 if (strncmp(temp[0].mode, temp[1].mode, 3))
197                         /* we do not run diff between different kind
198                          * of objects.
199                          */
200                         exit(0);
201                 if (complete_rewrite) {
202                         fflush(NULL);
203                         emit_rewrite_diff(name_a, name_b, temp);
204                         exit(0);
205                 }
206         }
207         fflush(NULL);
208         execlp("/bin/sh","sh", "-c", cmd, NULL);
209 }
210
211 struct diff_filespec *alloc_filespec(const char *path)
212 {
213         int namelen = strlen(path);
214         struct diff_filespec *spec = xmalloc(sizeof(*spec) + namelen + 1);
215
216         memset(spec, 0, sizeof(*spec));
217         spec->path = (char *)(spec + 1);
218         memcpy(spec->path, path, namelen+1);
219         return spec;
220 }
221
222 void fill_filespec(struct diff_filespec *spec, const unsigned char *sha1,
223                    unsigned short mode)
224 {
225         if (mode) {
226                 spec->mode = DIFF_FILE_CANON_MODE(mode);
227                 memcpy(spec->sha1, sha1, 20);
228                 spec->sha1_valid = !!memcmp(sha1, null_sha1, 20);
229         }
230 }
231
232 /*
233  * Given a name and sha1 pair, if the dircache tells us the file in
234  * the work tree has that object contents, return true, so that
235  * prepare_temp_file() does not have to inflate and extract.
236  */
237 static int work_tree_matches(const char *name, const unsigned char *sha1)
238 {
239         struct cache_entry *ce;
240         struct stat st;
241         int pos, len;
242
243         /* We do not read the cache ourselves here, because the
244          * benchmark with my previous version that always reads cache
245          * shows that it makes things worse for diff-tree comparing
246          * two linux-2.6 kernel trees in an already checked out work
247          * tree.  This is because most diff-tree comparisons deal with
248          * only a small number of files, while reading the cache is
249          * expensive for a large project, and its cost outweighs the
250          * savings we get by not inflating the object to a temporary
251          * file.  Practically, this code only helps when we are used
252          * by diff-cache --cached, which does read the cache before
253          * calling us.
254          */
255         if (!active_cache)
256                 return 0;
257
258         len = strlen(name);
259         pos = cache_name_pos(name, len);
260         if (pos < 0)
261                 return 0;
262         ce = active_cache[pos];
263         if ((lstat(name, &st) < 0) ||
264             !S_ISREG(st.st_mode) || /* careful! */
265             ce_match_stat(ce, &st) ||
266             memcmp(sha1, ce->sha1, 20))
267                 return 0;
268         /* we return 1 only when we can stat, it is a regular file,
269          * stat information matches, and sha1 recorded in the cache
270          * matches.  I.e. we know the file in the work tree really is
271          * the same as the <name, sha1> pair.
272          */
273         return 1;
274 }
275
276 static struct sha1_size_cache {
277         unsigned char sha1[20];
278         unsigned long size;
279 } **sha1_size_cache;
280 static int sha1_size_cache_nr, sha1_size_cache_alloc;
281
282 static struct sha1_size_cache *locate_size_cache(unsigned char *sha1,
283                                                  int find_only,
284                                                  unsigned long size)
285 {
286         int first, last;
287         struct sha1_size_cache *e;
288
289         first = 0;
290         last = sha1_size_cache_nr;
291         while (last > first) {
292                 int cmp, next = (last + first) >> 1;
293                 e = sha1_size_cache[next];
294                 cmp = memcmp(e->sha1, sha1, 20);
295                 if (!cmp)
296                         return e;
297                 if (cmp < 0) {
298                         last = next;
299                         continue;
300                 }
301                 first = next+1;
302         }
303         /* not found */
304         if (find_only)
305                 return NULL;
306         /* insert to make it at "first" */
307         if (sha1_size_cache_alloc <= sha1_size_cache_nr) {
308                 sha1_size_cache_alloc = alloc_nr(sha1_size_cache_alloc);
309                 sha1_size_cache = xrealloc(sha1_size_cache,
310                                            sha1_size_cache_alloc *
311                                            sizeof(*sha1_size_cache));
312         }
313         sha1_size_cache_nr++;
314         if (first < sha1_size_cache_nr)
315                 memmove(sha1_size_cache + first + 1, sha1_size_cache + first,
316                         (sha1_size_cache_nr - first - 1) *
317                         sizeof(*sha1_size_cache));
318         e = xmalloc(sizeof(struct sha1_size_cache));
319         sha1_size_cache[first] = e;
320         memcpy(e->sha1, sha1, 20);
321         e->size = size;
322         return e;
323 }
324
325 /*
326  * While doing rename detection and pickaxe operation, we may need to
327  * grab the data for the blob (or file) for our own in-core comparison.
328  * diff_filespec has data and size fields for this purpose.
329  */
330 int diff_populate_filespec(struct diff_filespec *s, int size_only)
331 {
332         int err = 0;
333         if (!DIFF_FILE_VALID(s))
334                 die("internal error: asking to populate invalid file.");
335         if (S_ISDIR(s->mode))
336                 return -1;
337
338         if (!use_size_cache)
339                 size_only = 0;
340
341         if (s->data)
342                 return err;
343         if (!s->sha1_valid ||
344             work_tree_matches(s->path, s->sha1)) {
345                 struct stat st;
346                 int fd;
347                 if (lstat(s->path, &st) < 0) {
348                         if (errno == ENOENT) {
349                         err_empty:
350                                 err = -1;
351                         empty:
352                                 s->data = "";
353                                 s->size = 0;
354                                 return err;
355                         }
356                 }
357                 s->size = st.st_size;
358                 if (!s->size)
359                         goto empty;
360                 if (size_only)
361                         return 0;
362                 if (S_ISLNK(st.st_mode)) {
363                         int ret;
364                         s->data = xmalloc(s->size);
365                         s->should_free = 1;
366                         ret = readlink(s->path, s->data, s->size);
367                         if (ret < 0) {
368                                 free(s->data);
369                                 goto err_empty;
370                         }
371                         return 0;
372                 }
373                 fd = open(s->path, O_RDONLY);
374                 if (fd < 0)
375                         goto err_empty;
376                 s->data = mmap(NULL, s->size, PROT_READ, MAP_PRIVATE, fd, 0);
377                 close(fd);
378                 if (s->data == MAP_FAILED)
379                         goto err_empty;
380                 s->should_munmap = 1;
381         }
382         else {
383                 char type[20];
384                 struct sha1_size_cache *e;
385
386                 if (size_only) {
387                         e = locate_size_cache(s->sha1, 1, 0);
388                         if (e) {
389                                 s->size = e->size;
390                                 return 0;
391                         }
392                         if (!sha1_object_info(s->sha1, type, &s->size))
393                                 locate_size_cache(s->sha1, 0, s->size);
394                 }
395                 else {
396                         s->data = read_sha1_file(s->sha1, type, &s->size);
397                         s->should_free = 1;
398                 }
399         }
400         return 0;
401 }
402
403 void diff_free_filespec_data(struct diff_filespec *s)
404 {
405         if (s->should_free)
406                 free(s->data);
407         else if (s->should_munmap)
408                 munmap(s->data, s->size);
409         s->should_free = s->should_munmap = 0;
410         s->data = NULL;
411 }
412
413 static void prep_temp_blob(struct diff_tempfile *temp,
414                            void *blob,
415                            unsigned long size,
416                            const unsigned char *sha1,
417                            int mode)
418 {
419         int fd;
420
421         fd = git_mkstemp(temp->tmp_path, TEMPFILE_PATH_LEN, ".diff_XXXXXX");
422         if (fd < 0)
423                 die("unable to create temp-file");
424         if (write(fd, blob, size) != size)
425                 die("unable to write temp-file");
426         close(fd);
427         temp->name = temp->tmp_path;
428         strcpy(temp->hex, sha1_to_hex(sha1));
429         temp->hex[40] = 0;
430         sprintf(temp->mode, "%06o", mode);
431 }
432
433 static void prepare_temp_file(const char *name,
434                               struct diff_tempfile *temp,
435                               struct diff_filespec *one)
436 {
437         if (!DIFF_FILE_VALID(one)) {
438         not_a_valid_file:
439                 /* A '-' entry produces this for file-2, and
440                  * a '+' entry produces this for file-1.
441                  */
442                 temp->name = "/dev/null";
443                 strcpy(temp->hex, ".");
444                 strcpy(temp->mode, ".");
445                 return;
446         }
447
448         if (!one->sha1_valid ||
449             work_tree_matches(name, one->sha1)) {
450                 struct stat st;
451                 if (lstat(name, &st) < 0) {
452                         if (errno == ENOENT)
453                                 goto not_a_valid_file;
454                         die("stat(%s): %s", name, strerror(errno));
455                 }
456                 if (S_ISLNK(st.st_mode)) {
457                         int ret;
458                         char *buf, buf_[1024];
459                         buf = ((sizeof(buf_) < st.st_size) ?
460                                xmalloc(st.st_size) : buf_);
461                         ret = readlink(name, buf, st.st_size);
462                         if (ret < 0)
463                                 die("readlink(%s)", name);
464                         prep_temp_blob(temp, buf, st.st_size,
465                                        (one->sha1_valid ?
466                                         one->sha1 : null_sha1),
467                                        (one->sha1_valid ?
468                                         one->mode : S_IFLNK));
469                 }
470                 else {
471                         /* we can borrow from the file in the work tree */
472                         temp->name = name;
473                         if (!one->sha1_valid)
474                                 strcpy(temp->hex, sha1_to_hex(null_sha1));
475                         else
476                                 strcpy(temp->hex, sha1_to_hex(one->sha1));
477                         /* Even though we may sometimes borrow the
478                          * contents from the work tree, we always want
479                          * one->mode.  mode is trustworthy even when
480                          * !(one->sha1_valid), as long as
481                          * DIFF_FILE_VALID(one).
482                          */
483                         sprintf(temp->mode, "%06o", one->mode);
484                 }
485                 return;
486         }
487         else {
488                 if (diff_populate_filespec(one, 0))
489                         die("cannot read data blob for %s", one->path);
490                 prep_temp_blob(temp, one->data, one->size,
491                                one->sha1, one->mode);
492         }
493 }
494
495 static void remove_tempfile(void)
496 {
497         int i;
498
499         for (i = 0; i < 2; i++)
500                 if (diff_temp[i].name == diff_temp[i].tmp_path) {
501                         unlink(diff_temp[i].name);
502                         diff_temp[i].name = NULL;
503                 }
504 }
505
506 static void remove_tempfile_on_signal(int signo)
507 {
508         remove_tempfile();
509 }
510
511 /* An external diff command takes:
512  *
513  * diff-cmd name infile1 infile1-sha1 infile1-mode \
514  *               infile2 infile2-sha1 infile2-mode [ rename-to ]
515  *
516  */
517 static void run_external_diff(const char *pgm,
518                               const char *name,
519                               const char *other,
520                               struct diff_filespec *one,
521                               struct diff_filespec *two,
522                               const char *xfrm_msg,
523                               int complete_rewrite)
524 {
525         struct diff_tempfile *temp = diff_temp;
526         pid_t pid;
527         int status;
528         static int atexit_asked = 0;
529         const char *othername;
530
531         othername = (other? other : name);
532         if (one && two) {
533                 prepare_temp_file(name, &temp[0], one);
534                 prepare_temp_file(othername, &temp[1], two);
535                 if (! atexit_asked &&
536                     (temp[0].name == temp[0].tmp_path ||
537                      temp[1].name == temp[1].tmp_path)) {
538                         atexit_asked = 1;
539                         atexit(remove_tempfile);
540                 }
541                 signal(SIGINT, remove_tempfile_on_signal);
542         }
543
544         fflush(NULL);
545         pid = fork();
546         if (pid < 0)
547                 die("unable to fork");
548         if (!pid) {
549                 if (pgm) {
550                         if (one && two) {
551                                 const char *exec_arg[10];
552                                 const char **arg = &exec_arg[0];
553                                 *arg++ = pgm;
554                                 *arg++ = name;
555                                 *arg++ = temp[0].name;
556                                 *arg++ = temp[0].hex;
557                                 *arg++ = temp[0].mode;
558                                 *arg++ = temp[1].name;
559                                 *arg++ = temp[1].hex;
560                                 *arg++ = temp[1].mode;
561                                 if (other) {
562                                         *arg++ = other;
563                                         *arg++ = xfrm_msg;
564                                 }
565                                 *arg = NULL;
566                                 execvp(pgm, (char *const*) exec_arg);
567                         }
568                         else
569                                 execlp(pgm, pgm, name, NULL);
570                 }
571                 /*
572                  * otherwise we use the built-in one.
573                  */
574                 if (one && two)
575                         builtin_diff(name, othername, temp, xfrm_msg,
576                                      complete_rewrite);
577                 else
578                         printf("* Unmerged path %s\n", name);
579                 exit(0);
580         }
581         if (waitpid(pid, &status, 0) < 0 ||
582             !WIFEXITED(status) || WEXITSTATUS(status)) {
583                 /* Earlier we did not check the exit status because
584                  * diff exits non-zero if files are different, and
585                  * we are not interested in knowing that.  It was a
586                  * mistake which made it harder to quit a diff-*
587                  * session that uses the git-apply-patch-script as
588                  * the GIT_EXTERNAL_DIFF.  A custom GIT_EXTERNAL_DIFF
589                  * should also exit non-zero only when it wants to
590                  * abort the entire diff-* session.
591                  */
592                 remove_tempfile();
593                 fprintf(stderr, "external diff died, stopping at %s.\n", name);
594                 exit(1);
595         }
596         remove_tempfile();
597 }
598
599 static void diff_fill_sha1_info(struct diff_filespec *one)
600 {
601         if (DIFF_FILE_VALID(one)) {
602                 if (!one->sha1_valid) {
603                         struct stat st;
604                         if (stat(one->path, &st) < 0)
605                                 die("stat %s", one->path);
606                         if (index_path(one->sha1, one->path, &st, 0))
607                                 die("cannot hash %s\n", one->path);
608                 }
609         }
610         else
611                 memset(one->sha1, 0, 20);
612 }
613
614 static void run_diff(struct diff_filepair *p)
615 {
616         const char *pgm = external_diff();
617         char msg[PATH_MAX*2+300], *xfrm_msg;
618         struct diff_filespec *one;
619         struct diff_filespec *two;
620         const char *name;
621         const char *other;
622         int complete_rewrite = 0;
623         int len;
624
625         if (DIFF_PAIR_UNMERGED(p)) {
626                 /* unmerged */
627                 run_external_diff(pgm, p->one->path, NULL, NULL, NULL, NULL,
628                                   0);
629                 return;
630         }
631
632         name = p->one->path;
633         other = (strcmp(name, p->two->path) ? p->two->path : NULL);
634         one = p->one; two = p->two;
635
636         diff_fill_sha1_info(one);
637         diff_fill_sha1_info(two);
638
639         len = 0;
640         switch (p->status) {
641         case DIFF_STATUS_COPIED:
642                 len += snprintf(msg + len, sizeof(msg) - len,
643                                 "similarity index %d%%\n"
644                                 "copy from %s\n"
645                                 "copy to %s\n",
646                                 (int)(0.5 + p->score * 100.0/MAX_SCORE),
647                                 name, other);
648                 break;
649         case DIFF_STATUS_RENAMED:
650                 len += snprintf(msg + len, sizeof(msg) - len,
651                                 "similarity index %d%%\n"
652                                 "rename from %s\n"
653                                 "rename to %s\n",
654                                 (int)(0.5 + p->score * 100.0/MAX_SCORE),
655                                 name, other);
656                 break;
657         case DIFF_STATUS_MODIFIED:
658                 if (p->score) {
659                         len += snprintf(msg + len, sizeof(msg) - len,
660                                         "dissimilarity index %d%%\n",
661                                         (int)(0.5 + p->score *
662                                               100.0/MAX_SCORE));
663                         complete_rewrite = 1;
664                         break;
665                 }
666                 /* fallthru */
667         default:
668                 /* nothing */
669                 ;
670         }
671
672         if (memcmp(one->sha1, two->sha1, 20)) {
673                 char one_sha1[41];
674                 memcpy(one_sha1, sha1_to_hex(one->sha1), 41);
675
676                 len += snprintf(msg + len, sizeof(msg) - len,
677                                 "index %.7s..%.7s", one_sha1,
678                                 sha1_to_hex(two->sha1));
679                 if (one->mode == two->mode)
680                         len += snprintf(msg + len, sizeof(msg) - len,
681                                         " %06o", one->mode);
682                 len += snprintf(msg + len, sizeof(msg) - len, "\n");
683         }
684
685         if (len)
686                 msg[--len] = 0;
687         xfrm_msg = len ? msg : NULL;
688
689         if (!pgm &&
690             DIFF_FILE_VALID(one) && DIFF_FILE_VALID(two) &&
691             (S_IFMT & one->mode) != (S_IFMT & two->mode)) {
692                 /* a filepair that changes between file and symlink
693                  * needs to be split into deletion and creation.
694                  */
695                 struct diff_filespec *null = alloc_filespec(two->path);
696                 run_external_diff(NULL, name, other, one, null, xfrm_msg, 0);
697                 free(null);
698                 null = alloc_filespec(one->path);
699                 run_external_diff(NULL, name, other, null, two, xfrm_msg, 0);
700                 free(null);
701         }
702         else
703                 run_external_diff(pgm, name, other, one, two, xfrm_msg,
704                                   complete_rewrite);
705 }
706
707 void diff_setup(struct diff_options *options)
708 {
709         memset(options, 0, sizeof(*options));
710         options->output_format = DIFF_FORMAT_RAW;
711         options->line_termination = '\n';
712         options->break_opt = -1;
713         options->rename_limit = -1;
714 }
715
716 int diff_setup_done(struct diff_options *options)
717 {
718         if ((options->find_copies_harder || 0 <= options->rename_limit) &&
719             options->detect_rename != DIFF_DETECT_COPY)
720                 return -1;
721         if (options->setup & DIFF_SETUP_USE_CACHE) {
722                 if (!active_cache)
723                         /* read-cache does not die even when it fails
724                          * so it is safe for us to do this here.  Also
725                          * it does not smudge active_cache or active_nr
726                          * when it fails, so we do not have to worry about
727                          * cleaning it up oufselves either.
728                          */
729                         read_cache();
730         }
731         if (options->setup & DIFF_SETUP_USE_SIZE_CACHE)
732                 use_size_cache = 1;
733
734         return 0;
735 }
736
737 int diff_opt_parse(struct diff_options *options, const char **av, int ac)
738 {
739         const char *arg = av[0];
740         if (!strcmp(arg, "-p") || !strcmp(arg, "-u"))
741                 options->output_format = DIFF_FORMAT_PATCH;
742         else if (!strcmp(arg, "-z"))
743                 options->line_termination = 0;
744         else if (!strncmp(arg, "-l", 2))
745                 options->rename_limit = strtoul(arg+2, NULL, 10);
746         else if (!strcmp(arg, "--name-only"))
747                 options->output_format = DIFF_FORMAT_NAME;
748         else if (!strcmp(arg, "--name-status"))
749                 options->output_format = DIFF_FORMAT_NAME_STATUS;
750         else if (!strcmp(arg, "-R"))
751                 options->reverse_diff = 1;
752         else if (!strncmp(arg, "-S", 2))
753                 options->pickaxe = arg + 2;
754         else if (!strcmp(arg, "-s"))
755                 options->output_format = DIFF_FORMAT_NO_OUTPUT;
756         else if (!strncmp(arg, "-O", 2))
757                 options->orderfile = arg + 2;
758         else if (!strncmp(arg, "--diff-filter=", 14))
759                 options->filter = arg + 14;
760         else if (!strcmp(arg, "--pickaxe-all"))
761                 options->pickaxe_opts = DIFF_PICKAXE_ALL;
762         else if (!strncmp(arg, "-B", 2)) {
763                 if ((options->break_opt =
764                      diff_scoreopt_parse(arg)) == -1)
765                         return -1;
766         }
767         else if (!strncmp(arg, "-M", 2)) {
768                 if ((options->rename_score =
769                      diff_scoreopt_parse(arg)) == -1)
770                         return -1;
771                 options->detect_rename = DIFF_DETECT_RENAME;
772         }
773         else if (!strncmp(arg, "-C", 2)) {
774                 if ((options->rename_score =
775                      diff_scoreopt_parse(arg)) == -1)
776                         return -1;
777                 options->detect_rename = DIFF_DETECT_COPY;
778         }
779         else if (!strcmp(arg, "--find-copies-harder"))
780                 options->find_copies_harder = 1;
781         else
782                 return 0;
783         return 1;
784 }
785
786 static int parse_num(const char **cp_p)
787 {
788         int num, scale, ch, cnt;
789         const char *cp = *cp_p;
790
791         cnt = num = 0;
792         scale = 1;
793         while ('0' <= (ch = *cp) && ch <= '9') {
794                 if (cnt++ < 5) {
795                         /* We simply ignore more than 5 digits precision. */
796                         scale *= 10;
797                         num = num * 10 + ch - '0';
798                 }
799                 cp++;
800         }
801         *cp_p = cp;
802
803         /* user says num divided by scale and we say internally that
804          * is MAX_SCORE * num / scale.
805          */
806         return (MAX_SCORE * num / scale);
807 }
808
809 int diff_scoreopt_parse(const char *opt)
810 {
811         int opt1, opt2, cmd;
812
813         if (*opt++ != '-')
814                 return -1;
815         cmd = *opt++;
816         if (cmd != 'M' && cmd != 'C' && cmd != 'B')
817                 return -1; /* that is not a -M, -C nor -B option */
818
819         opt1 = parse_num(&opt);
820         if (cmd != 'B')
821                 opt2 = 0;
822         else {
823                 if (*opt == 0)
824                         opt2 = 0;
825                 else if (*opt != '/')
826                         return -1; /* we expect -B80/99 or -B80 */
827                 else {
828                         opt++;
829                         opt2 = parse_num(&opt);
830                 }
831         }
832         if (*opt != 0)
833                 return -1;
834         return opt1 | (opt2 << 16);
835 }
836
837 struct diff_queue_struct diff_queued_diff;
838
839 void diff_q(struct diff_queue_struct *queue, struct diff_filepair *dp)
840 {
841         if (queue->alloc <= queue->nr) {
842                 queue->alloc = alloc_nr(queue->alloc);
843                 queue->queue = xrealloc(queue->queue,
844                                         sizeof(dp) * queue->alloc);
845         }
846         queue->queue[queue->nr++] = dp;
847 }
848
849 struct diff_filepair *diff_queue(struct diff_queue_struct *queue,
850                                  struct diff_filespec *one,
851                                  struct diff_filespec *two)
852 {
853         struct diff_filepair *dp = xmalloc(sizeof(*dp));
854         dp->one = one;
855         dp->two = two;
856         dp->score = 0;
857         dp->status = 0;
858         dp->source_stays = 0;
859         dp->broken_pair = 0;
860         if (queue)
861                 diff_q(queue, dp);
862         return dp;
863 }
864
865 void diff_free_filepair(struct diff_filepair *p)
866 {
867         diff_free_filespec_data(p->one);
868         diff_free_filespec_data(p->two);
869         free(p->one);
870         free(p->two);
871         free(p);
872 }
873
874 static void diff_flush_raw(struct diff_filepair *p,
875                            int line_termination,
876                            int inter_name_termination,
877                            int output_format)
878 {
879         int two_paths;
880         char status[10];
881
882         if (line_termination) {
883                 const char *const err =
884                         "path %s cannot be expressed without -z";
885                 if (strchr(p->one->path, line_termination) ||
886                     strchr(p->one->path, inter_name_termination))
887                         die(err, p->one->path);
888                 if (strchr(p->two->path, line_termination) ||
889                     strchr(p->two->path, inter_name_termination))
890                         die(err, p->two->path);
891         }
892
893         if (p->score)
894                 sprintf(status, "%c%03d", p->status,
895                         (int)(0.5 + p->score * 100.0/MAX_SCORE));
896         else {
897                 status[0] = p->status;
898                 status[1] = 0;
899         }
900         switch (p->status) {
901         case DIFF_STATUS_COPIED:
902         case DIFF_STATUS_RENAMED:
903                 two_paths = 1;
904                 break;
905         case DIFF_STATUS_ADDED:
906         case DIFF_STATUS_DELETED:
907                 two_paths = 0;
908                 break;
909         default:
910                 two_paths = 0;
911                 break;
912         }
913         if (output_format != DIFF_FORMAT_NAME_STATUS) {
914                 printf(":%06o %06o %s ",
915                        p->one->mode, p->two->mode, sha1_to_hex(p->one->sha1));
916                 printf("%s ", sha1_to_hex(p->two->sha1));
917         }
918         printf("%s%c%s",status, inter_name_termination, p->one->path);
919         if (two_paths)
920                 printf("%c%s", inter_name_termination, p->two->path);
921         putchar(line_termination);
922 }
923
924 static void diff_flush_name(struct diff_filepair *p,
925                             int line_termination)
926 {
927         printf("%s%c", p->two->path, line_termination);
928 }
929
930 int diff_unmodified_pair(struct diff_filepair *p)
931 {
932         /* This function is written stricter than necessary to support
933          * the currently implemented transformers, but the idea is to
934          * let transformers to produce diff_filepairs any way they want,
935          * and filter and clean them up here before producing the output.
936          */
937         struct diff_filespec *one, *two;
938
939         if (DIFF_PAIR_UNMERGED(p))
940                 return 0; /* unmerged is interesting */
941
942         one = p->one;
943         two = p->two;
944
945         /* deletion, addition, mode or type change
946          * and rename are all interesting.
947          */
948         if (DIFF_FILE_VALID(one) != DIFF_FILE_VALID(two) ||
949             DIFF_PAIR_MODE_CHANGED(p) ||
950             strcmp(one->path, two->path))
951                 return 0;
952
953         /* both are valid and point at the same path.  that is, we are
954          * dealing with a change.
955          */
956         if (one->sha1_valid && two->sha1_valid &&
957             !memcmp(one->sha1, two->sha1, sizeof(one->sha1)))
958                 return 1; /* no change */
959         if (!one->sha1_valid && !two->sha1_valid)
960                 return 1; /* both look at the same file on the filesystem. */
961         return 0;
962 }
963
964 static void diff_flush_patch(struct diff_filepair *p)
965 {
966         if (diff_unmodified_pair(p))
967                 return;
968
969         if ((DIFF_FILE_VALID(p->one) && S_ISDIR(p->one->mode)) ||
970             (DIFF_FILE_VALID(p->two) && S_ISDIR(p->two->mode)))
971                 return; /* no tree diffs in patch format */ 
972
973         run_diff(p);
974 }
975
976 int diff_queue_is_empty(void)
977 {
978         struct diff_queue_struct *q = &diff_queued_diff;
979         int i;
980         for (i = 0; i < q->nr; i++)
981                 if (!diff_unmodified_pair(q->queue[i]))
982                         return 0;
983         return 1;
984 }
985
986 #if DIFF_DEBUG
987 void diff_debug_filespec(struct diff_filespec *s, int x, const char *one)
988 {
989         fprintf(stderr, "queue[%d] %s (%s) %s %06o %s\n",
990                 x, one ? one : "",
991                 s->path,
992                 DIFF_FILE_VALID(s) ? "valid" : "invalid",
993                 s->mode,
994                 s->sha1_valid ? sha1_to_hex(s->sha1) : "");
995         fprintf(stderr, "queue[%d] %s size %lu flags %d\n",
996                 x, one ? one : "",
997                 s->size, s->xfrm_flags);
998 }
999
1000 void diff_debug_filepair(const struct diff_filepair *p, int i)
1001 {
1002         diff_debug_filespec(p->one, i, "one");
1003         diff_debug_filespec(p->two, i, "two");
1004         fprintf(stderr, "score %d, status %c stays %d broken %d\n",
1005                 p->score, p->status ? p->status : '?',
1006                 p->source_stays, p->broken_pair);
1007 }
1008
1009 void diff_debug_queue(const char *msg, struct diff_queue_struct *q)
1010 {
1011         int i;
1012         if (msg)
1013                 fprintf(stderr, "%s\n", msg);
1014         fprintf(stderr, "q->nr = %d\n", q->nr);
1015         for (i = 0; i < q->nr; i++) {
1016                 struct diff_filepair *p = q->queue[i];
1017                 diff_debug_filepair(p, i);
1018         }
1019 }
1020 #endif
1021
1022 static void diff_resolve_rename_copy(void)
1023 {
1024         int i, j;
1025         struct diff_filepair *p, *pp;
1026         struct diff_queue_struct *q = &diff_queued_diff;
1027
1028         diff_debug_queue("resolve-rename-copy", q);
1029
1030         for (i = 0; i < q->nr; i++) {
1031                 p = q->queue[i];
1032                 p->status = 0; /* undecided */
1033                 if (DIFF_PAIR_UNMERGED(p))
1034                         p->status = DIFF_STATUS_UNMERGED;
1035                 else if (!DIFF_FILE_VALID(p->one))
1036                         p->status = DIFF_STATUS_ADDED;
1037                 else if (!DIFF_FILE_VALID(p->two))
1038                         p->status = DIFF_STATUS_DELETED;
1039                 else if (DIFF_PAIR_TYPE_CHANGED(p))
1040                         p->status = DIFF_STATUS_TYPE_CHANGED;
1041
1042                 /* from this point on, we are dealing with a pair
1043                  * whose both sides are valid and of the same type, i.e.
1044                  * either in-place edit or rename/copy edit.
1045                  */
1046                 else if (DIFF_PAIR_RENAME(p)) {
1047                         if (p->source_stays) {
1048                                 p->status = DIFF_STATUS_COPIED;
1049                                 continue;
1050                         }
1051                         /* See if there is some other filepair that
1052                          * copies from the same source as us.  If so
1053                          * we are a copy.  Otherwise we are either a
1054                          * copy if the path stays, or a rename if it
1055                          * does not, but we already handled "stays" case.
1056                          */
1057                         for (j = i + 1; j < q->nr; j++) {
1058                                 pp = q->queue[j];
1059                                 if (strcmp(pp->one->path, p->one->path))
1060                                         continue; /* not us */
1061                                 if (!DIFF_PAIR_RENAME(pp))
1062                                         continue; /* not a rename/copy */
1063                                 /* pp is a rename/copy from the same source */
1064                                 p->status = DIFF_STATUS_COPIED;
1065                                 break;
1066                         }
1067                         if (!p->status)
1068                                 p->status = DIFF_STATUS_RENAMED;
1069                 }
1070                 else if (memcmp(p->one->sha1, p->two->sha1, 20) ||
1071                          p->one->mode != p->two->mode)
1072                         p->status = DIFF_STATUS_MODIFIED;
1073                 else {
1074                         /* This is a "no-change" entry and should not
1075                          * happen anymore, but prepare for broken callers.
1076                          */
1077                         error("feeding unmodified %s to diffcore",
1078                               p->one->path);
1079                         p->status = DIFF_STATUS_UNKNOWN;
1080                 }
1081         }
1082         diff_debug_queue("resolve-rename-copy done", q);
1083 }
1084
1085 void diff_flush(struct diff_options *options)
1086 {
1087         struct diff_queue_struct *q = &diff_queued_diff;
1088         int i;
1089         int inter_name_termination = '\t';
1090         int diff_output_format = options->output_format;
1091         int line_termination = options->line_termination;
1092
1093         if (!line_termination)
1094                 inter_name_termination = 0;
1095
1096         for (i = 0; i < q->nr; i++) {
1097                 struct diff_filepair *p = q->queue[i];
1098                 if ((diff_output_format == DIFF_FORMAT_NO_OUTPUT) ||
1099                     (p->status == DIFF_STATUS_UNKNOWN))
1100                         continue;
1101                 if (p->status == 0)
1102                         die("internal error in diff-resolve-rename-copy");
1103                 switch (diff_output_format) {
1104                 case DIFF_FORMAT_PATCH:
1105                         diff_flush_patch(p);
1106                         break;
1107                 case DIFF_FORMAT_RAW:
1108                 case DIFF_FORMAT_NAME_STATUS:
1109                         diff_flush_raw(p, line_termination,
1110                                        inter_name_termination,
1111                                        diff_output_format);
1112                         break;
1113                 case DIFF_FORMAT_NAME:
1114                         diff_flush_name(p, line_termination);
1115                         break;
1116                 }
1117                 diff_free_filepair(q->queue[i]);
1118         }
1119         free(q->queue);
1120         q->queue = NULL;
1121         q->nr = q->alloc = 0;
1122 }
1123
1124 static void diffcore_apply_filter(const char *filter)
1125 {
1126         int i;
1127         struct diff_queue_struct *q = &diff_queued_diff;
1128         struct diff_queue_struct outq;
1129         outq.queue = NULL;
1130         outq.nr = outq.alloc = 0;
1131
1132         if (!filter)
1133                 return;
1134
1135         if (strchr(filter, DIFF_STATUS_FILTER_AON)) {
1136                 int found;
1137                 for (i = found = 0; !found && i < q->nr; i++) {
1138                         struct diff_filepair *p = q->queue[i];
1139                         if (((p->status == DIFF_STATUS_MODIFIED) &&
1140                              ((p->score &&
1141                                strchr(filter, DIFF_STATUS_FILTER_BROKEN)) ||
1142                               (!p->score &&
1143                                strchr(filter, DIFF_STATUS_MODIFIED)))) ||
1144                             ((p->status != DIFF_STATUS_MODIFIED) &&
1145                              strchr(filter, p->status)))
1146                                 found++;
1147                 }
1148                 if (found)
1149                         return;
1150
1151                 /* otherwise we will clear the whole queue
1152                  * by copying the empty outq at the end of this
1153                  * function, but first clear the current entries
1154                  * in the queue.
1155                  */
1156                 for (i = 0; i < q->nr; i++)
1157                         diff_free_filepair(q->queue[i]);
1158         }
1159         else {
1160                 /* Only the matching ones */
1161                 for (i = 0; i < q->nr; i++) {
1162                         struct diff_filepair *p = q->queue[i];
1163
1164                         if (((p->status == DIFF_STATUS_MODIFIED) &&
1165                              ((p->score &&
1166                                strchr(filter, DIFF_STATUS_FILTER_BROKEN)) ||
1167                               (!p->score &&
1168                                strchr(filter, DIFF_STATUS_MODIFIED)))) ||
1169                             ((p->status != DIFF_STATUS_MODIFIED) &&
1170                              strchr(filter, p->status)))
1171                                 diff_q(&outq, p);
1172                         else
1173                                 diff_free_filepair(p);
1174                 }
1175         }
1176         free(q->queue);
1177         *q = outq;
1178 }
1179
1180 void diffcore_std(struct diff_options *options)
1181 {
1182         if (options->paths && options->paths[0])
1183                 diffcore_pathspec(options->paths);
1184         if (options->break_opt != -1)
1185                 diffcore_break(options->break_opt);
1186         if (options->detect_rename)
1187                 diffcore_rename(options);
1188         if (options->break_opt != -1)
1189                 diffcore_merge_broken();
1190         if (options->pickaxe)
1191                 diffcore_pickaxe(options->pickaxe, options->pickaxe_opts);
1192         if (options->orderfile)
1193                 diffcore_order(options->orderfile);
1194         diff_resolve_rename_copy();
1195         diffcore_apply_filter(options->filter);
1196 }
1197
1198
1199 void diffcore_std_no_resolve(struct diff_options *options)
1200 {
1201         if (options->pickaxe)
1202                 diffcore_pickaxe(options->pickaxe, options->pickaxe_opts);
1203         if (options->orderfile)
1204                 diffcore_order(options->orderfile);
1205         diffcore_apply_filter(options->filter);
1206 }
1207
1208 void diff_addremove(struct diff_options *options,
1209                     int addremove, unsigned mode,
1210                     const unsigned char *sha1,
1211                     const char *base, const char *path)
1212 {
1213         char concatpath[PATH_MAX];
1214         struct diff_filespec *one, *two;
1215
1216         /* This may look odd, but it is a preparation for
1217          * feeding "there are unchanged files which should
1218          * not produce diffs, but when you are doing copy
1219          * detection you would need them, so here they are"
1220          * entries to the diff-core.  They will be prefixed
1221          * with something like '=' or '*' (I haven't decided
1222          * which but should not make any difference).
1223          * Feeding the same new and old to diff_change() 
1224          * also has the same effect.
1225          * Before the final output happens, they are pruned after
1226          * merged into rename/copy pairs as appropriate.
1227          */
1228         if (options->reverse_diff)
1229                 addremove = (addremove == '+' ? '-' :
1230                              addremove == '-' ? '+' : addremove);
1231
1232         if (!path) path = "";
1233         sprintf(concatpath, "%s%s", base, path);
1234         one = alloc_filespec(concatpath);
1235         two = alloc_filespec(concatpath);
1236
1237         if (addremove != '+')
1238                 fill_filespec(one, sha1, mode);
1239         if (addremove != '-')
1240                 fill_filespec(two, sha1, mode);
1241
1242         diff_queue(&diff_queued_diff, one, two);
1243 }
1244
1245 void diff_change(struct diff_options *options,
1246                  unsigned old_mode, unsigned new_mode,
1247                  const unsigned char *old_sha1,
1248                  const unsigned char *new_sha1,
1249                  const char *base, const char *path) 
1250 {
1251         char concatpath[PATH_MAX];
1252         struct diff_filespec *one, *two;
1253
1254         if (options->reverse_diff) {
1255                 unsigned tmp;
1256                 const unsigned char *tmp_c;
1257                 tmp = old_mode; old_mode = new_mode; new_mode = tmp;
1258                 tmp_c = old_sha1; old_sha1 = new_sha1; new_sha1 = tmp_c;
1259         }
1260         if (!path) path = "";
1261         sprintf(concatpath, "%s%s", base, path);
1262         one = alloc_filespec(concatpath);
1263         two = alloc_filespec(concatpath);
1264         fill_filespec(one, old_sha1, old_mode);
1265         fill_filespec(two, new_sha1, new_mode);
1266
1267         diff_queue(&diff_queued_diff, one, two);
1268 }
1269
1270 void diff_unmerge(struct diff_options *options,
1271                   const char *path)
1272 {
1273         struct diff_filespec *one, *two;
1274         one = alloc_filespec(path);
1275         two = alloc_filespec(path);
1276         diff_queue(&diff_queued_diff, one, two);
1277 }