[PATCH] Diff overhaul, adding half of copy detection.
[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 <limits.h>
8 #include "cache.h"
9 #include "diff.h"
10 #include "diffcore.h"
11
12 static const char *diff_opts = "-pu";
13 static unsigned char null_sha1[20] = { 0, };
14
15 static int detect_rename;
16 static int reverse_diff;
17 static int diff_raw_output = -1;
18 static const char **pathspec;
19 static int speccnt;
20 static int minimum_score;
21
22 static const char *external_diff(void)
23 {
24         static const char *external_diff_cmd = NULL;
25         static int done_preparing = 0;
26
27         if (done_preparing)
28                 return external_diff_cmd;
29
30         /*
31          * Default values above are meant to match the
32          * Linux kernel development style.  Examples of
33          * alternative styles you can specify via environment
34          * variables are:
35          *
36          * GIT_DIFF_OPTS="-c";
37          */
38         if (gitenv("GIT_EXTERNAL_DIFF"))
39                 external_diff_cmd = gitenv("GIT_EXTERNAL_DIFF");
40
41         /* In case external diff fails... */
42         diff_opts = gitenv("GIT_DIFF_OPTS") ? : diff_opts;
43
44         done_preparing = 1;
45         return external_diff_cmd;
46 }
47
48 /* Help to copy the thing properly quoted for the shell safety.
49  * any single quote is replaced with '\'', and the caller is
50  * expected to enclose the result within a single quote pair.
51  *
52  * E.g.
53  *  original     sq_expand     result
54  *  name     ==> name      ==> 'name'
55  *  a b      ==> a b       ==> 'a b'
56  *  a'b      ==> a'\''b    ==> 'a'\''b'
57  */
58 static char *sq_expand(const char *src)
59 {
60         static char *buf = NULL;
61         int cnt, c;
62         const char *cp;
63         char *bp;
64
65         /* count bytes needed to store the quoted string. */
66         for (cnt = 1, cp = src; *cp; cnt++, cp++)
67                 if (*cp == '\'')
68                         cnt += 3;
69
70         buf = xmalloc(cnt);
71         bp = buf;
72         while ((c = *src++)) {
73                 if (c != '\'')
74                         *bp++ = c;
75                 else {
76                         bp = strcpy(bp, "'\\''");
77                         bp += 4;
78                 }
79         }
80         *bp = 0;
81         return buf;
82 }
83
84 static struct diff_tempfile {
85         const char *name; /* filename external diff should read from */
86         char hex[41];
87         char mode[10];
88         char tmp_path[50];
89 } diff_temp[2];
90
91 static void builtin_diff(const char *name_a,
92                          const char *name_b,
93                          struct diff_tempfile *temp,
94                          const char *xfrm_msg)
95 {
96         int i, next_at, cmd_size;
97         const char *diff_cmd = "diff -L'%s%s' -L'%s%s'";
98         const char *diff_arg  = "'%s' '%s'||:"; /* "||:" is to return 0 */
99         const char *input_name_sq[2];
100         const char *path0[2];
101         const char *path1[2];
102         const char *name_sq[2];
103         char *cmd;
104
105         name_sq[0] = sq_expand(name_a);
106         name_sq[1] = sq_expand(name_b);
107
108         /* diff_cmd and diff_arg have 6 %s in total which makes
109          * the sum of these strings 12 bytes larger than required.
110          * we use 2 spaces around diff-opts, and we need to count
111          * terminating NUL, so we subtract 9 here.
112          */
113         cmd_size = (strlen(diff_cmd) + strlen(diff_opts) +
114                         strlen(diff_arg) - 9);
115         for (i = 0; i < 2; i++) {
116                 input_name_sq[i] = sq_expand(temp[i].name);
117                 if (!strcmp(temp[i].name, "/dev/null")) {
118                         path0[i] = "/dev/null";
119                         path1[i] = "";
120                 } else {
121                         path0[i] = i ? "b/" : "a/";
122                         path1[i] = name_sq[i];
123                 }
124                 cmd_size += (strlen(path0[i]) + strlen(path1[i]) +
125                              strlen(input_name_sq[i]));
126         }
127
128         cmd = xmalloc(cmd_size);
129
130         next_at = 0;
131         next_at += snprintf(cmd+next_at, cmd_size-next_at,
132                             diff_cmd,
133                             path0[0], path1[0], path0[1], path1[1]);
134         next_at += snprintf(cmd+next_at, cmd_size-next_at,
135                             " %s ", diff_opts);
136         next_at += snprintf(cmd+next_at, cmd_size-next_at,
137                             diff_arg, input_name_sq[0], input_name_sq[1]);
138
139         printf("diff --git a/%s b/%s\n", name_a, name_b);
140         if (!path1[0][0])
141                 printf("new file mode %s\n", temp[1].mode);
142         else if (!path1[1][0])
143                 printf("deleted file mode %s\n", temp[0].mode);
144         else {
145                 if (strcmp(temp[0].mode, temp[1].mode)) {
146                         printf("old mode %s\n", temp[0].mode);
147                         printf("new mode %s\n", temp[1].mode);
148                 }
149                 if (xfrm_msg && xfrm_msg[0])
150                         fputs(xfrm_msg, stdout);
151
152                 if (strncmp(temp[0].mode, temp[1].mode, 3))
153                         /* we do not run diff between different kind
154                          * of objects.
155                          */
156                         exit(0);
157         }
158         fflush(NULL);
159         execlp("/bin/sh","sh", "-c", cmd, NULL);
160 }
161
162 struct diff_filespec *alloc_filespec(const char *path)
163 {
164         int namelen = strlen(path);
165         struct diff_filespec *spec = xmalloc(sizeof(*spec) + namelen + 1);
166         spec->path = (char *)(spec + 1);
167         strcpy(spec->path, path);
168         spec->should_free = spec->should_munmap = spec->file_valid = 0;
169         spec->xfrm_flags = 0;
170         spec->size = 0;
171         spec->data = 0;
172         return spec;
173 }
174
175 void fill_filespec(struct diff_filespec *spec, const unsigned char *sha1,
176                    unsigned short mode)
177 {
178         spec->mode = mode;
179         memcpy(spec->sha1, sha1, 20);
180         spec->sha1_valid = !!memcmp(sha1, null_sha1, 20);
181         spec->file_valid = 1;
182 }
183
184 /*
185  * Given a name and sha1 pair, if the dircache tells us the file in
186  * the work tree has that object contents, return true, so that
187  * prepare_temp_file() does not have to inflate and extract.
188  */
189 static int work_tree_matches(const char *name, const unsigned char *sha1)
190 {
191         struct cache_entry *ce;
192         struct stat st;
193         int pos, len;
194
195         /* We do not read the cache ourselves here, because the
196          * benchmark with my previous version that always reads cache
197          * shows that it makes things worse for diff-tree comparing
198          * two linux-2.6 kernel trees in an already checked out work
199          * tree.  This is because most diff-tree comparisons deal with
200          * only a small number of files, while reading the cache is
201          * expensive for a large project, and its cost outweighs the
202          * savings we get by not inflating the object to a temporary
203          * file.  Practically, this code only helps when we are used
204          * by diff-cache --cached, which does read the cache before
205          * calling us.
206          */
207         if (!active_cache)
208                 return 0;
209
210         len = strlen(name);
211         pos = cache_name_pos(name, len);
212         if (pos < 0)
213                 return 0;
214         ce = active_cache[pos];
215         if ((lstat(name, &st) < 0) ||
216             !S_ISREG(st.st_mode) || /* careful! */
217             ce_match_stat(ce, &st) ||
218             memcmp(sha1, ce->sha1, 20))
219                 return 0;
220         /* we return 1 only when we can stat, it is a regular file,
221          * stat information matches, and sha1 recorded in the cache
222          * matches.  I.e. we know the file in the work tree really is
223          * the same as the <name, sha1> pair.
224          */
225         return 1;
226 }
227
228 /*
229  * While doing rename detection and pickaxe operation, we may need to
230  * grab the data for the blob (or file) for our own in-core comparison.
231  * diff_filespec has data and size fields for this purpose.
232  */
233 int diff_populate_filespec(struct diff_filespec *s)
234 {
235         int err = 0;
236         if (!s->file_valid)
237                 die("internal error: asking to populate invalid file.");
238         if (S_ISDIR(s->mode))
239                 return -1;
240
241         if (s->data)
242                 return err;
243         if (!s->sha1_valid ||
244             work_tree_matches(s->path, s->sha1)) {
245                 struct stat st;
246                 int fd;
247                 if (lstat(s->path, &st) < 0) {
248                         if (errno == ENOENT) {
249                         err_empty:
250                                 err = -1;
251                         empty:
252                                 s->data = "";
253                                 s->size = 0;
254                                 return err;
255                         }
256                 }
257                 s->size = st.st_size;
258                 if (!s->size)
259                         goto empty;
260                 if (S_ISLNK(st.st_mode)) {
261                         int ret;
262                         s->data = xmalloc(s->size);
263                         s->should_free = 1;
264                         ret = readlink(s->path, s->data, s->size);
265                         if (ret < 0) {
266                                 free(s->data);
267                                 goto err_empty;
268                         }
269                         return 0;
270                 }
271                 fd = open(s->path, O_RDONLY);
272                 if (fd < 0)
273                         goto err_empty;
274                 s->data = mmap(NULL, s->size, PROT_READ, MAP_PRIVATE, fd, 0);
275                 s->should_munmap = 1;
276                 close(fd);
277         }
278         else {
279                 char type[20];
280                 s->data = read_sha1_file(s->sha1, type, &s->size);
281                 s->should_free = 1;
282         }
283         return 0;
284 }
285
286 void diff_free_filespec_data(struct diff_filespec *s)
287 {
288         if (s->should_free)
289                 free(s->data);
290         else if (s->should_munmap)
291                 munmap(s->data, s->size);
292         s->should_free = s->should_munmap = 0;
293         s->data = 0;
294 }
295
296 static void prep_temp_blob(struct diff_tempfile *temp,
297                            void *blob,
298                            unsigned long size,
299                            unsigned char *sha1,
300                            int mode)
301 {
302         int fd;
303
304         strcpy(temp->tmp_path, ".diff_XXXXXX");
305         fd = mkstemp(temp->tmp_path);
306         if (fd < 0)
307                 die("unable to create temp-file");
308         if (write(fd, blob, size) != size)
309                 die("unable to write temp-file");
310         close(fd);
311         temp->name = temp->tmp_path;
312         strcpy(temp->hex, sha1_to_hex(sha1));
313         temp->hex[40] = 0;
314         sprintf(temp->mode, "%06o", mode);
315 }
316
317 static void prepare_temp_file(const char *name,
318                               struct diff_tempfile *temp,
319                               struct diff_filespec *one)
320 {
321         if (!one->file_valid) {
322         not_a_valid_file:
323                 /* A '-' entry produces this for file-2, and
324                  * a '+' entry produces this for file-1.
325                  */
326                 temp->name = "/dev/null";
327                 strcpy(temp->hex, ".");
328                 strcpy(temp->mode, ".");
329                 return;
330         }
331
332         if (!one->sha1_valid ||
333             work_tree_matches(name, one->sha1)) {
334                 struct stat st;
335                 if (lstat(name, &st) < 0) {
336                         if (errno == ENOENT)
337                                 goto not_a_valid_file;
338                         die("stat(%s): %s", name, strerror(errno));
339                 }
340                 if (S_ISLNK(st.st_mode)) {
341                         int ret;
342                         char *buf, buf_[1024];
343                         buf = ((sizeof(buf_) < st.st_size) ?
344                                xmalloc(st.st_size) : buf_);
345                         ret = readlink(name, buf, st.st_size);
346                         if (ret < 0)
347                                 die("readlink(%s)", name);
348                         prep_temp_blob(temp, buf, st.st_size,
349                                        (one->sha1_valid ?
350                                         one->sha1 : null_sha1),
351                                        (one->sha1_valid ?
352                                         one->mode : S_IFLNK));
353                 }
354                 else {
355                         /* we can borrow from the file in the work tree */
356                         temp->name = name;
357                         if (!one->sha1_valid)
358                                 strcpy(temp->hex, sha1_to_hex(null_sha1));
359                         else
360                                 strcpy(temp->hex, sha1_to_hex(one->sha1));
361                         sprintf(temp->mode, "%06o",
362                                 S_IFREG |ce_permissions(st.st_mode));
363                 }
364                 return;
365         }
366         else {
367                 if (diff_populate_filespec(one))
368                         die("cannot read data blob for %s", one->path);
369                 prep_temp_blob(temp, one->data, one->size,
370                                one->sha1, one->mode);
371         }
372 }
373
374 static void remove_tempfile(void)
375 {
376         int i;
377
378         for (i = 0; i < 2; i++)
379                 if (diff_temp[i].name == diff_temp[i].tmp_path) {
380                         unlink(diff_temp[i].name);
381                         diff_temp[i].name = NULL;
382                 }
383 }
384
385 static void remove_tempfile_on_signal(int signo)
386 {
387         remove_tempfile();
388 }
389
390 static int matches_pathspec(const char *name)
391 {
392         int i;
393         int namelen;
394
395         if (speccnt == 0)
396                 return 1;
397
398         namelen = strlen(name);
399         for (i = 0; i < speccnt; i++) {
400                 int speclen = strlen(pathspec[i]);
401                 if (! strncmp(pathspec[i], name, speclen) &&
402                     speclen <= namelen &&
403                     (name[speclen] == 0 || name[speclen] == '/'))
404                         return 1;
405         }
406         return 0;
407 }
408
409 /* An external diff command takes:
410  *
411  * diff-cmd name infile1 infile1-sha1 infile1-mode \
412  *               infile2 infile2-sha1 infile2-mode [ rename-to ]
413  *
414  */
415 static void run_external_diff(const char *name,
416                               const char *other,
417                               struct diff_filespec *one,
418                               struct diff_filespec *two,
419                               const char *xfrm_msg)
420 {
421         struct diff_tempfile *temp = diff_temp;
422         pid_t pid;
423         int status;
424         static int atexit_asked = 0;
425
426         if (!matches_pathspec(name) && (!other || !matches_pathspec(other)))
427                 return;
428
429         if (one && two) {
430                 prepare_temp_file(name, &temp[0], one);
431                 prepare_temp_file(other ? : name, &temp[1], two);
432                 if (! atexit_asked &&
433                     (temp[0].name == temp[0].tmp_path ||
434                      temp[1].name == temp[1].tmp_path)) {
435                         atexit_asked = 1;
436                         atexit(remove_tempfile);
437                 }
438                 signal(SIGINT, remove_tempfile_on_signal);
439         }
440
441         fflush(NULL);
442         pid = fork();
443         if (pid < 0)
444                 die("unable to fork");
445         if (!pid) {
446                 const char *pgm = external_diff();
447                 if (pgm) {
448                         if (one && two) {
449                                 const char *exec_arg[10];
450                                 const char **arg = &exec_arg[0];
451                                 *arg++ = pgm;
452                                 *arg++ = name;
453                                 *arg++ = temp[0].name;
454                                 *arg++ = temp[0].hex;
455                                 *arg++ = temp[0].mode;
456                                 *arg++ = temp[1].name;
457                                 *arg++ = temp[1].hex;
458                                 *arg++ = temp[1].mode;
459                                 if (other) {
460                                         *arg++ = other;
461                                         *arg++ = xfrm_msg;
462                                 }
463                                 *arg = 0;
464                                 execvp(pgm, (char *const*) exec_arg);
465                         }
466                         else
467                                 execlp(pgm, pgm, name, NULL);
468                 }
469                 /*
470                  * otherwise we use the built-in one.
471                  */
472                 if (one && two)
473                         builtin_diff(name, other ? : name, temp, xfrm_msg);
474                 else
475                         printf("* Unmerged path %s\n", name);
476                 exit(0);
477         }
478         if (waitpid(pid, &status, 0) < 0 ||
479             !WIFEXITED(status) || WEXITSTATUS(status)) {
480                 /* Earlier we did not check the exit status because
481                  * diff exits non-zero if files are different, and
482                  * we are not interested in knowing that.  It was a
483                  * mistake which made it harder to quit a diff-*
484                  * session that uses the git-apply-patch-script as
485                  * the GIT_EXTERNAL_DIFF.  A custom GIT_EXTERNAL_DIFF
486                  * should also exit non-zero only when it wants to
487                  * abort the entire diff-* session.
488                  */
489                 remove_tempfile();
490                 fprintf(stderr, "external diff died, stopping at %s.\n", name);
491                 exit(1);
492         }
493         remove_tempfile();
494 }
495
496 int diff_scoreopt_parse(const char *opt)
497 {
498         int diglen, num, scale, i;
499         if (opt[0] != '-' || (opt[1] != 'M' && opt[1] != 'C'))
500                 return -1; /* that is not a -M nor -C option */
501         diglen = strspn(opt+2, "0123456789");
502         if (diglen == 0 || strlen(opt+2) != diglen)
503                 return 0; /* use default */
504         sscanf(opt+2, "%d", &num);
505         for (i = 0, scale = 1; i < diglen; i++)
506                 scale *= 10;
507
508         /* user says num divided by scale and we say internally that
509          * is MAX_SCORE * num / scale.
510          */
511         return MAX_SCORE * num / scale;
512 }
513
514 void diff_setup(int detect_rename_, int minimum_score_, int reverse_diff_,
515                 int diff_raw_output_,
516                 const char **pathspec_, int speccnt_)
517 {
518         detect_rename = detect_rename_;
519         reverse_diff = reverse_diff_;
520         pathspec = pathspec_;
521         diff_raw_output = diff_raw_output_;
522         speccnt = speccnt_;
523         minimum_score = minimum_score_ ? : DEFAULT_MINIMUM_SCORE;
524 }
525
526 static struct diff_queue_struct queued_diff;
527
528 struct diff_file_pair *diff_queue(struct diff_queue_struct *queue,
529                                   struct diff_filespec *one,
530                                   struct diff_filespec *two)
531 {
532         struct diff_file_pair *dp = xmalloc(sizeof(*dp));
533         dp->one = one;
534         dp->two = two;
535         dp->xfrm_msg = 0;
536         dp->orig_order = queue->nr;
537         dp->xfrm_work = 0;
538         if (queue->alloc <= queue->nr) {
539                 queue->alloc = alloc_nr(queue->alloc);
540                 queue->queue = xrealloc(queue->queue,
541                                        sizeof(dp) * queue->alloc);
542         }
543         queue->queue[queue->nr++] = dp;
544         return dp;
545 }
546
547 static const char *git_object_type(unsigned mode)
548 {
549         return S_ISDIR(mode) ? "tree" : "blob";
550 }
551
552 static void diff_flush_raw(struct diff_file_pair *p)
553 {
554         struct diff_filespec *it;
555         int addremove;
556
557         /* raw output does not have a way to express rename nor copy */
558         if (strcmp(p->one->path, p->two->path))
559                 return;
560
561         if (p->one->file_valid && p->two->file_valid) {
562                 char hex[41];
563                 strcpy(hex, sha1_to_hex(p->one->sha1));
564                 printf("*%06o->%06o %s %s->%s %s%c",
565                        p->one->mode, p->two->mode,
566                        git_object_type(p->one->mode),
567                        hex, sha1_to_hex(p->two->sha1),
568                        p->one->path, diff_raw_output);
569                 return;
570         }
571
572         if (p->one->file_valid) {
573                 it = p->one;
574                 addremove = '-';
575         } else {
576                 it = p->two;
577                 addremove = '+';
578         }
579
580         printf("%c%06o %s %s %s%c",
581                addremove,
582                it->mode, git_object_type(it->mode),
583                sha1_to_hex(it->sha1), it->path, diff_raw_output);
584 }
585
586 static void diff_flush_patch(struct diff_file_pair *p)
587 {
588         const char *name, *other;
589
590         name = p->one->path;
591         other = (strcmp(name, p->two->path) ? p->two->path : NULL);
592         if ((p->one->file_valid && S_ISDIR(p->one->mode)) ||
593             (p->two->file_valid && S_ISDIR(p->two->mode)))
594                 return; /* no tree diffs in patch format */ 
595
596         run_external_diff(name, other, p->one, p->two, p->xfrm_msg);
597 }
598
599 static int identical(struct diff_filespec *one, struct diff_filespec *two)
600 {
601         /* This function is written stricter than necessary to support
602          * the currently implemented transformers, but the idea is to
603          * let transformers to produce diff_file_pairs any way they want,
604          * and filter and clean them up here before producing the output.
605          */
606
607         if (!one->file_valid && !two->file_valid)
608                 return 1; /* not interesting */
609
610         /* deletion, addition, mode change and renames are all interesting. */
611         if ((one->file_valid != two->file_valid) || (one->mode != two->mode) ||
612             strcmp(one->path, two->path))
613                 return 0;
614
615         /* both are valid and point at the same path.  that is, we are
616          * dealing with a change.
617          */
618         if (one->sha1_valid && two->sha1_valid &&
619             !memcmp(one->sha1, two->sha1, sizeof(one->sha1)))
620                 return 1; /* no change */
621         if (!one->sha1_valid && !two->sha1_valid)
622                 return 1; /* both look at the same file on the filesystem. */
623         return 0;
624 }
625
626 static void diff_flush_one(struct diff_file_pair *p)
627 {
628         if (identical(p->one, p->two))
629                 return;
630         if (0 <= diff_raw_output)
631                 diff_flush_raw(p);
632         else
633                 diff_flush_patch(p);
634 }
635
636 void diff_flush(void)
637 {
638         struct diff_queue_struct *q = &queued_diff;
639         int i;
640
641         if (detect_rename)
642                 diff_detect_rename(q, detect_rename, minimum_score);
643         for (i = 0; i < q->nr; i++)
644                 diff_flush_one(q->queue[i]);
645
646         for (i = 0; i < q->nr; i++) {
647                 struct diff_file_pair *p = q->queue[i];
648                 diff_free_filespec_data(p->one);
649                 diff_free_filespec_data(p->two);
650                 free(p->xfrm_msg);
651                 free(p);
652         }
653         free(q->queue);
654         q->queue = NULL;
655         q->nr = q->alloc = 0;
656 }
657
658 void diff_addremove(int addremove, unsigned mode,
659                     const unsigned char *sha1,
660                     const char *base, const char *path)
661 {
662         char concatpath[PATH_MAX];
663         struct diff_filespec *one, *two;
664
665         /* This may look odd, but it is a preparation for
666          * feeding "there are unchanged files which should
667          * not produce diffs, but when you are doing copy
668          * detection you would need them, so here they are"
669          * entries to the diff-core.  They will be prefixed
670          * with something like '=' or '*' (I haven't decided
671          * which but should not make any difference).
672          * Feeding the same new and old to diff_change() should
673          * also have the same effect.  diff_flush() should
674          * filter the identical ones out at the final output
675          * stage.
676          */
677         if (reverse_diff)
678                 addremove = (addremove == '+' ? '-' :
679                              addremove == '-' ? '+' : addremove);
680
681         if (!path) path = "";
682         sprintf(concatpath, "%s%s", base, path);
683         one = alloc_filespec(concatpath);
684         two = alloc_filespec(concatpath);
685
686         if (addremove != '+')
687                 fill_filespec(one, sha1, mode);
688         if (addremove != '-')
689                 fill_filespec(two, sha1, mode);
690
691         diff_queue(&queued_diff, one, two);
692 }
693
694 void diff_change(unsigned old_mode, unsigned new_mode,
695                  const unsigned char *old_sha1,
696                  const unsigned char *new_sha1,
697                  const char *base, const char *path) {
698         char concatpath[PATH_MAX];
699         struct diff_filespec *one, *two;
700
701         if (reverse_diff) {
702                 unsigned tmp;
703                 const unsigned char *tmp_c;
704                 tmp = old_mode; old_mode = new_mode; new_mode = tmp;
705                 tmp_c = old_sha1; old_sha1 = new_sha1; new_sha1 = tmp_c;
706         }
707         if (!path) path = "";
708         sprintf(concatpath, "%s%s", base, path);
709         one = alloc_filespec(concatpath);
710         two = alloc_filespec(concatpath);
711         fill_filespec(one, old_sha1, old_mode);
712         fill_filespec(two, new_sha1, new_mode);
713
714         diff_queue(&queued_diff, one, two);
715 }
716
717 void diff_unmerge(const char *path)
718 {
719         if (0 <= diff_raw_output) {
720                 printf("U %s%c", path, diff_raw_output);
721                 return;
722         }
723         run_external_diff(path, NULL, NULL, NULL, NULL);
724 }