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