]> git.ipfire.org Git - thirdparty/git.git/blob - diff.c
git-apply: actually apply patches and update the index
[thirdparty/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 use_size_cache;
16
17 static 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 */
53 static 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
79 static 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
86 static 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 if (xfrm_msg && xfrm_msg[0])
138 puts(xfrm_msg);
139 }
140 else if (!path1[1][0]) {
141 printf("deleted file mode %s\n", temp[0].mode);
142 if (xfrm_msg && xfrm_msg[0])
143 puts(xfrm_msg);
144 }
145 else {
146 if (strcmp(temp[0].mode, temp[1].mode)) {
147 printf("old mode %s\n", temp[0].mode);
148 printf("new mode %s\n", temp[1].mode);
149 }
150 if (xfrm_msg && xfrm_msg[0])
151 puts(xfrm_msg);
152
153 if (strncmp(temp[0].mode, temp[1].mode, 3))
154 /* we do not run diff between different kind
155 * of objects.
156 */
157 exit(0);
158 }
159 fflush(NULL);
160 execlp("/bin/sh","sh", "-c", cmd, NULL);
161 }
162
163 struct diff_filespec *alloc_filespec(const char *path)
164 {
165 int namelen = strlen(path);
166 struct diff_filespec *spec = xmalloc(sizeof(*spec) + namelen + 1);
167 spec->path = (char *)(spec + 1);
168 strcpy(spec->path, path);
169 spec->should_free = spec->should_munmap = 0;
170 spec->xfrm_flags = 0;
171 spec->size = 0;
172 spec->data = NULL;
173 spec->mode = 0;
174 memset(spec->sha1, 0, 20);
175 return spec;
176 }
177
178 void fill_filespec(struct diff_filespec *spec, const unsigned char *sha1,
179 unsigned short mode)
180 {
181 if (mode) {
182 spec->mode = DIFF_FILE_CANON_MODE(mode);
183 memcpy(spec->sha1, sha1, 20);
184 spec->sha1_valid = !!memcmp(sha1, null_sha1, 20);
185 }
186 }
187
188 /*
189 * Given a name and sha1 pair, if the dircache tells us the file in
190 * the work tree has that object contents, return true, so that
191 * prepare_temp_file() does not have to inflate and extract.
192 */
193 static int work_tree_matches(const char *name, const unsigned char *sha1)
194 {
195 struct cache_entry *ce;
196 struct stat st;
197 int pos, len;
198
199 /* We do not read the cache ourselves here, because the
200 * benchmark with my previous version that always reads cache
201 * shows that it makes things worse for diff-tree comparing
202 * two linux-2.6 kernel trees in an already checked out work
203 * tree. This is because most diff-tree comparisons deal with
204 * only a small number of files, while reading the cache is
205 * expensive for a large project, and its cost outweighs the
206 * savings we get by not inflating the object to a temporary
207 * file. Practically, this code only helps when we are used
208 * by diff-cache --cached, which does read the cache before
209 * calling us.
210 */
211 if (!active_cache)
212 return 0;
213
214 len = strlen(name);
215 pos = cache_name_pos(name, len);
216 if (pos < 0)
217 return 0;
218 ce = active_cache[pos];
219 if ((lstat(name, &st) < 0) ||
220 !S_ISREG(st.st_mode) || /* careful! */
221 ce_match_stat(ce, &st) ||
222 memcmp(sha1, ce->sha1, 20))
223 return 0;
224 /* we return 1 only when we can stat, it is a regular file,
225 * stat information matches, and sha1 recorded in the cache
226 * matches. I.e. we know the file in the work tree really is
227 * the same as the <name, sha1> pair.
228 */
229 return 1;
230 }
231
232 static struct sha1_size_cache {
233 unsigned char sha1[20];
234 unsigned long size;
235 } **sha1_size_cache;
236 static int sha1_size_cache_nr, sha1_size_cache_alloc;
237
238 static struct sha1_size_cache *locate_size_cache(unsigned char *sha1,
239 unsigned long size)
240 {
241 int first, last;
242 struct sha1_size_cache *e;
243
244 first = 0;
245 last = sha1_size_cache_nr;
246 while (last > first) {
247 int next = (last + first) >> 1;
248 e = sha1_size_cache[next];
249 int cmp = memcmp(e->sha1, sha1, 20);
250 if (!cmp)
251 return e;
252 if (cmp < 0) {
253 last = next;
254 continue;
255 }
256 first = next+1;
257 }
258 /* not found */
259 if (size == UINT_MAX)
260 return NULL;
261 /* insert to make it at "first" */
262 if (sha1_size_cache_alloc <= sha1_size_cache_nr) {
263 sha1_size_cache_alloc = alloc_nr(sha1_size_cache_alloc);
264 sha1_size_cache = xrealloc(sha1_size_cache,
265 sha1_size_cache_alloc *
266 sizeof(*sha1_size_cache));
267 }
268 sha1_size_cache_nr++;
269 if (first < sha1_size_cache_nr)
270 memmove(sha1_size_cache + first + 1, sha1_size_cache + first,
271 (sha1_size_cache_nr - first - 1) *
272 sizeof(*sha1_size_cache));
273 e = xmalloc(sizeof(struct sha1_size_cache));
274 sha1_size_cache[first] = e;
275 memcpy(e->sha1, sha1, 20);
276 e->size = size;
277 return e;
278 }
279
280 /*
281 * While doing rename detection and pickaxe operation, we may need to
282 * grab the data for the blob (or file) for our own in-core comparison.
283 * diff_filespec has data and size fields for this purpose.
284 */
285 int diff_populate_filespec(struct diff_filespec *s, int size_only)
286 {
287 int err = 0;
288 if (!DIFF_FILE_VALID(s))
289 die("internal error: asking to populate invalid file.");
290 if (S_ISDIR(s->mode))
291 return -1;
292
293 if (!use_size_cache)
294 size_only = 0;
295
296 if (s->data)
297 return err;
298 if (!s->sha1_valid ||
299 work_tree_matches(s->path, s->sha1)) {
300 struct stat st;
301 int fd;
302 if (lstat(s->path, &st) < 0) {
303 if (errno == ENOENT) {
304 err_empty:
305 err = -1;
306 empty:
307 s->data = "";
308 s->size = 0;
309 return err;
310 }
311 }
312 s->size = st.st_size;
313 if (!s->size)
314 goto empty;
315 if (size_only)
316 return 0;
317 if (S_ISLNK(st.st_mode)) {
318 int ret;
319 s->data = xmalloc(s->size);
320 s->should_free = 1;
321 ret = readlink(s->path, s->data, s->size);
322 if (ret < 0) {
323 free(s->data);
324 goto err_empty;
325 }
326 return 0;
327 }
328 fd = open(s->path, O_RDONLY);
329 if (fd < 0)
330 goto err_empty;
331 s->data = mmap(NULL, s->size, PROT_READ, MAP_PRIVATE, fd, 0);
332 s->should_munmap = 1;
333 close(fd);
334 }
335 else {
336 char type[20];
337 struct sha1_size_cache *e;
338
339 if (size_only) {
340 e = locate_size_cache(s->sha1, UINT_MAX);
341 if (e) {
342 s->size = e->size;
343 return 0;
344 }
345 if (!sha1_file_size(s->sha1, &s->size))
346 locate_size_cache(s->sha1, s->size);
347 }
348 else {
349 s->data = read_sha1_file(s->sha1, type, &s->size);
350 s->should_free = 1;
351 }
352 }
353 return 0;
354 }
355
356 void diff_free_filespec_data(struct diff_filespec *s)
357 {
358 if (s->should_free)
359 free(s->data);
360 else if (s->should_munmap)
361 munmap(s->data, s->size);
362 s->should_free = s->should_munmap = 0;
363 s->data = NULL;
364 }
365
366 static void prep_temp_blob(struct diff_tempfile *temp,
367 void *blob,
368 unsigned long size,
369 unsigned char *sha1,
370 int mode)
371 {
372 int fd;
373
374 strcpy(temp->tmp_path, ".diff_XXXXXX");
375 fd = mkstemp(temp->tmp_path);
376 if (fd < 0)
377 die("unable to create temp-file");
378 if (write(fd, blob, size) != size)
379 die("unable to write temp-file");
380 close(fd);
381 temp->name = temp->tmp_path;
382 strcpy(temp->hex, sha1_to_hex(sha1));
383 temp->hex[40] = 0;
384 sprintf(temp->mode, "%06o", mode);
385 }
386
387 static void prepare_temp_file(const char *name,
388 struct diff_tempfile *temp,
389 struct diff_filespec *one)
390 {
391 if (!DIFF_FILE_VALID(one)) {
392 not_a_valid_file:
393 /* A '-' entry produces this for file-2, and
394 * a '+' entry produces this for file-1.
395 */
396 temp->name = "/dev/null";
397 strcpy(temp->hex, ".");
398 strcpy(temp->mode, ".");
399 return;
400 }
401
402 if (!one->sha1_valid ||
403 work_tree_matches(name, one->sha1)) {
404 struct stat st;
405 if (lstat(name, &st) < 0) {
406 if (errno == ENOENT)
407 goto not_a_valid_file;
408 die("stat(%s): %s", name, strerror(errno));
409 }
410 if (S_ISLNK(st.st_mode)) {
411 int ret;
412 char *buf, buf_[1024];
413 buf = ((sizeof(buf_) < st.st_size) ?
414 xmalloc(st.st_size) : buf_);
415 ret = readlink(name, buf, st.st_size);
416 if (ret < 0)
417 die("readlink(%s)", name);
418 prep_temp_blob(temp, buf, st.st_size,
419 (one->sha1_valid ?
420 one->sha1 : null_sha1),
421 (one->sha1_valid ?
422 one->mode : S_IFLNK));
423 }
424 else {
425 /* we can borrow from the file in the work tree */
426 temp->name = name;
427 if (!one->sha1_valid)
428 strcpy(temp->hex, sha1_to_hex(null_sha1));
429 else
430 strcpy(temp->hex, sha1_to_hex(one->sha1));
431 /* Even though we may sometimes borrow the
432 * contents from the work tree, we always want
433 * one->mode. mode is trustworthy even when
434 * !(one->sha1_valid), as long as
435 * DIFF_FILE_VALID(one).
436 */
437 sprintf(temp->mode, "%06o", one->mode);
438 }
439 return;
440 }
441 else {
442 if (diff_populate_filespec(one, 0))
443 die("cannot read data blob for %s", one->path);
444 prep_temp_blob(temp, one->data, one->size,
445 one->sha1, one->mode);
446 }
447 }
448
449 static void remove_tempfile(void)
450 {
451 int i;
452
453 for (i = 0; i < 2; i++)
454 if (diff_temp[i].name == diff_temp[i].tmp_path) {
455 unlink(diff_temp[i].name);
456 diff_temp[i].name = NULL;
457 }
458 }
459
460 static void remove_tempfile_on_signal(int signo)
461 {
462 remove_tempfile();
463 }
464
465 /* An external diff command takes:
466 *
467 * diff-cmd name infile1 infile1-sha1 infile1-mode \
468 * infile2 infile2-sha1 infile2-mode [ rename-to ]
469 *
470 */
471 static void run_external_diff(const char *pgm,
472 const char *name,
473 const char *other,
474 struct diff_filespec *one,
475 struct diff_filespec *two,
476 const char *xfrm_msg)
477 {
478 struct diff_tempfile *temp = diff_temp;
479 pid_t pid;
480 int status;
481 static int atexit_asked = 0;
482
483 if (one && two) {
484 prepare_temp_file(name, &temp[0], one);
485 prepare_temp_file(other ? : name, &temp[1], two);
486 if (! atexit_asked &&
487 (temp[0].name == temp[0].tmp_path ||
488 temp[1].name == temp[1].tmp_path)) {
489 atexit_asked = 1;
490 atexit(remove_tempfile);
491 }
492 signal(SIGINT, remove_tempfile_on_signal);
493 }
494
495 fflush(NULL);
496 pid = fork();
497 if (pid < 0)
498 die("unable to fork");
499 if (!pid) {
500 if (pgm) {
501 if (one && two) {
502 const char *exec_arg[10];
503 const char **arg = &exec_arg[0];
504 *arg++ = pgm;
505 *arg++ = name;
506 *arg++ = temp[0].name;
507 *arg++ = temp[0].hex;
508 *arg++ = temp[0].mode;
509 *arg++ = temp[1].name;
510 *arg++ = temp[1].hex;
511 *arg++ = temp[1].mode;
512 if (other) {
513 *arg++ = other;
514 *arg++ = xfrm_msg;
515 }
516 *arg = NULL;
517 execvp(pgm, (char *const*) exec_arg);
518 }
519 else
520 execlp(pgm, pgm, name, NULL);
521 }
522 /*
523 * otherwise we use the built-in one.
524 */
525 if (one && two)
526 builtin_diff(name, other ? : name, temp, xfrm_msg);
527 else
528 printf("* Unmerged path %s\n", name);
529 exit(0);
530 }
531 if (waitpid(pid, &status, 0) < 0 ||
532 !WIFEXITED(status) || WEXITSTATUS(status)) {
533 /* Earlier we did not check the exit status because
534 * diff exits non-zero if files are different, and
535 * we are not interested in knowing that. It was a
536 * mistake which made it harder to quit a diff-*
537 * session that uses the git-apply-patch-script as
538 * the GIT_EXTERNAL_DIFF. A custom GIT_EXTERNAL_DIFF
539 * should also exit non-zero only when it wants to
540 * abort the entire diff-* session.
541 */
542 remove_tempfile();
543 fprintf(stderr, "external diff died, stopping at %s.\n", name);
544 exit(1);
545 }
546 remove_tempfile();
547 }
548
549 static void run_diff(const char *name,
550 const char *other,
551 struct diff_filespec *one,
552 struct diff_filespec *two,
553 const char *xfrm_msg)
554 {
555 const char *pgm = external_diff();
556 if (!pgm &&
557 DIFF_FILE_VALID(one) && DIFF_FILE_VALID(two) &&
558 (S_IFMT & one->mode) != (S_IFMT & two->mode)) {
559 /* a filepair that changes between file and symlink
560 * needs to be split into deletion and creation.
561 */
562 struct diff_filespec *null = alloc_filespec(two->path);
563 run_external_diff(NULL, name, other, one, null, xfrm_msg);
564 free(null);
565 null = alloc_filespec(one->path);
566 run_external_diff(NULL, name, other, null, two, xfrm_msg);
567 free(null);
568 }
569 else
570 run_external_diff(pgm, name, other, one, two, xfrm_msg);
571 }
572
573 void diff_setup(int flags)
574 {
575 if (flags & DIFF_SETUP_REVERSE)
576 reverse_diff = 1;
577 if (flags & DIFF_SETUP_USE_CACHE) {
578 if (!active_cache)
579 /* read-cache does not die even when it fails
580 * so it is safe for us to do this here. Also
581 * it does not smudge active_cache or active_nr
582 * when it fails, so we do not have to worry about
583 * cleaning it up oufselves either.
584 */
585 read_cache();
586 }
587 if (flags & DIFF_SETUP_USE_SIZE_CACHE)
588 use_size_cache = 1;
589
590 }
591
592 static int parse_num(const char **cp_p)
593 {
594 int num, scale, ch, cnt;
595 const char *cp = *cp_p;
596
597 cnt = num = 0;
598 scale = 1;
599 while ('0' <= (ch = *cp) && ch <= '9') {
600 if (cnt++ < 5) {
601 /* We simply ignore more than 5 digits precision. */
602 scale *= 10;
603 num = num * 10 + ch - '0';
604 }
605 *cp++;
606 }
607 *cp_p = cp;
608
609 /* user says num divided by scale and we say internally that
610 * is MAX_SCORE * num / scale.
611 */
612 return (MAX_SCORE * num / scale);
613 }
614
615 int diff_scoreopt_parse(const char *opt)
616 {
617 int opt1, opt2, cmd;
618
619 if (*opt++ != '-')
620 return -1;
621 cmd = *opt++;
622 if (cmd != 'M' && cmd != 'C' && cmd != 'B')
623 return -1; /* that is not a -M, -C nor -B option */
624
625 opt1 = parse_num(&opt);
626 if (cmd != 'B')
627 opt2 = 0;
628 else {
629 if (*opt == 0)
630 opt2 = 0;
631 else if (*opt != '/')
632 return -1; /* we expect -B80/99 or -B80 */
633 else {
634 opt++;
635 opt2 = parse_num(&opt);
636 }
637 }
638 if (*opt != 0)
639 return -1;
640 return opt1 | (opt2 << 16);
641 }
642
643 struct diff_queue_struct diff_queued_diff;
644
645 void diff_q(struct diff_queue_struct *queue, struct diff_filepair *dp)
646 {
647 if (queue->alloc <= queue->nr) {
648 queue->alloc = alloc_nr(queue->alloc);
649 queue->queue = xrealloc(queue->queue,
650 sizeof(dp) * queue->alloc);
651 }
652 queue->queue[queue->nr++] = dp;
653 }
654
655 struct diff_filepair *diff_queue(struct diff_queue_struct *queue,
656 struct diff_filespec *one,
657 struct diff_filespec *two)
658 {
659 struct diff_filepair *dp = xmalloc(sizeof(*dp));
660 dp->one = one;
661 dp->two = two;
662 dp->score = 0;
663 dp->source_stays = 0;
664 dp->broken_pair = 0;
665 diff_q(queue, dp);
666 return dp;
667 }
668
669 void diff_free_filepair(struct diff_filepair *p)
670 {
671 diff_free_filespec_data(p->one);
672 diff_free_filespec_data(p->two);
673 free(p);
674 }
675
676 static void diff_flush_raw(struct diff_filepair *p,
677 int line_termination,
678 int inter_name_termination)
679 {
680 int two_paths;
681 char status[10];
682
683 if (line_termination) {
684 const char *err = "path %s cannot be expressed without -z";
685 if (strchr(p->one->path, line_termination) ||
686 strchr(p->one->path, inter_name_termination))
687 die(err, p->one->path);
688 if (strchr(p->two->path, line_termination) ||
689 strchr(p->two->path, inter_name_termination))
690 die(err, p->two->path);
691 }
692
693 switch (p->status) {
694 case 'C': case 'R':
695 two_paths = 1;
696 sprintf(status, "%c%03d", p->status,
697 (int)(0.5 + p->score * 100.0/MAX_SCORE));
698 break;
699 case 'N': case 'D':
700 two_paths = 0;
701 if (p->score)
702 sprintf(status, "%c%03d", p->status,
703 (int)(0.5 + p->score * 100.0/MAX_SCORE));
704 else {
705 status[0] = p->status;
706 status[1] = 0;
707 }
708 break;
709 default:
710 two_paths = 0;
711 status[0] = p->status;
712 status[1] = 0;
713 break;
714 }
715 printf(":%06o %06o %s ",
716 p->one->mode, p->two->mode, sha1_to_hex(p->one->sha1));
717 printf("%s %s%c%s",
718 sha1_to_hex(p->two->sha1),
719 status,
720 inter_name_termination,
721 p->one->path);
722 if (two_paths)
723 printf("%c%s", inter_name_termination, p->two->path);
724 putchar(line_termination);
725 }
726
727 int diff_unmodified_pair(struct diff_filepair *p)
728 {
729 /* This function is written stricter than necessary to support
730 * the currently implemented transformers, but the idea is to
731 * let transformers to produce diff_filepairs any way they want,
732 * and filter and clean them up here before producing the output.
733 */
734 struct diff_filespec *one, *two;
735
736 if (DIFF_PAIR_UNMERGED(p))
737 return 0; /* unmerged is interesting */
738
739 one = p->one;
740 two = p->two;
741
742 /* deletion, addition, mode or type change
743 * and rename are all interesting.
744 */
745 if (DIFF_FILE_VALID(one) != DIFF_FILE_VALID(two) ||
746 DIFF_PAIR_MODE_CHANGED(p) ||
747 strcmp(one->path, two->path))
748 return 0;
749
750 /* both are valid and point at the same path. that is, we are
751 * dealing with a change.
752 */
753 if (one->sha1_valid && two->sha1_valid &&
754 !memcmp(one->sha1, two->sha1, sizeof(one->sha1)))
755 return 1; /* no change */
756 if (!one->sha1_valid && !two->sha1_valid)
757 return 1; /* both look at the same file on the filesystem. */
758 return 0;
759 }
760
761 static void diff_flush_patch(struct diff_filepair *p)
762 {
763 const char *name, *other;
764 char msg_[PATH_MAX*2+200], *msg;
765
766 if (diff_unmodified_pair(p))
767 return;
768
769 name = p->one->path;
770 other = (strcmp(name, p->two->path) ? p->two->path : NULL);
771 if ((DIFF_FILE_VALID(p->one) && S_ISDIR(p->one->mode)) ||
772 (DIFF_FILE_VALID(p->two) && S_ISDIR(p->two->mode)))
773 return; /* no tree diffs in patch format */
774
775 switch (p->status) {
776 case 'C':
777 sprintf(msg_,
778 "similarity index %d%%\n"
779 "copy from %s\n"
780 "copy to %s",
781 (int)(0.5 + p->score * 100.0/MAX_SCORE),
782 p->one->path, p->two->path);
783 msg = msg_;
784 break;
785 case 'R':
786 sprintf(msg_,
787 "similarity index %d%%\n"
788 "rename old %s\n"
789 "rename new %s",
790 (int)(0.5 + p->score * 100.0/MAX_SCORE),
791 p->one->path, p->two->path);
792 msg = msg_;
793 break;
794 case 'D': case 'N':
795 if (DIFF_PAIR_BROKEN(p)) {
796 sprintf(msg_,
797 "dissimilarity index %d%%",
798 (int)(0.5 + p->score * 100.0/MAX_SCORE));
799 msg = msg_;
800 }
801 else
802 msg = NULL;
803 break;
804 default:
805 msg = NULL;
806 }
807
808 if (DIFF_PAIR_UNMERGED(p))
809 run_diff(name, NULL, NULL, NULL, NULL);
810 else
811 run_diff(name, other, p->one, p->two, msg);
812 }
813
814 int diff_queue_is_empty(void)
815 {
816 struct diff_queue_struct *q = &diff_queued_diff;
817 int i;
818 for (i = 0; i < q->nr; i++)
819 if (!diff_unmodified_pair(q->queue[i]))
820 return 0;
821 return 1;
822 }
823
824 #if DIFF_DEBUG
825 void diff_debug_filespec(struct diff_filespec *s, int x, const char *one)
826 {
827 fprintf(stderr, "queue[%d] %s (%s) %s %06o %s\n",
828 x, one ? : "",
829 s->path,
830 DIFF_FILE_VALID(s) ? "valid" : "invalid",
831 s->mode,
832 s->sha1_valid ? sha1_to_hex(s->sha1) : "");
833 fprintf(stderr, "queue[%d] %s size %lu flags %d\n",
834 x, one ? : "",
835 s->size, s->xfrm_flags);
836 }
837
838 void diff_debug_filepair(const struct diff_filepair *p, int i)
839 {
840 diff_debug_filespec(p->one, i, "one");
841 diff_debug_filespec(p->two, i, "two");
842 fprintf(stderr, "score %d, status %c stays %d broken %d\n",
843 p->score, p->status ? : '?',
844 p->source_stays, p->broken_pair);
845 }
846
847 void diff_debug_queue(const char *msg, struct diff_queue_struct *q)
848 {
849 int i;
850 if (msg)
851 fprintf(stderr, "%s\n", msg);
852 fprintf(stderr, "q->nr = %d\n", q->nr);
853 for (i = 0; i < q->nr; i++) {
854 struct diff_filepair *p = q->queue[i];
855 diff_debug_filepair(p, i);
856 }
857 }
858 #endif
859
860 static void diff_resolve_rename_copy(void)
861 {
862 int i, j;
863 struct diff_filepair *p, *pp;
864 struct diff_queue_struct *q = &diff_queued_diff;
865
866 diff_debug_queue("resolve-rename-copy", q);
867
868 for (i = 0; i < q->nr; i++) {
869 p = q->queue[i];
870 p->status = 0; /* undecided */
871 if (DIFF_PAIR_UNMERGED(p))
872 p->status = 'U';
873 else if (!DIFF_FILE_VALID(p->one))
874 p->status = 'N';
875 else if (!DIFF_FILE_VALID(p->two))
876 p->status = 'D';
877 else if (DIFF_PAIR_TYPE_CHANGED(p))
878 p->status = 'T';
879
880 /* from this point on, we are dealing with a pair
881 * whose both sides are valid and of the same type, i.e.
882 * either in-place edit or rename/copy edit.
883 */
884 else if (DIFF_PAIR_RENAME(p)) {
885 if (p->source_stays) {
886 p->status = 'C';
887 continue;
888 }
889 /* See if there is some other filepair that
890 * copies from the same source as us. If so
891 * we are a copy. Otherwise we are a rename.
892 */
893 for (j = i + 1; j < q->nr; j++) {
894 pp = q->queue[j];
895 if (strcmp(pp->one->path, p->one->path))
896 continue; /* not us */
897 if (!DIFF_PAIR_RENAME(pp))
898 continue; /* not a rename/copy */
899 /* pp is a rename/copy from the same source */
900 p->status = 'C';
901 break;
902 }
903 if (!p->status)
904 p->status = 'R';
905 }
906 else if (memcmp(p->one->sha1, p->two->sha1, 20) ||
907 p->one->mode != p->two->mode)
908 p->status = 'M';
909 else {
910 /* This is a "no-change" entry and should not
911 * happen anymore, but prepare for broken callers.
912 */
913 error("feeding unmodified %s to diffcore",
914 p->one->path);
915 p->status = 'X';
916 }
917 }
918 diff_debug_queue("resolve-rename-copy done", q);
919 }
920
921 void diff_flush(int diff_output_style, int resolve_rename_copy)
922 {
923 struct diff_queue_struct *q = &diff_queued_diff;
924 int i;
925 int line_termination = '\n';
926 int inter_name_termination = '\t';
927
928 if (diff_output_style == DIFF_FORMAT_MACHINE)
929 line_termination = inter_name_termination = 0;
930 if (resolve_rename_copy)
931 diff_resolve_rename_copy();
932
933 for (i = 0; i < q->nr; i++) {
934 struct diff_filepair *p = q->queue[i];
935 if ((diff_output_style == DIFF_FORMAT_NO_OUTPUT) ||
936 (p->status == 'X'))
937 continue;
938 if (p->status == 0)
939 die("internal error in diff-resolve-rename-copy");
940 switch (diff_output_style) {
941 case DIFF_FORMAT_PATCH:
942 diff_flush_patch(p);
943 break;
944 case DIFF_FORMAT_HUMAN:
945 case DIFF_FORMAT_MACHINE:
946 diff_flush_raw(p, line_termination,
947 inter_name_termination);
948 break;
949 }
950 }
951 for (i = 0; i < q->nr; i++)
952 diff_free_filepair(q->queue[i]);
953 free(q->queue);
954 q->queue = NULL;
955 q->nr = q->alloc = 0;
956 }
957
958 void diffcore_std(const char **paths,
959 int detect_rename, int rename_score,
960 const char *pickaxe, int pickaxe_opts,
961 int break_opt,
962 const char *orderfile)
963 {
964 if (paths && paths[0])
965 diffcore_pathspec(paths);
966 if (0 <= break_opt)
967 diffcore_break(break_opt);
968 if (detect_rename)
969 diffcore_rename(detect_rename, rename_score);
970 if (0 <= break_opt)
971 diffcore_merge_broken();
972 if (pickaxe)
973 diffcore_pickaxe(pickaxe, pickaxe_opts);
974 if (orderfile)
975 diffcore_order(orderfile);
976 }
977
978 void diff_addremove(int addremove, unsigned mode,
979 const unsigned char *sha1,
980 const char *base, const char *path)
981 {
982 char concatpath[PATH_MAX];
983 struct diff_filespec *one, *two;
984
985 /* This may look odd, but it is a preparation for
986 * feeding "there are unchanged files which should
987 * not produce diffs, but when you are doing copy
988 * detection you would need them, so here they are"
989 * entries to the diff-core. They will be prefixed
990 * with something like '=' or '*' (I haven't decided
991 * which but should not make any difference).
992 * Feeding the same new and old to diff_change()
993 * also has the same effect.
994 * Before the final output happens, they are pruned after
995 * merged into rename/copy pairs as appropriate.
996 */
997 if (reverse_diff)
998 addremove = (addremove == '+' ? '-' :
999 addremove == '-' ? '+' : addremove);
1000
1001 if (!path) path = "";
1002 sprintf(concatpath, "%s%s", base, path);
1003 one = alloc_filespec(concatpath);
1004 two = alloc_filespec(concatpath);
1005
1006 if (addremove != '+')
1007 fill_filespec(one, sha1, mode);
1008 if (addremove != '-')
1009 fill_filespec(two, sha1, mode);
1010
1011 diff_queue(&diff_queued_diff, one, two);
1012 }
1013
1014 void diff_helper_input(unsigned old_mode,
1015 unsigned new_mode,
1016 const unsigned char *old_sha1,
1017 const unsigned char *new_sha1,
1018 const char *old_path,
1019 int status,
1020 int score,
1021 const char *new_path)
1022 {
1023 struct diff_filespec *one, *two;
1024 struct diff_filepair *dp;
1025
1026 one = alloc_filespec(old_path);
1027 two = alloc_filespec(new_path);
1028 if (old_mode)
1029 fill_filespec(one, old_sha1, old_mode);
1030 if (new_mode)
1031 fill_filespec(two, new_sha1, new_mode);
1032 dp = diff_queue(&diff_queued_diff, one, two);
1033 dp->score = score * MAX_SCORE / 100;
1034 dp->status = status;
1035 }
1036
1037 void diff_change(unsigned old_mode, unsigned new_mode,
1038 const unsigned char *old_sha1,
1039 const unsigned char *new_sha1,
1040 const char *base, const char *path)
1041 {
1042 char concatpath[PATH_MAX];
1043 struct diff_filespec *one, *two;
1044
1045 if (reverse_diff) {
1046 unsigned tmp;
1047 const unsigned char *tmp_c;
1048 tmp = old_mode; old_mode = new_mode; new_mode = tmp;
1049 tmp_c = old_sha1; old_sha1 = new_sha1; new_sha1 = tmp_c;
1050 }
1051 if (!path) path = "";
1052 sprintf(concatpath, "%s%s", base, path);
1053 one = alloc_filespec(concatpath);
1054 two = alloc_filespec(concatpath);
1055 fill_filespec(one, old_sha1, old_mode);
1056 fill_filespec(two, new_sha1, new_mode);
1057
1058 diff_queue(&diff_queued_diff, one, two);
1059 }
1060
1061 void diff_unmerge(const char *path)
1062 {
1063 struct diff_filespec *one, *two;
1064 one = alloc_filespec(path);
1065 two = alloc_filespec(path);
1066 diff_queue(&diff_queued_diff, one, two);
1067 }