5a2aee7acc0b54e6be8d6a884895098e0a739127
[git.git] / diff.c
1 /*
2  * Copyright (C) 2005 Junio C Hamano
3  */
4 #include <sys/types.h>
5 #include <sys/wait.h>
6 #include <signal.h>
7 #include "cache.h"
8 #include "diff.h"
9 #include "diffcore.h"
10
11 static const char *diff_opts = "-pu";
12 static unsigned char null_sha1[20] = { 0, };
13
14 static int reverse_diff;
15 static int generate_patch;
16 static int line_termination = '\n';
17 static int inter_name_termination = '\t';
18
19 static const char *external_diff(void)
20 {
21         static const char *external_diff_cmd = NULL;
22         static int done_preparing = 0;
23
24         if (done_preparing)
25                 return external_diff_cmd;
26
27         /*
28          * Default values above are meant to match the
29          * Linux kernel development style.  Examples of
30          * alternative styles you can specify via environment
31          * variables are:
32          *
33          * GIT_DIFF_OPTS="-c";
34          */
35         if (gitenv("GIT_EXTERNAL_DIFF"))
36                 external_diff_cmd = gitenv("GIT_EXTERNAL_DIFF");
37
38         /* In case external diff fails... */
39         diff_opts = gitenv("GIT_DIFF_OPTS") ? : diff_opts;
40
41         done_preparing = 1;
42         return external_diff_cmd;
43 }
44
45 /* Help to copy the thing properly quoted for the shell safety.
46  * any single quote is replaced with '\'', and the caller is
47  * expected to enclose the result within a single quote pair.
48  *
49  * E.g.
50  *  original     sq_expand     result
51  *  name     ==> name      ==> 'name'
52  *  a b      ==> a b       ==> 'a b'
53  *  a'b      ==> a'\''b    ==> 'a'\''b'
54  */
55 static char *sq_expand(const char *src)
56 {
57         static char *buf = NULL;
58         int cnt, c;
59         const char *cp;
60         char *bp;
61
62         /* count bytes needed to store the quoted string. */
63         for (cnt = 1, cp = src; *cp; cnt++, cp++)
64                 if (*cp == '\'')
65                         cnt += 3;
66
67         buf = xmalloc(cnt);
68         bp = buf;
69         while ((c = *src++)) {
70                 if (c != '\'')
71                         *bp++ = c;
72                 else {
73                         bp = strcpy(bp, "'\\''");
74                         bp += 4;
75                 }
76         }
77         *bp = 0;
78         return buf;
79 }
80
81 static struct diff_tempfile {
82         const char *name; /* filename external diff should read from */
83         char hex[41];
84         char mode[10];
85         char tmp_path[50];
86 } diff_temp[2];
87
88 static void builtin_diff(const char *name_a,
89                          const char *name_b,
90                          struct diff_tempfile *temp,
91                          const char *xfrm_msg)
92 {
93         int i, next_at, cmd_size;
94         const char *diff_cmd = "diff -L'%s%s' -L'%s%s'";
95         const char *diff_arg  = "'%s' '%s'||:"; /* "||:" is to return 0 */
96         const char *input_name_sq[2];
97         const char *path0[2];
98         const char *path1[2];
99         const char *name_sq[2];
100         char *cmd;
101
102         name_sq[0] = sq_expand(name_a);
103         name_sq[1] = sq_expand(name_b);
104
105         /* diff_cmd and diff_arg have 6 %s in total which makes
106          * the sum of these strings 12 bytes larger than required.
107          * we use 2 spaces around diff-opts, and we need to count
108          * terminating NUL, so we subtract 9 here.
109          */
110         cmd_size = (strlen(diff_cmd) + strlen(diff_opts) +
111                         strlen(diff_arg) - 9);
112         for (i = 0; i < 2; i++) {
113                 input_name_sq[i] = sq_expand(temp[i].name);
114                 if (!strcmp(temp[i].name, "/dev/null")) {
115                         path0[i] = "/dev/null";
116                         path1[i] = "";
117                 } else {
118                         path0[i] = i ? "b/" : "a/";
119                         path1[i] = name_sq[i];
120                 }
121                 cmd_size += (strlen(path0[i]) + strlen(path1[i]) +
122                              strlen(input_name_sq[i]));
123         }
124
125         cmd = xmalloc(cmd_size);
126
127         next_at = 0;
128         next_at += snprintf(cmd+next_at, cmd_size-next_at,
129                             diff_cmd,
130                             path0[0], path1[0], path0[1], path1[1]);
131         next_at += snprintf(cmd+next_at, cmd_size-next_at,
132                             " %s ", diff_opts);
133         next_at += snprintf(cmd+next_at, cmd_size-next_at,
134                             diff_arg, input_name_sq[0], input_name_sq[1]);
135
136         printf("diff --git a/%s b/%s\n", name_a, name_b);
137         if (!path1[0][0])
138                 printf("new file mode %s\n", temp[1].mode);
139         else if (!path1[1][0])
140                 printf("deleted file mode %s\n", temp[0].mode);
141         else {
142                 if (strcmp(temp[0].mode, temp[1].mode)) {
143                         printf("old mode %s\n", temp[0].mode);
144                         printf("new mode %s\n", temp[1].mode);
145                 }
146                 if (xfrm_msg && xfrm_msg[0])
147                         fputs(xfrm_msg, stdout);
148
149                 if (strncmp(temp[0].mode, temp[1].mode, 3))
150                         /* we do not run diff between different kind
151                          * of objects.
152                          */
153                         exit(0);
154         }
155         fflush(NULL);
156         execlp("/bin/sh","sh", "-c", cmd, NULL);
157 }
158
159 struct diff_filespec *alloc_filespec(const char *path)
160 {
161         int namelen = strlen(path);
162         struct diff_filespec *spec = xmalloc(sizeof(*spec) + namelen + 1);
163         spec->path = (char *)(spec + 1);
164         strcpy(spec->path, path);
165         spec->should_free = spec->should_munmap = 0;
166         spec->xfrm_flags = 0;
167         spec->size = 0;
168         spec->data = NULL;
169         spec->mode = 0;
170         memset(spec->sha1, 0, 20);
171         return spec;
172 }
173
174 void fill_filespec(struct diff_filespec *spec, const unsigned char *sha1,
175                    unsigned short mode)
176 {
177         if (mode) { /* just playing defensive */
178                 spec->mode = mode;
179                 memcpy(spec->sha1, sha1, 20);
180                 spec->sha1_valid = !!memcmp(sha1, null_sha1, 20);
181         }
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 (!DIFF_FILE_VALID(s))
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 = NULL;
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 (!DIFF_FILE_VALID(one)) {
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 /* An external diff command takes:
391  *
392  * diff-cmd name infile1 infile1-sha1 infile1-mode \
393  *               infile2 infile2-sha1 infile2-mode [ rename-to ]
394  *
395  */
396 static void run_external_diff(const char *name,
397                               const char *other,
398                               struct diff_filespec *one,
399                               struct diff_filespec *two,
400                               const char *xfrm_msg)
401 {
402         struct diff_tempfile *temp = diff_temp;
403         pid_t pid;
404         int status;
405         static int atexit_asked = 0;
406
407         if (one && two) {
408                 prepare_temp_file(name, &temp[0], one);
409                 prepare_temp_file(other ? : name, &temp[1], two);
410                 if (! atexit_asked &&
411                     (temp[0].name == temp[0].tmp_path ||
412                      temp[1].name == temp[1].tmp_path)) {
413                         atexit_asked = 1;
414                         atexit(remove_tempfile);
415                 }
416                 signal(SIGINT, remove_tempfile_on_signal);
417         }
418
419         fflush(NULL);
420         pid = fork();
421         if (pid < 0)
422                 die("unable to fork");
423         if (!pid) {
424                 const char *pgm = external_diff();
425                 if (pgm) {
426                         if (one && two) {
427                                 const char *exec_arg[10];
428                                 const char **arg = &exec_arg[0];
429                                 *arg++ = pgm;
430                                 *arg++ = name;
431                                 *arg++ = temp[0].name;
432                                 *arg++ = temp[0].hex;
433                                 *arg++ = temp[0].mode;
434                                 *arg++ = temp[1].name;
435                                 *arg++ = temp[1].hex;
436                                 *arg++ = temp[1].mode;
437                                 if (other) {
438                                         *arg++ = other;
439                                         *arg++ = xfrm_msg;
440                                 }
441                                 *arg = NULL;
442                                 execvp(pgm, (char *const*) exec_arg);
443                         }
444                         else
445                                 execlp(pgm, pgm, name, NULL);
446                 }
447                 /*
448                  * otherwise we use the built-in one.
449                  */
450                 if (one && two)
451                         builtin_diff(name, other ? : name, temp, xfrm_msg);
452                 else
453                         printf("* Unmerged path %s\n", name);
454                 exit(0);
455         }
456         if (waitpid(pid, &status, 0) < 0 ||
457             !WIFEXITED(status) || WEXITSTATUS(status)) {
458                 /* Earlier we did not check the exit status because
459                  * diff exits non-zero if files are different, and
460                  * we are not interested in knowing that.  It was a
461                  * mistake which made it harder to quit a diff-*
462                  * session that uses the git-apply-patch-script as
463                  * the GIT_EXTERNAL_DIFF.  A custom GIT_EXTERNAL_DIFF
464                  * should also exit non-zero only when it wants to
465                  * abort the entire diff-* session.
466                  */
467                 remove_tempfile();
468                 fprintf(stderr, "external diff died, stopping at %s.\n", name);
469                 exit(1);
470         }
471         remove_tempfile();
472 }
473
474 void diff_setup(int reverse_diff_)
475 {
476         reverse_diff = reverse_diff_;
477 }
478
479 struct diff_queue_struct diff_queued_diff;
480
481 void diff_q(struct diff_queue_struct *queue, struct diff_filepair *dp)
482 {
483         if (queue->alloc <= queue->nr) {
484                 queue->alloc = alloc_nr(queue->alloc);
485                 queue->queue = xrealloc(queue->queue,
486                                         sizeof(dp) * queue->alloc);
487         }
488         queue->queue[queue->nr++] = dp;
489 }
490
491 struct diff_filepair *diff_queue(struct diff_queue_struct *queue,
492                                  struct diff_filespec *one,
493                                  struct diff_filespec *two)
494 {
495         struct diff_filepair *dp = xmalloc(sizeof(*dp));
496         dp->one = one;
497         dp->two = two;
498         dp->score = 0;
499         dp->orig_order = queue->nr;
500         dp->rename_rank = 0;
501         diff_q(queue, dp);
502         return dp;
503 }
504
505 static void diff_flush_raw(struct diff_filepair *p)
506 {
507         int two_paths;
508         char status[10];
509         switch (p->status) {
510         case 'C': case 'R':
511                 two_paths = 1;
512                 sprintf(status, "%c%1d", p->status, p->score);
513                 break;
514         default:
515                 two_paths = 0;
516                 status[0] = p->status;
517                 status[1] = 0;
518                 break;
519         }
520         printf(":%06o %06o %s ",
521                p->one->mode, p->two->mode, sha1_to_hex(p->one->sha1));
522         printf("%s %s%c%s",
523                sha1_to_hex(p->two->sha1),
524                status,
525                inter_name_termination,
526                p->one->path);
527         if (two_paths)
528                 printf("%c%s", inter_name_termination, p->two->path);
529         putchar(line_termination);
530 }
531
532 int diff_unmodified_pair(struct diff_filepair *p)
533 {
534         /* This function is written stricter than necessary to support
535          * the currently implemented transformers, but the idea is to
536          * let transformers to produce diff_filepairs any way they want,
537          * and filter and clean them up here before producing the output.
538          */
539         struct diff_filespec *one, *two;
540
541         if (DIFF_PAIR_UNMERGED(p))
542                 return 0; /* unmerged is interesting */
543
544         one = p->one;
545         two = p->two;
546
547         /* deletion, addition, mode change and renames are all interesting. */
548         if (DIFF_FILE_VALID(one) != DIFF_FILE_VALID(two) ||
549             (one->mode != two->mode) ||
550             strcmp(one->path, two->path))
551                 return 0;
552
553         /* both are valid and point at the same path.  that is, we are
554          * dealing with a change.
555          */
556         if (one->sha1_valid && two->sha1_valid &&
557             !memcmp(one->sha1, two->sha1, sizeof(one->sha1)))
558                 return 1; /* no change */
559         if (!one->sha1_valid && !two->sha1_valid)
560                 return 1; /* both look at the same file on the filesystem. */
561         return 0;
562 }
563
564 static void diff_flush_patch(struct diff_filepair *p)
565 {
566         const char *name, *other;
567         char msg_[PATH_MAX*2+200], *msg;
568
569         /* diffcore_prune() keeps "stay" entries for diff-raw
570          * copy/rename detection, but when we are generating
571          * patches we do not need them.
572          */
573         if (diff_unmodified_pair(p))
574                 return;
575
576         name = p->one->path;
577         other = (strcmp(name, p->two->path) ? p->two->path : NULL);
578         if ((DIFF_FILE_VALID(p->one) && S_ISDIR(p->one->mode)) ||
579             (DIFF_FILE_VALID(p->two) && S_ISDIR(p->two->mode)))
580                 return; /* no tree diffs in patch format */ 
581
582         switch (p->status) {
583         case 'C':
584                 sprintf(msg_,
585                         "similarity index %d%%\n"
586                         "copy from %s\n"
587                         "copy to %s\n",
588                         (int)(0.5 + p->score * 100/MAX_SCORE),
589                         p->one->path, p->two->path);
590                 msg = msg_;
591                 break;
592         case 'R':
593                 sprintf(msg_,
594                         "similarity index %d%%\n"
595                         "rename old %s\n"
596                         "rename new %s\n",
597                         (int)(0.5 + p->score * 100/MAX_SCORE),
598                         p->one->path, p->two->path);
599                 msg = msg_;
600                 break;
601         default:
602                 msg = NULL;
603         }
604
605         if (DIFF_PAIR_UNMERGED(p))
606                 run_external_diff(name, NULL, NULL, NULL, NULL);
607         else
608                 run_external_diff(name, other, p->one, p->two, msg);
609 }
610
611 int diff_needs_to_stay(struct diff_queue_struct *q, int i,
612                        struct diff_filespec *it)
613 {
614         /* If it will be used in later entry (either stay or used
615          * as the source of rename/copy), we need to copy, not rename.
616          */
617         while (i < q->nr) {
618                 struct diff_filepair *p = q->queue[i++];
619                 if (!DIFF_FILE_VALID(p->two))
620                         continue; /* removed is fine */
621                 if (strcmp(p->one->path, it->path))
622                         continue; /* not relevant */
623
624                 /* p has its src set to *it and it is not a delete;
625                  * it will be used for in-place change, rename/copy,
626                  * or just stays there.  We cannot rename it out.
627                  */
628                 return 1;
629         }
630         return 0;
631 }
632
633 static int diff_used_as_source(struct diff_queue_struct *q, int lim,
634                                struct diff_filespec *it)
635 {
636         int i;
637         for (i = 0; i < lim; i++) {
638                 struct diff_filepair *p = q->queue[i++];
639                 if (!strcmp(p->one->path, it->path))
640                         return 1;
641         }
642         return 0;
643 }
644
645 void diffcore_prune(void)
646 {
647         /*
648          * Although rename/copy detection wants to have "no-change"
649          * entries fed into them, the downstream do not need to see
650          * them, unless we had rename/copy for the same path earlier.
651          * This function removes such entries.
652          *
653          * The applications that use rename/copy should:
654          *
655          * (1) feed change and "no-change" entries via diff_queue().
656          * (2) call diffcore_rename, and any other future diffcore_xxx
657          *     that would benefit by still having "no-change" entries.
658          * (3) call diffcore_prune
659          * (4) call other diffcore_xxx that do not need to see
660          *     "no-change" entries.
661          * (5) call diff_flush().
662          */
663         struct diff_queue_struct *q = &diff_queued_diff;
664         struct diff_queue_struct outq;
665         int i;
666
667         outq.queue = NULL;
668         outq.nr = outq.alloc = 0;
669
670         for (i = 0; i < q->nr; i++) {
671                 struct diff_filepair *p = q->queue[i];
672                 if (!diff_unmodified_pair(p) ||
673                     diff_used_as_source(q, i, p->one))
674                         diff_q(&outq, p);
675                 else
676                         free(p);
677         }
678         free(q->queue);
679         *q = outq;
680         return;
681 }
682
683 int diff_queue_is_empty(void)
684 {
685         struct diff_queue_struct *q = &diff_queued_diff;
686         return q->nr == 0;
687 }
688
689 void diff_flush(int diff_output_style, int resolve_rename_copy)
690 {
691         struct diff_queue_struct *q = &diff_queued_diff;
692         int i;
693
694         generate_patch = 0;
695         switch (diff_output_style) {
696         case DIFF_FORMAT_HUMAN:
697                 line_termination = '\n';
698                 inter_name_termination = '\t';
699                 break;
700         case DIFF_FORMAT_MACHINE:
701                 line_termination = inter_name_termination = 0;
702                 break;
703         case DIFF_FORMAT_PATCH:
704                 generate_patch = 1;
705                 break;
706         }
707         for (i = 0; i < q->nr; i++) {
708                 struct diff_filepair *p = q->queue[i];
709                 if (resolve_rename_copy) {
710                         if (DIFF_PAIR_UNMERGED(p))
711                                 p->status = 'U';
712                         else if (!DIFF_FILE_VALID((p)->one))
713                                 p->status = 'N';
714                         else if (!DIFF_FILE_VALID((p)->two))
715                                 p->status = 'D';
716                         else if (strcmp(p->one->path, p->two->path)) {
717                                 /* This is rename or copy.  Which one is it? */
718                                 if (diff_needs_to_stay(q, i+1, p->one))
719                                         p->status = 'C';
720                                 else
721                                         p->status = 'R';
722                         }
723                         else
724                                 p->status = 'M';
725                 }
726                 if (generate_patch)
727                         diff_flush_patch(p);
728                 else
729                         diff_flush_raw(p);
730         }
731
732         for (i = 0; i < q->nr; i++) {
733                 struct diff_filepair *p = q->queue[i];
734                 diff_free_filespec_data(p->one);
735                 diff_free_filespec_data(p->two);
736                 free(p);
737         }
738         free(q->queue);
739         q->queue = NULL;
740         q->nr = q->alloc = 0;
741 }
742
743 void diff_addremove(int addremove, unsigned mode,
744                     const unsigned char *sha1,
745                     const char *base, const char *path)
746 {
747         char concatpath[PATH_MAX];
748         struct diff_filespec *one, *two;
749
750         /* This may look odd, but it is a preparation for
751          * feeding "there are unchanged files which should
752          * not produce diffs, but when you are doing copy
753          * detection you would need them, so here they are"
754          * entries to the diff-core.  They will be prefixed
755          * with something like '=' or '*' (I haven't decided
756          * which but should not make any difference).
757          * Feeding the same new and old to diff_change() 
758          * also has the same effect.  diffcore_prune() should
759          * be used to filter uninteresting ones out before the
760          * final output happens.
761          */
762         if (reverse_diff)
763                 addremove = (addremove == '+' ? '-' :
764                              addremove == '-' ? '+' : addremove);
765
766         if (!path) path = "";
767         sprintf(concatpath, "%s%s", base, path);
768         one = alloc_filespec(concatpath);
769         two = alloc_filespec(concatpath);
770
771         if (addremove != '+')
772                 fill_filespec(one, sha1, mode);
773         if (addremove != '-')
774                 fill_filespec(two, sha1, mode);
775
776         diff_queue(&diff_queued_diff, one, two);
777 }
778
779 void diff_helper_input(unsigned old_mode,
780                        unsigned new_mode,
781                        const unsigned char *old_sha1,
782                        const unsigned char *new_sha1,
783                        const char *old_path,
784                        int status,
785                        int score,
786                        const char *new_path)
787 {
788         struct diff_filespec *one, *two;
789         struct diff_filepair *dp;
790
791         one = alloc_filespec(old_path);
792         two = alloc_filespec(new_path);
793         if (old_mode)
794                 fill_filespec(one, old_sha1, old_mode);
795         if (new_mode)
796                 fill_filespec(two, new_sha1, new_mode);
797         dp = diff_queue(&diff_queued_diff, one, two);
798         dp->score = score;
799         dp->status = status;
800 }
801
802 void diff_change(unsigned old_mode, unsigned new_mode,
803                  const unsigned char *old_sha1,
804                  const unsigned char *new_sha1,
805                  const char *base, const char *path) 
806 {
807         char concatpath[PATH_MAX];
808         struct diff_filespec *one, *two;
809
810         if (reverse_diff) {
811                 unsigned tmp;
812                 const unsigned char *tmp_c;
813                 tmp = old_mode; old_mode = new_mode; new_mode = tmp;
814                 tmp_c = old_sha1; old_sha1 = new_sha1; new_sha1 = tmp_c;
815         }
816         if (!path) path = "";
817         sprintf(concatpath, "%s%s", base, path);
818         one = alloc_filespec(concatpath);
819         two = alloc_filespec(concatpath);
820         fill_filespec(one, old_sha1, old_mode);
821         fill_filespec(two, new_sha1, new_mode);
822
823         diff_queue(&diff_queued_diff, one, two);
824 }
825
826 void diff_unmerge(const char *path)
827 {
828         struct diff_filespec *one, *two;
829         one = alloc_filespec(path);
830         two = alloc_filespec(path);
831         diff_queue(&diff_queued_diff, one, two);
832 }