git-whatchanged: allow other pagers
[git/git.git] / diff.c
CommitLineData
be3cfa85
JH
1/*
2 * Copyright (C) 2005 Junio C Hamano
3 */
4#include <sys/types.h>
5#include <sys/wait.h>
532149d7 6#include <signal.h>
a1df57ab 7#include <limits.h>
86436c28
JH
8#include "cache.h"
9#include "diff.h"
5c97558c 10#include "delta.h"
86436c28 11
d19938ab 12static const char *diff_opts = "-pu";
5c97558c 13static unsigned char null_sha1[20] = { 0, };
57fe64a4
JH
14#define MAX_SCORE 10000
15#define DEFAULT_MINIMUM_SCORE 5000
86436c28 16
be3cfa85 17static const char *external_diff(void)
86436c28 18{
d19938ab 19 static const char *external_diff_cmd = NULL;
be3cfa85
JH
20 static int done_preparing = 0;
21
22 if (done_preparing)
23 return external_diff_cmd;
24
86436c28
JH
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 *
86436c28
JH
31 * GIT_DIFF_OPTS="-c";
32 */
d19938ab
JH
33 if (gitenv("GIT_EXTERNAL_DIFF"))
34 external_diff_cmd = gitenv("GIT_EXTERNAL_DIFF");
be3cfa85
JH
35
36 /* In case external diff fails... */
d19938ab 37 diff_opts = gitenv("GIT_DIFF_OPTS") ? : diff_opts;
be3cfa85
JH
38
39 done_preparing = 1;
40 return external_diff_cmd;
86436c28
JH
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
57fe64a4 60 /* count bytes needed to store the quoted string. */
86436c28
JH
61 for (cnt = 1, cp = src; *cp; cnt++, cp++)
62 if (*cp == '\'')
63 cnt += 3;
64
812666c8 65 buf = xmalloc(cnt);
86436c28
JH
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
be3cfa85
JH
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
5c97558c
JH
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;
5b486c3b 90 * if false, use the name and read from
5c97558c
JH
91 * the filesystem.
92 */
5b486c3b 93 unsigned file_valid : 1; /* if false the file does not exist */
5c97558c
JH
94};
95
915838c3
JH
96static void builtin_diff(const char *name_a,
97 const char *name_b,
57fe64a4
JH
98 struct diff_tempfile *temp,
99 int rename_score)
86436c28 100{
9669e17a 101 int i, next_at, cmd_size;
c983370e 102 const char *diff_cmd = "diff -L'%s%s' -L'%s%s'";
6fa28064 103 const char *diff_arg = "'%s' '%s'||:"; /* "||:" is to return 0 */
2f978138
JH
104 const char *input_name_sq[2];
105 const char *path0[2];
106 const char *path1[2];
915838c3 107 const char *name_sq[2];
2f978138 108 char *cmd;
915838c3
JH
109
110 name_sq[0] = sq_expand(name_a);
111 name_sq[1] = sq_expand(name_b);
112
c983370e
JH
113 /* diff_cmd and diff_arg have 6 %s in total which makes
114 * the sum of these strings 12 bytes larger than required.
be3cfa85 115 * we use 2 spaces around diff-opts, and we need to count
c983370e 116 * terminating NUL, so we subtract 9 here.
be3cfa85 117 */
9669e17a 118 cmd_size = (strlen(diff_cmd) + strlen(diff_opts) +
c983370e 119 strlen(diff_arg) - 9);
2f978138
JH
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] = "";
2f978138 125 } else {
0980d9b3 126 path0[i] = i ? "b/" : "a/";
915838c3 127 path1[i] = name_sq[i];
2f978138
JH
128 }
129 cmd_size += (strlen(path0[i]) + strlen(path1[i]) +
c983370e 130 strlen(input_name_sq[i]));
2f978138 131 }
be3cfa85 132
2f978138
JH
133 cmd = xmalloc(cmd_size);
134
135 next_at = 0;
be3cfa85 136 next_at += snprintf(cmd+next_at, cmd_size-next_at,
2f978138 137 diff_cmd,
c983370e 138 path0[0], path1[0], path0[1], path1[1]);
be3cfa85
JH
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,
2f978138
JH
142 diff_arg, input_name_sq[0], input_name_sq[1]);
143
915838c3 144 printf("diff --git a/%s b/%s\n", name_a, name_b);
c983370e 145 if (!path1[0][0])
b58f23b3 146 printf("new file mode %s\n", temp[1].mode);
c983370e 147 else if (!path1[1][0])
b58f23b3 148 printf("deleted file mode %s\n", temp[0].mode);
273b9834 149 else {
b58f23b3
JH
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 }
915838c3 154 if (strcmp(name_a, name_b)) {
57fe64a4
JH
155 if (0 < rename_score)
156 printf("rename similarity index %d%%\n",
157 (int)(0.5+
158 rename_score*100.0/MAX_SCORE));
915838c3
JH
159 printf("rename old %s\n", name_a);
160 printf("rename new %s\n", name_b);
161 }
273b9834
JH
162 if (strncmp(temp[0].mode, temp[1].mode, 3))
163 /* we do not run diff between different kind
164 * of objects.
165 */
b28858bf
JH
166 exit(0);
167 }
c983370e 168 fflush(NULL);
be3cfa85 169 execlp("/bin/sh","sh", "-c", cmd, NULL);
86436c28
JH
170}
171
b46f0b6d
JH
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;
5c97558c 182
b46f0b6d
JH
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
915838c3 187 * tree. This is because most diff-tree comparisons deal with
b46f0b6d
JH
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.
57fe64a4 194 */
b46f0b6d
JH
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];
b28858bf
JH
203 if ((lstat(name, &st) < 0) ||
204 !S_ISREG(st.st_mode) ||
5d728c84 205 ce_match_stat(ce, &st) ||
b46f0b6d
JH
206 memcmp(sha1, ce->sha1, 20))
207 return 0;
208 return 1;
209}
210
b28858bf
JH
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
be3cfa85
JH
232static void prepare_temp_file(const char *name,
233 struct diff_tempfile *temp,
234 struct diff_spec *one)
86436c28 235{
be3cfa85
JH
236 if (!one->file_valid) {
237 not_a_valid_file:
532149d7
JH
238 /* A '-' entry produces this for file-2, and
239 * a '+' entry produces this for file-1.
240 */
be3cfa85
JH
241 temp->name = "/dev/null";
242 strcpy(temp->hex, ".");
243 strcpy(temp->mode, ".");
86436c28
JH
244 return;
245 }
be3cfa85 246
5c97558c
JH
247 if (!one->sha1_valid ||
248 work_tree_matches(name, one->blob_sha1)) {
be3cfa85 249 struct stat st;
b46f0b6d 250 temp->name = name;
b28858bf 251 if (lstat(temp->name, &st) < 0) {
be3cfa85
JH
252 if (errno == ENOENT)
253 goto not_a_valid_file;
254 die("stat(%s): %s", temp->name, strerror(errno));
255 }
b28858bf
JH
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;
be3cfa85
JH
279 }
280 else {
be3cfa85
JH
281 void *blob;
282 char type[20];
283 unsigned long size;
284
b46f0b6d 285 blob = read_sha1_file(one->blob_sha1, type, &size);
be3cfa85
JH
286 if (!blob || strcmp(type, "blob"))
287 die("unable to read blob object for %s (%s)",
b46f0b6d 288 name, sha1_to_hex(one->blob_sha1));
b28858bf 289 prep_temp_blob(temp, blob, size, one->blob_sha1, one->mode);
be3cfa85 290 free(blob);
be3cfa85
JH
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
532149d7
JH
305static void remove_tempfile_on_signal(int signo)
306{
307 remove_tempfile();
308}
309
5c97558c
JH
310static int detect_rename;
311static int reverse_diff;
57fe64a4 312static int diff_raw_output = -1;
5c97558c
JH
313static const char **pathspec;
314static int speccnt;
57fe64a4 315static int minimum_score;
5c97558c
JH
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
be3cfa85
JH
336/* An external diff command takes:
337 *
338 * diff-cmd name infile1 infile1-sha1 infile1-mode \
5c97558c 339 * infile2 infile2-sha1 infile2-mode [ rename-to ]
be3cfa85
JH
340 *
341 */
5c97558c
JH
342static void run_external_diff(const char *name,
343 const char *other,
344 struct diff_spec *one,
57fe64a4
JH
345 struct diff_spec *two,
346 int rename_score)
be3cfa85
JH
347{
348 struct diff_tempfile *temp = diff_temp;
532149d7
JH
349 pid_t pid;
350 int status;
be3cfa85
JH
351 static int atexit_asked = 0;
352
5c97558c
JH
353 if (!matches_pathspec(name) && (!other || !matches_pathspec(other)))
354 return;
355
77eb2720
JH
356 if (one && two) {
357 prepare_temp_file(name, &temp[0], one);
915838c3 358 prepare_temp_file(other ? : name, &temp[1], two);
77eb2720
JH
359 if (! atexit_asked &&
360 (temp[0].name == temp[0].tmp_path ||
361 temp[1].name == temp[1].tmp_path)) {
362 atexit_asked = 1;
363 atexit(remove_tempfile);
364 }
532149d7 365 signal(SIGINT, remove_tempfile_on_signal);
be3cfa85
JH
366 }
367
368 fflush(NULL);
369 pid = fork();
370 if (pid < 0)
371 die("unable to fork");
372 if (!pid) {
373 const char *pgm = external_diff();
5c97558c
JH
374 if (pgm) {
375 if (one && two) {
376 const char *exec_arg[9];
377 const char **arg = &exec_arg[0];
378 *arg++ = pgm;
379 *arg++ = name;
380 *arg++ = temp[0].name;
381 *arg++ = temp[0].hex;
382 *arg++ = temp[0].mode;
383 *arg++ = temp[1].name;
384 *arg++ = temp[1].hex;
385 *arg++ = temp[1].mode;
386 if (other)
387 *arg++ = other;
e99d59ff 388 *arg = NULL;
5c97558c
JH
389 execvp(pgm, (char *const*) exec_arg);
390 }
77eb2720
JH
391 else
392 execlp(pgm, pgm, name, NULL);
393 }
be3cfa85
JH
394 /*
395 * otherwise we use the built-in one.
396 */
77eb2720 397 if (one && two)
57fe64a4 398 builtin_diff(name, other ? : name, temp, rename_score);
77eb2720
JH
399 else
400 printf("* Unmerged path %s\n", name);
be3cfa85
JH
401 exit(0);
402 }
6fa28064
JH
403 if (waitpid(pid, &status, 0) < 0 ||
404 !WIFEXITED(status) || WEXITSTATUS(status)) {
405 /* Earlier we did not check the exit status because
532149d7 406 * diff exits non-zero if files are different, and
6fa28064
JH
407 * we are not interested in knowing that. It was a
408 * mistake which made it harder to quit a diff-*
409 * session that uses the git-apply-patch-script as
410 * the GIT_EXTERNAL_DIFF. A custom GIT_EXTERNAL_DIFF
411 * should also exit non-zero only when it wants to
412 * abort the entire diff-* session.
532149d7
JH
413 */
414 remove_tempfile();
6fa28064
JH
415 fprintf(stderr, "external diff died, stopping at %s.\n", name);
416 exit(1);
532149d7 417 }
be3cfa85
JH
418 remove_tempfile();
419}
420
5c97558c
JH
421/*
422 * We do not detect circular renames. Just hold created and deleted
423 * entries and later attempt to match them up. If they do not match,
424 * then spit them out as deletes or creates as original.
425 */
426
427static struct diff_spec_hold {
428 struct diff_spec_hold *next;
429 struct diff_spec it;
430 unsigned long size;
431 int flags;
432#define MATCHED 1
433#define SHOULD_FREE 2
434#define SHOULD_MUNMAP 4
435 void *data;
436 char path[1];
437} *createdfile, *deletedfile;
438
439static void hold_diff(const char *name,
440 struct diff_spec *one,
441 struct diff_spec *two)
442{
443 struct diff_spec_hold **list, *elem;
444
445 if (one->file_valid && two->file_valid)
446 die("internal error");
447
448 if (!detect_rename) {
57fe64a4 449 run_external_diff(name, NULL, one, two, -1);
5c97558c
JH
450 return;
451 }
452 elem = xmalloc(sizeof(*elem) + strlen(name));
453 strcpy(elem->path, name);
454 elem->size = 0;
455 elem->data = NULL;
456 elem->flags = 0;
457 if (one->file_valid) {
458 list = &deletedfile;
459 elem->it = *one;
460 }
461 else {
462 list = &createdfile;
463 elem->it = *two;
464 }
465 elem->next = *list;
466 *list = elem;
467}
468
469static int populate_data(struct diff_spec_hold *s)
470{
471 char type[20];
472
473 if (s->data)
474 return 0;
475 if (s->it.sha1_valid) {
476 s->data = read_sha1_file(s->it.blob_sha1, type, &s->size);
477 s->flags |= SHOULD_FREE;
478 }
479 else {
480 struct stat st;
481 int fd;
482 fd = open(s->path, O_RDONLY);
483 if (fd < 0)
484 return -1;
485 if (fstat(fd, &st)) {
486 close(fd);
487 return -1;
488 }
489 s->size = st.st_size;
490 s->data = mmap(NULL, s->size, PROT_READ, MAP_PRIVATE, fd, 0);
491 close(fd);
492 if (!s->size)
493 s->data = "";
494 else
495 s->flags |= SHOULD_MUNMAP;
496 }
497 return 0;
498}
499
500static void free_data(struct diff_spec_hold *s)
501{
502 if (s->flags & SHOULD_FREE)
503 free(s->data);
504 else if (s->flags & SHOULD_MUNMAP)
505 munmap(s->data, s->size);
506 s->flags &= ~(SHOULD_FREE|SHOULD_MUNMAP);
e99d59ff 507 s->data = NULL;
5c97558c
JH
508}
509
510static void flush_remaining_diff(struct diff_spec_hold *elem,
511 int on_created_list)
512{
513 static struct diff_spec null_file_spec;
514
515 null_file_spec.file_valid = 0;
516 for ( ; elem ; elem = elem->next) {
517 free_data(elem);
518 if (elem->flags & MATCHED)
519 continue;
520 if (on_created_list)
521 run_external_diff(elem->path, NULL,
57fe64a4 522 &null_file_spec, &elem->it, -1);
5c97558c
JH
523 else
524 run_external_diff(elem->path, NULL,
57fe64a4 525 &elem->it, &null_file_spec, -1);
5c97558c
JH
526 }
527}
528
529static int is_exact_match(struct diff_spec_hold *src,
530 struct diff_spec_hold *dst)
531{
532 if (src->it.sha1_valid && dst->it.sha1_valid &&
533 !memcmp(src->it.blob_sha1, dst->it.blob_sha1, 20))
534 return 1;
535 if (populate_data(src) || populate_data(dst))
536 /* this is an error but will be caught downstream */
537 return 0;
538 if (src->size == dst->size &&
539 !memcmp(src->data, dst->data, src->size))
540 return 1;
541 return 0;
542}
543
e99d59ff 544static int estimate_similarity(struct diff_spec_hold *src, struct diff_spec_hold *dst)
5c97558c
JH
545{
546 /* src points at a deleted file and dst points at a created
547 * file. They may be quite similar, in which case we want to
548 * say src is renamed to dst.
549 *
550 * Compare them and return how similar they are, representing
57fe64a4
JH
551 * the score as an integer between 0 and 10000, except
552 * where they match exactly it is considered better than anything
553 * else.
5c97558c
JH
554 */
555 void *delta;
556 unsigned long delta_size;
57fe64a4 557 int score;
5c97558c
JH
558
559 delta_size = ((src->size < dst->size) ?
560 (dst->size - src->size) : (src->size - dst->size));
561
562 /* We would not consider rename followed by more than
57fe64a4
JH
563 * minimum_score/MAX_SCORE edits; that is, delta_size must be smaller
564 * than (src->size + dst->size)/2 * minimum_score/MAX_SCORE,
565 * which means...
5c97558c 566 */
57fe64a4
JH
567
568 if ((src->size+dst->size)*minimum_score < delta_size*MAX_SCORE*2)
5c97558c
JH
569 return 0;
570
571 delta = diff_delta(src->data, src->size,
572 dst->data, dst->size,
573 &delta_size);
574 free(delta);
575
576 /* This "delta" is really xdiff with adler32 and all the
577 * overheads but it is a quick and dirty approximation.
578 *
57fe64a4
JH
579 * Now we will give some score to it. 100% edit gets
580 * 0 points and 0% edit gets MAX_SCORE points. That is, every
581 * 1/MAX_SCORE edit gets 1 point penalty. The amount of penalty is:
5c97558c 582 *
57fe64a4 583 * (delta_size * 2 / (src->size + dst->size)) * MAX_SCORE
5c97558c
JH
584 *
585 */
57fe64a4
JH
586 score = MAX_SCORE-(MAX_SCORE*2*delta_size/(src->size+dst->size));
587 if (score < 0) return 0;
588 if (MAX_SCORE < score) return MAX_SCORE;
589 return score;
5c97558c
JH
590}
591
592struct diff_score {
593 struct diff_spec_hold *src;
594 struct diff_spec_hold *dst;
595 int score;
596};
597
598static int score_compare(const void *a_, const void *b_)
599{
600 const struct diff_score *a = a_, *b = b_;
601 return b->score - a->score;
602}
603
604static void flush_rename_pair(struct diff_spec_hold *src,
57fe64a4
JH
605 struct diff_spec_hold *dst,
606 int rename_score)
5c97558c
JH
607{
608 src->flags |= MATCHED;
609 dst->flags |= MATCHED;
610 free_data(src);
611 free_data(dst);
612 run_external_diff(src->path, dst->path,
57fe64a4 613 &src->it, &dst->it, rename_score);
5c97558c
JH
614}
615
616static void free_held_diff(struct diff_spec_hold *list)
617{
618 struct diff_spec_hold *h;
619 for (h = list; list; list = h) {
620 h = list->next;
621 free_data(list);
622 free(list);
623 }
624}
625
626void diff_flush(void)
627{
628 int num_create, num_delete, c, d;
629 struct diff_spec_hold *elem, *src, *dst;
630 struct diff_score *mx;
631
632 /* We really want to cull the candidates list early
633 * with cheap tests in order to avoid doing deltas.
5b486c3b
JH
634 *
635 * With the current callers, we should not have already
636 * matched entries at this point, but it is nonetheless
637 * checked for sanity.
5c97558c
JH
638 */
639 for (dst = createdfile; dst; dst = dst->next) {
5b486c3b
JH
640 if (dst->flags & MATCHED)
641 continue;
5c97558c 642 for (src = deletedfile; src; src = src->next) {
5b486c3b
JH
643 if (src->flags & MATCHED)
644 continue;
5c97558c
JH
645 if (! is_exact_match(src, dst))
646 continue;
57fe64a4 647 flush_rename_pair(src, dst, MAX_SCORE);
5c97558c
JH
648 break;
649 }
650 }
651
652 /* Count surviving candidates */
653 for (num_create = 0, elem = createdfile; elem; elem = elem->next)
654 if (!(elem->flags & MATCHED))
655 num_create++;
656
657 for (num_delete = 0, elem = deletedfile; elem; elem = elem->next)
658 if (!(elem->flags & MATCHED))
659 num_delete++;
660
661 if (num_create == 0 || num_delete == 0)
662 goto exit_path;
663
664 mx = xmalloc(sizeof(*mx) * num_create * num_delete);
665 for (c = 0, dst = createdfile; dst; dst = dst->next) {
666 int base = c * num_delete;
667 if (dst->flags & MATCHED)
668 continue;
669 for (d = 0, src = deletedfile; src; src = src->next) {
670 struct diff_score *m = &mx[base+d];
671 if (src->flags & MATCHED)
672 continue;
673 m->src = src;
674 m->dst = dst;
675 m->score = estimate_similarity(src, dst);
676 d++;
677 }
678 c++;
679 }
57fe64a4 680 qsort(mx, num_create*num_delete, sizeof(*mx), score_compare);
5c97558c 681
5b486c3b 682#if 0
5c97558c
JH
683 for (c = 0; c < num_create * num_delete; c++) {
684 src = mx[c].src;
685 dst = mx[c].dst;
686 if ((src->flags & MATCHED) || (dst->flags & MATCHED))
687 continue;
5b486c3b
JH
688 fprintf(stderr,
689 "**score ** %d %s %s\n",
690 mx[c].score, src->path, dst->path);
5c97558c 691 }
5b486c3b 692#endif
5c97558c
JH
693
694 for (c = 0; c < num_create * num_delete; c++) {
695 src = mx[c].src;
696 dst = mx[c].dst;
697 if ((src->flags & MATCHED) || (dst->flags & MATCHED))
698 continue;
57fe64a4 699 if (mx[c].score < minimum_score)
5c97558c 700 break;
57fe64a4 701 flush_rename_pair(src, dst, mx[c].score);
5c97558c 702 }
5b486c3b 703 free(mx);
5c97558c
JH
704
705 exit_path:
706 flush_remaining_diff(createdfile, 1);
707 flush_remaining_diff(deletedfile, 0);
708 free_held_diff(createdfile);
709 free_held_diff(deletedfile);
710 createdfile = deletedfile = NULL;
711}
712
57fe64a4
JH
713int diff_scoreopt_parse(const char *opt)
714{
715 int diglen, num, scale, i;
716 if (opt[0] != '-' || opt[1] != 'M')
717 return -1; /* that is not -M option */
718 diglen = strspn(opt+2, "0123456789");
719 if (diglen == 0 || strlen(opt+2) != diglen)
720 return 0; /* use default */
721 sscanf(opt+2, "%d", &num);
722 for (i = 0, scale = 1; i < diglen; i++)
723 scale *= 10;
724
725 /* user says num divided by scale and we say internally that
726 * is MAX_SCORE * num / scale.
727 */
728 return MAX_SCORE * num / scale;
729}
730
5c97558c 731void diff_setup(int detect_rename_, int minimum_score_, int reverse_diff_,
57fe64a4 732 int diff_raw_output_,
5c97558c
JH
733 const char **pathspec_, int speccnt_)
734{
735 free_held_diff(createdfile);
736 free_held_diff(deletedfile);
737 createdfile = deletedfile = NULL;
738
739 detect_rename = detect_rename_;
740 reverse_diff = reverse_diff_;
741 pathspec = pathspec_;
57fe64a4 742 diff_raw_output = diff_raw_output_;
5c97558c 743 speccnt = speccnt_;
57fe64a4
JH
744 minimum_score = minimum_score_ ? : DEFAULT_MINIMUM_SCORE;
745}
746
747static const char *git_object_type(unsigned mode)
748{
749 return S_ISDIR(mode) ? "tree" : "blob";
5c97558c
JH
750}
751
77eb2720
JH
752void diff_addremove(int addremove, unsigned mode,
753 const unsigned char *sha1,
754 const char *base, const char *path)
be3cfa85 755{
77eb2720 756 char concatpath[PATH_MAX];
be3cfa85
JH
757 struct diff_spec spec[2], *one, *two;
758
7ca45252
JH
759 if (reverse_diff)
760 addremove = (addremove == '+' ? '-' : '+');
761
57fe64a4
JH
762 if (0 <= diff_raw_output) {
763 if (!path)
764 path = "";
57fe64a4
JH
765 printf("%c%06o %s %s %s%s%c",
766 addremove,
767 mode,
768 git_object_type(mode), sha1_to_hex(sha1),
769 base, path, diff_raw_output);
770 return;
771 }
772 if (S_ISDIR(mode))
773 return;
774
b46f0b6d 775 memcpy(spec[0].blob_sha1, sha1, 20);
77eb2720 776 spec[0].mode = mode;
5c97558c
JH
777 spec[0].sha1_valid = !!memcmp(sha1, null_sha1, 20);
778 spec[0].file_valid = 1;
be3cfa85
JH
779 spec[1].file_valid = 0;
780
77eb2720 781 if (addremove == '+') {
be3cfa85
JH
782 one = spec + 1; two = spec;
783 } else {
784 one = spec; two = one + 1;
785 }
5c97558c 786
77eb2720
JH
787 if (path) {
788 strcpy(concatpath, base);
77eb2720
JH
789 strcat(concatpath, path);
790 }
5c97558c 791 hold_diff(path ? concatpath : base, one, two);
be3cfa85
JH
792}
793
77eb2720
JH
794void diff_change(unsigned old_mode, unsigned new_mode,
795 const unsigned char *old_sha1,
796 const unsigned char *new_sha1,
797 const char *base, const char *path) {
798 char concatpath[PATH_MAX];
799 struct diff_spec spec[2];
800
7ca45252
JH
801 if (reverse_diff) {
802 unsigned tmp;
803 const unsigned char *tmp_c;
804 tmp = old_mode; old_mode = new_mode; new_mode = tmp;
805 tmp_c = old_sha1; old_sha1 = new_sha1; new_sha1 = tmp_c;
806 }
807
57fe64a4
JH
808 if (0 <= diff_raw_output) {
809 char old_hex[41];
810 strcpy(old_hex, sha1_to_hex(old_sha1));
811
812 if (!path)
813 path = "";
7ca45252
JH
814 printf("*%06o->%06o %s %s->%s %s%s%c",
815 old_mode, new_mode,
816 git_object_type(new_mode),
817 old_hex, sha1_to_hex(new_sha1),
818 base, path, diff_raw_output);
57fe64a4
JH
819 return;
820 }
821 if (S_ISDIR(new_mode))
822 return;
823
5c97558c
JH
824 if (path) {
825 strcpy(concatpath, base);
826 strcat(concatpath, path);
827 }
828
b46f0b6d 829 memcpy(spec[0].blob_sha1, old_sha1, 20);
77eb2720 830 spec[0].mode = old_mode;
b46f0b6d 831 memcpy(spec[1].blob_sha1, new_sha1, 20);
77eb2720 832 spec[1].mode = new_mode;
5c97558c
JH
833 spec[0].sha1_valid = !!memcmp(old_sha1, null_sha1, 20);
834 spec[1].sha1_valid = !!memcmp(new_sha1, null_sha1, 20);
835 spec[1].file_valid = spec[0].file_valid = 1;
be3cfa85 836
5c97558c
JH
837 /* We do not look at changed files as candidate for
838 * rename detection ever.
839 */
57fe64a4
JH
840 run_external_diff(path ? concatpath : base, NULL,
841 &spec[0], &spec[1], -1);
77eb2720 842}
be3cfa85 843
77eb2720
JH
844void diff_unmerge(const char *path)
845{
57fe64a4
JH
846 if (0 <= diff_raw_output) {
847 printf("U %s%c", path, diff_raw_output);
848 return;
849 }
850 run_external_diff(path, NULL, NULL, NULL, -1);
86436c28 851}