a080d94c37c8a99cdbc3b8ed510c4a52cea508e8
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
11static const char *diff_opts = "-pu";
12static unsigned char null_sha1[20] = { 0, };
13
14static int reverse_diff;
15static int use_size_cache;
16
17static const char *external_diff(void)
18{
19 static const char *external_diff_cmd = NULL;
20 static int done_preparing = 0;
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 if (gitenv("GIT_EXTERNAL_DIFF"))
34 external_diff_cmd = gitenv("GIT_EXTERNAL_DIFF");
35
36 /* In case external diff fails... */
37 diff_opts = gitenv("GIT_DIFF_OPTS") ? : diff_opts;
38
39 done_preparing = 1;
40 return external_diff_cmd;
41}
42
43/* Help to copy the thing properly quoted for the shell safety.
44 * any single quote is replaced with '\'', and the caller is
45 * expected to enclose the result within a single quote pair.
46 *
47 * E.g.
48 * original sq_expand result
49 * name ==> name ==> 'name'
50 * a b ==> a b ==> 'a b'
51 * a'b ==> a'\''b ==> 'a'\''b'
52 */
53static char *sq_expand(const char *src)
54{
55 static char *buf = NULL;
56 int cnt, c;
57 const char *cp;
58 char *bp;
59
60 /* count bytes needed to store the quoted string. */
61 for (cnt = 1, cp = src; *cp; cnt++, cp++)
62 if (*cp == '\'')
63 cnt += 3;
64
65 buf = xmalloc(cnt);
66 bp = buf;
67 while ((c = *src++)) {
68 if (c != '\'')
69 *bp++ = c;
70 else {
71 bp = strcpy(bp, "'\\''");
72 bp += 4;
73 }
74 }
75 *bp = 0;
76 return buf;
77}
78
79static struct diff_tempfile {
80 const char *name; /* filename external diff should read from */
81 char hex[41];
82 char mode[10];
83 char tmp_path[50];
84} diff_temp[2];
85
86static void builtin_diff(const char *name_a,
87 const char *name_b,
88 struct diff_tempfile *temp,
89 const char *xfrm_msg)
90{
91 int i, next_at, cmd_size;
92 const char *diff_cmd = "diff -L'%s%s' -L'%s%s'";
93 const char *diff_arg = "'%s' '%s'||:"; /* "||:" is to return 0 */
94 const char *input_name_sq[2];
95 const char *path0[2];
96 const char *path1[2];
97 const char *name_sq[2];
98 char *cmd;
99
100 name_sq[0] = sq_expand(name_a);
101 name_sq[1] = sq_expand(name_b);
102
103 /* diff_cmd and diff_arg have 6 %s in total which makes
104 * the sum of these strings 12 bytes larger than required.
105 * we use 2 spaces around diff-opts, and we need to count
106 * terminating NUL, so we subtract 9 here.
107 */
108 cmd_size = (strlen(diff_cmd) + strlen(diff_opts) +
109 strlen(diff_arg) - 9);
110 for (i = 0; i < 2; i++) {
111 input_name_sq[i] = sq_expand(temp[i].name);
112 if (!strcmp(temp[i].name, "/dev/null")) {
113 path0[i] = "/dev/null";
114 path1[i] = "";
115 } else {
116 path0[i] = i ? "b/" : "a/";
117 path1[i] = name_sq[i];
118 }
119 cmd_size += (strlen(path0[i]) + strlen(path1[i]) +
120 strlen(input_name_sq[i]));
121 }
122
123 cmd = xmalloc(cmd_size);
124
125 next_at = 0;
126 next_at += snprintf(cmd+next_at, cmd_size-next_at,
127 diff_cmd,
128 path0[0], path1[0], path0[1], path1[1]);
129 next_at += snprintf(cmd+next_at, cmd_size-next_at,
130 " %s ", diff_opts);
131 next_at += snprintf(cmd+next_at, cmd_size-next_at,
132 diff_arg, input_name_sq[0], input_name_sq[1]);
133
134 printf("diff --git a/%s b/%s\n", name_a, name_b);
135 if (!path1[0][0])
136 printf("new file mode %s\n", temp[1].mode);
137 else if (!path1[1][0])
138 printf("deleted file mode %s\n", temp[0].mode);
139 else {
140 if (strcmp(temp[0].mode, temp[1].mode)) {
141 printf("old mode %s\n", temp[0].mode);
142 printf("new mode %s\n", temp[1].mode);
143 }
144 if (xfrm_msg && xfrm_msg[0])
145 puts(xfrm_msg);
146
147 if (strncmp(temp[0].mode, temp[1].mode, 3))
148 /* we do not run diff between different kind
149 * of objects.
150 */
151 exit(0);
152 }
153 fflush(NULL);
154 execlp("/bin/sh","sh", "-c", cmd, NULL);
155}
156
157struct diff_filespec *alloc_filespec(const char *path)
158{
159 int namelen = strlen(path);
160 struct diff_filespec *spec = xmalloc(sizeof(*spec) + namelen + 1);
161 spec->path = (char *)(spec + 1);
162 strcpy(spec->path, path);
163 spec->should_free = spec->should_munmap = 0;
164 spec->xfrm_flags = 0;
165 spec->size = 0;
166 spec->data = NULL;
167 spec->mode = 0;
168 memset(spec->sha1, 0, 20);
169 return spec;
170}
171
172void fill_filespec(struct diff_filespec *spec, const unsigned char *sha1,
173 unsigned short mode)
174{
175 if (mode) {
176 spec->mode = DIFF_FILE_CANON_MODE(mode);
177 memcpy(spec->sha1, sha1, 20);
178 spec->sha1_valid = !!memcmp(sha1, null_sha1, 20);
179 }
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 */
187static 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
226static struct sha1_size_cache {
227 unsigned char sha1[20];
228 unsigned long size;
229} **sha1_size_cache;
230static int sha1_size_cache_nr, sha1_size_cache_alloc;
231
232static struct sha1_size_cache *locate_size_cache(unsigned char *sha1,
233 unsigned long size)
234{
235 int first, last;
236 struct sha1_size_cache *e;
237
238 first = 0;
239 last = sha1_size_cache_nr;
240 while (last > first) {
241 int next = (last + first) >> 1;
242 e = sha1_size_cache[next];
243 int cmp = memcmp(e->sha1, sha1, 20);
244 if (!cmp)
245 return e;
246 if (cmp < 0) {
247 last = next;
248 continue;
249 }
250 first = next+1;
251 }
252 /* not found */
253 if (size == UINT_MAX)
254 return NULL;
255 /* insert to make it at "first" */
256 if (sha1_size_cache_alloc <= sha1_size_cache_nr) {
257 sha1_size_cache_alloc = alloc_nr(sha1_size_cache_alloc);
258 sha1_size_cache = xrealloc(sha1_size_cache,
259 sha1_size_cache_alloc *
260 sizeof(*sha1_size_cache));
261 }
262 sha1_size_cache_nr++;
263 if (first < sha1_size_cache_nr)
264 memmove(sha1_size_cache + first + 1, sha1_size_cache + first,
265 (sha1_size_cache_nr - first - 1) *
266 sizeof(*sha1_size_cache));
267 e = xmalloc(sizeof(struct sha1_size_cache));
268 sha1_size_cache[first] = e;
269 memcpy(e->sha1, sha1, 20);
270 e->size = size;
271 return e;
272}
273
274/*
275 * While doing rename detection and pickaxe operation, we may need to
276 * grab the data for the blob (or file) for our own in-core comparison.
277 * diff_filespec has data and size fields for this purpose.
278 */
279int diff_populate_filespec(struct diff_filespec *s, int size_only)
280{
281 int err = 0;
282 if (!DIFF_FILE_VALID(s))
283 die("internal error: asking to populate invalid file.");
284 if (S_ISDIR(s->mode))
285 return -1;
286
287 if (!use_size_cache)
288 size_only = 0;
289
290 if (s->data)
291 return err;
292 if (!s->sha1_valid ||
293 work_tree_matches(s->path, s->sha1)) {
294 struct stat st;
295 int fd;
296 if (lstat(s->path, &st) < 0) {
297 if (errno == ENOENT) {
298 err_empty:
299 err = -1;
300 empty:
301 s->data = "";
302 s->size = 0;
303 return err;
304 }
305 }
306 s->size = st.st_size;
307 if (!s->size)
308 goto empty;
309 if (size_only)
310 return 0;
311 if (S_ISLNK(st.st_mode)) {
312 int ret;
313 s->data = xmalloc(s->size);
314 s->should_free = 1;
315 ret = readlink(s->path, s->data, s->size);
316 if (ret < 0) {
317 free(s->data);
318 goto err_empty;
319 }
320 return 0;
321 }
322 fd = open(s->path, O_RDONLY);
323 if (fd < 0)
324 goto err_empty;
325 s->data = mmap(NULL, s->size, PROT_READ, MAP_PRIVATE, fd, 0);
326 s->should_munmap = 1;
327 close(fd);
328 }
329 else {
330 /* We cannot do size only for SHA1 blobs */
331 char type[20];
332 struct sha1_size_cache *e;
333
334 if (size_only) {
335 e = locate_size_cache(s->sha1, UINT_MAX);
336 if (e) {
337 s->size = e->size;
338 return 0;
339 }
340 }
341 s->data = read_sha1_file(s->sha1, type, &s->size);
342 s->should_free = 1;
343 if (s->data && size_only)
344 locate_size_cache(s->sha1, s->size);
345 }
346 return 0;
347}
348
349void diff_free_filespec_data(struct diff_filespec *s)
350{
351 if (s->should_free)
352 free(s->data);
353 else if (s->should_munmap)
354 munmap(s->data, s->size);
355 s->should_free = s->should_munmap = 0;
356 s->data = NULL;
357}
358
359static void prep_temp_blob(struct diff_tempfile *temp,
360 void *blob,
361 unsigned long size,
362 unsigned char *sha1,
363 int mode)
364{
365 int fd;
366
367 strcpy(temp->tmp_path, ".diff_XXXXXX");
368 fd = mkstemp(temp->tmp_path);
369 if (fd < 0)
370 die("unable to create temp-file");
371 if (write(fd, blob, size) != size)
372 die("unable to write temp-file");
373 close(fd);
374 temp->name = temp->tmp_path;
375 strcpy(temp->hex, sha1_to_hex(sha1));
376 temp->hex[40] = 0;
377 sprintf(temp->mode, "%06o", mode);
378}
379
380static void prepare_temp_file(const char *name,
381 struct diff_tempfile *temp,
382 struct diff_filespec *one)
383{
384 if (!DIFF_FILE_VALID(one)) {
385 not_a_valid_file:
386 /* A '-' entry produces this for file-2, and
387 * a '+' entry produces this for file-1.
388 */
389 temp->name = "/dev/null";
390 strcpy(temp->hex, ".");
391 strcpy(temp->mode, ".");
392 return;
393 }
394
395 if (!one->sha1_valid ||
396 work_tree_matches(name, one->sha1)) {
397 struct stat st;
398 if (lstat(name, &st) < 0) {
399 if (errno == ENOENT)
400 goto not_a_valid_file;
401 die("stat(%s): %s", name, strerror(errno));
402 }
403 if (S_ISLNK(st.st_mode)) {
404 int ret;
405 char *buf, buf_[1024];
406 buf = ((sizeof(buf_) < st.st_size) ?
407 xmalloc(st.st_size) : buf_);
408 ret = readlink(name, buf, st.st_size);
409 if (ret < 0)
410 die("readlink(%s)", name);
411 prep_temp_blob(temp, buf, st.st_size,
412 (one->sha1_valid ?
413 one->sha1 : null_sha1),
414 (one->sha1_valid ?
415 one->mode : S_IFLNK));
416 }
417 else {
418 /* we can borrow from the file in the work tree */
419 temp->name = name;
420 if (!one->sha1_valid)
421 strcpy(temp->hex, sha1_to_hex(null_sha1));
422 else
423 strcpy(temp->hex, sha1_to_hex(one->sha1));
424 /* Even though we may sometimes borrow the
425 * contents from the work tree, we always want
426 * one->mode. mode is trustworthy even when
427 * !(one->sha1_valid), as long as
428 * DIFF_FILE_VALID(one).
429 */
430 sprintf(temp->mode, "%06o", one->mode);
431 }
432 return;
433 }
434 else {
435 if (diff_populate_filespec(one, 0))
436 die("cannot read data blob for %s", one->path);
437 prep_temp_blob(temp, one->data, one->size,
438 one->sha1, one->mode);
439 }
440}
441
442static void remove_tempfile(void)
443{
444 int i;
445
446 for (i = 0; i < 2; i++)
447 if (diff_temp[i].name == diff_temp[i].tmp_path) {
448 unlink(diff_temp[i].name);
449 diff_temp[i].name = NULL;
450 }
451}
452
453static void remove_tempfile_on_signal(int signo)
454{
455 remove_tempfile();
456}
457
458/* An external diff command takes:
459 *
460 * diff-cmd name infile1 infile1-sha1 infile1-mode \
461 * infile2 infile2-sha1 infile2-mode [ rename-to ]
462 *
463 */
464static void run_external_diff(const char *pgm,
465 const char *name,
466 const char *other,
467 struct diff_filespec *one,
468 struct diff_filespec *two,
469 const char *xfrm_msg)
470{
471 struct diff_tempfile *temp = diff_temp;
472 pid_t pid;
473 int status;
474 static int atexit_asked = 0;
475
476 if (one && two) {
477 prepare_temp_file(name, &temp[0], one);
478 prepare_temp_file(other ? : name, &temp[1], two);
479 if (! atexit_asked &&
480 (temp[0].name == temp[0].tmp_path ||
481 temp[1].name == temp[1].tmp_path)) {
482 atexit_asked = 1;
483 atexit(remove_tempfile);
484 }
485 signal(SIGINT, remove_tempfile_on_signal);
486 }
487
488 fflush(NULL);
489 pid = fork();
490 if (pid < 0)
491 die("unable to fork");
492 if (!pid) {
493 if (pgm) {
494 if (one && two) {
495 const char *exec_arg[10];
496 const char **arg = &exec_arg[0];
497 *arg++ = pgm;
498 *arg++ = name;
499 *arg++ = temp[0].name;
500 *arg++ = temp[0].hex;
501 *arg++ = temp[0].mode;
502 *arg++ = temp[1].name;
503 *arg++ = temp[1].hex;
504 *arg++ = temp[1].mode;
505 if (other) {
506 *arg++ = other;
507 *arg++ = xfrm_msg;
508 }
509 *arg = NULL;
510 execvp(pgm, (char *const*) exec_arg);
511 }
512 else
513 execlp(pgm, pgm, name, NULL);
514 }
515 /*
516 * otherwise we use the built-in one.
517 */
518 if (one && two)
519 builtin_diff(name, other ? : name, temp, xfrm_msg);
520 else
521 printf("* Unmerged path %s\n", name);
522 exit(0);
523 }
524 if (waitpid(pid, &status, 0) < 0 ||
525 !WIFEXITED(status) || WEXITSTATUS(status)) {
526 /* Earlier we did not check the exit status because
527 * diff exits non-zero if files are different, and
528 * we are not interested in knowing that. It was a
529 * mistake which made it harder to quit a diff-*
530 * session that uses the git-apply-patch-script as
531 * the GIT_EXTERNAL_DIFF. A custom GIT_EXTERNAL_DIFF
532 * should also exit non-zero only when it wants to
533 * abort the entire diff-* session.
534 */
535 remove_tempfile();
536 fprintf(stderr, "external diff died, stopping at %s.\n", name);
537 exit(1);
538 }
539 remove_tempfile();
540}
541
542static void run_diff(const char *name,
543 const char *other,
544 struct diff_filespec *one,
545 struct diff_filespec *two,
546 const char *xfrm_msg)
547{
548 const char *pgm = external_diff();
549 if (!pgm &&
550 DIFF_FILE_VALID(one) && DIFF_FILE_VALID(two) &&
551 (S_IFMT & one->mode) != (S_IFMT & two->mode)) {
552 /* a filepair that changes between file and symlink
553 * needs to be split into deletion and creation.
554 */
555 struct diff_filespec *null = alloc_filespec(two->path);
556 run_external_diff(NULL, name, other, one, null, xfrm_msg);
557 free(null);
558 null = alloc_filespec(one->path);
559 run_external_diff(NULL, name, other, null, two, xfrm_msg);
560 free(null);
561 }
562 else
563 run_external_diff(pgm, name, other, one, two, xfrm_msg);
564}
565
566void diff_setup(int flags)
567{
568 if (flags & DIFF_SETUP_REVERSE)
569 reverse_diff = 1;
570 if (flags & DIFF_SETUP_USE_CACHE) {
571 if (!active_cache)
572 /* read-cache does not die even when it fails
573 * so it is safe for us to do this here. Also
574 * it does not smudge active_cache or active_nr
575 * when it fails, so we do not have to worry about
576 * cleaning it up oufselves either.
577 */
578 read_cache();
579 }
580 if (flags & DIFF_SETUP_USE_SIZE_CACHE)
581 use_size_cache = 1;
582
583}
584
585struct diff_queue_struct diff_queued_diff;
586
587void diff_q(struct diff_queue_struct *queue, struct diff_filepair *dp)
588{
589 if (queue->alloc <= queue->nr) {
590 queue->alloc = alloc_nr(queue->alloc);
591 queue->queue = xrealloc(queue->queue,
592 sizeof(dp) * queue->alloc);
593 }
594 queue->queue[queue->nr++] = dp;
595}
596
597struct diff_filepair *diff_queue(struct diff_queue_struct *queue,
598 struct diff_filespec *one,
599 struct diff_filespec *two)
600{
601 struct diff_filepair *dp = xmalloc(sizeof(*dp));
602 dp->one = one;
603 dp->two = two;
604 dp->score = 0;
605 dp->source_stays = 0;
606 diff_q(queue, dp);
607 return dp;
608}
609
610void diff_free_filepair(struct diff_filepair *p)
611{
612 diff_free_filespec_data(p->one);
613 diff_free_filespec_data(p->two);
614 free(p);
615}
616
617static void diff_flush_raw(struct diff_filepair *p,
618 int line_termination,
619 int inter_name_termination)
620{
621 int two_paths;
622 char status[10];
623
624 if (line_termination) {
625 const char *err = "path %s cannot be expressed without -z";
626 if (strchr(p->one->path, line_termination) ||
627 strchr(p->one->path, inter_name_termination))
628 die(err, p->one->path);
629 if (strchr(p->two->path, line_termination) ||
630 strchr(p->two->path, inter_name_termination))
631 die(err, p->two->path);
632 }
633
634 switch (p->status) {
635 case 'C': case 'R':
636 two_paths = 1;
637 sprintf(status, "%c%03d", p->status,
638 (int)(0.5 + p->score * 100.0/MAX_SCORE));
639 break;
640 default:
641 two_paths = 0;
642 status[0] = p->status;
643 status[1] = 0;
644 break;
645 }
646 printf(":%06o %06o %s ",
647 p->one->mode, p->two->mode, sha1_to_hex(p->one->sha1));
648 printf("%s %s%c%s",
649 sha1_to_hex(p->two->sha1),
650 status,
651 inter_name_termination,
652 p->one->path);
653 if (two_paths)
654 printf("%c%s", inter_name_termination, p->two->path);
655 putchar(line_termination);
656}
657
658int diff_unmodified_pair(struct diff_filepair *p)
659{
660 /* This function is written stricter than necessary to support
661 * the currently implemented transformers, but the idea is to
662 * let transformers to produce diff_filepairs any way they want,
663 * and filter and clean them up here before producing the output.
664 */
665 struct diff_filespec *one, *two;
666
667 if (DIFF_PAIR_UNMERGED(p))
668 return 0; /* unmerged is interesting */
669
670 one = p->one;
671 two = p->two;
672
673 /* deletion, addition, mode or type change
674 * and rename are all interesting.
675 */
676 if (DIFF_FILE_VALID(one) != DIFF_FILE_VALID(two) ||
677 DIFF_PAIR_MODE_CHANGED(p) ||
678 strcmp(one->path, two->path))
679 return 0;
680
681 /* both are valid and point at the same path. that is, we are
682 * dealing with a change.
683 */
684 if (one->sha1_valid && two->sha1_valid &&
685 !memcmp(one->sha1, two->sha1, sizeof(one->sha1)))
686 return 1; /* no change */
687 if (!one->sha1_valid && !two->sha1_valid)
688 return 1; /* both look at the same file on the filesystem. */
689 return 0;
690}
691
692static void diff_flush_patch(struct diff_filepair *p)
693{
694 const char *name, *other;
695 char msg_[PATH_MAX*2+200], *msg;
696
697 if (diff_unmodified_pair(p))
698 return;
699
700 name = p->one->path;
701 other = (strcmp(name, p->two->path) ? p->two->path : NULL);
702 if ((DIFF_FILE_VALID(p->one) && S_ISDIR(p->one->mode)) ||
703 (DIFF_FILE_VALID(p->two) && S_ISDIR(p->two->mode)))
704 return; /* no tree diffs in patch format */
705
706 switch (p->status) {
707 case 'C':
708 sprintf(msg_,
709 "similarity index %d%%\n"
710 "copy from %s\n"
711 "copy to %s",
712 (int)(0.5 + p->score * 100.0/MAX_SCORE),
713 p->one->path, p->two->path);
714 msg = msg_;
715 break;
716 case 'R':
717 sprintf(msg_,
718 "similarity index %d%%\n"
719 "rename old %s\n"
720 "rename new %s",
721 (int)(0.5 + p->score * 100.0/MAX_SCORE),
722 p->one->path, p->two->path);
723 msg = msg_;
724 break;
725 default:
726 msg = NULL;
727 }
728
729 if (DIFF_PAIR_UNMERGED(p))
730 run_diff(name, NULL, NULL, NULL, NULL);
731 else
732 run_diff(name, other, p->one, p->two, msg);
733}
734
735int diff_queue_is_empty(void)
736{
737 struct diff_queue_struct *q = &diff_queued_diff;
738 int i;
739 for (i = 0; i < q->nr; i++)
740 if (!diff_unmodified_pair(q->queue[i]))
741 return 0;
742 return 1;
743}
744
745#if DIFF_DEBUG
746void diff_debug_filespec(struct diff_filespec *s, int x, const char *one)
747{
748 fprintf(stderr, "queue[%d] %s (%s) %s %06o %s\n",
749 x, one ? : "",
750 s->path,
751 DIFF_FILE_VALID(s) ? "valid" : "invalid",
752 s->mode,
753 s->sha1_valid ? sha1_to_hex(s->sha1) : "");
754 fprintf(stderr, "queue[%d] %s size %lu flags %d\n",
755 x, one ? : "",
756 s->size, s->xfrm_flags);
757}
758
759void diff_debug_filepair(const struct diff_filepair *p, int i)
760{
761 diff_debug_filespec(p->one, i, "one");
762 diff_debug_filespec(p->two, i, "two");
763 fprintf(stderr, "score %d, status %c source_stays %d\n",
764 p->score, p->status ? : '?', p->source_stays);
765}
766
767void diff_debug_queue(const char *msg, struct diff_queue_struct *q)
768{
769 int i;
770 if (msg)
771 fprintf(stderr, "%s\n", msg);
772 fprintf(stderr, "q->nr = %d\n", q->nr);
773 for (i = 0; i < q->nr; i++) {
774 struct diff_filepair *p = q->queue[i];
775 diff_debug_filepair(p, i);
776 }
777}
778#endif
779
780static void diff_resolve_rename_copy(void)
781{
782 int i, j;
783 struct diff_filepair *p, *pp;
784 struct diff_queue_struct *q = &diff_queued_diff;
785
786 diff_debug_queue("resolve-rename-copy", q);
787
788 for (i = 0; i < q->nr; i++) {
789 p = q->queue[i];
790 p->status = 0; /* undecided */
791 if (DIFF_PAIR_UNMERGED(p))
792 p->status = 'U';
793 else if (!DIFF_FILE_VALID(p->one))
794 p->status = 'N';
795 else if (!DIFF_FILE_VALID(p->two))
796 p->status = 'D';
797 else if (DIFF_PAIR_TYPE_CHANGED(p))
798 p->status = 'T';
799
800 /* from this point on, we are dealing with a pair
801 * whose both sides are valid and of the same type, i.e.
802 * either in-place edit or rename/copy edit.
803 */
804 else if (DIFF_PAIR_RENAME(p)) {
805 if (p->source_stays) {
806 p->status = 'C';
807 continue;
808 }
809 /* See if there is some other filepair that
810 * copies from the same source as us. If so
811 * we are a copy. Otherwise we are a rename.
812 */
813 for (j = i + 1; j < q->nr; j++) {
814 pp = q->queue[j];
815 if (strcmp(pp->one->path, p->one->path))
816 continue; /* not us */
817 if (!DIFF_PAIR_RENAME(pp))
818 continue; /* not a rename/copy */
819 /* pp is a rename/copy from the same source */
820 p->status = 'C';
821 break;
822 }
823 if (!p->status)
824 p->status = 'R';
825 }
826 else if (memcmp(p->one->sha1, p->two->sha1, 20) ||
827 p->one->mode != p->two->mode)
828 p->status = 'M';
829 else
830 /* this is a "no-change" entry.
831 * should not happen anymore.
832 * p->status = 'X';
833 */
834 die("internal error in diffcore: unmodified entry remains");
835 }
836 diff_debug_queue("resolve-rename-copy done", q);
837}
838
839void diff_flush(int diff_output_style, int resolve_rename_copy)
840{
841 struct diff_queue_struct *q = &diff_queued_diff;
842 int i;
843 int line_termination = '\n';
844 int inter_name_termination = '\t';
845
846 if (diff_output_style == DIFF_FORMAT_MACHINE)
847 line_termination = inter_name_termination = 0;
848 if (resolve_rename_copy)
849 diff_resolve_rename_copy();
850
851 for (i = 0; i < q->nr; i++) {
852 struct diff_filepair *p = q->queue[i];
853 if ((diff_output_style == DIFF_FORMAT_NO_OUTPUT) ||
854 (p->status == 'X'))
855 continue;
856 if (p->status == 0)
857 die("internal error in diff-resolve-rename-copy");
858 switch (diff_output_style) {
859 case DIFF_FORMAT_PATCH:
860 diff_flush_patch(p);
861 break;
862 case DIFF_FORMAT_HUMAN:
863 case DIFF_FORMAT_MACHINE:
864 diff_flush_raw(p, line_termination,
865 inter_name_termination);
866 break;
867 }
868 }
869 for (i = 0; i < q->nr; i++)
870 diff_free_filepair(q->queue[i]);
871 free(q->queue);
872 q->queue = NULL;
873 q->nr = q->alloc = 0;
874}
875
876void diffcore_std(const char **paths,
877 int detect_rename, int rename_score,
878 const char *pickaxe, int pickaxe_opts)
879{
880 if (paths && paths[0])
881 diffcore_pathspec(paths);
882 if (detect_rename)
883 diffcore_rename(detect_rename, rename_score);
884 if (pickaxe)
885 diffcore_pickaxe(pickaxe, pickaxe_opts);
886}
887
888void diff_addremove(int addremove, unsigned mode,
889 const unsigned char *sha1,
890 const char *base, const char *path)
891{
892 char concatpath[PATH_MAX];
893 struct diff_filespec *one, *two;
894
895 /* This may look odd, but it is a preparation for
896 * feeding "there are unchanged files which should
897 * not produce diffs, but when you are doing copy
898 * detection you would need them, so here they are"
899 * entries to the diff-core. They will be prefixed
900 * with something like '=' or '*' (I haven't decided
901 * which but should not make any difference).
902 * Feeding the same new and old to diff_change()
903 * also has the same effect.
904 * Before the final output happens, they are pruned after
905 * merged into rename/copy pairs as appropriate.
906 */
907 if (reverse_diff)
908 addremove = (addremove == '+' ? '-' :
909 addremove == '-' ? '+' : addremove);
910
911 if (!path) path = "";
912 sprintf(concatpath, "%s%s", base, path);
913 one = alloc_filespec(concatpath);
914 two = alloc_filespec(concatpath);
915
916 if (addremove != '+')
917 fill_filespec(one, sha1, mode);
918 if (addremove != '-')
919 fill_filespec(two, sha1, mode);
920
921 diff_queue(&diff_queued_diff, one, two);
922}
923
924void diff_helper_input(unsigned old_mode,
925 unsigned new_mode,
926 const unsigned char *old_sha1,
927 const unsigned char *new_sha1,
928 const char *old_path,
929 int status,
930 int score,
931 const char *new_path)
932{
933 struct diff_filespec *one, *two;
934 struct diff_filepair *dp;
935
936 one = alloc_filespec(old_path);
937 two = alloc_filespec(new_path);
938 if (old_mode)
939 fill_filespec(one, old_sha1, old_mode);
940 if (new_mode)
941 fill_filespec(two, new_sha1, new_mode);
942 dp = diff_queue(&diff_queued_diff, one, two);
943 dp->score = score * MAX_SCORE / 100;
944 dp->status = status;
945}
946
947void diff_change(unsigned old_mode, unsigned new_mode,
948 const unsigned char *old_sha1,
949 const unsigned char *new_sha1,
950 const char *base, const char *path)
951{
952 char concatpath[PATH_MAX];
953 struct diff_filespec *one, *two;
954
955 if (reverse_diff) {
956 unsigned tmp;
957 const unsigned char *tmp_c;
958 tmp = old_mode; old_mode = new_mode; new_mode = tmp;
959 tmp_c = old_sha1; old_sha1 = new_sha1; new_sha1 = tmp_c;
960 }
961 if (!path) path = "";
962 sprintf(concatpath, "%s%s", base, path);
963 one = alloc_filespec(concatpath);
964 two = alloc_filespec(concatpath);
965 fill_filespec(one, old_sha1, old_mode);
966 fill_filespec(two, new_sha1, new_mode);
967
968 diff_queue(&diff_queued_diff, one, two);
969}
970
971void diff_unmerge(const char *path)
972{
973 struct diff_filespec *one, *two;
974 one = alloc_filespec(path);
975 two = alloc_filespec(path);
976 diff_queue(&diff_queued_diff, one, two);
977}