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