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