]> git.ipfire.org Git - thirdparty/git.git/blame - diff.c
builtin-diff: \No newline at end of file.
[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"
3443546f 11#include "xdiff/xdiff.h"
86436c28 12
d19938ab 13static const char *diff_opts = "-pu";
427dcb4b 14
f0c6b2a2 15static int use_size_cache;
86436c28 16
3299c6f6
JH
17int diff_rename_limit_default = -1;
18
9ce392f4
JH
19int git_diff_config(const char *var, const char *value)
20{
21 if (!strcmp(var, "diff.renamelimit")) {
22 diff_rename_limit_default = git_config_int(var, value);
23 return 0;
24 }
25
26 return git_default_config(var, value);
27}
28
cf9dfc66
JH
29static char *quote_one(const char *str)
30{
31 int needlen;
32 char *xp;
33
34 if (!str)
35 return NULL;
36 needlen = quote_c_style(str, NULL, NULL, 0);
37 if (!needlen)
38 return strdup(str);
39 xp = xmalloc(needlen + 1);
40 quote_c_style(str, xp, NULL, 0);
41 return xp;
42}
43
44static char *quote_two(const char *one, const char *two)
45{
46 int need_one = quote_c_style(one, NULL, NULL, 1);
47 int need_two = quote_c_style(two, NULL, NULL, 1);
48 char *xp;
49
50 if (need_one + need_two) {
51 if (!need_one) need_one = strlen(one);
52 if (!need_two) need_one = strlen(two);
53
54 xp = xmalloc(need_one + need_two + 3);
55 xp[0] = '"';
56 quote_c_style(one, xp + 1, NULL, 1);
57 quote_c_style(two, xp + need_one + 1, NULL, 1);
58 strcpy(xp + need_one + need_two + 1, "\"");
59 return xp;
60 }
61 need_one = strlen(one);
62 need_two = strlen(two);
63 xp = xmalloc(need_one + need_two + 1);
64 strcpy(xp, one);
65 strcpy(xp + need_one, two);
66 return xp;
67}
68
be3cfa85 69static const char *external_diff(void)
86436c28 70{
d19938ab 71 static const char *external_diff_cmd = NULL;
be3cfa85 72 static int done_preparing = 0;
c7c81b3a 73 const char *env_diff_opts;
be3cfa85
JH
74
75 if (done_preparing)
76 return external_diff_cmd;
77
86436c28
JH
78 /*
79 * Default values above are meant to match the
80 * Linux kernel development style. Examples of
81 * alternative styles you can specify via environment
82 * variables are:
83 *
86436c28
JH
84 * GIT_DIFF_OPTS="-c";
85 */
a9ab586a 86 external_diff_cmd = getenv("GIT_EXTERNAL_DIFF");
be3cfa85
JH
87
88 /* In case external diff fails... */
a9ab586a 89 env_diff_opts = getenv("GIT_DIFF_OPTS");
c7c81b3a 90 if (env_diff_opts) diff_opts = env_diff_opts;
be3cfa85
JH
91
92 done_preparing = 1;
93 return external_diff_cmd;
86436c28
JH
94}
95
64f8a631
HE
96#define TEMPFILE_PATH_LEN 50
97
be3cfa85 98static struct diff_tempfile {
427dcb4b 99 const char *name; /* filename external diff should read from */
be3cfa85
JH
100 char hex[41];
101 char mode[10];
64f8a631 102 char tmp_path[TEMPFILE_PATH_LEN];
be3cfa85
JH
103} diff_temp[2];
104
366175ef
JH
105static int count_lines(const char *filename)
106{
107 FILE *in;
108 int count, ch, completely_empty = 1, nl_just_seen = 0;
109 in = fopen(filename, "r");
110 count = 0;
111 while ((ch = fgetc(in)) != EOF)
112 if (ch == '\n') {
113 count++;
114 nl_just_seen = 1;
115 completely_empty = 0;
116 }
117 else {
118 nl_just_seen = 0;
119 completely_empty = 0;
120 }
121 fclose(in);
122 if (completely_empty)
123 return 0;
124 if (!nl_just_seen)
125 count++; /* no trailing newline */
126 return count;
127}
128
129static void print_line_count(int count)
130{
131 switch (count) {
132 case 0:
133 printf("0,0");
134 break;
135 case 1:
136 printf("1");
137 break;
138 default:
139 printf("1,%d", count);
140 break;
141 }
142}
143
144static void copy_file(int prefix, const char *filename)
145{
146 FILE *in;
147 int ch, nl_just_seen = 1;
148 in = fopen(filename, "r");
149 while ((ch = fgetc(in)) != EOF) {
150 if (nl_just_seen)
151 putchar(prefix);
152 putchar(ch);
153 if (ch == '\n')
154 nl_just_seen = 1;
155 else
156 nl_just_seen = 0;
157 }
158 fclose(in);
159 if (!nl_just_seen)
160 printf("\n\\ No newline at end of file\n");
161}
162
163static void emit_rewrite_diff(const char *name_a,
164 const char *name_b,
165 struct diff_tempfile *temp)
166{
167 /* Use temp[i].name as input, name_a and name_b as labels */
168 int lc_a, lc_b;
169 lc_a = count_lines(temp[0].name);
170 lc_b = count_lines(temp[1].name);
171 printf("--- %s\n+++ %s\n@@ -", name_a, name_b);
172 print_line_count(lc_a);
173 printf(" +");
174 print_line_count(lc_b);
175 printf(" @@\n");
176 if (lc_a)
177 copy_file('-', temp[0].name);
178 if (lc_b)
179 copy_file('+', temp[1].name);
180}
181
3443546f
LT
182static int fill_mmfile(mmfile_t *mf, const char *file)
183{
184 int fd = open(file, O_RDONLY);
185 struct stat st;
186 char *buf;
187 unsigned long size;
188
189 mf->ptr = NULL;
190 mf->size = 0;
191 if (fd < 0)
192 return 0;
193 fstat(fd, &st);
194 size = st.st_size;
195 buf = xmalloc(size);
196 mf->ptr = buf;
197 mf->size = size;
198 while (size) {
199 int retval = read(fd, buf, size);
200 if (retval < 0) {
201 if (errno == EINTR || errno == EAGAIN)
202 continue;
203 break;
204 }
205 if (!retval)
206 break;
207 buf += retval;
208 size -= retval;
209 }
210 mf->size -= size;
211 close(fd);
212 return 0;
213}
214
215static int fn_out(void *priv, mmbuffer_t *mb, int nbuf)
216{
217 int i;
218
219 for (i = 0; i < nbuf; i++)
220 if (!fwrite(mb[i].ptr, mb[i].size, 1, stdout))
221 return -1;
222 return 0;
223}
224
8676eb43 225static const char *builtin_diff(const char *name_a,
915838c3 226 const char *name_b,
57fe64a4 227 struct diff_tempfile *temp,
366175ef 228 const char *xfrm_msg,
8676eb43
LT
229 int complete_rewrite,
230 const char **args)
86436c28 231{
9669e17a 232 int i, next_at, cmd_size;
3443546f 233 mmfile_t mf1, mf2;
cf9dfc66 234 const char *const diff_cmd = "diff -L%s -L%s";
694a764f 235 const char *const diff_arg = "-- %s %s||:"; /* "||:" is to return 0 */
2f978138 236 const char *input_name_sq[2];
cf9dfc66 237 const char *label_path[2];
2f978138 238 char *cmd;
915838c3 239
cf9dfc66
JH
240 /* diff_cmd and diff_arg have 4 %s in total which makes
241 * the sum of these strings 8 bytes larger than required.
be3cfa85 242 * we use 2 spaces around diff-opts, and we need to count
cf9dfc66
JH
243 * terminating NUL; we used to subtract 5 here, but we do not
244 * care about small leaks in this subprocess that is about
245 * to exec "diff" anymore.
be3cfa85 246 */
cf9dfc66
JH
247 cmd_size = (strlen(diff_cmd) + strlen(diff_opts) + strlen(diff_arg)
248 + 128);
249
2f978138 250 for (i = 0; i < 2; i++) {
6fb737be 251 input_name_sq[i] = sq_quote(temp[i].name);
cf9dfc66
JH
252 if (!strcmp(temp[i].name, "/dev/null"))
253 label_path[i] = "/dev/null";
254 else if (!i)
255 label_path[i] = sq_quote(quote_two("a/", name_a));
256 else
257 label_path[i] = sq_quote(quote_two("b/", name_b));
258 cmd_size += (strlen(label_path[i]) + strlen(input_name_sq[i]));
2f978138 259 }
be3cfa85 260
2f978138
JH
261 cmd = xmalloc(cmd_size);
262
263 next_at = 0;
be3cfa85 264 next_at += snprintf(cmd+next_at, cmd_size-next_at,
cf9dfc66 265 diff_cmd, label_path[0], label_path[1]);
be3cfa85
JH
266 next_at += snprintf(cmd+next_at, cmd_size-next_at,
267 " %s ", diff_opts);
268 next_at += snprintf(cmd+next_at, cmd_size-next_at,
2f978138
JH
269 diff_arg, input_name_sq[0], input_name_sq[1]);
270
cf9dfc66
JH
271 printf("diff --git %s %s\n",
272 quote_two("a/", name_a), quote_two("b/", name_b));
273 if (label_path[0][0] == '/') {
274 /* dev/null */
b58f23b3 275 printf("new file mode %s\n", temp[1].mode);
70aadac0
JH
276 if (xfrm_msg && xfrm_msg[0])
277 puts(xfrm_msg);
278 }
cf9dfc66 279 else if (label_path[1][0] == '/') {
b58f23b3 280 printf("deleted file mode %s\n", temp[0].mode);
70aadac0
JH
281 if (xfrm_msg && xfrm_msg[0])
282 puts(xfrm_msg);
283 }
273b9834 284 else {
b58f23b3
JH
285 if (strcmp(temp[0].mode, temp[1].mode)) {
286 printf("old mode %s\n", temp[0].mode);
287 printf("new mode %s\n", temp[1].mode);
288 }
427dcb4b 289 if (xfrm_msg && xfrm_msg[0])
09d9d1a6 290 puts(xfrm_msg);
8676eb43
LT
291 /*
292 * we do not run diff between different kind
293 * of objects.
294 */
273b9834 295 if (strncmp(temp[0].mode, temp[1].mode, 3))
8676eb43 296 return NULL;
366175ef 297 if (complete_rewrite) {
366175ef 298 emit_rewrite_diff(name_a, name_b, temp);
8676eb43 299 return NULL;
366175ef 300 }
b28858bf 301 }
8676eb43 302
3443546f
LT
303 /* Un-quote the paths */
304 if (label_path[0][0] != '/')
305 label_path[0] = quote_two("a/", name_a);
306 if (label_path[1][0] != '/')
307 label_path[1] = quote_two("b/", name_b);
308
309 printf("--- %s\n", label_path[0]);
310 printf("+++ %s\n", label_path[1]);
311
312 if (fill_mmfile(&mf1, temp[0].name) < 0 ||
313 fill_mmfile(&mf2, temp[1].name) < 0)
314 die("unable to read files to diff");
315
316 /* Crazy xdl interfaces.. */
317 {
318 xpparam_t xpp;
319 xdemitconf_t xecfg;
320 xdemitcb_t ecb;
321
322 xpp.flags = XDF_NEED_MINIMAL;
323 xecfg.ctxlen = 3;
324 ecb.outf = fn_out;
325 xdl_diff(&mf1, &mf2, &xpp, &xecfg, &ecb);
326 }
327
328 free(mf1.ptr);
329 free(mf2.ptr);
330 return NULL;
86436c28
JH
331}
332
427dcb4b
JH
333struct diff_filespec *alloc_filespec(const char *path)
334{
335 int namelen = strlen(path);
336 struct diff_filespec *spec = xmalloc(sizeof(*spec) + namelen + 1);
705a7148
LT
337
338 memset(spec, 0, sizeof(*spec));
427dcb4b 339 spec->path = (char *)(spec + 1);
705a7148 340 memcpy(spec->path, path, namelen+1);
427dcb4b
JH
341 return spec;
342}
343
344void fill_filespec(struct diff_filespec *spec, const unsigned char *sha1,
345 unsigned short mode)
346{
4130b995
JH
347 if (mode) {
348 spec->mode = DIFF_FILE_CANON_MODE(mode);
81e50eab
JH
349 memcpy(spec->sha1, sha1, 20);
350 spec->sha1_valid = !!memcmp(sha1, null_sha1, 20);
351 }
427dcb4b
JH
352}
353
b46f0b6d
JH
354/*
355 * Given a name and sha1 pair, if the dircache tells us the file in
356 * the work tree has that object contents, return true, so that
357 * prepare_temp_file() does not have to inflate and extract.
358 */
359static int work_tree_matches(const char *name, const unsigned char *sha1)
360{
361 struct cache_entry *ce;
362 struct stat st;
363 int pos, len;
5c97558c 364
b46f0b6d
JH
365 /* We do not read the cache ourselves here, because the
366 * benchmark with my previous version that always reads cache
367 * shows that it makes things worse for diff-tree comparing
368 * two linux-2.6 kernel trees in an already checked out work
915838c3 369 * tree. This is because most diff-tree comparisons deal with
b46f0b6d
JH
370 * only a small number of files, while reading the cache is
371 * expensive for a large project, and its cost outweighs the
372 * savings we get by not inflating the object to a temporary
373 * file. Practically, this code only helps when we are used
374 * by diff-cache --cached, which does read the cache before
375 * calling us.
57fe64a4 376 */
b46f0b6d
JH
377 if (!active_cache)
378 return 0;
379
380 len = strlen(name);
381 pos = cache_name_pos(name, len);
382 if (pos < 0)
383 return 0;
384 ce = active_cache[pos];
b28858bf 385 if ((lstat(name, &st) < 0) ||
427dcb4b 386 !S_ISREG(st.st_mode) || /* careful! */
5f73076c 387 ce_match_stat(ce, &st, 0) ||
b46f0b6d
JH
388 memcmp(sha1, ce->sha1, 20))
389 return 0;
427dcb4b
JH
390 /* we return 1 only when we can stat, it is a regular file,
391 * stat information matches, and sha1 recorded in the cache
392 * matches. I.e. we know the file in the work tree really is
393 * the same as the <name, sha1> pair.
394 */
b46f0b6d
JH
395 return 1;
396}
397
f0c6b2a2
JH
398static struct sha1_size_cache {
399 unsigned char sha1[20];
400 unsigned long size;
401} **sha1_size_cache;
402static int sha1_size_cache_nr, sha1_size_cache_alloc;
403
404static struct sha1_size_cache *locate_size_cache(unsigned char *sha1,
0601e131 405 int find_only,
f0c6b2a2
JH
406 unsigned long size)
407{
408 int first, last;
409 struct sha1_size_cache *e;
410
411 first = 0;
412 last = sha1_size_cache_nr;
413 while (last > first) {
0601e131 414 int cmp, next = (last + first) >> 1;
f0c6b2a2 415 e = sha1_size_cache[next];
0601e131 416 cmp = memcmp(e->sha1, sha1, 20);
f0c6b2a2
JH
417 if (!cmp)
418 return e;
419 if (cmp < 0) {
420 last = next;
421 continue;
422 }
423 first = next+1;
424 }
425 /* not found */
0601e131 426 if (find_only)
f0c6b2a2
JH
427 return NULL;
428 /* insert to make it at "first" */
429 if (sha1_size_cache_alloc <= sha1_size_cache_nr) {
430 sha1_size_cache_alloc = alloc_nr(sha1_size_cache_alloc);
431 sha1_size_cache = xrealloc(sha1_size_cache,
432 sha1_size_cache_alloc *
433 sizeof(*sha1_size_cache));
434 }
435 sha1_size_cache_nr++;
436 if (first < sha1_size_cache_nr)
437 memmove(sha1_size_cache + first + 1, sha1_size_cache + first,
438 (sha1_size_cache_nr - first - 1) *
439 sizeof(*sha1_size_cache));
440 e = xmalloc(sizeof(struct sha1_size_cache));
441 sha1_size_cache[first] = e;
442 memcpy(e->sha1, sha1, 20);
443 e->size = size;
444 return e;
445}
446
427dcb4b
JH
447/*
448 * While doing rename detection and pickaxe operation, we may need to
449 * grab the data for the blob (or file) for our own in-core comparison.
450 * diff_filespec has data and size fields for this purpose.
451 */
f0c6b2a2 452int diff_populate_filespec(struct diff_filespec *s, int size_only)
427dcb4b
JH
453{
454 int err = 0;
81e50eab 455 if (!DIFF_FILE_VALID(s))
427dcb4b
JH
456 die("internal error: asking to populate invalid file.");
457 if (S_ISDIR(s->mode))
458 return -1;
459
f0c6b2a2
JH
460 if (!use_size_cache)
461 size_only = 0;
462
427dcb4b
JH
463 if (s->data)
464 return err;
465 if (!s->sha1_valid ||
466 work_tree_matches(s->path, s->sha1)) {
467 struct stat st;
468 int fd;
469 if (lstat(s->path, &st) < 0) {
470 if (errno == ENOENT) {
471 err_empty:
472 err = -1;
473 empty:
474 s->data = "";
475 s->size = 0;
476 return err;
477 }
478 }
479 s->size = st.st_size;
480 if (!s->size)
481 goto empty;
f0c6b2a2
JH
482 if (size_only)
483 return 0;
427dcb4b
JH
484 if (S_ISLNK(st.st_mode)) {
485 int ret;
486 s->data = xmalloc(s->size);
487 s->should_free = 1;
488 ret = readlink(s->path, s->data, s->size);
489 if (ret < 0) {
490 free(s->data);
491 goto err_empty;
492 }
493 return 0;
494 }
495 fd = open(s->path, O_RDONLY);
496 if (fd < 0)
497 goto err_empty;
498 s->data = mmap(NULL, s->size, PROT_READ, MAP_PRIVATE, fd, 0);
427dcb4b 499 close(fd);
e35f9824
PR
500 if (s->data == MAP_FAILED)
501 goto err_empty;
502 s->should_munmap = 1;
427dcb4b
JH
503 }
504 else {
505 char type[20];
f0c6b2a2
JH
506 struct sha1_size_cache *e;
507
508 if (size_only) {
0601e131 509 e = locate_size_cache(s->sha1, 1, 0);
f0c6b2a2
JH
510 if (e) {
511 s->size = e->size;
512 return 0;
513 }
36e4d74a 514 if (!sha1_object_info(s->sha1, type, &s->size))
0601e131 515 locate_size_cache(s->sha1, 0, s->size);
65c2e0c3
JH
516 }
517 else {
518 s->data = read_sha1_file(s->sha1, type, &s->size);
519 s->should_free = 1;
f0c6b2a2 520 }
427dcb4b
JH
521 }
522 return 0;
523}
524
19397b45 525void diff_free_filespec_data(struct diff_filespec *s)
427dcb4b
JH
526{
527 if (s->should_free)
528 free(s->data);
529 else if (s->should_munmap)
530 munmap(s->data, s->size);
19397b45
JH
531 s->should_free = s->should_munmap = 0;
532 s->data = NULL;
427dcb4b
JH
533}
534
b28858bf
JH
535static void prep_temp_blob(struct diff_tempfile *temp,
536 void *blob,
537 unsigned long size,
88cd621d 538 const unsigned char *sha1,
b28858bf
JH
539 int mode)
540{
541 int fd;
542
64f8a631 543 fd = git_mkstemp(temp->tmp_path, TEMPFILE_PATH_LEN, ".diff_XXXXXX");
b28858bf
JH
544 if (fd < 0)
545 die("unable to create temp-file");
546 if (write(fd, blob, size) != size)
547 die("unable to write temp-file");
548 close(fd);
549 temp->name = temp->tmp_path;
550 strcpy(temp->hex, sha1_to_hex(sha1));
551 temp->hex[40] = 0;
552 sprintf(temp->mode, "%06o", mode);
553}
554
be3cfa85
JH
555static void prepare_temp_file(const char *name,
556 struct diff_tempfile *temp,
427dcb4b 557 struct diff_filespec *one)
86436c28 558{
81e50eab 559 if (!DIFF_FILE_VALID(one)) {
be3cfa85 560 not_a_valid_file:
532149d7
JH
561 /* A '-' entry produces this for file-2, and
562 * a '+' entry produces this for file-1.
563 */
be3cfa85
JH
564 temp->name = "/dev/null";
565 strcpy(temp->hex, ".");
566 strcpy(temp->mode, ".");
86436c28
JH
567 return;
568 }
be3cfa85 569
5c97558c 570 if (!one->sha1_valid ||
427dcb4b 571 work_tree_matches(name, one->sha1)) {
be3cfa85 572 struct stat st;
427dcb4b 573 if (lstat(name, &st) < 0) {
be3cfa85
JH
574 if (errno == ENOENT)
575 goto not_a_valid_file;
427dcb4b 576 die("stat(%s): %s", name, strerror(errno));
be3cfa85 577 }
b28858bf
JH
578 if (S_ISLNK(st.st_mode)) {
579 int ret;
7e4a2a84
JH
580 char buf[PATH_MAX + 1]; /* ought to be SYMLINK_MAX */
581 if (sizeof(buf) <= st.st_size)
582 die("symlink too long: %s", name);
b28858bf
JH
583 ret = readlink(name, buf, st.st_size);
584 if (ret < 0)
585 die("readlink(%s)", name);
586 prep_temp_blob(temp, buf, st.st_size,
587 (one->sha1_valid ?
427dcb4b 588 one->sha1 : null_sha1),
b28858bf
JH
589 (one->sha1_valid ?
590 one->mode : S_IFLNK));
591 }
592 else {
427dcb4b
JH
593 /* we can borrow from the file in the work tree */
594 temp->name = name;
b28858bf
JH
595 if (!one->sha1_valid)
596 strcpy(temp->hex, sha1_to_hex(null_sha1));
597 else
427dcb4b 598 strcpy(temp->hex, sha1_to_hex(one->sha1));
9d429ff6
JH
599 /* Even though we may sometimes borrow the
600 * contents from the work tree, we always want
601 * one->mode. mode is trustworthy even when
602 * !(one->sha1_valid), as long as
603 * DIFF_FILE_VALID(one).
604 */
605 sprintf(temp->mode, "%06o", one->mode);
b28858bf
JH
606 }
607 return;
be3cfa85
JH
608 }
609 else {
f0c6b2a2 610 if (diff_populate_filespec(one, 0))
427dcb4b
JH
611 die("cannot read data blob for %s", one->path);
612 prep_temp_blob(temp, one->data, one->size,
613 one->sha1, one->mode);
be3cfa85
JH
614 }
615}
616
617static void remove_tempfile(void)
618{
619 int i;
620
621 for (i = 0; i < 2; i++)
622 if (diff_temp[i].name == diff_temp[i].tmp_path) {
623 unlink(diff_temp[i].name);
624 diff_temp[i].name = NULL;
625 }
626}
627
532149d7
JH
628static void remove_tempfile_on_signal(int signo)
629{
630 remove_tempfile();
6a1f79c1
PB
631 signal(SIGINT, SIG_DFL);
632 raise(signo);
532149d7
JH
633}
634
8676eb43
LT
635static int spawn_prog(const char *pgm, const char **arg)
636{
637 pid_t pid;
638 int status;
639
640 fflush(NULL);
641 pid = fork();
642 if (pid < 0)
643 die("unable to fork");
644 if (!pid) {
645 execvp(pgm, (char *const*) arg);
646 exit(255);
647 }
648
649 while (waitpid(pid, &status, 0) < 0) {
650 if (errno == EINTR)
651 continue;
652 return -1;
653 }
654
655 /* Earlier we did not check the exit status because
656 * diff exits non-zero if files are different, and
657 * we are not interested in knowing that. It was a
658 * mistake which made it harder to quit a diff-*
659 * session that uses the git-apply-patch-script as
660 * the GIT_EXTERNAL_DIFF. A custom GIT_EXTERNAL_DIFF
661 * should also exit non-zero only when it wants to
662 * abort the entire diff-* session.
663 */
664 if (WIFEXITED(status) && !WEXITSTATUS(status))
665 return 0;
666 return -1;
667}
668
be3cfa85
JH
669/* An external diff command takes:
670 *
671 * diff-cmd name infile1 infile1-sha1 infile1-mode \
5c97558c 672 * infile2 infile2-sha1 infile2-mode [ rename-to ]
be3cfa85
JH
673 *
674 */
4130b995
JH
675static void run_external_diff(const char *pgm,
676 const char *name,
5c97558c 677 const char *other,
427dcb4b
JH
678 struct diff_filespec *one,
679 struct diff_filespec *two,
366175ef
JH
680 const char *xfrm_msg,
681 int complete_rewrite)
be3cfa85 682{
8676eb43 683 const char *spawn_arg[10];
be3cfa85 684 struct diff_tempfile *temp = diff_temp;
8676eb43 685 int retval;
be3cfa85 686 static int atexit_asked = 0;
c7c81b3a 687 const char *othername;
be3cfa85 688
c7c81b3a 689 othername = (other? other : name);
77eb2720
JH
690 if (one && two) {
691 prepare_temp_file(name, &temp[0], one);
c7c81b3a 692 prepare_temp_file(othername, &temp[1], two);
77eb2720
JH
693 if (! atexit_asked &&
694 (temp[0].name == temp[0].tmp_path ||
695 temp[1].name == temp[1].tmp_path)) {
696 atexit_asked = 1;
697 atexit(remove_tempfile);
698 }
532149d7 699 signal(SIGINT, remove_tempfile_on_signal);
be3cfa85
JH
700 }
701
8676eb43
LT
702 if (pgm) {
703 const char **arg = &spawn_arg[0];
704 if (one && two) {
705 *arg++ = pgm;
706 *arg++ = name;
707 *arg++ = temp[0].name;
708 *arg++ = temp[0].hex;
709 *arg++ = temp[0].mode;
710 *arg++ = temp[1].name;
711 *arg++ = temp[1].hex;
712 *arg++ = temp[1].mode;
713 if (other) {
714 *arg++ = other;
715 *arg++ = xfrm_msg;
5c97558c 716 }
8676eb43
LT
717 } else {
718 *arg++ = pgm;
719 *arg++ = name;
77eb2720 720 }
8676eb43
LT
721 *arg = NULL;
722 } else {
723 if (one && two) {
724 pgm = builtin_diff(name, othername, temp, xfrm_msg, complete_rewrite, spawn_arg);
725 } else
77eb2720 726 printf("* Unmerged path %s\n", name);
be3cfa85 727 }
8676eb43
LT
728
729 retval = 0;
730 if (pgm)
731 retval = spawn_prog(pgm, spawn_arg);
732 remove_tempfile();
733 if (retval) {
6fa28064
JH
734 fprintf(stderr, "external diff died, stopping at %s.\n", name);
735 exit(1);
532149d7 736 }
be3cfa85
JH
737}
738
ec1fcc16
JH
739static void diff_fill_sha1_info(struct diff_filespec *one)
740{
741 if (DIFF_FILE_VALID(one)) {
742 if (!one->sha1_valid) {
743 struct stat st;
975b31dc 744 if (lstat(one->path, &st) < 0)
ec1fcc16
JH
745 die("stat %s", one->path);
746 if (index_path(one->sha1, one->path, &st, 0))
747 die("cannot hash %s\n", one->path);
748 }
749 }
750 else
751 memset(one->sha1, 0, 20);
752}
753
80b1e511 754static void run_diff(struct diff_filepair *p, struct diff_options *o)
4130b995
JH
755{
756 const char *pgm = external_diff();
ec1fcc16 757 char msg[PATH_MAX*2+300], *xfrm_msg;
366175ef
JH
758 struct diff_filespec *one;
759 struct diff_filespec *two;
760 const char *name;
761 const char *other;
cf9dfc66 762 char *name_munged, *other_munged;
366175ef 763 int complete_rewrite = 0;
ec1fcc16 764 int len;
366175ef
JH
765
766 if (DIFF_PAIR_UNMERGED(p)) {
767 /* unmerged */
768 run_external_diff(pgm, p->one->path, NULL, NULL, NULL, NULL,
769 0);
770 return;
771 }
772
773 name = p->one->path;
774 other = (strcmp(name, p->two->path) ? p->two->path : NULL);
cf9dfc66
JH
775 name_munged = quote_one(name);
776 other_munged = quote_one(other);
366175ef 777 one = p->one; two = p->two;
ec1fcc16
JH
778
779 diff_fill_sha1_info(one);
780 diff_fill_sha1_info(two);
781
782 len = 0;
366175ef 783 switch (p->status) {
e7baa4f4 784 case DIFF_STATUS_COPIED:
ec1fcc16
JH
785 len += snprintf(msg + len, sizeof(msg) - len,
786 "similarity index %d%%\n"
787 "copy from %s\n"
788 "copy to %s\n",
789 (int)(0.5 + p->score * 100.0/MAX_SCORE),
cf9dfc66 790 name_munged, other_munged);
366175ef 791 break;
e7baa4f4 792 case DIFF_STATUS_RENAMED:
ec1fcc16
JH
793 len += snprintf(msg + len, sizeof(msg) - len,
794 "similarity index %d%%\n"
795 "rename from %s\n"
796 "rename to %s\n",
797 (int)(0.5 + p->score * 100.0/MAX_SCORE),
cf9dfc66 798 name_munged, other_munged);
366175ef 799 break;
e7baa4f4 800 case DIFF_STATUS_MODIFIED:
366175ef 801 if (p->score) {
ec1fcc16
JH
802 len += snprintf(msg + len, sizeof(msg) - len,
803 "dissimilarity index %d%%\n",
804 (int)(0.5 + p->score *
805 100.0/MAX_SCORE));
366175ef
JH
806 complete_rewrite = 1;
807 break;
808 }
809 /* fallthru */
810 default:
ec1fcc16
JH
811 /* nothing */
812 ;
366175ef
JH
813 }
814
ec1fcc16
JH
815 if (memcmp(one->sha1, two->sha1, 20)) {
816 char one_sha1[41];
46a6c262 817 int abbrev = o->full_index ? 40 : DEFAULT_ABBREV;
ec1fcc16
JH
818 memcpy(one_sha1, sha1_to_hex(one->sha1), 41);
819
820 len += snprintf(msg + len, sizeof(msg) - len,
47dd0d59
JH
821 "index %.*s..%.*s",
822 abbrev, one_sha1, abbrev,
823 sha1_to_hex(two->sha1));
ec1fcc16
JH
824 if (one->mode == two->mode)
825 len += snprintf(msg + len, sizeof(msg) - len,
826 " %06o", one->mode);
827 len += snprintf(msg + len, sizeof(msg) - len, "\n");
828 }
829
830 if (len)
831 msg[--len] = 0;
832 xfrm_msg = len ? msg : NULL;
833
4130b995
JH
834 if (!pgm &&
835 DIFF_FILE_VALID(one) && DIFF_FILE_VALID(two) &&
836 (S_IFMT & one->mode) != (S_IFMT & two->mode)) {
837 /* a filepair that changes between file and symlink
838 * needs to be split into deletion and creation.
839 */
840 struct diff_filespec *null = alloc_filespec(two->path);
366175ef 841 run_external_diff(NULL, name, other, one, null, xfrm_msg, 0);
4130b995
JH
842 free(null);
843 null = alloc_filespec(one->path);
366175ef 844 run_external_diff(NULL, name, other, null, two, xfrm_msg, 0);
4130b995
JH
845 free(null);
846 }
847 else
366175ef
JH
848 run_external_diff(pgm, name, other, one, two, xfrm_msg,
849 complete_rewrite);
cf9dfc66
JH
850
851 free(name_munged);
852 free(other_munged);
4130b995
JH
853}
854
6b5ee137 855void diff_setup(struct diff_options *options)
5c97558c 856{
6b5ee137
JH
857 memset(options, 0, sizeof(*options));
858 options->output_format = DIFF_FORMAT_RAW;
859 options->line_termination = '\n';
860 options->break_opt = -1;
8082d8d3 861 options->rename_limit = -1;
ac1b3d12
LT
862
863 options->change = diff_change;
864 options->add_remove = diff_addremove;
6b5ee137
JH
865}
866
867int diff_setup_done(struct diff_options *options)
868{
3299c6f6
JH
869 if ((options->find_copies_harder &&
870 options->detect_rename != DIFF_DETECT_COPY) ||
871 (0 <= options->rename_limit && !options->detect_rename))
6b5ee137 872 return -1;
3299c6f6
JH
873 if (options->detect_rename && options->rename_limit < 0)
874 options->rename_limit = diff_rename_limit_default;
6b5ee137 875 if (options->setup & DIFF_SETUP_USE_CACHE) {
f0c6b2a2
JH
876 if (!active_cache)
877 /* read-cache does not die even when it fails
878 * so it is safe for us to do this here. Also
879 * it does not smudge active_cache or active_nr
880 * when it fails, so we do not have to worry about
82f9d58a 881 * cleaning it up ourselves either.
f0c6b2a2
JH
882 */
883 read_cache();
884 }
6b5ee137 885 if (options->setup & DIFF_SETUP_USE_SIZE_CACHE)
f0c6b2a2 886 use_size_cache = 1;
47dd0d59
JH
887 if (options->abbrev <= 0 || 40 < options->abbrev)
888 options->abbrev = 40; /* full */
6b5ee137
JH
889
890 return 0;
891}
892
893int diff_opt_parse(struct diff_options *options, const char **av, int ac)
894{
895 const char *arg = av[0];
896 if (!strcmp(arg, "-p") || !strcmp(arg, "-u"))
897 options->output_format = DIFF_FORMAT_PATCH;
898 else if (!strcmp(arg, "-z"))
899 options->line_termination = 0;
8082d8d3
JH
900 else if (!strncmp(arg, "-l", 2))
901 options->rename_limit = strtoul(arg+2, NULL, 10);
80b1e511
JH
902 else if (!strcmp(arg, "--full-index"))
903 options->full_index = 1;
6b5ee137
JH
904 else if (!strcmp(arg, "--name-only"))
905 options->output_format = DIFF_FORMAT_NAME;
946f5f7c
JH
906 else if (!strcmp(arg, "--name-status"))
907 options->output_format = DIFF_FORMAT_NAME_STATUS;
6b5ee137
JH
908 else if (!strcmp(arg, "-R"))
909 options->reverse_diff = 1;
910 else if (!strncmp(arg, "-S", 2))
911 options->pickaxe = arg + 2;
912 else if (!strcmp(arg, "-s"))
913 options->output_format = DIFF_FORMAT_NO_OUTPUT;
914 else if (!strncmp(arg, "-O", 2))
915 options->orderfile = arg + 2;
916 else if (!strncmp(arg, "--diff-filter=", 14))
917 options->filter = arg + 14;
918 else if (!strcmp(arg, "--pickaxe-all"))
919 options->pickaxe_opts = DIFF_PICKAXE_ALL;
920 else if (!strncmp(arg, "-B", 2)) {
921 if ((options->break_opt =
922 diff_scoreopt_parse(arg)) == -1)
923 return -1;
924 }
925 else if (!strncmp(arg, "-M", 2)) {
926 if ((options->rename_score =
927 diff_scoreopt_parse(arg)) == -1)
928 return -1;
929 options->detect_rename = DIFF_DETECT_RENAME;
930 }
931 else if (!strncmp(arg, "-C", 2)) {
932 if ((options->rename_score =
933 diff_scoreopt_parse(arg)) == -1)
934 return -1;
935 options->detect_rename = DIFF_DETECT_COPY;
936 }
937 else if (!strcmp(arg, "--find-copies-harder"))
938 options->find_copies_harder = 1;
47dd0d59 939 else if (!strcmp(arg, "--abbrev"))
46a6c262 940 options->abbrev = DEFAULT_ABBREV;
6b1ddbdd 941 else if (!strncmp(arg, "--abbrev=", 9)) {
47dd0d59 942 options->abbrev = strtoul(arg + 9, NULL, 10);
6b1ddbdd
JH
943 if (options->abbrev < MINIMUM_ABBREV)
944 options->abbrev = MINIMUM_ABBREV;
945 else if (40 < options->abbrev)
946 options->abbrev = 40;
947 }
6b5ee137
JH
948 else
949 return 0;
950 return 1;
5c97558c
JH
951}
952
0e3994fa
JH
953static int parse_num(const char **cp_p)
954{
1b1480ff
PA
955 unsigned long num, scale;
956 int ch, dot;
0e3994fa
JH
957 const char *cp = *cp_p;
958
1b1480ff 959 num = 0;
0e3994fa 960 scale = 1;
1b1480ff
PA
961 dot = 0;
962 for(;;) {
963 ch = *cp;
964 if ( !dot && ch == '.' ) {
965 scale = 1;
966 dot = 1;
967 } else if ( ch == '%' ) {
968 scale = dot ? scale*100 : 100;
969 cp++; /* % is always at the end */
970 break;
971 } else if ( ch >= '0' && ch <= '9' ) {
972 if ( scale < 100000 ) {
973 scale *= 10;
974 num = (num*10) + (ch-'0');
975 }
976 } else {
977 break;
0e3994fa 978 }
5de36bfe 979 cp++;
0e3994fa
JH
980 }
981 *cp_p = cp;
982
983 /* user says num divided by scale and we say internally that
984 * is MAX_SCORE * num / scale.
985 */
1b1480ff 986 return (num >= scale) ? MAX_SCORE : (MAX_SCORE * num / scale);
0e3994fa
JH
987}
988
989int diff_scoreopt_parse(const char *opt)
990{
eeaa4603 991 int opt1, opt2, cmd;
0e3994fa
JH
992
993 if (*opt++ != '-')
994 return -1;
995 cmd = *opt++;
996 if (cmd != 'M' && cmd != 'C' && cmd != 'B')
997 return -1; /* that is not a -M, -C nor -B option */
998
999 opt1 = parse_num(&opt);
eeaa4603
JH
1000 if (cmd != 'B')
1001 opt2 = 0;
1002 else {
1003 if (*opt == 0)
1004 opt2 = 0;
1005 else if (*opt != '/')
1006 return -1; /* we expect -B80/99 or -B80 */
1007 else {
1008 opt++;
1009 opt2 = parse_num(&opt);
1010 }
1011 }
0e3994fa
JH
1012 if (*opt != 0)
1013 return -1;
eeaa4603 1014 return opt1 | (opt2 << 16);
0e3994fa
JH
1015}
1016
6b14d7fa
JH
1017struct diff_queue_struct diff_queued_diff;
1018
1019void diff_q(struct diff_queue_struct *queue, struct diff_filepair *dp)
5c97558c 1020{
6b14d7fa
JH
1021 if (queue->alloc <= queue->nr) {
1022 queue->alloc = alloc_nr(queue->alloc);
1023 queue->queue = xrealloc(queue->queue,
1024 sizeof(dp) * queue->alloc);
81e50eab 1025 }
6b14d7fa 1026 queue->queue[queue->nr++] = dp;
5c97558c
JH
1027}
1028
52e95789 1029struct diff_filepair *diff_queue(struct diff_queue_struct *queue,
6b14d7fa
JH
1030 struct diff_filespec *one,
1031 struct diff_filespec *two)
5c97558c 1032{
52e95789 1033 struct diff_filepair *dp = xmalloc(sizeof(*dp));
427dcb4b
JH
1034 dp->one = one;
1035 dp->two = two;
f7c1512a 1036 dp->score = 0;
2210100a 1037 dp->status = 0;
15d061b4 1038 dp->source_stays = 0;
f345b0a0 1039 dp->broken_pair = 0;
5098bafb
JH
1040 if (queue)
1041 diff_q(queue, dp);
427dcb4b 1042 return dp;
5c97558c
JH
1043}
1044
226406f6
JH
1045void diff_free_filepair(struct diff_filepair *p)
1046{
19397b45
JH
1047 diff_free_filespec_data(p->one);
1048 diff_free_filespec_data(p->two);
5098bafb
JH
1049 free(p->one);
1050 free(p->two);
226406f6
JH
1051 free(p);
1052}
1053
47dd0d59 1054/* This is different from find_unique_abbrev() in that
297a1aad 1055 * it stuffs the result with dots for alignment.
47dd0d59
JH
1056 */
1057const char *diff_unique_abbrev(const unsigned char *sha1, int len)
1058{
1059 int abblen;
1060 const char *abbrev;
1061 if (len == 40)
1062 return sha1_to_hex(sha1);
1063
1064 abbrev = find_unique_abbrev(sha1, len);
297a1aad
JH
1065 if (!abbrev)
1066 return sha1_to_hex(sha1);
47dd0d59
JH
1067 abblen = strlen(abbrev);
1068 if (abblen < 37) {
1069 static char hex[41];
1070 if (len < abblen && abblen <= len + 2)
1071 sprintf(hex, "%s%.*s", abbrev, len+3-abblen, "..");
1072 else
1073 sprintf(hex, "%s...", abbrev);
1074 return hex;
1075 }
1076 return sha1_to_hex(sha1);
1077}
1078
bceafe75
JH
1079static void diff_flush_raw(struct diff_filepair *p,
1080 int line_termination,
946f5f7c 1081 int inter_name_termination,
47dd0d59 1082 struct diff_options *options)
5c97558c 1083{
b6d8f309
JH
1084 int two_paths;
1085 char status[10];
47dd0d59 1086 int abbrev = options->abbrev;
cf9dfc66 1087 const char *path_one, *path_two;
47dd0d59 1088 int output_format = options->output_format;
25d5ea41 1089
cf9dfc66
JH
1090 path_one = p->one->path;
1091 path_two = p->two->path;
25d5ea41 1092 if (line_termination) {
cf9dfc66
JH
1093 path_one = quote_one(path_one);
1094 path_two = quote_one(path_two);
25d5ea41
JH
1095 }
1096
366175ef
JH
1097 if (p->score)
1098 sprintf(status, "%c%03d", p->status,
1099 (int)(0.5 + p->score * 100.0/MAX_SCORE));
1100 else {
1101 status[0] = p->status;
1102 status[1] = 0;
1103 }
b6d8f309 1104 switch (p->status) {
e7baa4f4
JH
1105 case DIFF_STATUS_COPIED:
1106 case DIFF_STATUS_RENAMED:
b6d8f309 1107 two_paths = 1;
b6d8f309 1108 break;
e7baa4f4
JH
1109 case DIFF_STATUS_ADDED:
1110 case DIFF_STATUS_DELETED:
f345b0a0 1111 two_paths = 0;
f345b0a0 1112 break;
b6d8f309
JH
1113 default:
1114 two_paths = 0;
b6d8f309 1115 break;
6b14d7fa 1116 }
946f5f7c
JH
1117 if (output_format != DIFF_FORMAT_NAME_STATUS) {
1118 printf(":%06o %06o %s ",
47dd0d59
JH
1119 p->one->mode, p->two->mode,
1120 diff_unique_abbrev(p->one->sha1, abbrev));
1121 printf("%s ",
1122 diff_unique_abbrev(p->two->sha1, abbrev));
946f5f7c 1123 }
cf9dfc66 1124 printf("%s%c%s", status, inter_name_termination, path_one);
b6d8f309 1125 if (two_paths)
cf9dfc66 1126 printf("%c%s", inter_name_termination, path_two);
b6d8f309 1127 putchar(line_termination);
cf9dfc66
JH
1128 if (path_one != p->one->path)
1129 free((void*)path_one);
1130 if (path_two != p->two->path)
1131 free((void*)path_two);
5c97558c
JH
1132}
1133
52f28529 1134static void diff_flush_name(struct diff_filepair *p,
cf9dfc66 1135 int inter_name_termination,
52f28529
JH
1136 int line_termination)
1137{
cf9dfc66
JH
1138 char *path = p->two->path;
1139
1140 if (line_termination)
1141 path = quote_one(p->two->path);
1142 else
1143 path = p->two->path;
1144 printf("%s%c", path, line_termination);
1145 if (p->two->path != path)
1146 free(path);
52f28529
JH
1147}
1148
f7c1512a 1149int diff_unmodified_pair(struct diff_filepair *p)
5c97558c 1150{
427dcb4b
JH
1151 /* This function is written stricter than necessary to support
1152 * the currently implemented transformers, but the idea is to
52e95789 1153 * let transformers to produce diff_filepairs any way they want,
427dcb4b 1154 * and filter and clean them up here before producing the output.
5c97558c 1155 */
6b14d7fa
JH
1156 struct diff_filespec *one, *two;
1157
1158 if (DIFF_PAIR_UNMERGED(p))
1159 return 0; /* unmerged is interesting */
5c97558c 1160
6b14d7fa
JH
1161 one = p->one;
1162 two = p->two;
5c97558c 1163
4130b995
JH
1164 /* deletion, addition, mode or type change
1165 * and rename are all interesting.
1166 */
81e50eab 1167 if (DIFF_FILE_VALID(one) != DIFF_FILE_VALID(two) ||
4130b995 1168 DIFF_PAIR_MODE_CHANGED(p) ||
427dcb4b
JH
1169 strcmp(one->path, two->path))
1170 return 0;
57fe64a4 1171
427dcb4b
JH
1172 /* both are valid and point at the same path. that is, we are
1173 * dealing with a change.
57fe64a4 1174 */
427dcb4b
JH
1175 if (one->sha1_valid && two->sha1_valid &&
1176 !memcmp(one->sha1, two->sha1, sizeof(one->sha1)))
1177 return 1; /* no change */
1178 if (!one->sha1_valid && !two->sha1_valid)
1179 return 1; /* both look at the same file on the filesystem. */
1180 return 0;
57fe64a4
JH
1181}
1182
80b1e511 1183static void diff_flush_patch(struct diff_filepair *p, struct diff_options *o)
f7c1512a 1184{
f7c1512a
JH
1185 if (diff_unmodified_pair(p))
1186 return;
1187
f7c1512a
JH
1188 if ((DIFF_FILE_VALID(p->one) && S_ISDIR(p->one->mode)) ||
1189 (DIFF_FILE_VALID(p->two) && S_ISDIR(p->two->mode)))
1190 return; /* no tree diffs in patch format */
1191
80b1e511 1192 run_diff(p, o);
f7c1512a
JH
1193}
1194
bceafe75 1195int diff_queue_is_empty(void)
f7c1512a 1196{
bceafe75 1197 struct diff_queue_struct *q = &diff_queued_diff;
25d5ea41
JH
1198 int i;
1199 for (i = 0; i < q->nr; i++)
1200 if (!diff_unmodified_pair(q->queue[i]))
1201 return 0;
1202 return 1;
f7c1512a
JH
1203}
1204
25d5ea41
JH
1205#if DIFF_DEBUG
1206void diff_debug_filespec(struct diff_filespec *s, int x, const char *one)
1207{
1208 fprintf(stderr, "queue[%d] %s (%s) %s %06o %s\n",
c7c81b3a 1209 x, one ? one : "",
25d5ea41
JH
1210 s->path,
1211 DIFF_FILE_VALID(s) ? "valid" : "invalid",
1212 s->mode,
1213 s->sha1_valid ? sha1_to_hex(s->sha1) : "");
1214 fprintf(stderr, "queue[%d] %s size %lu flags %d\n",
c7c81b3a 1215 x, one ? one : "",
25d5ea41
JH
1216 s->size, s->xfrm_flags);
1217}
1218
1219void diff_debug_filepair(const struct diff_filepair *p, int i)
1220{
1221 diff_debug_filespec(p->one, i, "one");
1222 diff_debug_filespec(p->two, i, "two");
f345b0a0 1223 fprintf(stderr, "score %d, status %c stays %d broken %d\n",
c7c81b3a 1224 p->score, p->status ? p->status : '?',
f345b0a0 1225 p->source_stays, p->broken_pair);
25d5ea41
JH
1226}
1227
1228void diff_debug_queue(const char *msg, struct diff_queue_struct *q)
6b14d7fa 1229{
6b14d7fa 1230 int i;
25d5ea41
JH
1231 if (msg)
1232 fprintf(stderr, "%s\n", msg);
1233 fprintf(stderr, "q->nr = %d\n", q->nr);
6b14d7fa
JH
1234 for (i = 0; i < q->nr; i++) {
1235 struct diff_filepair *p = q->queue[i];
25d5ea41
JH
1236 diff_debug_filepair(p, i);
1237 }
1238}
1239#endif
1240
1241static void diff_resolve_rename_copy(void)
1242{
1243 int i, j;
1244 struct diff_filepair *p, *pp;
1245 struct diff_queue_struct *q = &diff_queued_diff;
1246
25d5ea41
JH
1247 diff_debug_queue("resolve-rename-copy", q);
1248
1249 for (i = 0; i < q->nr; i++) {
1250 p = q->queue[i];
96716a19 1251 p->status = 0; /* undecided */
bceafe75 1252 if (DIFF_PAIR_UNMERGED(p))
e7baa4f4 1253 p->status = DIFF_STATUS_UNMERGED;
15d061b4 1254 else if (!DIFF_FILE_VALID(p->one))
e7baa4f4 1255 p->status = DIFF_STATUS_ADDED;
2cd68882 1256 else if (!DIFF_FILE_VALID(p->two))
e7baa4f4 1257 p->status = DIFF_STATUS_DELETED;
96716a19 1258 else if (DIFF_PAIR_TYPE_CHANGED(p))
e7baa4f4 1259 p->status = DIFF_STATUS_TYPE_CHANGED;
96716a19
JH
1260
1261 /* from this point on, we are dealing with a pair
1262 * whose both sides are valid and of the same type, i.e.
1263 * either in-place edit or rename/copy edit.
1264 */
01c4e70f 1265 else if (DIFF_PAIR_RENAME(p)) {
15d061b4 1266 if (p->source_stays) {
e7baa4f4 1267 p->status = DIFF_STATUS_COPIED;
15d061b4
JH
1268 continue;
1269 }
1270 /* See if there is some other filepair that
1271 * copies from the same source as us. If so
6bac10d8
JH
1272 * we are a copy. Otherwise we are either a
1273 * copy if the path stays, or a rename if it
1274 * does not, but we already handled "stays" case.
25d5ea41 1275 */
15d061b4 1276 for (j = i + 1; j < q->nr; j++) {
25d5ea41
JH
1277 pp = q->queue[j];
1278 if (strcmp(pp->one->path, p->one->path))
15d061b4 1279 continue; /* not us */
01c4e70f 1280 if (!DIFF_PAIR_RENAME(pp))
15d061b4
JH
1281 continue; /* not a rename/copy */
1282 /* pp is a rename/copy from the same source */
e7baa4f4 1283 p->status = DIFF_STATUS_COPIED;
15d061b4 1284 break;
25d5ea41
JH
1285 }
1286 if (!p->status)
e7baa4f4 1287 p->status = DIFF_STATUS_RENAMED;
bceafe75 1288 }
9fdade06
JH
1289 else if (memcmp(p->one->sha1, p->two->sha1, 20) ||
1290 p->one->mode != p->two->mode)
e7baa4f4 1291 p->status = DIFF_STATUS_MODIFIED;
67574c40
JH
1292 else {
1293 /* This is a "no-change" entry and should not
1294 * happen anymore, but prepare for broken callers.
15d061b4 1295 */
67574c40
JH
1296 error("feeding unmodified %s to diffcore",
1297 p->one->path);
e7baa4f4 1298 p->status = DIFF_STATUS_UNKNOWN;
67574c40 1299 }
6b14d7fa 1300 }
25d5ea41 1301 diff_debug_queue("resolve-rename-copy done", q);
38c6f780
JH
1302}
1303
6b5ee137 1304void diff_flush(struct diff_options *options)
38c6f780
JH
1305{
1306 struct diff_queue_struct *q = &diff_queued_diff;
1307 int i;
bceafe75 1308 int inter_name_termination = '\t';
6b5ee137
JH
1309 int diff_output_format = options->output_format;
1310 int line_termination = options->line_termination;
38c6f780 1311
e68b6f15
LT
1312 if (!line_termination)
1313 inter_name_termination = 0;
bceafe75 1314
f7c1512a 1315 for (i = 0; i < q->nr; i++) {
f7c1512a 1316 struct diff_filepair *p = q->queue[i];
6b5ee137 1317 if ((diff_output_format == DIFF_FORMAT_NO_OUTPUT) ||
e7baa4f4 1318 (p->status == DIFF_STATUS_UNKNOWN))
96716a19 1319 continue;
bceafe75 1320 if (p->status == 0)
96716a19 1321 die("internal error in diff-resolve-rename-copy");
6b5ee137 1322 switch (diff_output_format) {
bceafe75 1323 case DIFF_FORMAT_PATCH:
80b1e511 1324 diff_flush_patch(p, options);
bceafe75 1325 break;
e68b6f15 1326 case DIFF_FORMAT_RAW:
946f5f7c 1327 case DIFF_FORMAT_NAME_STATUS:
bceafe75 1328 diff_flush_raw(p, line_termination,
946f5f7c 1329 inter_name_termination,
47dd0d59 1330 options);
bceafe75 1331 break;
52f28529 1332 case DIFF_FORMAT_NAME:
cf9dfc66
JH
1333 diff_flush_name(p,
1334 inter_name_termination,
1335 line_termination);
52f28529 1336 break;
bceafe75 1337 }
226406f6 1338 diff_free_filepair(q->queue[i]);
90a734dc 1339 }
427dcb4b
JH
1340 free(q->queue);
1341 q->queue = NULL;
1342 q->nr = q->alloc = 0;
5c97558c
JH
1343}
1344
f2ce9fde
JH
1345static void diffcore_apply_filter(const char *filter)
1346{
1347 int i;
1348 struct diff_queue_struct *q = &diff_queued_diff;
1349 struct diff_queue_struct outq;
1350 outq.queue = NULL;
1351 outq.nr = outq.alloc = 0;
1352
1353 if (!filter)
1354 return;
1355
e7baa4f4 1356 if (strchr(filter, DIFF_STATUS_FILTER_AON)) {
f2ce9fde
JH
1357 int found;
1358 for (i = found = 0; !found && i < q->nr; i++) {
1359 struct diff_filepair *p = q->queue[i];
e7baa4f4
JH
1360 if (((p->status == DIFF_STATUS_MODIFIED) &&
1361 ((p->score &&
1362 strchr(filter, DIFF_STATUS_FILTER_BROKEN)) ||
1363 (!p->score &&
1364 strchr(filter, DIFF_STATUS_MODIFIED)))) ||
1365 ((p->status != DIFF_STATUS_MODIFIED) &&
1366 strchr(filter, p->status)))
f2ce9fde
JH
1367 found++;
1368 }
1369 if (found)
1370 return;
1371
1372 /* otherwise we will clear the whole queue
1373 * by copying the empty outq at the end of this
1374 * function, but first clear the current entries
1375 * in the queue.
1376 */
1377 for (i = 0; i < q->nr; i++)
1378 diff_free_filepair(q->queue[i]);
1379 }
1380 else {
1381 /* Only the matching ones */
1382 for (i = 0; i < q->nr; i++) {
1383 struct diff_filepair *p = q->queue[i];
e7baa4f4
JH
1384
1385 if (((p->status == DIFF_STATUS_MODIFIED) &&
1386 ((p->score &&
1387 strchr(filter, DIFF_STATUS_FILTER_BROKEN)) ||
1388 (!p->score &&
1389 strchr(filter, DIFF_STATUS_MODIFIED)))) ||
1390 ((p->status != DIFF_STATUS_MODIFIED) &&
1391 strchr(filter, p->status)))
f2ce9fde
JH
1392 diff_q(&outq, p);
1393 else
1394 diff_free_filepair(p);
1395 }
1396 }
1397 free(q->queue);
1398 *q = outq;
1399}
1400
6b5ee137 1401void diffcore_std(struct diff_options *options)
befe8639 1402{
6b5ee137
JH
1403 if (options->paths && options->paths[0])
1404 diffcore_pathspec(options->paths);
1405 if (options->break_opt != -1)
1406 diffcore_break(options->break_opt);
1407 if (options->detect_rename)
8082d8d3 1408 diffcore_rename(options);
6b5ee137 1409 if (options->break_opt != -1)
eeaa4603 1410 diffcore_merge_broken();
6b5ee137
JH
1411 if (options->pickaxe)
1412 diffcore_pickaxe(options->pickaxe, options->pickaxe_opts);
1413 if (options->orderfile)
1414 diffcore_order(options->orderfile);
f2ce9fde 1415 diff_resolve_rename_copy();
6b5ee137 1416 diffcore_apply_filter(options->filter);
f2ce9fde
JH
1417}
1418
1419
6b5ee137 1420void diffcore_std_no_resolve(struct diff_options *options)
f2ce9fde 1421{
6b5ee137
JH
1422 if (options->pickaxe)
1423 diffcore_pickaxe(options->pickaxe, options->pickaxe_opts);
1424 if (options->orderfile)
1425 diffcore_order(options->orderfile);
1426 diffcore_apply_filter(options->filter);
befe8639
JH
1427}
1428
6b5ee137
JH
1429void diff_addremove(struct diff_options *options,
1430 int addremove, unsigned mode,
77eb2720
JH
1431 const unsigned char *sha1,
1432 const char *base, const char *path)
be3cfa85 1433{
77eb2720 1434 char concatpath[PATH_MAX];
427dcb4b
JH
1435 struct diff_filespec *one, *two;
1436
1437 /* This may look odd, but it is a preparation for
1438 * feeding "there are unchanged files which should
1439 * not produce diffs, but when you are doing copy
1440 * detection you would need them, so here they are"
1441 * entries to the diff-core. They will be prefixed
1442 * with something like '=' or '*' (I haven't decided
1443 * which but should not make any difference).
f7c1512a 1444 * Feeding the same new and old to diff_change()
bceafe75
JH
1445 * also has the same effect.
1446 * Before the final output happens, they are pruned after
1447 * merged into rename/copy pairs as appropriate.
427dcb4b 1448 */
6b5ee137 1449 if (options->reverse_diff)
427dcb4b
JH
1450 addremove = (addremove == '+' ? '-' :
1451 addremove == '-' ? '+' : addremove);
7ca45252 1452
427dcb4b
JH
1453 if (!path) path = "";
1454 sprintf(concatpath, "%s%s", base, path);
1455 one = alloc_filespec(concatpath);
1456 two = alloc_filespec(concatpath);
be3cfa85 1457
427dcb4b
JH
1458 if (addremove != '+')
1459 fill_filespec(one, sha1, mode);
1460 if (addremove != '-')
1461 fill_filespec(two, sha1, mode);
5c97558c 1462
38c6f780 1463 diff_queue(&diff_queued_diff, one, two);
be3cfa85
JH
1464}
1465
6b5ee137
JH
1466void diff_change(struct diff_options *options,
1467 unsigned old_mode, unsigned new_mode,
77eb2720
JH
1468 const unsigned char *old_sha1,
1469 const unsigned char *new_sha1,
81e50eab
JH
1470 const char *base, const char *path)
1471{
77eb2720 1472 char concatpath[PATH_MAX];
427dcb4b 1473 struct diff_filespec *one, *two;
77eb2720 1474
6b5ee137 1475 if (options->reverse_diff) {
7ca45252
JH
1476 unsigned tmp;
1477 const unsigned char *tmp_c;
1478 tmp = old_mode; old_mode = new_mode; new_mode = tmp;
1479 tmp_c = old_sha1; old_sha1 = new_sha1; new_sha1 = tmp_c;
1480 }
427dcb4b
JH
1481 if (!path) path = "";
1482 sprintf(concatpath, "%s%s", base, path);
1483 one = alloc_filespec(concatpath);
1484 two = alloc_filespec(concatpath);
1485 fill_filespec(one, old_sha1, old_mode);
1486 fill_filespec(two, new_sha1, new_mode);
1487
38c6f780 1488 diff_queue(&diff_queued_diff, one, two);
77eb2720 1489}
be3cfa85 1490
6b5ee137
JH
1491void diff_unmerge(struct diff_options *options,
1492 const char *path)
77eb2720 1493{
6b14d7fa
JH
1494 struct diff_filespec *one, *two;
1495 one = alloc_filespec(path);
1496 two = alloc_filespec(path);
1497 diff_queue(&diff_queued_diff, one, two);
86436c28 1498}