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