c4b6b76f504f9aa7574f04febb5673d32c4e9ce7
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 "delta.h"
11
12static const char *diff_opts = "-pu";
13static unsigned char null_sha1[20] = { 0, };
14#define MAX_SCORE 10000
15#define DEFAULT_MINIMUM_SCORE 5000
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;
81 char hex[41];
82 char mode[10];
83 char tmp_path[50];
84} diff_temp[2];
85
86struct diff_spec {
87 unsigned char blob_sha1[20];
88 unsigned short mode; /* file mode */
89 unsigned sha1_valid : 1; /* if true, use blob_sha1 and trust mode;
90 * if false, use the name and read from
91 * the filesystem.
92 */
93 unsigned file_valid : 1; /* if false the file does not exist */
94};
95
96static void builtin_diff(const char *name_a,
97 const char *name_b,
98 struct diff_tempfile *temp,
99 int rename_score)
100{
101 int i, next_at, cmd_size;
102 const char *diff_cmd = "diff -L'%s%s' -L'%s%s'";
103 const char *diff_arg = "'%s' '%s'||:"; /* "||:" is to return 0 */
104 const char *input_name_sq[2];
105 const char *path0[2];
106 const char *path1[2];
107 const char *name_sq[2];
108 char *cmd;
109
110 name_sq[0] = sq_expand(name_a);
111 name_sq[1] = sq_expand(name_b);
112
113 /* diff_cmd and diff_arg have 6 %s in total which makes
114 * the sum of these strings 12 bytes larger than required.
115 * we use 2 spaces around diff-opts, and we need to count
116 * terminating NUL, so we subtract 9 here.
117 */
118 cmd_size = (strlen(diff_cmd) + strlen(diff_opts) +
119 strlen(diff_arg) - 9);
120 for (i = 0; i < 2; i++) {
121 input_name_sq[i] = sq_expand(temp[i].name);
122 if (!strcmp(temp[i].name, "/dev/null")) {
123 path0[i] = "/dev/null";
124 path1[i] = "";
125 } else {
126 path0[i] = i ? "b/" : "a/";
127 path1[i] = name_sq[i];
128 }
129 cmd_size += (strlen(path0[i]) + strlen(path1[i]) +
130 strlen(input_name_sq[i]));
131 }
132
133 cmd = xmalloc(cmd_size);
134
135 next_at = 0;
136 next_at += snprintf(cmd+next_at, cmd_size-next_at,
137 diff_cmd,
138 path0[0], path1[0], path0[1], path1[1]);
139 next_at += snprintf(cmd+next_at, cmd_size-next_at,
140 " %s ", diff_opts);
141 next_at += snprintf(cmd+next_at, cmd_size-next_at,
142 diff_arg, input_name_sq[0], input_name_sq[1]);
143
144 printf("diff --git a/%s b/%s\n", name_a, name_b);
145 if (!path1[0][0])
146 printf("new file mode %s\n", temp[1].mode);
147 else if (!path1[1][0])
148 printf("deleted file mode %s\n", temp[0].mode);
149 else {
150 if (strcmp(temp[0].mode, temp[1].mode)) {
151 printf("old mode %s\n", temp[0].mode);
152 printf("new mode %s\n", temp[1].mode);
153 }
154 if (strcmp(name_a, name_b)) {
155 if (0 < rename_score)
156 printf("rename similarity index %d%%\n",
157 (int)(0.5+
158 rename_score*100.0/MAX_SCORE));
159 printf("rename old %s\n", name_a);
160 printf("rename new %s\n", name_b);
161 }
162 if (strncmp(temp[0].mode, temp[1].mode, 3))
163 /* we do not run diff between different kind
164 * of objects.
165 */
166 exit(0);
167 }
168 fflush(NULL);
169 execlp("/bin/sh","sh", "-c", cmd, NULL);
170}
171
172/*
173 * Given a name and sha1 pair, if the dircache tells us the file in
174 * the work tree has that object contents, return true, so that
175 * prepare_temp_file() does not have to inflate and extract.
176 */
177static int work_tree_matches(const char *name, const unsigned char *sha1)
178{
179 struct cache_entry *ce;
180 struct stat st;
181 int pos, len;
182
183 /* We do not read the cache ourselves here, because the
184 * benchmark with my previous version that always reads cache
185 * shows that it makes things worse for diff-tree comparing
186 * two linux-2.6 kernel trees in an already checked out work
187 * tree. This is because most diff-tree comparisons deal with
188 * only a small number of files, while reading the cache is
189 * expensive for a large project, and its cost outweighs the
190 * savings we get by not inflating the object to a temporary
191 * file. Practically, this code only helps when we are used
192 * by diff-cache --cached, which does read the cache before
193 * calling us.
194 */
195 if (!active_cache)
196 return 0;
197
198 len = strlen(name);
199 pos = cache_name_pos(name, len);
200 if (pos < 0)
201 return 0;
202 ce = active_cache[pos];
203 if ((lstat(name, &st) < 0) ||
204 !S_ISREG(st.st_mode) ||
205 ce_match_stat(ce, &st) ||
206 memcmp(sha1, ce->sha1, 20))
207 return 0;
208 return 1;
209}
210
211static void prep_temp_blob(struct diff_tempfile *temp,
212 void *blob,
213 unsigned long size,
214 unsigned char *sha1,
215 int mode)
216{
217 int fd;
218
219 strcpy(temp->tmp_path, ".diff_XXXXXX");
220 fd = mkstemp(temp->tmp_path);
221 if (fd < 0)
222 die("unable to create temp-file");
223 if (write(fd, blob, size) != size)
224 die("unable to write temp-file");
225 close(fd);
226 temp->name = temp->tmp_path;
227 strcpy(temp->hex, sha1_to_hex(sha1));
228 temp->hex[40] = 0;
229 sprintf(temp->mode, "%06o", mode);
230}
231
232static void prepare_temp_file(const char *name,
233 struct diff_tempfile *temp,
234 struct diff_spec *one)
235{
236 if (!one->file_valid) {
237 not_a_valid_file:
238 /* A '-' entry produces this for file-2, and
239 * a '+' entry produces this for file-1.
240 */
241 temp->name = "/dev/null";
242 strcpy(temp->hex, ".");
243 strcpy(temp->mode, ".");
244 return;
245 }
246
247 if (!one->sha1_valid ||
248 work_tree_matches(name, one->blob_sha1)) {
249 struct stat st;
250 temp->name = name;
251 if (lstat(temp->name, &st) < 0) {
252 if (errno == ENOENT)
253 goto not_a_valid_file;
254 die("stat(%s): %s", temp->name, strerror(errno));
255 }
256 if (S_ISLNK(st.st_mode)) {
257 int ret;
258 char *buf, buf_[1024];
259 buf = ((sizeof(buf_) < st.st_size) ?
260 xmalloc(st.st_size) : buf_);
261 ret = readlink(name, buf, st.st_size);
262 if (ret < 0)
263 die("readlink(%s)", name);
264 prep_temp_blob(temp, buf, st.st_size,
265 (one->sha1_valid ?
266 one->blob_sha1 : null_sha1),
267 (one->sha1_valid ?
268 one->mode : S_IFLNK));
269 }
270 else {
271 if (!one->sha1_valid)
272 strcpy(temp->hex, sha1_to_hex(null_sha1));
273 else
274 strcpy(temp->hex, sha1_to_hex(one->blob_sha1));
275 sprintf(temp->mode, "%06o",
276 S_IFREG |ce_permissions(st.st_mode));
277 }
278 return;
279 }
280 else {
281 void *blob;
282 char type[20];
283 unsigned long size;
284
285 blob = read_sha1_file(one->blob_sha1, type, &size);
286 if (!blob || strcmp(type, "blob"))
287 die("unable to read blob object for %s (%s)",
288 name, sha1_to_hex(one->blob_sha1));
289 prep_temp_blob(temp, blob, size, one->blob_sha1, one->mode);
290 free(blob);
291 }
292}
293
294static void remove_tempfile(void)
295{
296 int i;
297
298 for (i = 0; i < 2; i++)
299 if (diff_temp[i].name == diff_temp[i].tmp_path) {
300 unlink(diff_temp[i].name);
301 diff_temp[i].name = NULL;
302 }
303}
304
305static void remove_tempfile_on_signal(int signo)
306{
307 remove_tempfile();
308}
309
310static int detect_rename;
311static int reverse_diff;
312static int diff_raw_output = -1;
313static const char **pathspec;
314static int speccnt;
315static int minimum_score;
316
317static int matches_pathspec(const char *name)
318{
319 int i;
320 int namelen;
321
322 if (speccnt == 0)
323 return 1;
324
325 namelen = strlen(name);
326 for (i = 0; i < speccnt; i++) {
327 int speclen = strlen(pathspec[i]);
328 if (! strncmp(pathspec[i], name, speclen) &&
329 speclen <= namelen &&
330 (name[speclen] == 0 || name[speclen] == '/'))
331 return 1;
332 }
333 return 0;
334}
335
336/* An external diff command takes:
337 *
338 * diff-cmd name infile1 infile1-sha1 infile1-mode \
339 * infile2 infile2-sha1 infile2-mode [ rename-to ]
340 *
341 */
342static void run_external_diff(const char *name,
343 const char *other,
344 struct diff_spec *one,
345 struct diff_spec *two,
346 int rename_score)
347{
348 struct diff_tempfile *temp = diff_temp;
349 pid_t pid;
350 int status;
351 static int atexit_asked = 0;
352
353 if (reverse_diff) {
354 struct diff_spec *tmp_spec;
355 tmp_spec = one; one = two; two = tmp_spec;
356 if (other) {
357 const char *tmp;
358 tmp = name; name = other; other = tmp;
359 }
360 }
361
362 if (!matches_pathspec(name) && (!other || !matches_pathspec(other)))
363 return;
364
365 if (one && two) {
366 prepare_temp_file(name, &temp[0], one);
367 prepare_temp_file(other ? : name, &temp[1], two);
368 if (! atexit_asked &&
369 (temp[0].name == temp[0].tmp_path ||
370 temp[1].name == temp[1].tmp_path)) {
371 atexit_asked = 1;
372 atexit(remove_tempfile);
373 }
374 signal(SIGINT, remove_tempfile_on_signal);
375 }
376
377 fflush(NULL);
378 pid = fork();
379 if (pid < 0)
380 die("unable to fork");
381 if (!pid) {
382 const char *pgm = external_diff();
383 if (pgm) {
384 if (one && two) {
385 const char *exec_arg[9];
386 const char **arg = &exec_arg[0];
387 *arg++ = pgm;
388 *arg++ = name;
389 *arg++ = temp[0].name;
390 *arg++ = temp[0].hex;
391 *arg++ = temp[0].mode;
392 *arg++ = temp[1].name;
393 *arg++ = temp[1].hex;
394 *arg++ = temp[1].mode;
395 if (other)
396 *arg++ = other;
397 *arg = 0;
398 execvp(pgm, (char *const*) exec_arg);
399 }
400 else
401 execlp(pgm, pgm, name, NULL);
402 }
403 /*
404 * otherwise we use the built-in one.
405 */
406 if (one && two)
407 builtin_diff(name, other ? : name, temp, rename_score);
408 else
409 printf("* Unmerged path %s\n", name);
410 exit(0);
411 }
412 if (waitpid(pid, &status, 0) < 0 ||
413 !WIFEXITED(status) || WEXITSTATUS(status)) {
414 /* Earlier we did not check the exit status because
415 * diff exits non-zero if files are different, and
416 * we are not interested in knowing that. It was a
417 * mistake which made it harder to quit a diff-*
418 * session that uses the git-apply-patch-script as
419 * the GIT_EXTERNAL_DIFF. A custom GIT_EXTERNAL_DIFF
420 * should also exit non-zero only when it wants to
421 * abort the entire diff-* session.
422 */
423 remove_tempfile();
424 fprintf(stderr, "external diff died, stopping at %s.\n", name);
425 exit(1);
426 }
427 remove_tempfile();
428}
429
430/*
431 * We do not detect circular renames. Just hold created and deleted
432 * entries and later attempt to match them up. If they do not match,
433 * then spit them out as deletes or creates as original.
434 */
435
436static struct diff_spec_hold {
437 struct diff_spec_hold *next;
438 struct diff_spec it;
439 unsigned long size;
440 int flags;
441#define MATCHED 1
442#define SHOULD_FREE 2
443#define SHOULD_MUNMAP 4
444 void *data;
445 char path[1];
446} *createdfile, *deletedfile;
447
448static void hold_diff(const char *name,
449 struct diff_spec *one,
450 struct diff_spec *two)
451{
452 struct diff_spec_hold **list, *elem;
453
454 if (one->file_valid && two->file_valid)
455 die("internal error");
456
457 if (!detect_rename) {
458 run_external_diff(name, NULL, one, two, -1);
459 return;
460 }
461 elem = xmalloc(sizeof(*elem) + strlen(name));
462 strcpy(elem->path, name);
463 elem->size = 0;
464 elem->data = NULL;
465 elem->flags = 0;
466 if (one->file_valid) {
467 list = &deletedfile;
468 elem->it = *one;
469 }
470 else {
471 list = &createdfile;
472 elem->it = *two;
473 }
474 elem->next = *list;
475 *list = elem;
476}
477
478static int populate_data(struct diff_spec_hold *s)
479{
480 char type[20];
481
482 if (s->data)
483 return 0;
484 if (s->it.sha1_valid) {
485 s->data = read_sha1_file(s->it.blob_sha1, type, &s->size);
486 s->flags |= SHOULD_FREE;
487 }
488 else {
489 struct stat st;
490 int fd;
491 fd = open(s->path, O_RDONLY);
492 if (fd < 0)
493 return -1;
494 if (fstat(fd, &st)) {
495 close(fd);
496 return -1;
497 }
498 s->size = st.st_size;
499 s->data = mmap(NULL, s->size, PROT_READ, MAP_PRIVATE, fd, 0);
500 close(fd);
501 if (!s->size)
502 s->data = "";
503 else
504 s->flags |= SHOULD_MUNMAP;
505 }
506 return 0;
507}
508
509static void free_data(struct diff_spec_hold *s)
510{
511 if (s->flags & SHOULD_FREE)
512 free(s->data);
513 else if (s->flags & SHOULD_MUNMAP)
514 munmap(s->data, s->size);
515 s->flags &= ~(SHOULD_FREE|SHOULD_MUNMAP);
516 s->data = 0;
517}
518
519static void flush_remaining_diff(struct diff_spec_hold *elem,
520 int on_created_list)
521{
522 static struct diff_spec null_file_spec;
523
524 null_file_spec.file_valid = 0;
525 for ( ; elem ; elem = elem->next) {
526 free_data(elem);
527 if (elem->flags & MATCHED)
528 continue;
529 if (on_created_list)
530 run_external_diff(elem->path, NULL,
531 &null_file_spec, &elem->it, -1);
532 else
533 run_external_diff(elem->path, NULL,
534 &elem->it, &null_file_spec, -1);
535 }
536}
537
538static int is_exact_match(struct diff_spec_hold *src,
539 struct diff_spec_hold *dst)
540{
541 if (src->it.sha1_valid && dst->it.sha1_valid &&
542 !memcmp(src->it.blob_sha1, dst->it.blob_sha1, 20))
543 return 1;
544 if (populate_data(src) || populate_data(dst))
545 /* this is an error but will be caught downstream */
546 return 0;
547 if (src->size == dst->size &&
548 !memcmp(src->data, dst->data, src->size))
549 return 1;
550 return 0;
551}
552
553int estimate_similarity(struct diff_spec_hold *src, struct diff_spec_hold *dst)
554{
555 /* src points at a deleted file and dst points at a created
556 * file. They may be quite similar, in which case we want to
557 * say src is renamed to dst.
558 *
559 * Compare them and return how similar they are, representing
560 * the score as an integer between 0 and 10000, except
561 * where they match exactly it is considered better than anything
562 * else.
563 */
564 void *delta;
565 unsigned long delta_size;
566 int score;
567
568 delta_size = ((src->size < dst->size) ?
569 (dst->size - src->size) : (src->size - dst->size));
570
571 /* We would not consider rename followed by more than
572 * minimum_score/MAX_SCORE edits; that is, delta_size must be smaller
573 * than (src->size + dst->size)/2 * minimum_score/MAX_SCORE,
574 * which means...
575 */
576
577 if ((src->size+dst->size)*minimum_score < delta_size*MAX_SCORE*2)
578 return 0;
579
580 delta = diff_delta(src->data, src->size,
581 dst->data, dst->size,
582 &delta_size);
583 free(delta);
584
585 /* This "delta" is really xdiff with adler32 and all the
586 * overheads but it is a quick and dirty approximation.
587 *
588 * Now we will give some score to it. 100% edit gets
589 * 0 points and 0% edit gets MAX_SCORE points. That is, every
590 * 1/MAX_SCORE edit gets 1 point penalty. The amount of penalty is:
591 *
592 * (delta_size * 2 / (src->size + dst->size)) * MAX_SCORE
593 *
594 */
595 score = MAX_SCORE-(MAX_SCORE*2*delta_size/(src->size+dst->size));
596 if (score < 0) return 0;
597 if (MAX_SCORE < score) return MAX_SCORE;
598 return score;
599}
600
601struct diff_score {
602 struct diff_spec_hold *src;
603 struct diff_spec_hold *dst;
604 int score;
605};
606
607static int score_compare(const void *a_, const void *b_)
608{
609 const struct diff_score *a = a_, *b = b_;
610 return b->score - a->score;
611}
612
613static void flush_rename_pair(struct diff_spec_hold *src,
614 struct diff_spec_hold *dst,
615 int rename_score)
616{
617 src->flags |= MATCHED;
618 dst->flags |= MATCHED;
619 free_data(src);
620 free_data(dst);
621 run_external_diff(src->path, dst->path,
622 &src->it, &dst->it, rename_score);
623}
624
625static void free_held_diff(struct diff_spec_hold *list)
626{
627 struct diff_spec_hold *h;
628 for (h = list; list; list = h) {
629 h = list->next;
630 free_data(list);
631 free(list);
632 }
633}
634
635void diff_flush(void)
636{
637 int num_create, num_delete, c, d;
638 struct diff_spec_hold *elem, *src, *dst;
639 struct diff_score *mx;
640
641 /* We really want to cull the candidates list early
642 * with cheap tests in order to avoid doing deltas.
643 *
644 * With the current callers, we should not have already
645 * matched entries at this point, but it is nonetheless
646 * checked for sanity.
647 */
648 for (dst = createdfile; dst; dst = dst->next) {
649 if (dst->flags & MATCHED)
650 continue;
651 for (src = deletedfile; src; src = src->next) {
652 if (src->flags & MATCHED)
653 continue;
654 if (! is_exact_match(src, dst))
655 continue;
656 flush_rename_pair(src, dst, MAX_SCORE);
657 break;
658 }
659 }
660
661 /* Count surviving candidates */
662 for (num_create = 0, elem = createdfile; elem; elem = elem->next)
663 if (!(elem->flags & MATCHED))
664 num_create++;
665
666 for (num_delete = 0, elem = deletedfile; elem; elem = elem->next)
667 if (!(elem->flags & MATCHED))
668 num_delete++;
669
670 if (num_create == 0 || num_delete == 0)
671 goto exit_path;
672
673 mx = xmalloc(sizeof(*mx) * num_create * num_delete);
674 for (c = 0, dst = createdfile; dst; dst = dst->next) {
675 int base = c * num_delete;
676 if (dst->flags & MATCHED)
677 continue;
678 for (d = 0, src = deletedfile; src; src = src->next) {
679 struct diff_score *m = &mx[base+d];
680 if (src->flags & MATCHED)
681 continue;
682 m->src = src;
683 m->dst = dst;
684 m->score = estimate_similarity(src, dst);
685 d++;
686 }
687 c++;
688 }
689 qsort(mx, num_create*num_delete, sizeof(*mx), score_compare);
690
691#if 0
692 for (c = 0; c < num_create * num_delete; c++) {
693 src = mx[c].src;
694 dst = mx[c].dst;
695 if ((src->flags & MATCHED) || (dst->flags & MATCHED))
696 continue;
697 fprintf(stderr,
698 "**score ** %d %s %s\n",
699 mx[c].score, src->path, dst->path);
700 }
701#endif
702
703 for (c = 0; c < num_create * num_delete; c++) {
704 src = mx[c].src;
705 dst = mx[c].dst;
706 if ((src->flags & MATCHED) || (dst->flags & MATCHED))
707 continue;
708 if (mx[c].score < minimum_score)
709 break;
710 flush_rename_pair(src, dst, mx[c].score);
711 }
712 free(mx);
713
714 exit_path:
715 flush_remaining_diff(createdfile, 1);
716 flush_remaining_diff(deletedfile, 0);
717 free_held_diff(createdfile);
718 free_held_diff(deletedfile);
719 createdfile = deletedfile = NULL;
720}
721
722int diff_scoreopt_parse(const char *opt)
723{
724 int diglen, num, scale, i;
725 if (opt[0] != '-' || opt[1] != 'M')
726 return -1; /* that is not -M option */
727 diglen = strspn(opt+2, "0123456789");
728 if (diglen == 0 || strlen(opt+2) != diglen)
729 return 0; /* use default */
730 sscanf(opt+2, "%d", &num);
731 for (i = 0, scale = 1; i < diglen; i++)
732 scale *= 10;
733
734 /* user says num divided by scale and we say internally that
735 * is MAX_SCORE * num / scale.
736 */
737 return MAX_SCORE * num / scale;
738}
739
740void diff_setup(int detect_rename_, int minimum_score_, int reverse_diff_,
741 int diff_raw_output_,
742 const char **pathspec_, int speccnt_)
743{
744 free_held_diff(createdfile);
745 free_held_diff(deletedfile);
746 createdfile = deletedfile = NULL;
747
748 detect_rename = detect_rename_;
749 reverse_diff = reverse_diff_;
750 pathspec = pathspec_;
751 diff_raw_output = diff_raw_output_;
752 speccnt = speccnt_;
753 minimum_score = minimum_score_ ? : DEFAULT_MINIMUM_SCORE;
754}
755
756static const char *git_object_type(unsigned mode)
757{
758 return S_ISDIR(mode) ? "tree" : "blob";
759}
760
761void diff_addremove(int addremove, unsigned mode,
762 const unsigned char *sha1,
763 const char *base, const char *path)
764{
765 char concatpath[PATH_MAX];
766 struct diff_spec spec[2], *one, *two;
767
768 if (0 <= diff_raw_output) {
769 if (!path)
770 path = "";
771 if (reverse_diff)
772 addremove = (addremove == '+' ? '-' : '+');
773 printf("%c%06o %s %s %s%s%c",
774 addremove,
775 mode,
776 git_object_type(mode), sha1_to_hex(sha1),
777 base, path, diff_raw_output);
778 return;
779 }
780 if (S_ISDIR(mode))
781 return;
782
783 memcpy(spec[0].blob_sha1, sha1, 20);
784 spec[0].mode = mode;
785 spec[0].sha1_valid = !!memcmp(sha1, null_sha1, 20);
786 spec[0].file_valid = 1;
787 spec[1].file_valid = 0;
788
789 if (addremove == '+') {
790 one = spec + 1; two = spec;
791 } else {
792 one = spec; two = one + 1;
793 }
794
795 if (path) {
796 strcpy(concatpath, base);
797 strcat(concatpath, path);
798 }
799 hold_diff(path ? concatpath : base, one, two);
800}
801
802void 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 char concatpath[PATH_MAX];
807 struct diff_spec spec[2];
808
809 if (0 <= diff_raw_output) {
810 char old_hex[41];
811 strcpy(old_hex, sha1_to_hex(old_sha1));
812
813 if (!path)
814 path = "";
815 if (reverse_diff)
816 printf("*%06o->%06o %s %s->%s %s%s%c",
817 new_mode, old_mode,
818 git_object_type(new_mode),
819 sha1_to_hex(new_sha1), old_hex,
820 base, path, diff_raw_output);
821 else
822 printf("*%06o->%06o %s %s->%s %s%s%c",
823 old_mode, new_mode,
824 git_object_type(new_mode),
825 old_hex, sha1_to_hex(new_sha1),
826 base, path, diff_raw_output);
827 return;
828 }
829 if (S_ISDIR(new_mode))
830 return;
831
832 if (path) {
833 strcpy(concatpath, base);
834 strcat(concatpath, path);
835 }
836
837 memcpy(spec[0].blob_sha1, old_sha1, 20);
838 spec[0].mode = old_mode;
839 memcpy(spec[1].blob_sha1, new_sha1, 20);
840 spec[1].mode = new_mode;
841 spec[0].sha1_valid = !!memcmp(old_sha1, null_sha1, 20);
842 spec[1].sha1_valid = !!memcmp(new_sha1, null_sha1, 20);
843 spec[1].file_valid = spec[0].file_valid = 1;
844
845 /* We do not look at changed files as candidate for
846 * rename detection ever.
847 */
848 run_external_diff(path ? concatpath : base, NULL,
849 &spec[0], &spec[1], -1);
850}
851
852void diff_unmerge(const char *path)
853{
854 if (0 <= diff_raw_output) {
855 printf("U %s%c", path, diff_raw_output);
856 return;
857 }
858 run_external_diff(path, NULL, NULL, NULL, -1);
859}