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