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