]> git.ipfire.org Git - thirdparty/git.git/blame - diff.c
git-tag: Fix -l option to use better shell style globs.
[thirdparty/git.git] / diff.c
CommitLineData
6973dcae
JH
1/*
2 * Copyright (C) 2005 Junio C Hamano
3 */
6973dcae
JH
4#include "cache.h"
5#include "quote.h"
6#include "diff.h"
7#include "diffcore.h"
051308f6 8#include "delta.h"
6973dcae 9#include "xdiff-interface.h"
7c92fe0e 10#include "color.h"
8c701249 11#include "attr.h"
6973dcae 12
1510fea7
SP
13#ifdef NO_FAST_WORKING_DIRECTORY
14#define FAST_WORKING_DIRECTORY 0
15#else
16#define FAST_WORKING_DIRECTORY 1
17#endif
18
96f1e58f 19static int diff_detect_rename_default;
801235c5 20static int diff_rename_limit_default = -1;
96f1e58f 21static int diff_use_color_default;
6973dcae 22
7c92fe0e 23static char diff_colors[][COLOR_MAXLEN] = {
f37399e6 24 "\033[m", /* reset */
448c3ef1
JH
25 "", /* PLAIN (normal) */
26 "\033[1m", /* METAINFO (bold) */
27 "\033[36m", /* FRAGINFO (cyan) */
28 "\033[31m", /* OLD (red) */
29 "\033[32m", /* NEW (green) */
30 "\033[33m", /* COMMIT (yellow) */
31 "\033[41m", /* WHITESPACE (red background) */
cd112cef
JS
32};
33
801235c5
JH
34static int parse_diff_color_slot(const char *var, int ofs)
35{
36 if (!strcasecmp(var+ofs, "plain"))
37 return DIFF_PLAIN;
38 if (!strcasecmp(var+ofs, "meta"))
39 return DIFF_METAINFO;
40 if (!strcasecmp(var+ofs, "frag"))
41 return DIFF_FRAGINFO;
42 if (!strcasecmp(var+ofs, "old"))
43 return DIFF_FILE_OLD;
44 if (!strcasecmp(var+ofs, "new"))
45 return DIFF_FILE_NEW;
ce436973
JK
46 if (!strcasecmp(var+ofs, "commit"))
47 return DIFF_COMMIT;
448c3ef1
JH
48 if (!strcasecmp(var+ofs, "whitespace"))
49 return DIFF_WHITESPACE;
801235c5
JH
50 die("bad config variable '%s'", var);
51}
52
f1af60bd
JH
53static struct ll_diff_driver {
54 const char *name;
55 struct ll_diff_driver *next;
56 char *cmd;
57} *user_diff, **user_diff_tail;
58
e0e324a4
JH
59static void read_config_if_needed(void)
60{
61 if (!user_diff_tail) {
62 user_diff_tail = &user_diff;
63 git_config(git_diff_ui_config);
64 }
65}
66
f1af60bd
JH
67/*
68 * Currently there is only "diff.<drivername>.command" variable;
69 * because there are "diff.color.<slot>" variables, we are parsing
70 * this in a bit convoluted way to allow low level diff driver
71 * called "color".
72 */
73static int parse_lldiff_command(const char *var, const char *ep, const char *value)
74{
75 const char *name;
76 int namelen;
77 struct ll_diff_driver *drv;
78
79 name = var + 5;
80 namelen = ep - name;
81 for (drv = user_diff; drv; drv = drv->next)
82 if (!strncmp(drv->name, name, namelen) && !drv->name[namelen])
83 break;
84 if (!drv) {
85 char *namebuf;
86 drv = xcalloc(1, sizeof(struct ll_diff_driver));
87 namebuf = xmalloc(namelen + 1);
88 memcpy(namebuf, name, namelen);
89 namebuf[namelen] = 0;
90 drv->name = namebuf;
91 drv->next = NULL;
92 if (!user_diff_tail)
93 user_diff_tail = &user_diff;
94 *user_diff_tail = drv;
95 user_diff_tail = &(drv->next);
96 }
97
98 if (!value)
99 return error("%s: lacks value", var);
100 drv->cmd = strdup(value);
101 return 0;
102}
103
e0e324a4
JH
104/*
105 * 'diff.<what>.funcname' attribute can be specified in the configuration
106 * to define a customized regexp to find the beginning of a function to
107 * be used for hunk header lines of "diff -p" style output.
108 */
109static struct funcname_pattern {
110 char *name;
111 char *pattern;
112 struct funcname_pattern *next;
113} *funcname_pattern_list;
114
115static int parse_funcname_pattern(const char *var, const char *ep, const char *value)
116{
117 const char *name;
118 int namelen;
119 struct funcname_pattern *pp;
120
121 name = var + 5; /* "diff." */
122 namelen = ep - name;
123
124 for (pp = funcname_pattern_list; pp; pp = pp->next)
125 if (!strncmp(pp->name, name, namelen) && !pp->name[namelen])
126 break;
127 if (!pp) {
128 char *namebuf;
129 pp = xcalloc(1, sizeof(*pp));
130 namebuf = xmalloc(namelen + 1);
131 memcpy(namebuf, name, namelen);
132 namebuf[namelen] = 0;
133 pp->name = namebuf;
134 pp->next = funcname_pattern_list;
135 funcname_pattern_list = pp;
136 }
137 if (pp->pattern)
138 free(pp->pattern);
139 pp->pattern = xstrdup(value);
140 return 0;
141}
142
83ad63cf
JH
143/*
144 * These are to give UI layer defaults.
145 * The core-level commands such as git-diff-files should
146 * never be affected by the setting of diff.renames
147 * the user happens to have in the configuration file.
148 */
149int git_diff_ui_config(const char *var, const char *value)
801235c5
JH
150{
151 if (!strcmp(var, "diff.renamelimit")) {
152 diff_rename_limit_default = git_config_int(var, value);
153 return 0;
154 }
a159ca0c 155 if (!strcmp(var, "diff.color") || !strcmp(var, "color.diff")) {
7c92fe0e 156 diff_use_color_default = git_config_colorbool(var, value);
801235c5
JH
157 return 0;
158 }
b68ea12e
EW
159 if (!strcmp(var, "diff.renames")) {
160 if (!value)
161 diff_detect_rename_default = DIFF_DETECT_RENAME;
162 else if (!strcasecmp(value, "copies") ||
163 !strcasecmp(value, "copy"))
164 diff_detect_rename_default = DIFF_DETECT_COPY;
165 else if (git_config_bool(var,value))
166 diff_detect_rename_default = DIFF_DETECT_RENAME;
167 return 0;
168 }
f1af60bd
JH
169 if (!prefixcmp(var, "diff.")) {
170 const char *ep = strrchr(var, '.');
171
e0e324a4
JH
172 if (ep != var + 4) {
173 if (!strcmp(ep, ".command"))
174 return parse_lldiff_command(var, ep, value);
175 if (!strcmp(ep, ".funcname"))
176 return parse_funcname_pattern(var, ep, value);
177 }
f1af60bd 178 }
1968d77d 179 if (!prefixcmp(var, "diff.color.") || !prefixcmp(var, "color.diff.")) {
801235c5 180 int slot = parse_diff_color_slot(var, 11);
7c92fe0e 181 color_parse(value, var, diff_colors[slot]);
801235c5
JH
182 return 0;
183 }
f1af60bd 184
801235c5
JH
185 return git_default_config(var, value);
186}
187
6973dcae
JH
188static char *quote_one(const char *str)
189{
190 int needlen;
191 char *xp;
192
193 if (!str)
194 return NULL;
195 needlen = quote_c_style(str, NULL, NULL, 0);
196 if (!needlen)
9befac47 197 return xstrdup(str);
6973dcae
JH
198 xp = xmalloc(needlen + 1);
199 quote_c_style(str, xp, NULL, 0);
200 return xp;
201}
202
203static char *quote_two(const char *one, const char *two)
204{
205 int need_one = quote_c_style(one, NULL, NULL, 1);
206 int need_two = quote_c_style(two, NULL, NULL, 1);
207 char *xp;
208
209 if (need_one + need_two) {
210 if (!need_one) need_one = strlen(one);
211 if (!need_two) need_one = strlen(two);
212
213 xp = xmalloc(need_one + need_two + 3);
214 xp[0] = '"';
215 quote_c_style(one, xp + 1, NULL, 1);
216 quote_c_style(two, xp + need_one + 1, NULL, 1);
217 strcpy(xp + need_one + need_two + 1, "\"");
218 return xp;
219 }
220 need_one = strlen(one);
221 need_two = strlen(two);
222 xp = xmalloc(need_one + need_two + 1);
223 strcpy(xp, one);
224 strcpy(xp + need_one, two);
225 return xp;
226}
227
228static const char *external_diff(void)
229{
230 static const char *external_diff_cmd = NULL;
231 static int done_preparing = 0;
232
233 if (done_preparing)
234 return external_diff_cmd;
235 external_diff_cmd = getenv("GIT_EXTERNAL_DIFF");
236 done_preparing = 1;
237 return external_diff_cmd;
238}
239
6973dcae
JH
240static struct diff_tempfile {
241 const char *name; /* filename external diff should read from */
242 char hex[41];
243 char mode[10];
1472966c 244 char tmp_path[PATH_MAX];
6973dcae
JH
245} diff_temp[2];
246
247static int count_lines(const char *data, int size)
248{
249 int count, ch, completely_empty = 1, nl_just_seen = 0;
250 count = 0;
251 while (0 < size--) {
252 ch = *data++;
253 if (ch == '\n') {
254 count++;
255 nl_just_seen = 1;
256 completely_empty = 0;
257 }
258 else {
259 nl_just_seen = 0;
260 completely_empty = 0;
261 }
262 }
263 if (completely_empty)
264 return 0;
265 if (!nl_just_seen)
266 count++; /* no trailing newline */
267 return count;
268}
269
270static void print_line_count(int count)
271{
272 switch (count) {
273 case 0:
274 printf("0,0");
275 break;
276 case 1:
277 printf("1");
278 break;
279 default:
280 printf("1,%d", count);
281 break;
282 }
283}
284
13e36ec5
JS
285static void copy_file(int prefix, const char *data, int size,
286 const char *set, const char *reset)
6973dcae
JH
287{
288 int ch, nl_just_seen = 1;
289 while (0 < size--) {
290 ch = *data++;
13e36ec5
JS
291 if (nl_just_seen) {
292 fputs(set, stdout);
6973dcae 293 putchar(prefix);
13e36ec5
JS
294 }
295 if (ch == '\n') {
6973dcae 296 nl_just_seen = 1;
13e36ec5
JS
297 fputs(reset, stdout);
298 } else
6973dcae 299 nl_just_seen = 0;
13e36ec5 300 putchar(ch);
6973dcae
JH
301 }
302 if (!nl_just_seen)
13e36ec5 303 printf("%s\n\\ No newline at end of file\n", reset);
6973dcae
JH
304}
305
306static void emit_rewrite_diff(const char *name_a,
307 const char *name_b,
308 struct diff_filespec *one,
13e36ec5
JS
309 struct diff_filespec *two,
310 int color_diff)
6973dcae
JH
311{
312 int lc_a, lc_b;
1a9eb3b9 313 const char *name_a_tab, *name_b_tab;
13e36ec5
JS
314 const char *metainfo = diff_get_color(color_diff, DIFF_METAINFO);
315 const char *fraginfo = diff_get_color(color_diff, DIFF_FRAGINFO);
316 const char *old = diff_get_color(color_diff, DIFF_FILE_OLD);
317 const char *new = diff_get_color(color_diff, DIFF_FILE_NEW);
318 const char *reset = diff_get_color(color_diff, DIFF_RESET);
1a9eb3b9 319
8a13becc
JH
320 name_a += (*name_a == '/');
321 name_b += (*name_b == '/');
1a9eb3b9
JH
322 name_a_tab = strchr(name_a, ' ') ? "\t" : "";
323 name_b_tab = strchr(name_b, ' ') ? "\t" : "";
324
6973dcae
JH
325 diff_populate_filespec(one, 0);
326 diff_populate_filespec(two, 0);
327 lc_a = count_lines(one->data, one->size);
328 lc_b = count_lines(two->data, two->size);
13e36ec5
JS
329 printf("%s--- a/%s%s%s\n%s+++ b/%s%s%s\n%s@@ -",
330 metainfo, name_a, name_a_tab, reset,
331 metainfo, name_b, name_b_tab, reset, fraginfo);
6973dcae
JH
332 print_line_count(lc_a);
333 printf(" +");
334 print_line_count(lc_b);
13e36ec5 335 printf(" @@%s\n", reset);
6973dcae 336 if (lc_a)
13e36ec5 337 copy_file('-', one->data, one->size, old, reset);
6973dcae 338 if (lc_b)
13e36ec5 339 copy_file('+', two->data, two->size, new, reset);
6973dcae
JH
340}
341
342static int fill_mmfile(mmfile_t *mf, struct diff_filespec *one)
343{
344 if (!DIFF_FILE_VALID(one)) {
d2543b8e 345 mf->ptr = (char *)""; /* does not matter */
6973dcae
JH
346 mf->size = 0;
347 return 0;
348 }
349 else if (diff_populate_filespec(one, 0))
350 return -1;
351 mf->ptr = one->data;
352 mf->size = one->size;
353 return 0;
354}
355
f59a59e2
JS
356struct diff_words_buffer {
357 mmfile_t text;
358 long alloc;
359 long current; /* output pointer */
360 int suppressed_newline;
361};
362
363static void diff_words_append(char *line, unsigned long len,
364 struct diff_words_buffer *buffer)
365{
366 if (buffer->text.size + len > buffer->alloc) {
367 buffer->alloc = (buffer->text.size + len) * 3 / 2;
368 buffer->text.ptr = xrealloc(buffer->text.ptr, buffer->alloc);
369 }
370 line++;
371 len--;
372 memcpy(buffer->text.ptr + buffer->text.size, line, len);
373 buffer->text.size += len;
374}
375
376struct diff_words_data {
377 struct xdiff_emit_state xm;
378 struct diff_words_buffer minus, plus;
379};
380
381static void print_word(struct diff_words_buffer *buffer, int len, int color,
382 int suppress_newline)
383{
384 const char *ptr;
385 int eol = 0;
386
387 if (len == 0)
388 return;
389
390 ptr = buffer->text.ptr + buffer->current;
391 buffer->current += len;
392
393 if (ptr[len - 1] == '\n') {
394 eol = 1;
395 len--;
396 }
397
398 fputs(diff_get_color(1, color), stdout);
399 fwrite(ptr, len, 1, stdout);
400 fputs(diff_get_color(1, DIFF_RESET), stdout);
401
402 if (eol) {
403 if (suppress_newline)
404 buffer->suppressed_newline = 1;
405 else
406 putchar('\n');
407 }
408}
409
410static void fn_out_diff_words_aux(void *priv, char *line, unsigned long len)
411{
412 struct diff_words_data *diff_words = priv;
413
414 if (diff_words->minus.suppressed_newline) {
415 if (line[0] != '+')
416 putchar('\n');
417 diff_words->minus.suppressed_newline = 0;
418 }
419
420 len--;
421 switch (line[0]) {
422 case '-':
423 print_word(&diff_words->minus, len, DIFF_FILE_OLD, 1);
424 break;
425 case '+':
426 print_word(&diff_words->plus, len, DIFF_FILE_NEW, 0);
427 break;
428 case ' ':
429 print_word(&diff_words->plus, len, DIFF_PLAIN, 0);
430 diff_words->minus.current += len;
431 break;
432 }
433}
434
435/* this executes the word diff on the accumulated buffers */
436static void diff_words_show(struct diff_words_data *diff_words)
437{
438 xpparam_t xpp;
439 xdemitconf_t xecfg;
440 xdemitcb_t ecb;
441 mmfile_t minus, plus;
442 int i;
443
30b25010 444 memset(&xecfg, 0, sizeof(xecfg));
f59a59e2
JS
445 minus.size = diff_words->minus.text.size;
446 minus.ptr = xmalloc(minus.size);
447 memcpy(minus.ptr, diff_words->minus.text.ptr, minus.size);
448 for (i = 0; i < minus.size; i++)
449 if (isspace(minus.ptr[i]))
450 minus.ptr[i] = '\n';
451 diff_words->minus.current = 0;
452
453 plus.size = diff_words->plus.text.size;
454 plus.ptr = xmalloc(plus.size);
455 memcpy(plus.ptr, diff_words->plus.text.ptr, plus.size);
456 for (i = 0; i < plus.size; i++)
457 if (isspace(plus.ptr[i]))
458 plus.ptr[i] = '\n';
459 diff_words->plus.current = 0;
460
461 xpp.flags = XDF_NEED_MINIMAL;
462 xecfg.ctxlen = diff_words->minus.alloc + diff_words->plus.alloc;
f59a59e2
JS
463 ecb.outf = xdiff_outf;
464 ecb.priv = diff_words;
465 diff_words->xm.consume = fn_out_diff_words_aux;
466 xdl_diff(&minus, &plus, &xpp, &xecfg, &ecb);
467
468 free(minus.ptr);
469 free(plus.ptr);
470 diff_words->minus.text.size = diff_words->plus.text.size = 0;
471
472 if (diff_words->minus.suppressed_newline) {
473 putchar('\n');
474 diff_words->minus.suppressed_newline = 0;
475 }
476}
477
6973dcae 478struct emit_callback {
cd112cef
JS
479 struct xdiff_emit_state xm;
480 int nparents, color_diff;
6973dcae 481 const char **label_path;
f59a59e2 482 struct diff_words_data *diff_words;
34a5e1a2 483 int *found_changesp;
6973dcae
JH
484};
485
f59a59e2
JS
486static void free_diff_words_data(struct emit_callback *ecbdata)
487{
488 if (ecbdata->diff_words) {
489 /* flush buffers */
490 if (ecbdata->diff_words->minus.text.size ||
491 ecbdata->diff_words->plus.text.size)
492 diff_words_show(ecbdata->diff_words);
493
494 if (ecbdata->diff_words->minus.text.ptr)
495 free (ecbdata->diff_words->minus.text.ptr);
496 if (ecbdata->diff_words->plus.text.ptr)
497 free (ecbdata->diff_words->plus.text.ptr);
498 free(ecbdata->diff_words);
499 ecbdata->diff_words = NULL;
500 }
501}
502
ce436973 503const char *diff_get_color(int diff_use_color, enum color_diff ix)
cd112cef
JS
504{
505 if (diff_use_color)
50f575fc
LT
506 return diff_colors[ix];
507 return "";
cd112cef
JS
508}
509
448c3ef1
JH
510static void emit_line(const char *set, const char *reset, const char *line, int len)
511{
512 if (len > 0 && line[len-1] == '\n')
513 len--;
514 fputs(set, stdout);
515 fwrite(line, len, 1, stdout);
516 puts(reset);
517}
518
c5a8c3ec
JS
519static void emit_line_with_ws(int nparents,
520 const char *set, const char *reset, const char *ws,
521 const char *line, int len)
448c3ef1 522{
c5a8c3ec 523 int col0 = nparents;
448c3ef1
JH
524 int last_tab_in_indent = -1;
525 int last_space_in_indent = -1;
526 int i;
527 int tail = len;
528 int need_highlight_leading_space = 0;
448c3ef1
JH
529 /* The line is a newly added line. Does it have funny leading
530 * whitespaces? In indent, SP should never precede a TAB.
531 */
532 for (i = col0; i < len; i++) {
533 if (line[i] == '\t') {
534 last_tab_in_indent = i;
535 if (0 <= last_space_in_indent)
536 need_highlight_leading_space = 1;
537 }
538 else if (line[i] == ' ')
539 last_space_in_indent = i;
540 else
541 break;
542 }
543 fputs(set, stdout);
544 fwrite(line, col0, 1, stdout);
545 fputs(reset, stdout);
546 if (((i == len) || line[i] == '\n') && i != col0) {
547 /* The whole line was indent */
548 emit_line(ws, reset, line + col0, len - col0);
549 return;
550 }
551 i = col0;
552 if (need_highlight_leading_space) {
553 while (i < last_tab_in_indent) {
554 if (line[i] == ' ') {
555 fputs(ws, stdout);
556 putchar(' ');
557 fputs(reset, stdout);
558 }
559 else
560 putchar(line[i]);
561 i++;
562 }
563 }
564 tail = len - 1;
565 if (line[tail] == '\n' && i < tail)
566 tail--;
567 while (i < tail) {
568 if (!isspace(line[tail]))
569 break;
570 tail--;
571 }
572 if ((i < tail && line[tail + 1] != '\n')) {
573 /* This has whitespace between tail+1..len */
574 fputs(set, stdout);
575 fwrite(line + i, tail - i + 1, 1, stdout);
576 fputs(reset, stdout);
577 emit_line(ws, reset, line + tail + 1, len - tail - 1);
578 }
579 else
580 emit_line(set, reset, line + i, len - i);
581}
582
c5a8c3ec
JS
583static void emit_add_line(const char *reset, struct emit_callback *ecbdata, const char *line, int len)
584{
585 const char *ws = diff_get_color(ecbdata->color_diff, DIFF_WHITESPACE);
586 const char *set = diff_get_color(ecbdata->color_diff, DIFF_FILE_NEW);
587
588 if (!*ws)
589 emit_line(set, reset, line, len);
590 else
591 emit_line_with_ws(ecbdata->nparents, set, reset, ws,
592 line, len);
593}
594
cd112cef 595static void fn_out_consume(void *priv, char *line, unsigned long len)
6973dcae
JH
596{
597 int i;
448c3ef1 598 int color;
6973dcae 599 struct emit_callback *ecbdata = priv;
ce436973
JK
600 const char *set = diff_get_color(ecbdata->color_diff, DIFF_METAINFO);
601 const char *reset = diff_get_color(ecbdata->color_diff, DIFF_RESET);
6973dcae 602
34a5e1a2
JS
603 *(ecbdata->found_changesp) = 1;
604
6973dcae 605 if (ecbdata->label_path[0]) {
1a9eb3b9
JH
606 const char *name_a_tab, *name_b_tab;
607
608 name_a_tab = strchr(ecbdata->label_path[0], ' ') ? "\t" : "";
609 name_b_tab = strchr(ecbdata->label_path[1], ' ') ? "\t" : "";
610
611 printf("%s--- %s%s%s\n",
612 set, ecbdata->label_path[0], reset, name_a_tab);
613 printf("%s+++ %s%s%s\n",
614 set, ecbdata->label_path[1], reset, name_b_tab);
6973dcae
JH
615 ecbdata->label_path[0] = ecbdata->label_path[1] = NULL;
616 }
cd112cef
JS
617
618 /* This is not really necessary for now because
619 * this codepath only deals with two-way diffs.
620 */
621 for (i = 0; i < len && line[i] == '@'; i++)
622 ;
623 if (2 <= i && i < len && line[i] == ' ') {
624 ecbdata->nparents = i - 1;
448c3ef1
JH
625 emit_line(diff_get_color(ecbdata->color_diff, DIFF_FRAGINFO),
626 reset, line, len);
627 return;
cd112cef 628 }
448c3ef1
JH
629
630 if (len < ecbdata->nparents) {
50f575fc 631 set = reset;
448c3ef1
JH
632 emit_line(reset, reset, line, len);
633 return;
cd112cef 634 }
448c3ef1
JH
635
636 color = DIFF_PLAIN;
637 if (ecbdata->diff_words && ecbdata->nparents != 1)
638 /* fall back to normal diff */
639 free_diff_words_data(ecbdata);
640 if (ecbdata->diff_words) {
641 if (line[0] == '-') {
642 diff_words_append(line, len,
643 &ecbdata->diff_words->minus);
644 return;
645 } else if (line[0] == '+') {
646 diff_words_append(line, len,
647 &ecbdata->diff_words->plus);
648 return;
649 }
650 if (ecbdata->diff_words->minus.text.size ||
651 ecbdata->diff_words->plus.text.size)
652 diff_words_show(ecbdata->diff_words);
653 line++;
50f575fc 654 len--;
448c3ef1
JH
655 emit_line(set, reset, line, len);
656 return;
657 }
658 for (i = 0; i < ecbdata->nparents && len; i++) {
659 if (line[i] == '-')
660 color = DIFF_FILE_OLD;
661 else if (line[i] == '+')
662 color = DIFF_FILE_NEW;
663 }
664
665 if (color != DIFF_FILE_NEW) {
666 emit_line(diff_get_color(ecbdata->color_diff, color),
667 reset, line, len);
668 return;
669 }
670 emit_add_line(reset, ecbdata, line, len);
6973dcae
JH
671}
672
673static char *pprint_rename(const char *a, const char *b)
674{
675 const char *old = a;
676 const char *new = b;
677 char *name = NULL;
678 int pfx_length, sfx_length;
679 int len_a = strlen(a);
680 int len_b = strlen(b);
e5bfbf9b
AJ
681 int qlen_a = quote_c_style(a, NULL, NULL, 0);
682 int qlen_b = quote_c_style(b, NULL, NULL, 0);
683
684 if (qlen_a || qlen_b) {
685 if (qlen_a) len_a = qlen_a;
686 if (qlen_b) len_b = qlen_b;
687 name = xmalloc( len_a + len_b + 5 );
688 if (qlen_a)
689 quote_c_style(a, name, NULL, 0);
690 else
691 memcpy(name, a, len_a);
692 memcpy(name + len_a, " => ", 4);
693 if (qlen_b)
694 quote_c_style(b, name + len_a + 4, NULL, 0);
695 else
696 memcpy(name + len_a + 4, b, len_b + 1);
697 return name;
698 }
6973dcae
JH
699
700 /* Find common prefix */
701 pfx_length = 0;
702 while (*old && *new && *old == *new) {
703 if (*old == '/')
704 pfx_length = old - a + 1;
705 old++;
706 new++;
707 }
708
709 /* Find common suffix */
710 old = a + len_a;
711 new = b + len_b;
712 sfx_length = 0;
713 while (a <= old && b <= new && *old == *new) {
714 if (*old == '/')
715 sfx_length = len_a - (old - a);
716 old--;
717 new--;
718 }
719
720 /*
721 * pfx{mid-a => mid-b}sfx
722 * {pfx-a => pfx-b}sfx
723 * pfx{sfx-a => sfx-b}
724 * name-a => name-b
725 */
726 if (pfx_length + sfx_length) {
cc908b82
JH
727 int a_midlen = len_a - pfx_length - sfx_length;
728 int b_midlen = len_b - pfx_length - sfx_length;
729 if (a_midlen < 0) a_midlen = 0;
730 if (b_midlen < 0) b_midlen = 0;
731
e3008464 732 name = xmalloc(pfx_length + a_midlen + b_midlen + sfx_length + 7);
6973dcae
JH
733 sprintf(name, "%.*s{%.*s => %.*s}%s",
734 pfx_length, a,
cc908b82
JH
735 a_midlen, a + pfx_length,
736 b_midlen, b + pfx_length,
6973dcae
JH
737 a + len_a - sfx_length);
738 }
739 else {
740 name = xmalloc(len_a + len_b + 5);
741 sprintf(name, "%s => %s", a, b);
742 }
743 return name;
744}
745
746struct diffstat_t {
747 struct xdiff_emit_state xm;
748
749 int nr;
750 int alloc;
751 struct diffstat_file {
752 char *name;
753 unsigned is_unmerged:1;
754 unsigned is_binary:1;
755 unsigned is_renamed:1;
756 unsigned int added, deleted;
757 } **files;
758};
759
760static struct diffstat_file *diffstat_add(struct diffstat_t *diffstat,
761 const char *name_a,
762 const char *name_b)
763{
764 struct diffstat_file *x;
765 x = xcalloc(sizeof (*x), 1);
766 if (diffstat->nr == diffstat->alloc) {
767 diffstat->alloc = alloc_nr(diffstat->alloc);
768 diffstat->files = xrealloc(diffstat->files,
769 diffstat->alloc * sizeof(x));
770 }
771 diffstat->files[diffstat->nr++] = x;
772 if (name_b) {
773 x->name = pprint_rename(name_a, name_b);
774 x->is_renamed = 1;
775 }
776 else
9befac47 777 x->name = xstrdup(name_a);
6973dcae
JH
778 return x;
779}
780
781static void diffstat_consume(void *priv, char *line, unsigned long len)
782{
783 struct diffstat_t *diffstat = priv;
784 struct diffstat_file *x = diffstat->files[diffstat->nr - 1];
785
786 if (line[0] == '+')
787 x->added++;
788 else if (line[0] == '-')
789 x->deleted++;
790}
791
698ce6f8 792const char mime_boundary_leader[] = "------------";
6973dcae 793
a2540023
JH
794static int scale_linear(int it, int width, int max_change)
795{
796 /*
3ed74e60
JS
797 * make sure that at least one '-' is printed if there were deletions,
798 * and likewise for '+'.
a2540023 799 */
3ed74e60
JS
800 if (max_change < 2)
801 return it;
802 return ((it - 1) * (width - 1) + max_change - 1) / (max_change - 1);
a2540023
JH
803}
804
785f7432
JH
805static void show_name(const char *prefix, const char *name, int len,
806 const char *reset, const char *set)
a2540023 807{
785f7432 808 printf(" %s%s%-*s%s |", set, prefix, len, name, reset);
a2540023
JH
809}
810
785f7432 811static void show_graph(char ch, int cnt, const char *set, const char *reset)
a2540023
JH
812{
813 if (cnt <= 0)
814 return;
785f7432 815 printf("%s", set);
a2540023
JH
816 while (cnt--)
817 putchar(ch);
785f7432 818 printf("%s", reset);
a2540023
JH
819}
820
821static void show_stats(struct diffstat_t* data, struct diff_options *options)
6973dcae 822{
6973dcae 823 int i, len, add, del, total, adds = 0, dels = 0;
a2540023 824 int max_change = 0, max_len = 0;
6973dcae 825 int total_files = data->nr;
a2540023 826 int width, name_width;
785f7432 827 const char *reset, *set, *add_c, *del_c;
6973dcae
JH
828
829 if (data->nr == 0)
830 return;
831
a2540023
JH
832 width = options->stat_width ? options->stat_width : 80;
833 name_width = options->stat_name_width ? options->stat_name_width : 50;
834
835 /* Sanity: give at least 5 columns to the graph,
836 * but leave at least 10 columns for the name.
837 */
838 if (width < name_width + 15) {
839 if (name_width <= 25)
840 width = name_width + 15;
841 else
842 name_width = width - 15;
843 }
844
845 /* Find the longest filename and max number of changes */
785f7432
JH
846 reset = diff_get_color(options->color_diff, DIFF_RESET);
847 set = diff_get_color(options->color_diff, DIFF_PLAIN);
848 add_c = diff_get_color(options->color_diff, DIFF_FILE_NEW);
849 del_c = diff_get_color(options->color_diff, DIFF_FILE_OLD);
850
6973dcae
JH
851 for (i = 0; i < data->nr; i++) {
852 struct diffstat_file *file = data->files[i];
a2540023
JH
853 int change = file->added + file->deleted;
854
e5bfbf9b
AJ
855 if (!file->is_renamed) { /* renames are already quoted by pprint_rename */
856 len = quote_c_style(file->name, NULL, NULL, 0);
857 if (len) {
858 char *qname = xmalloc(len + 1);
859 quote_c_style(file->name, qname, NULL, 0);
860 free(file->name);
861 file->name = qname;
862 }
a2540023 863 }
6973dcae
JH
864
865 len = strlen(file->name);
866 if (max_len < len)
867 max_len = len;
868
869 if (file->is_binary || file->is_unmerged)
870 continue;
a2540023
JH
871 if (max_change < change)
872 max_change = change;
6973dcae
JH
873 }
874
a2540023
JH
875 /* Compute the width of the graph part;
876 * 10 is for one blank at the beginning of the line plus
877 * " | count " between the name and the graph.
878 *
879 * From here on, name_width is the width of the name area,
880 * and width is the width of the graph area.
881 */
882 name_width = (name_width < max_len) ? name_width : max_len;
883 if (width < (name_width + 10) + max_change)
884 width = width - (name_width + 10);
885 else
886 width = max_change;
887
6973dcae 888 for (i = 0; i < data->nr; i++) {
d2543b8e 889 const char *prefix = "";
6973dcae
JH
890 char *name = data->files[i]->name;
891 int added = data->files[i]->added;
892 int deleted = data->files[i]->deleted;
a2540023 893 int name_len;
6973dcae
JH
894
895 /*
896 * "scale" the filename
897 */
a2540023
JH
898 len = name_width;
899 name_len = strlen(name);
900 if (name_width < name_len) {
6973dcae
JH
901 char *slash;
902 prefix = "...";
a2540023
JH
903 len -= 3;
904 name += name_len - len;
6973dcae
JH
905 slash = strchr(name, '/');
906 if (slash)
907 name = slash;
908 }
6973dcae
JH
909
910 if (data->files[i]->is_binary) {
785f7432 911 show_name(prefix, name, len, reset, set);
b1882587
AP
912 printf(" Bin ");
913 printf("%s%d%s", del_c, deleted, reset);
914 printf(" -> ");
915 printf("%s%d%s", add_c, added, reset);
916 printf(" bytes");
917 printf("\n");
6973dcae
JH
918 goto free_diffstat_file;
919 }
920 else if (data->files[i]->is_unmerged) {
785f7432 921 show_name(prefix, name, len, reset, set);
a2540023 922 printf(" Unmerged\n");
6973dcae
JH
923 goto free_diffstat_file;
924 }
925 else if (!data->files[i]->is_renamed &&
926 (added + deleted == 0)) {
927 total_files--;
928 goto free_diffstat_file;
929 }
930
a2540023
JH
931 /*
932 * scale the add/delete
933 */
6973dcae
JH
934 add = added;
935 del = deleted;
936 total = add + del;
937 adds += add;
938 dels += del;
939
a2540023 940 if (width <= max_change) {
a2540023 941 add = scale_linear(add, width, max_change);
3ed74e60
JS
942 del = scale_linear(del, width, max_change);
943 total = add + del;
6973dcae 944 }
785f7432 945 show_name(prefix, name, len, reset, set);
a2540023 946 printf("%5d ", added + deleted);
785f7432
JH
947 show_graph('+', add, add_c, reset);
948 show_graph('-', del, del_c, reset);
a2540023 949 putchar('\n');
6973dcae
JH
950 free_diffstat_file:
951 free(data->files[i]->name);
952 free(data->files[i]);
953 }
954 free(data->files);
785f7432
JH
955 printf("%s %d files changed, %d insertions(+), %d deletions(-)%s\n",
956 set, total_files, adds, dels, reset);
6973dcae
JH
957}
958
ebd124c6
NP
959static void show_shortstats(struct diffstat_t* data)
960{
961 int i, adds = 0, dels = 0, total_files = data->nr;
962
963 if (data->nr == 0)
964 return;
965
966 for (i = 0; i < data->nr; i++) {
967 if (!data->files[i]->is_binary &&
968 !data->files[i]->is_unmerged) {
969 int added = data->files[i]->added;
970 int deleted= data->files[i]->deleted;
971 if (!data->files[i]->is_renamed &&
972 (added + deleted == 0)) {
973 total_files--;
974 } else {
975 adds += added;
976 dels += deleted;
977 }
978 }
979 free(data->files[i]->name);
980 free(data->files[i]);
981 }
982 free(data->files);
983
984 printf(" %d files changed, %d insertions(+), %d deletions(-)\n",
985 total_files, adds, dels);
986}
987
74e2abe5
JH
988static void show_numstat(struct diffstat_t* data, struct diff_options *options)
989{
990 int i;
991
992 for (i = 0; i < data->nr; i++) {
993 struct diffstat_file *file = data->files[i];
994
bfddbc5e
JH
995 if (file->is_binary)
996 printf("-\t-\t");
997 else
998 printf("%d\t%d\t", file->added, file->deleted);
e5bfbf9b 999 if (options->line_termination && !file->is_renamed &&
74e2abe5
JH
1000 quote_c_style(file->name, NULL, NULL, 0))
1001 quote_c_style(file->name, NULL, stdout, 0);
1002 else
1003 fputs(file->name, stdout);
1004 putchar(options->line_termination);
1005 }
1006}
1007
88246898
JS
1008struct checkdiff_t {
1009 struct xdiff_emit_state xm;
1010 const char *filename;
c5a8c3ec 1011 int lineno, color_diff;
88246898
JS
1012};
1013
1014static void checkdiff_consume(void *priv, char *line, unsigned long len)
1015{
1016 struct checkdiff_t *data = priv;
c5a8c3ec
JS
1017 const char *ws = diff_get_color(data->color_diff, DIFF_WHITESPACE);
1018 const char *reset = diff_get_color(data->color_diff, DIFF_RESET);
1019 const char *set = diff_get_color(data->color_diff, DIFF_FILE_NEW);
88246898
JS
1020
1021 if (line[0] == '+') {
c5a8c3ec 1022 int i, spaces = 0, space_before_tab = 0, white_space_at_end = 0;
88246898 1023
88246898
JS
1024 /* check space before tab */
1025 for (i = 1; i < len && (line[i] == ' ' || line[i] == '\t'); i++)
1026 if (line[i] == ' ')
1027 spaces++;
1028 if (line[i - 1] == '\t' && spaces)
c5a8c3ec 1029 space_before_tab = 1;
88246898
JS
1030
1031 /* check white space at line end */
1032 if (line[len - 1] == '\n')
1033 len--;
1034 if (isspace(line[len - 1]))
c5a8c3ec
JS
1035 white_space_at_end = 1;
1036
1037 if (space_before_tab || white_space_at_end) {
1038 printf("%s:%d: %s", data->filename, data->lineno, ws);
1039 if (space_before_tab) {
1040 printf("space before tab");
1041 if (white_space_at_end)
1042 putchar(',');
1043 }
1044 if (white_space_at_end)
1045 printf("white space at end");
1046 printf(":%s ", reset);
1047 emit_line_with_ws(1, set, reset, ws, line, len);
1048 }
e6d40d65
JS
1049
1050 data->lineno++;
88246898
JS
1051 } else if (line[0] == ' ')
1052 data->lineno++;
1053 else if (line[0] == '@') {
1054 char *plus = strchr(line, '+');
1055 if (plus)
9e848163 1056 data->lineno = strtol(plus, NULL, 10);
88246898
JS
1057 else
1058 die("invalid diff");
1059 }
1060}
1061
0660626c
JH
1062static unsigned char *deflate_it(char *data,
1063 unsigned long size,
1064 unsigned long *result_size)
051308f6 1065{
0660626c
JH
1066 int bound;
1067 unsigned char *deflated;
1068 z_stream stream;
1069
1070 memset(&stream, 0, sizeof(stream));
12f6c308 1071 deflateInit(&stream, zlib_compression_level);
0660626c
JH
1072 bound = deflateBound(&stream, size);
1073 deflated = xmalloc(bound);
1074 stream.next_out = deflated;
1075 stream.avail_out = bound;
1076
1077 stream.next_in = (unsigned char *)data;
1078 stream.avail_in = size;
1079 while (deflate(&stream, Z_FINISH) == Z_OK)
1080 ; /* nothing */
1081 deflateEnd(&stream);
1082 *result_size = stream.total_out;
1083 return deflated;
051308f6
JH
1084}
1085
d4c452f0 1086static void emit_binary_diff_body(mmfile_t *one, mmfile_t *two)
051308f6 1087{
0660626c
JH
1088 void *cp;
1089 void *delta;
1090 void *deflated;
1091 void *data;
1092 unsigned long orig_size;
1093 unsigned long delta_size;
1094 unsigned long deflate_size;
1095 unsigned long data_size;
051308f6 1096
0660626c
JH
1097 /* We could do deflated delta, or we could do just deflated two,
1098 * whichever is smaller.
051308f6 1099 */
0660626c
JH
1100 delta = NULL;
1101 deflated = deflate_it(two->ptr, two->size, &deflate_size);
1102 if (one->size && two->size) {
1103 delta = diff_delta(one->ptr, one->size,
1104 two->ptr, two->size,
1105 &delta_size, deflate_size);
1106 if (delta) {
1107 void *to_free = delta;
1108 orig_size = delta_size;
1109 delta = deflate_it(delta, delta_size, &delta_size);
1110 free(to_free);
051308f6
JH
1111 }
1112 }
051308f6 1113
0660626c
JH
1114 if (delta && delta_size < deflate_size) {
1115 printf("delta %lu\n", orig_size);
1116 free(deflated);
1117 data = delta;
1118 data_size = delta_size;
1119 }
1120 else {
1121 printf("literal %lu\n", two->size);
1122 free(delta);
1123 data = deflated;
1124 data_size = deflate_size;
1125 }
051308f6 1126
0660626c
JH
1127 /* emit data encoded in base85 */
1128 cp = data;
1129 while (data_size) {
1130 int bytes = (52 < data_size) ? 52 : data_size;
051308f6 1131 char line[70];
0660626c 1132 data_size -= bytes;
051308f6
JH
1133 if (bytes <= 26)
1134 line[0] = bytes + 'A' - 1;
1135 else
1136 line[0] = bytes - 26 + 'a' - 1;
1137 encode_85(line + 1, cp, bytes);
1d7f171c 1138 cp = (char *) cp + bytes;
051308f6
JH
1139 puts(line);
1140 }
1141 printf("\n");
0660626c 1142 free(data);
051308f6
JH
1143}
1144
d4c452f0
JH
1145static void emit_binary_diff(mmfile_t *one, mmfile_t *two)
1146{
1147 printf("GIT binary patch\n");
1148 emit_binary_diff_body(one, two);
1149 emit_binary_diff_body(two, one);
1150}
1151
8c701249
JH
1152static void setup_diff_attr_check(struct git_attr_check *check)
1153{
1154 static struct git_attr *attr_diff;
1155
29a3eefd 1156 if (!attr_diff) {
8c701249 1157 attr_diff = git_attr("diff", 4);
29a3eefd
JH
1158 }
1159 check[0].attr = attr_diff;
8c701249
JH
1160}
1161
29a3eefd 1162static void diff_filespec_check_attr(struct diff_filespec *one)
6973dcae 1163{
e0e324a4 1164 struct git_attr_check attr_diff_check;
2c3fa66f 1165 int check_from_data = 0;
8c701249 1166
29a3eefd
JH
1167 if (one->checked_attr)
1168 return;
1169
e0e324a4 1170 setup_diff_attr_check(&attr_diff_check);
29a3eefd 1171 one->is_binary = 0;
e0e324a4 1172 one->funcname_pattern_ident = NULL;
29a3eefd 1173
e0e324a4 1174 if (!git_checkattr(one->path, 1, &attr_diff_check)) {
29a3eefd
JH
1175 const char *value;
1176
1177 /* binaryness */
e0e324a4 1178 value = attr_diff_check.value;
515106fa 1179 if (ATTR_TRUE(value))
29a3eefd 1180 ;
515106fa 1181 else if (ATTR_FALSE(value))
29a3eefd 1182 one->is_binary = 1;
2c3fa66f
JH
1183 else
1184 check_from_data = 1;
f258475a 1185
e0e324a4 1186 /* funcname pattern ident */
f258475a
JH
1187 if (ATTR_TRUE(value) || ATTR_FALSE(value) || ATTR_UNSET(value))
1188 ;
1189 else
e0e324a4 1190 one->funcname_pattern_ident = value;
515106fa 1191 }
40250af4 1192
2c3fa66f
JH
1193 if (check_from_data) {
1194 if (!one->data && DIFF_FILE_VALID(one))
1195 diff_populate_filespec(one, 0);
29a3eefd 1196
2c3fa66f
JH
1197 if (one->data)
1198 one->is_binary = buffer_is_binary(one->data, one->size);
1199 }
29a3eefd
JH
1200}
1201
1202int diff_filespec_is_binary(struct diff_filespec *one)
1203{
1204 diff_filespec_check_attr(one);
1205 return one->is_binary;
6973dcae
JH
1206}
1207
e0e324a4 1208static const char *funcname_pattern(const char *ident)
f258475a 1209{
e0e324a4 1210 struct funcname_pattern *pp;
f258475a 1211
e0e324a4
JH
1212 read_config_if_needed();
1213 for (pp = funcname_pattern_list; pp; pp = pp->next)
1214 if (!strcmp(ident, pp->name))
1215 return pp->pattern;
f258475a
JH
1216 return NULL;
1217}
1218
d3a93dc9
JH
1219static struct builtin_funcname_pattern {
1220 const char *name;
1221 const char *pattern;
1222} builtin_funcname_pattern[] = {
1223 { "java", "!^[ ]*\\(catch\\|do\\|for\\|if\\|instanceof\\|"
1224 "new\\|return\\|switch\\|throw\\|while\\)\n"
1225 "^[ ]*\\(\\([ ]*"
1226 "[A-Za-z_][A-Za-z_0-9]*\\)\\{2,\\}"
1227 "[ ]*([^;]*$\\)" },
1228 { "tex", "^\\(\\\\\\(sub\\)*section{.*\\)$" },
1229};
1230
e0e324a4 1231static const char *diff_funcname_pattern(struct diff_filespec *one)
f258475a 1232{
e0e324a4 1233 const char *ident, *pattern;
d3a93dc9 1234 int i;
f258475a
JH
1235
1236 diff_filespec_check_attr(one);
e0e324a4 1237 ident = one->funcname_pattern_ident;
f258475a
JH
1238
1239 if (!ident)
1240 /*
1241 * If the config file has "funcname.default" defined, that
1242 * regexp is used; otherwise NULL is returned and xemit uses
1243 * the built-in default.
1244 */
e0e324a4 1245 return funcname_pattern("default");
f258475a
JH
1246
1247 /* Look up custom "funcname.$ident" regexp from config. */
e0e324a4
JH
1248 pattern = funcname_pattern(ident);
1249 if (pattern)
1250 return pattern;
f258475a
JH
1251
1252 /*
1253 * And define built-in fallback patterns here. Note that
1254 * these can be overriden by the user's config settings.
1255 */
d3a93dc9
JH
1256 for (i = 0; i < ARRAY_SIZE(builtin_funcname_pattern); i++)
1257 if (!strcmp(ident, builtin_funcname_pattern[i].name))
1258 return builtin_funcname_pattern[i].pattern;
f258475a
JH
1259
1260 return NULL;
1261}
1262
6973dcae
JH
1263static void builtin_diff(const char *name_a,
1264 const char *name_b,
1265 struct diff_filespec *one,
1266 struct diff_filespec *two,
1267 const char *xfrm_msg,
051308f6 1268 struct diff_options *o,
6973dcae
JH
1269 int complete_rewrite)
1270{
1271 mmfile_t mf1, mf2;
1272 const char *lbl[2];
1273 char *a_one, *b_two;
ce436973
JK
1274 const char *set = diff_get_color(o->color_diff, DIFF_METAINFO);
1275 const char *reset = diff_get_color(o->color_diff, DIFF_RESET);
6973dcae 1276
50892777
JH
1277 a_one = quote_two("a/", name_a + (*name_a == '/'));
1278 b_two = quote_two("b/", name_b + (*name_b == '/'));
6973dcae
JH
1279 lbl[0] = DIFF_FILE_VALID(one) ? a_one : "/dev/null";
1280 lbl[1] = DIFF_FILE_VALID(two) ? b_two : "/dev/null";
50f575fc 1281 printf("%sdiff --git %s %s%s\n", set, a_one, b_two, reset);
6973dcae
JH
1282 if (lbl[0][0] == '/') {
1283 /* /dev/null */
50f575fc
LT
1284 printf("%snew file mode %06o%s\n", set, two->mode, reset);
1285 if (xfrm_msg && xfrm_msg[0])
1286 printf("%s%s%s\n", set, xfrm_msg, reset);
6973dcae
JH
1287 }
1288 else if (lbl[1][0] == '/') {
50f575fc
LT
1289 printf("%sdeleted file mode %06o%s\n", set, one->mode, reset);
1290 if (xfrm_msg && xfrm_msg[0])
1291 printf("%s%s%s\n", set, xfrm_msg, reset);
6973dcae
JH
1292 }
1293 else {
1294 if (one->mode != two->mode) {
50f575fc
LT
1295 printf("%sold mode %06o%s\n", set, one->mode, reset);
1296 printf("%snew mode %06o%s\n", set, two->mode, reset);
cd112cef 1297 }
50f575fc
LT
1298 if (xfrm_msg && xfrm_msg[0])
1299 printf("%s%s%s\n", set, xfrm_msg, reset);
6973dcae
JH
1300 /*
1301 * we do not run diff between different kind
1302 * of objects.
1303 */
1304 if ((one->mode ^ two->mode) & S_IFMT)
1305 goto free_ab_and_return;
1306 if (complete_rewrite) {
13e36ec5
JS
1307 emit_rewrite_diff(name_a, name_b, one, two,
1308 o->color_diff);
34a5e1a2 1309 o->found_changes = 1;
6973dcae
JH
1310 goto free_ab_and_return;
1311 }
1312 }
1313
1314 if (fill_mmfile(&mf1, one) < 0 || fill_mmfile(&mf2, two) < 0)
1315 die("unable to read files to diff");
1316
29a3eefd
JH
1317 if (!o->text &&
1318 (diff_filespec_is_binary(one) || diff_filespec_is_binary(two))) {
0660626c
JH
1319 /* Quite common confusing case */
1320 if (mf1.size == mf2.size &&
1321 !memcmp(mf1.ptr, mf2.ptr, mf1.size))
1322 goto free_ab_and_return;
1323 if (o->binary)
051308f6
JH
1324 emit_binary_diff(&mf1, &mf2);
1325 else
1326 printf("Binary files %s and %s differ\n",
1327 lbl[0], lbl[1]);
34a5e1a2 1328 o->found_changes = 1;
051308f6 1329 }
6973dcae
JH
1330 else {
1331 /* Crazy xdl interfaces.. */
1332 const char *diffopts = getenv("GIT_DIFF_OPTS");
1333 xpparam_t xpp;
1334 xdemitconf_t xecfg;
1335 xdemitcb_t ecb;
1336 struct emit_callback ecbdata;
e0e324a4 1337 const char *funcname_pattern;
f258475a 1338
e0e324a4
JH
1339 funcname_pattern = diff_funcname_pattern(one);
1340 if (!funcname_pattern)
1341 funcname_pattern = diff_funcname_pattern(two);
6973dcae 1342
30b25010 1343 memset(&xecfg, 0, sizeof(xecfg));
cd112cef 1344 memset(&ecbdata, 0, sizeof(ecbdata));
6973dcae 1345 ecbdata.label_path = lbl;
cd112cef 1346 ecbdata.color_diff = o->color_diff;
34a5e1a2 1347 ecbdata.found_changesp = &o->found_changes;
0d21efa5 1348 xpp.flags = XDF_NEED_MINIMAL | o->xdl_opts;
ee1e5412 1349 xecfg.ctxlen = o->context;
6973dcae 1350 xecfg.flags = XDL_EMIT_FUNCNAMES;
e0e324a4
JH
1351 if (funcname_pattern)
1352 xdiff_set_find_func(&xecfg, funcname_pattern);
6973dcae
JH
1353 if (!diffopts)
1354 ;
cc44c765 1355 else if (!prefixcmp(diffopts, "--unified="))
6973dcae 1356 xecfg.ctxlen = strtoul(diffopts + 10, NULL, 10);
cc44c765 1357 else if (!prefixcmp(diffopts, "-u"))
6973dcae 1358 xecfg.ctxlen = strtoul(diffopts + 2, NULL, 10);
cd112cef 1359 ecb.outf = xdiff_outf;
6973dcae 1360 ecb.priv = &ecbdata;
cd112cef 1361 ecbdata.xm.consume = fn_out_consume;
f59a59e2
JS
1362 if (o->color_diff_words)
1363 ecbdata.diff_words =
1364 xcalloc(1, sizeof(struct diff_words_data));
6973dcae 1365 xdl_diff(&mf1, &mf2, &xpp, &xecfg, &ecb);
f59a59e2
JS
1366 if (o->color_diff_words)
1367 free_diff_words_data(&ecbdata);
6973dcae
JH
1368 }
1369
1370 free_ab_and_return:
fc3abdf5
JH
1371 diff_free_filespec_data(one);
1372 diff_free_filespec_data(two);
6973dcae
JH
1373 free(a_one);
1374 free(b_two);
1375 return;
1376}
1377
1378static void builtin_diffstat(const char *name_a, const char *name_b,
1379 struct diff_filespec *one,
1380 struct diff_filespec *two,
710158e3 1381 struct diffstat_t *diffstat,
0d21efa5 1382 struct diff_options *o,
710158e3 1383 int complete_rewrite)
6973dcae
JH
1384{
1385 mmfile_t mf1, mf2;
1386 struct diffstat_file *data;
1387
1388 data = diffstat_add(diffstat, name_a, name_b);
1389
1390 if (!one || !two) {
1391 data->is_unmerged = 1;
1392 return;
1393 }
710158e3
JH
1394 if (complete_rewrite) {
1395 diff_populate_filespec(one, 0);
1396 diff_populate_filespec(two, 0);
1397 data->deleted = count_lines(one->data, one->size);
1398 data->added = count_lines(two->data, two->size);
fc3abdf5 1399 goto free_and_return;
710158e3 1400 }
6973dcae
JH
1401 if (fill_mmfile(&mf1, one) < 0 || fill_mmfile(&mf2, two) < 0)
1402 die("unable to read files to diff");
1403
29a3eefd 1404 if (diff_filespec_is_binary(one) || diff_filespec_is_binary(two)) {
6973dcae 1405 data->is_binary = 1;
b1882587
AP
1406 data->added = mf2.size;
1407 data->deleted = mf1.size;
1408 } else {
6973dcae
JH
1409 /* Crazy xdl interfaces.. */
1410 xpparam_t xpp;
1411 xdemitconf_t xecfg;
1412 xdemitcb_t ecb;
1413
30b25010 1414 memset(&xecfg, 0, sizeof(xecfg));
0d21efa5 1415 xpp.flags = XDF_NEED_MINIMAL | o->xdl_opts;
6973dcae
JH
1416 ecb.outf = xdiff_outf;
1417 ecb.priv = diffstat;
1418 xdl_diff(&mf1, &mf2, &xpp, &xecfg, &ecb);
1419 }
fc3abdf5
JH
1420
1421 free_and_return:
1422 diff_free_filespec_data(one);
1423 diff_free_filespec_data(two);
6973dcae
JH
1424}
1425
88246898
JS
1426static void builtin_checkdiff(const char *name_a, const char *name_b,
1427 struct diff_filespec *one,
c5a8c3ec 1428 struct diff_filespec *two, struct diff_options *o)
88246898
JS
1429{
1430 mmfile_t mf1, mf2;
1431 struct checkdiff_t data;
1432
1433 if (!two)
1434 return;
1435
1436 memset(&data, 0, sizeof(data));
1437 data.xm.consume = checkdiff_consume;
1438 data.filename = name_b ? name_b : name_a;
1439 data.lineno = 0;
c5a8c3ec 1440 data.color_diff = o->color_diff;
88246898
JS
1441
1442 if (fill_mmfile(&mf1, one) < 0 || fill_mmfile(&mf2, two) < 0)
1443 die("unable to read files to diff");
1444
29a3eefd 1445 if (diff_filespec_is_binary(two))
fc3abdf5 1446 goto free_and_return;
88246898
JS
1447 else {
1448 /* Crazy xdl interfaces.. */
1449 xpparam_t xpp;
1450 xdemitconf_t xecfg;
1451 xdemitcb_t ecb;
1452
30b25010 1453 memset(&xecfg, 0, sizeof(xecfg));
88246898 1454 xpp.flags = XDF_NEED_MINIMAL;
88246898
JS
1455 ecb.outf = xdiff_outf;
1456 ecb.priv = &data;
1457 xdl_diff(&mf1, &mf2, &xpp, &xecfg, &ecb);
1458 }
fc3abdf5
JH
1459 free_and_return:
1460 diff_free_filespec_data(one);
1461 diff_free_filespec_data(two);
88246898
JS
1462}
1463
6973dcae
JH
1464struct diff_filespec *alloc_filespec(const char *path)
1465{
1466 int namelen = strlen(path);
1467 struct diff_filespec *spec = xmalloc(sizeof(*spec) + namelen + 1);
1468
1469 memset(spec, 0, sizeof(*spec));
1470 spec->path = (char *)(spec + 1);
1471 memcpy(spec->path, path, namelen+1);
1472 return spec;
1473}
1474
1475void fill_filespec(struct diff_filespec *spec, const unsigned char *sha1,
1476 unsigned short mode)
1477{
1478 if (mode) {
1479 spec->mode = canon_mode(mode);
e702496e 1480 hashcpy(spec->sha1, sha1);
0bef57ee 1481 spec->sha1_valid = !is_null_sha1(sha1);
6973dcae
JH
1482 }
1483}
1484
1485/*
5adf317b 1486 * Given a name and sha1 pair, if the index tells us the file in
6973dcae
JH
1487 * the work tree has that object contents, return true, so that
1488 * prepare_temp_file() does not have to inflate and extract.
1489 */
1510fea7 1490static int reuse_worktree_file(const char *name, const unsigned char *sha1, int want_file)
6973dcae
JH
1491{
1492 struct cache_entry *ce;
1493 struct stat st;
1494 int pos, len;
1495
1496 /* We do not read the cache ourselves here, because the
1497 * benchmark with my previous version that always reads cache
1498 * shows that it makes things worse for diff-tree comparing
1499 * two linux-2.6 kernel trees in an already checked out work
1500 * tree. This is because most diff-tree comparisons deal with
1501 * only a small number of files, while reading the cache is
1502 * expensive for a large project, and its cost outweighs the
1503 * savings we get by not inflating the object to a temporary
1504 * file. Practically, this code only helps when we are used
1505 * by diff-cache --cached, which does read the cache before
1506 * calling us.
1507 */
1508 if (!active_cache)
1509 return 0;
1510
1510fea7
SP
1511 /* We want to avoid the working directory if our caller
1512 * doesn't need the data in a normal file, this system
1513 * is rather slow with its stat/open/mmap/close syscalls,
1514 * and the object is contained in a pack file. The pack
1515 * is probably already open and will be faster to obtain
1516 * the data through than the working directory. Loose
1517 * objects however would tend to be slower as they need
1518 * to be individually opened and inflated.
1519 */
5caf9232 1520 if (!FAST_WORKING_DIRECTORY && !want_file && has_sha1_pack(sha1, NULL))
1510fea7
SP
1521 return 0;
1522
6973dcae
JH
1523 len = strlen(name);
1524 pos = cache_name_pos(name, len);
1525 if (pos < 0)
1526 return 0;
1527 ce = active_cache[pos];
1528 if ((lstat(name, &st) < 0) ||
1529 !S_ISREG(st.st_mode) || /* careful! */
1530 ce_match_stat(ce, &st, 0) ||
a89fccd2 1531 hashcmp(sha1, ce->sha1))
6973dcae
JH
1532 return 0;
1533 /* we return 1 only when we can stat, it is a regular file,
1534 * stat information matches, and sha1 recorded in the cache
1535 * matches. I.e. we know the file in the work tree really is
1536 * the same as the <name, sha1> pair.
1537 */
1538 return 1;
1539}
1540
3afaa72d
JH
1541static int populate_from_stdin(struct diff_filespec *s)
1542{
1543#define INCREMENT 1024
1544 char *buf;
1545 unsigned long size;
8a912bcb 1546 ssize_t got;
3afaa72d
JH
1547
1548 size = 0;
1549 buf = NULL;
1550 while (1) {
1551 buf = xrealloc(buf, size + INCREMENT);
1552 got = xread(0, buf + size, INCREMENT);
1553 if (!got)
1554 break; /* EOF */
1555 if (got < 0)
1556 return error("error while reading from stdin %s",
1557 strerror(errno));
1558 size += got;
1559 }
1560 s->should_munmap = 0;
1561 s->data = buf;
1562 s->size = size;
1563 s->should_free = 1;
1564 return 0;
1565}
1566
04786756
LT
1567static int diff_populate_gitlink(struct diff_filespec *s, int size_only)
1568{
1569 int len;
1570 char *data = xmalloc(100);
1571 len = snprintf(data, 100,
1572 "Subproject commit %s\n", sha1_to_hex(s->sha1));
1573 s->data = data;
1574 s->size = len;
1575 s->should_free = 1;
1576 if (size_only) {
1577 s->data = NULL;
1578 free(data);
1579 }
1580 return 0;
1581}
1582
6973dcae
JH
1583/*
1584 * While doing rename detection and pickaxe operation, we may need to
1585 * grab the data for the blob (or file) for our own in-core comparison.
1586 * diff_filespec has data and size fields for this purpose.
1587 */
1588int diff_populate_filespec(struct diff_filespec *s, int size_only)
1589{
1590 int err = 0;
1591 if (!DIFF_FILE_VALID(s))
1592 die("internal error: asking to populate invalid file.");
1593 if (S_ISDIR(s->mode))
1594 return -1;
1595
6973dcae 1596 if (s->data)
fc3abdf5 1597 return 0;
04786756 1598
6e0b8ed6
JH
1599 if (size_only && 0 < s->size)
1600 return 0;
1601
302b9282 1602 if (S_ISGITLINK(s->mode))
04786756
LT
1603 return diff_populate_gitlink(s, size_only);
1604
6973dcae 1605 if (!s->sha1_valid ||
1510fea7 1606 reuse_worktree_file(s->path, s->sha1, 0)) {
6973dcae
JH
1607 struct stat st;
1608 int fd;
6c510bee
LT
1609 char *buf;
1610 unsigned long size;
1611
3afaa72d
JH
1612 if (!strcmp(s->path, "-"))
1613 return populate_from_stdin(s);
1614
6973dcae
JH
1615 if (lstat(s->path, &st) < 0) {
1616 if (errno == ENOENT) {
1617 err_empty:
1618 err = -1;
1619 empty:
d2543b8e 1620 s->data = (char *)"";
6973dcae
JH
1621 s->size = 0;
1622 return err;
1623 }
1624 }
dc49cd76 1625 s->size = xsize_t(st.st_size);
6973dcae
JH
1626 if (!s->size)
1627 goto empty;
1628 if (size_only)
1629 return 0;
1630 if (S_ISLNK(st.st_mode)) {
1631 int ret;
1632 s->data = xmalloc(s->size);
1633 s->should_free = 1;
1634 ret = readlink(s->path, s->data, s->size);
1635 if (ret < 0) {
1636 free(s->data);
1637 goto err_empty;
1638 }
1639 return 0;
1640 }
1641 fd = open(s->path, O_RDONLY);
1642 if (fd < 0)
1643 goto err_empty;
c4712e45 1644 s->data = xmmap(NULL, s->size, PROT_READ, MAP_PRIVATE, fd, 0);
6973dcae 1645 close(fd);
6973dcae 1646 s->should_munmap = 1;
6c510bee
LT
1647
1648 /*
1649 * Convert from working tree format to canonical git format
1650 */
6c510bee 1651 size = s->size;
ac78e548
AR
1652 buf = convert_to_git(s->path, s->data, &size);
1653 if (buf) {
6c510bee
LT
1654 munmap(s->data, s->size);
1655 s->should_munmap = 0;
1656 s->data = buf;
1657 s->size = size;
1658 s->should_free = 1;
1659 }
6973dcae
JH
1660 }
1661 else {
21666f1a 1662 enum object_type type;
6e0b8ed6 1663 if (size_only)
21666f1a 1664 type = sha1_object_info(s->sha1, &s->size);
6973dcae 1665 else {
21666f1a 1666 s->data = read_sha1_file(s->sha1, &type, &s->size);
6973dcae
JH
1667 s->should_free = 1;
1668 }
1669 }
1670 return 0;
1671}
1672
1673void diff_free_filespec_data(struct diff_filespec *s)
1674{
1675 if (s->should_free)
1676 free(s->data);
1677 else if (s->should_munmap)
1678 munmap(s->data, s->size);
fc3abdf5
JH
1679
1680 if (s->should_free || s->should_munmap) {
1681 s->should_free = s->should_munmap = 0;
1682 s->data = NULL;
1683 }
6973dcae
JH
1684 free(s->cnt_data);
1685 s->cnt_data = NULL;
1686}
1687
1688static void prep_temp_blob(struct diff_tempfile *temp,
1689 void *blob,
1690 unsigned long size,
1691 const unsigned char *sha1,
1692 int mode)
1693{
1694 int fd;
1695
1472966c 1696 fd = git_mkstemp(temp->tmp_path, PATH_MAX, ".diff_XXXXXX");
6973dcae 1697 if (fd < 0)
e7a7be88 1698 die("unable to create temp-file: %s", strerror(errno));
93822c22 1699 if (write_in_full(fd, blob, size) != size)
6973dcae
JH
1700 die("unable to write temp-file");
1701 close(fd);
1702 temp->name = temp->tmp_path;
1703 strcpy(temp->hex, sha1_to_hex(sha1));
1704 temp->hex[40] = 0;
1705 sprintf(temp->mode, "%06o", mode);
1706}
1707
1708static void prepare_temp_file(const char *name,
1709 struct diff_tempfile *temp,
1710 struct diff_filespec *one)
1711{
1712 if (!DIFF_FILE_VALID(one)) {
1713 not_a_valid_file:
1714 /* A '-' entry produces this for file-2, and
1715 * a '+' entry produces this for file-1.
1716 */
1717 temp->name = "/dev/null";
1718 strcpy(temp->hex, ".");
1719 strcpy(temp->mode, ".");
1720 return;
1721 }
1722
1723 if (!one->sha1_valid ||
1510fea7 1724 reuse_worktree_file(name, one->sha1, 1)) {
6973dcae
JH
1725 struct stat st;
1726 if (lstat(name, &st) < 0) {
1727 if (errno == ENOENT)
1728 goto not_a_valid_file;
1729 die("stat(%s): %s", name, strerror(errno));
1730 }
1731 if (S_ISLNK(st.st_mode)) {
1732 int ret;
1733 char buf[PATH_MAX + 1]; /* ought to be SYMLINK_MAX */
dc49cd76 1734 size_t sz = xsize_t(st.st_size);
6973dcae
JH
1735 if (sizeof(buf) <= st.st_size)
1736 die("symlink too long: %s", name);
dc49cd76 1737 ret = readlink(name, buf, sz);
6973dcae
JH
1738 if (ret < 0)
1739 die("readlink(%s)", name);
dc49cd76 1740 prep_temp_blob(temp, buf, sz,
6973dcae
JH
1741 (one->sha1_valid ?
1742 one->sha1 : null_sha1),
1743 (one->sha1_valid ?
1744 one->mode : S_IFLNK));
1745 }
1746 else {
1747 /* we can borrow from the file in the work tree */
1748 temp->name = name;
1749 if (!one->sha1_valid)
1750 strcpy(temp->hex, sha1_to_hex(null_sha1));
1751 else
1752 strcpy(temp->hex, sha1_to_hex(one->sha1));
1753 /* Even though we may sometimes borrow the
1754 * contents from the work tree, we always want
1755 * one->mode. mode is trustworthy even when
1756 * !(one->sha1_valid), as long as
1757 * DIFF_FILE_VALID(one).
1758 */
1759 sprintf(temp->mode, "%06o", one->mode);
1760 }
1761 return;
1762 }
1763 else {
1764 if (diff_populate_filespec(one, 0))
1765 die("cannot read data blob for %s", one->path);
1766 prep_temp_blob(temp, one->data, one->size,
1767 one->sha1, one->mode);
1768 }
1769}
1770
1771static void remove_tempfile(void)
1772{
1773 int i;
1774
1775 for (i = 0; i < 2; i++)
1776 if (diff_temp[i].name == diff_temp[i].tmp_path) {
1777 unlink(diff_temp[i].name);
1778 diff_temp[i].name = NULL;
1779 }
1780}
1781
1782static void remove_tempfile_on_signal(int signo)
1783{
1784 remove_tempfile();
1785 signal(SIGINT, SIG_DFL);
1786 raise(signo);
1787}
1788
1789static int spawn_prog(const char *pgm, const char **arg)
1790{
1791 pid_t pid;
1792 int status;
1793
1794 fflush(NULL);
1795 pid = fork();
1796 if (pid < 0)
1797 die("unable to fork");
1798 if (!pid) {
1799 execvp(pgm, (char *const*) arg);
1800 exit(255);
1801 }
1802
1803 while (waitpid(pid, &status, 0) < 0) {
1804 if (errno == EINTR)
1805 continue;
1806 return -1;
1807 }
1808
1809 /* Earlier we did not check the exit status because
1810 * diff exits non-zero if files are different, and
1811 * we are not interested in knowing that. It was a
1812 * mistake which made it harder to quit a diff-*
1813 * session that uses the git-apply-patch-script as
1814 * the GIT_EXTERNAL_DIFF. A custom GIT_EXTERNAL_DIFF
1815 * should also exit non-zero only when it wants to
1816 * abort the entire diff-* session.
1817 */
1818 if (WIFEXITED(status) && !WEXITSTATUS(status))
1819 return 0;
1820 return -1;
1821}
1822
1823/* An external diff command takes:
1824 *
1825 * diff-cmd name infile1 infile1-sha1 infile1-mode \
1826 * infile2 infile2-sha1 infile2-mode [ rename-to ]
1827 *
1828 */
1829static void run_external_diff(const char *pgm,
1830 const char *name,
1831 const char *other,
1832 struct diff_filespec *one,
1833 struct diff_filespec *two,
1834 const char *xfrm_msg,
1835 int complete_rewrite)
1836{
1837 const char *spawn_arg[10];
1838 struct diff_tempfile *temp = diff_temp;
1839 int retval;
1840 static int atexit_asked = 0;
1841 const char *othername;
1842 const char **arg = &spawn_arg[0];
1843
1844 othername = (other? other : name);
1845 if (one && two) {
1846 prepare_temp_file(name, &temp[0], one);
1847 prepare_temp_file(othername, &temp[1], two);
1848 if (! atexit_asked &&
1849 (temp[0].name == temp[0].tmp_path ||
1850 temp[1].name == temp[1].tmp_path)) {
1851 atexit_asked = 1;
1852 atexit(remove_tempfile);
1853 }
1854 signal(SIGINT, remove_tempfile_on_signal);
1855 }
1856
1857 if (one && two) {
1858 *arg++ = pgm;
1859 *arg++ = name;
1860 *arg++ = temp[0].name;
1861 *arg++ = temp[0].hex;
1862 *arg++ = temp[0].mode;
1863 *arg++ = temp[1].name;
1864 *arg++ = temp[1].hex;
1865 *arg++ = temp[1].mode;
1866 if (other) {
1867 *arg++ = other;
1868 *arg++ = xfrm_msg;
1869 }
1870 } else {
1871 *arg++ = pgm;
1872 *arg++ = name;
1873 }
1874 *arg = NULL;
1875 retval = spawn_prog(pgm, spawn_arg);
1876 remove_tempfile();
1877 if (retval) {
1878 fprintf(stderr, "external diff died, stopping at %s.\n", name);
1879 exit(1);
1880 }
1881}
1882
f1af60bd
JH
1883static const char *external_diff_attr(const char *name)
1884{
1885 struct git_attr_check attr_diff_check;
1886
1887 setup_diff_attr_check(&attr_diff_check);
1888 if (!git_checkattr(name, 1, &attr_diff_check)) {
1889 const char *value = attr_diff_check.value;
1890 if (!ATTR_TRUE(value) &&
1891 !ATTR_FALSE(value) &&
1892 !ATTR_UNSET(value)) {
1893 struct ll_diff_driver *drv;
1894
e0e324a4 1895 read_config_if_needed();
f1af60bd
JH
1896 for (drv = user_diff; drv; drv = drv->next)
1897 if (!strcmp(drv->name, value))
1898 return drv->cmd;
1899 }
1900 }
1901 return NULL;
1902}
1903
6973dcae
JH
1904static void run_diff_cmd(const char *pgm,
1905 const char *name,
1906 const char *other,
1907 struct diff_filespec *one,
1908 struct diff_filespec *two,
1909 const char *xfrm_msg,
051308f6 1910 struct diff_options *o,
6973dcae
JH
1911 int complete_rewrite)
1912{
f1af60bd
JH
1913 if (!o->allow_external)
1914 pgm = NULL;
1915 else {
1916 const char *cmd = external_diff_attr(name);
1917 if (cmd)
1918 pgm = cmd;
1919 }
1920
6973dcae
JH
1921 if (pgm) {
1922 run_external_diff(pgm, name, other, one, two, xfrm_msg,
1923 complete_rewrite);
1924 return;
1925 }
1926 if (one && two)
1927 builtin_diff(name, other ? other : name,
051308f6 1928 one, two, xfrm_msg, o, complete_rewrite);
6973dcae
JH
1929 else
1930 printf("* Unmerged path %s\n", name);
1931}
1932
1933static void diff_fill_sha1_info(struct diff_filespec *one)
1934{
1935 if (DIFF_FILE_VALID(one)) {
1936 if (!one->sha1_valid) {
1937 struct stat st;
5332b2af
JS
1938 if (!strcmp(one->path, "-")) {
1939 hashcpy(one->sha1, null_sha1);
1940 return;
1941 }
6973dcae
JH
1942 if (lstat(one->path, &st) < 0)
1943 die("stat %s", one->path);
1944 if (index_path(one->sha1, one->path, &st, 0))
1945 die("cannot hash %s\n", one->path);
1946 }
1947 }
1948 else
e702496e 1949 hashclr(one->sha1);
6973dcae
JH
1950}
1951
125b7630
RS
1952static int similarity_index(struct diff_filepair *p)
1953{
1954 return p->score * 100 / MAX_SCORE;
1955}
1956
6973dcae
JH
1957static void run_diff(struct diff_filepair *p, struct diff_options *o)
1958{
1959 const char *pgm = external_diff();
1960 char msg[PATH_MAX*2+300], *xfrm_msg;
1961 struct diff_filespec *one;
1962 struct diff_filespec *two;
1963 const char *name;
1964 const char *other;
1965 char *name_munged, *other_munged;
1966 int complete_rewrite = 0;
1967 int len;
1968
1969 if (DIFF_PAIR_UNMERGED(p)) {
1970 /* unmerged */
051308f6 1971 run_diff_cmd(pgm, p->one->path, NULL, NULL, NULL, NULL, o, 0);
6973dcae
JH
1972 return;
1973 }
1974
1975 name = p->one->path;
1976 other = (strcmp(name, p->two->path) ? p->two->path : NULL);
1977 name_munged = quote_one(name);
1978 other_munged = quote_one(other);
1979 one = p->one; two = p->two;
1980
1981 diff_fill_sha1_info(one);
1982 diff_fill_sha1_info(two);
1983
1984 len = 0;
1985 switch (p->status) {
1986 case DIFF_STATUS_COPIED:
1987 len += snprintf(msg + len, sizeof(msg) - len,
1988 "similarity index %d%%\n"
1989 "copy from %s\n"
1990 "copy to %s\n",
125b7630 1991 similarity_index(p), name_munged, other_munged);
6973dcae
JH
1992 break;
1993 case DIFF_STATUS_RENAMED:
1994 len += snprintf(msg + len, sizeof(msg) - len,
1995 "similarity index %d%%\n"
1996 "rename from %s\n"
1997 "rename to %s\n",
125b7630 1998 similarity_index(p), name_munged, other_munged);
6973dcae
JH
1999 break;
2000 case DIFF_STATUS_MODIFIED:
2001 if (p->score) {
2002 len += snprintf(msg + len, sizeof(msg) - len,
2003 "dissimilarity index %d%%\n",
125b7630 2004 similarity_index(p));
6973dcae
JH
2005 complete_rewrite = 1;
2006 break;
2007 }
2008 /* fallthru */
2009 default:
2010 /* nothing */
2011 ;
2012 }
2013
a89fccd2 2014 if (hashcmp(one->sha1, two->sha1)) {
6973dcae 2015 int abbrev = o->full_index ? 40 : DEFAULT_ABBREV;
6973dcae 2016
82793c55
JH
2017 if (o->binary) {
2018 mmfile_t mf;
29a3eefd
JH
2019 if ((!fill_mmfile(&mf, one) && diff_filespec_is_binary(one)) ||
2020 (!fill_mmfile(&mf, two) && diff_filespec_is_binary(two)))
82793c55
JH
2021 abbrev = 40;
2022 }
6973dcae
JH
2023 len += snprintf(msg + len, sizeof(msg) - len,
2024 "index %.*s..%.*s",
dcb3450f
LT
2025 abbrev, sha1_to_hex(one->sha1),
2026 abbrev, sha1_to_hex(two->sha1));
6973dcae
JH
2027 if (one->mode == two->mode)
2028 len += snprintf(msg + len, sizeof(msg) - len,
2029 " %06o", one->mode);
2030 len += snprintf(msg + len, sizeof(msg) - len, "\n");
2031 }
2032
2033 if (len)
2034 msg[--len] = 0;
2035 xfrm_msg = len ? msg : NULL;
2036
2037 if (!pgm &&
2038 DIFF_FILE_VALID(one) && DIFF_FILE_VALID(two) &&
2039 (S_IFMT & one->mode) != (S_IFMT & two->mode)) {
2040 /* a filepair that changes between file and symlink
2041 * needs to be split into deletion and creation.
2042 */
2043 struct diff_filespec *null = alloc_filespec(two->path);
051308f6 2044 run_diff_cmd(NULL, name, other, one, null, xfrm_msg, o, 0);
6973dcae
JH
2045 free(null);
2046 null = alloc_filespec(one->path);
051308f6 2047 run_diff_cmd(NULL, name, other, null, two, xfrm_msg, o, 0);
6973dcae
JH
2048 free(null);
2049 }
2050 else
051308f6 2051 run_diff_cmd(pgm, name, other, one, two, xfrm_msg, o,
6973dcae
JH
2052 complete_rewrite);
2053
2054 free(name_munged);
2055 free(other_munged);
2056}
2057
2058static void run_diffstat(struct diff_filepair *p, struct diff_options *o,
2059 struct diffstat_t *diffstat)
2060{
2061 const char *name;
2062 const char *other;
710158e3 2063 int complete_rewrite = 0;
6973dcae
JH
2064
2065 if (DIFF_PAIR_UNMERGED(p)) {
2066 /* unmerged */
0d21efa5 2067 builtin_diffstat(p->one->path, NULL, NULL, NULL, diffstat, o, 0);
6973dcae
JH
2068 return;
2069 }
2070
2071 name = p->one->path;
2072 other = (strcmp(name, p->two->path) ? p->two->path : NULL);
2073
2074 diff_fill_sha1_info(p->one);
2075 diff_fill_sha1_info(p->two);
2076
710158e3
JH
2077 if (p->status == DIFF_STATUS_MODIFIED && p->score)
2078 complete_rewrite = 1;
0d21efa5 2079 builtin_diffstat(name, other, p->one, p->two, diffstat, o, complete_rewrite);
6973dcae
JH
2080}
2081
88246898
JS
2082static void run_checkdiff(struct diff_filepair *p, struct diff_options *o)
2083{
2084 const char *name;
2085 const char *other;
2086
2087 if (DIFF_PAIR_UNMERGED(p)) {
2088 /* unmerged */
2089 return;
2090 }
2091
2092 name = p->one->path;
2093 other = (strcmp(name, p->two->path) ? p->two->path : NULL);
2094
2095 diff_fill_sha1_info(p->one);
2096 diff_fill_sha1_info(p->two);
2097
c5a8c3ec 2098 builtin_checkdiff(name, other, p->one, p->two, o);
88246898
JS
2099}
2100
6973dcae
JH
2101void diff_setup(struct diff_options *options)
2102{
2103 memset(options, 0, sizeof(*options));
6973dcae
JH
2104 options->line_termination = '\n';
2105 options->break_opt = -1;
2106 options->rename_limit = -1;
ee1e5412 2107 options->context = 3;
3969cf7d 2108 options->msg_sep = "";
6973dcae
JH
2109
2110 options->change = diff_change;
2111 options->add_remove = diff_addremove;
801235c5 2112 options->color_diff = diff_use_color_default;
b68ea12e 2113 options->detect_rename = diff_detect_rename_default;
6973dcae
JH
2114}
2115
2116int diff_setup_done(struct diff_options *options)
2117{
d7de00f7
TH
2118 int count = 0;
2119
2120 if (options->output_format & DIFF_FORMAT_NAME)
2121 count++;
2122 if (options->output_format & DIFF_FORMAT_NAME_STATUS)
2123 count++;
2124 if (options->output_format & DIFF_FORMAT_CHECKDIFF)
2125 count++;
2126 if (options->output_format & DIFF_FORMAT_NO_OUTPUT)
2127 count++;
2128 if (count > 1)
2129 die("--name-only, --name-status, --check and -s are mutually exclusive");
2130
03b9d560
JH
2131 if (options->find_copies_harder)
2132 options->detect_rename = DIFF_DETECT_COPY;
2133
c6744349
TH
2134 if (options->output_format & (DIFF_FORMAT_NAME |
2135 DIFF_FORMAT_NAME_STATUS |
2136 DIFF_FORMAT_CHECKDIFF |
2137 DIFF_FORMAT_NO_OUTPUT))
2138 options->output_format &= ~(DIFF_FORMAT_RAW |
74e2abe5 2139 DIFF_FORMAT_NUMSTAT |
c6744349 2140 DIFF_FORMAT_DIFFSTAT |
ebd124c6 2141 DIFF_FORMAT_SHORTSTAT |
c6744349
TH
2142 DIFF_FORMAT_SUMMARY |
2143 DIFF_FORMAT_PATCH);
2144
6973dcae
JH
2145 /*
2146 * These cases always need recursive; we do not drop caller-supplied
2147 * recursive bits for other formats here.
2148 */
c6744349 2149 if (options->output_format & (DIFF_FORMAT_PATCH |
74e2abe5 2150 DIFF_FORMAT_NUMSTAT |
c6744349 2151 DIFF_FORMAT_DIFFSTAT |
ebd124c6 2152 DIFF_FORMAT_SHORTSTAT |
d7014dc0 2153 DIFF_FORMAT_SUMMARY |
c6744349 2154 DIFF_FORMAT_CHECKDIFF))
6973dcae 2155 options->recursive = 1;
5e363541 2156 /*
3969cf7d 2157 * Also pickaxe would not work very well if you do not say recursive
5e363541 2158 */
3969cf7d
JH
2159 if (options->pickaxe)
2160 options->recursive = 1;
5e363541 2161
6973dcae
JH
2162 if (options->detect_rename && options->rename_limit < 0)
2163 options->rename_limit = diff_rename_limit_default;
2164 if (options->setup & DIFF_SETUP_USE_CACHE) {
2165 if (!active_cache)
2166 /* read-cache does not die even when it fails
2167 * so it is safe for us to do this here. Also
2168 * it does not smudge active_cache or active_nr
2169 * when it fails, so we do not have to worry about
2170 * cleaning it up ourselves either.
2171 */
2172 read_cache();
2173 }
6973dcae
JH
2174 if (options->abbrev <= 0 || 40 < options->abbrev)
2175 options->abbrev = 40; /* full */
2176
68aacb2f
JH
2177 /*
2178 * It does not make sense to show the first hit we happened
2179 * to have found. It does not make sense not to return with
2180 * exit code in such a case either.
2181 */
2182 if (options->quiet) {
2183 options->output_format = DIFF_FORMAT_NO_OUTPUT;
2184 options->exit_with_status = 1;
2185 }
2186
2187 /*
2188 * If we postprocess in diffcore, we cannot simply return
2189 * upon the first hit. We need to run diff as usual.
2190 */
2191 if (options->pickaxe || options->filter)
2192 options->quiet = 0;
2193
6973dcae
JH
2194 return 0;
2195}
2196
d2543b8e 2197static int opt_arg(const char *arg, int arg_short, const char *arg_long, int *val)
ee1e5412
LT
2198{
2199 char c, *eq;
2200 int len;
2201
2202 if (*arg != '-')
2203 return 0;
2204 c = *++arg;
2205 if (!c)
2206 return 0;
2207 if (c == arg_short) {
2208 c = *++arg;
2209 if (!c)
2210 return 1;
2211 if (val && isdigit(c)) {
2212 char *end;
2213 int n = strtoul(arg, &end, 10);
2214 if (*end)
2215 return 0;
2216 *val = n;
2217 return 1;
2218 }
2219 return 0;
2220 }
2221 if (c != '-')
2222 return 0;
2223 arg++;
2224 eq = strchr(arg, '=');
2225 if (eq)
2226 len = eq - arg;
2227 else
2228 len = strlen(arg);
2229 if (!len || strncmp(arg, arg_long, len))
2230 return 0;
2231 if (eq) {
2232 int n;
2233 char *end;
2234 if (!isdigit(*++eq))
2235 return 0;
2236 n = strtoul(eq, &end, 10);
2237 if (*end)
2238 return 0;
2239 *val = n;
2240 }
2241 return 1;
2242}
2243
16befb8b
JH
2244static int diff_scoreopt_parse(const char *opt);
2245
6973dcae
JH
2246int diff_opt_parse(struct diff_options *options, const char **av, int ac)
2247{
2248 const char *arg = av[0];
2249 if (!strcmp(arg, "-p") || !strcmp(arg, "-u"))
c6744349 2250 options->output_format |= DIFF_FORMAT_PATCH;
ee1e5412 2251 else if (opt_arg(arg, 'U', "unified", &options->context))
c6744349 2252 options->output_format |= DIFF_FORMAT_PATCH;
a610786f
TH
2253 else if (!strcmp(arg, "--raw"))
2254 options->output_format |= DIFF_FORMAT_RAW;
6973dcae 2255 else if (!strcmp(arg, "--patch-with-raw")) {
c6744349 2256 options->output_format |= DIFF_FORMAT_PATCH | DIFF_FORMAT_RAW;
6973dcae 2257 }
74e2abe5
JH
2258 else if (!strcmp(arg, "--numstat")) {
2259 options->output_format |= DIFF_FORMAT_NUMSTAT;
2260 }
ebd124c6
NP
2261 else if (!strcmp(arg, "--shortstat")) {
2262 options->output_format |= DIFF_FORMAT_SHORTSTAT;
2263 }
cc44c765 2264 else if (!prefixcmp(arg, "--stat")) {
5c5b2ea9
LT
2265 char *end;
2266 int width = options->stat_width;
2267 int name_width = options->stat_name_width;
2268 arg += 6;
2269 end = (char *)arg;
2270
2271 switch (*arg) {
2272 case '-':
cc44c765 2273 if (!prefixcmp(arg, "-width="))
5c5b2ea9 2274 width = strtoul(arg + 7, &end, 10);
cc44c765 2275 else if (!prefixcmp(arg, "-name-width="))
5c5b2ea9
LT
2276 name_width = strtoul(arg + 12, &end, 10);
2277 break;
2278 case '=':
2279 width = strtoul(arg+1, &end, 10);
2280 if (*end == ',')
2281 name_width = strtoul(end+1, &end, 10);
2282 }
2283
2284 /* Important! This checks all the error cases! */
2285 if (*end)
2286 return 0;
c6744349 2287 options->output_format |= DIFF_FORMAT_DIFFSTAT;
5c5b2ea9
LT
2288 options->stat_name_width = name_width;
2289 options->stat_width = width;
a2540023 2290 }
88246898 2291 else if (!strcmp(arg, "--check"))
c6744349 2292 options->output_format |= DIFF_FORMAT_CHECKDIFF;
4bbd261b 2293 else if (!strcmp(arg, "--summary"))
c6744349 2294 options->output_format |= DIFF_FORMAT_SUMMARY;
6973dcae 2295 else if (!strcmp(arg, "--patch-with-stat")) {
c6744349 2296 options->output_format |= DIFF_FORMAT_PATCH | DIFF_FORMAT_DIFFSTAT;
6973dcae
JH
2297 }
2298 else if (!strcmp(arg, "-z"))
2299 options->line_termination = 0;
cc44c765 2300 else if (!prefixcmp(arg, "-l"))
6973dcae
JH
2301 options->rename_limit = strtoul(arg+2, NULL, 10);
2302 else if (!strcmp(arg, "--full-index"))
2303 options->full_index = 1;
0660626c 2304 else if (!strcmp(arg, "--binary")) {
c6744349 2305 options->output_format |= DIFF_FORMAT_PATCH;
82793c55 2306 options->binary = 1;
0660626c 2307 }
63ac4501 2308 else if (!strcmp(arg, "-a") || !strcmp(arg, "--text")) {
6d64ea96
SF
2309 options->text = 1;
2310 }
6973dcae 2311 else if (!strcmp(arg, "--name-only"))
c6744349 2312 options->output_format |= DIFF_FORMAT_NAME;
6973dcae 2313 else if (!strcmp(arg, "--name-status"))
c6744349 2314 options->output_format |= DIFF_FORMAT_NAME_STATUS;
6973dcae
JH
2315 else if (!strcmp(arg, "-R"))
2316 options->reverse_diff = 1;
cc44c765 2317 else if (!prefixcmp(arg, "-S"))
6973dcae 2318 options->pickaxe = arg + 2;
c6744349
TH
2319 else if (!strcmp(arg, "-s")) {
2320 options->output_format |= DIFF_FORMAT_NO_OUTPUT;
2321 }
cc44c765 2322 else if (!prefixcmp(arg, "-O"))
6973dcae 2323 options->orderfile = arg + 2;
cc44c765 2324 else if (!prefixcmp(arg, "--diff-filter="))
6973dcae
JH
2325 options->filter = arg + 14;
2326 else if (!strcmp(arg, "--pickaxe-all"))
2327 options->pickaxe_opts = DIFF_PICKAXE_ALL;
2328 else if (!strcmp(arg, "--pickaxe-regex"))
2329 options->pickaxe_opts = DIFF_PICKAXE_REGEX;
cc44c765 2330 else if (!prefixcmp(arg, "-B")) {
6973dcae
JH
2331 if ((options->break_opt =
2332 diff_scoreopt_parse(arg)) == -1)
2333 return -1;
2334 }
cc44c765 2335 else if (!prefixcmp(arg, "-M")) {
6973dcae
JH
2336 if ((options->rename_score =
2337 diff_scoreopt_parse(arg)) == -1)
2338 return -1;
2339 options->detect_rename = DIFF_DETECT_RENAME;
2340 }
cc44c765 2341 else if (!prefixcmp(arg, "-C")) {
ca6c0970
JS
2342 if (options->detect_rename == DIFF_DETECT_COPY)
2343 options->find_copies_harder = 1;
6973dcae
JH
2344 if ((options->rename_score =
2345 diff_scoreopt_parse(arg)) == -1)
2346 return -1;
2347 options->detect_rename = DIFF_DETECT_COPY;
2348 }
2349 else if (!strcmp(arg, "--find-copies-harder"))
2350 options->find_copies_harder = 1;
750f7b66
LT
2351 else if (!strcmp(arg, "--follow"))
2352 options->follow_renames = 1;
6973dcae
JH
2353 else if (!strcmp(arg, "--abbrev"))
2354 options->abbrev = DEFAULT_ABBREV;
cc44c765 2355 else if (!prefixcmp(arg, "--abbrev=")) {
6973dcae
JH
2356 options->abbrev = strtoul(arg + 9, NULL, 10);
2357 if (options->abbrev < MINIMUM_ABBREV)
2358 options->abbrev = MINIMUM_ABBREV;
2359 else if (40 < options->abbrev)
2360 options->abbrev = 40;
2361 }
cd112cef
JS
2362 else if (!strcmp(arg, "--color"))
2363 options->color_diff = 1;
fef88bb0
JH
2364 else if (!strcmp(arg, "--no-color"))
2365 options->color_diff = 0;
0d21efa5
JS
2366 else if (!strcmp(arg, "-w") || !strcmp(arg, "--ignore-all-space"))
2367 options->xdl_opts |= XDF_IGNORE_WHITESPACE;
2368 else if (!strcmp(arg, "-b") || !strcmp(arg, "--ignore-space-change"))
2369 options->xdl_opts |= XDF_IGNORE_WHITESPACE_CHANGE;
859f9c45
JS
2370 else if (!strcmp(arg, "--ignore-space-at-eol"))
2371 options->xdl_opts |= XDF_IGNORE_WHITESPACE_AT_EOL;
f59a59e2
JS
2372 else if (!strcmp(arg, "--color-words"))
2373 options->color_diff = options->color_diff_words = 1;
b68ea12e
EW
2374 else if (!strcmp(arg, "--no-renames"))
2375 options->detect_rename = 0;
41bbf9d5
AR
2376 else if (!strcmp(arg, "--exit-code"))
2377 options->exit_with_status = 1;
68aacb2f
JH
2378 else if (!strcmp(arg, "--quiet"))
2379 options->quiet = 1;
72909bef
JS
2380 else if (!strcmp(arg, "--ext-diff"))
2381 options->allow_external = 1;
2382 else if (!strcmp(arg, "--no-ext-diff"))
2383 options->allow_external = 0;
6973dcae
JH
2384 else
2385 return 0;
2386 return 1;
2387}
2388
2389static int parse_num(const char **cp_p)
2390{
2391 unsigned long num, scale;
2392 int ch, dot;
2393 const char *cp = *cp_p;
2394
2395 num = 0;
2396 scale = 1;
2397 dot = 0;
2398 for(;;) {
2399 ch = *cp;
2400 if ( !dot && ch == '.' ) {
2401 scale = 1;
2402 dot = 1;
2403 } else if ( ch == '%' ) {
2404 scale = dot ? scale*100 : 100;
2405 cp++; /* % is always at the end */
2406 break;
2407 } else if ( ch >= '0' && ch <= '9' ) {
2408 if ( scale < 100000 ) {
2409 scale *= 10;
2410 num = (num*10) + (ch-'0');
2411 }
2412 } else {
2413 break;
2414 }
2415 cp++;
2416 }
2417 *cp_p = cp;
2418
2419 /* user says num divided by scale and we say internally that
2420 * is MAX_SCORE * num / scale.
2421 */
dc49cd76 2422 return (int)((num >= scale) ? MAX_SCORE : (MAX_SCORE * num / scale));
6973dcae
JH
2423}
2424
16befb8b 2425static int diff_scoreopt_parse(const char *opt)
6973dcae
JH
2426{
2427 int opt1, opt2, cmd;
2428
2429 if (*opt++ != '-')
2430 return -1;
2431 cmd = *opt++;
2432 if (cmd != 'M' && cmd != 'C' && cmd != 'B')
2433 return -1; /* that is not a -M, -C nor -B option */
2434
2435 opt1 = parse_num(&opt);
2436 if (cmd != 'B')
2437 opt2 = 0;
2438 else {
2439 if (*opt == 0)
2440 opt2 = 0;
2441 else if (*opt != '/')
2442 return -1; /* we expect -B80/99 or -B80 */
2443 else {
2444 opt++;
2445 opt2 = parse_num(&opt);
2446 }
2447 }
2448 if (*opt != 0)
2449 return -1;
2450 return opt1 | (opt2 << 16);
2451}
2452
2453struct diff_queue_struct diff_queued_diff;
2454
2455void diff_q(struct diff_queue_struct *queue, struct diff_filepair *dp)
2456{
2457 if (queue->alloc <= queue->nr) {
2458 queue->alloc = alloc_nr(queue->alloc);
2459 queue->queue = xrealloc(queue->queue,
2460 sizeof(dp) * queue->alloc);
2461 }
2462 queue->queue[queue->nr++] = dp;
2463}
2464
2465struct diff_filepair *diff_queue(struct diff_queue_struct *queue,
2466 struct diff_filespec *one,
2467 struct diff_filespec *two)
2468{
ef677686 2469 struct diff_filepair *dp = xcalloc(1, sizeof(*dp));
6973dcae
JH
2470 dp->one = one;
2471 dp->two = two;
6973dcae
JH
2472 if (queue)
2473 diff_q(queue, dp);
2474 return dp;
2475}
2476
2477void diff_free_filepair(struct diff_filepair *p)
2478{
2479 diff_free_filespec_data(p->one);
2480 diff_free_filespec_data(p->two);
2481 free(p->one);
2482 free(p->two);
2483 free(p);
2484}
2485
2486/* This is different from find_unique_abbrev() in that
2487 * it stuffs the result with dots for alignment.
2488 */
2489const char *diff_unique_abbrev(const unsigned char *sha1, int len)
2490{
2491 int abblen;
2492 const char *abbrev;
2493 if (len == 40)
2494 return sha1_to_hex(sha1);
2495
2496 abbrev = find_unique_abbrev(sha1, len);
2497 if (!abbrev)
2498 return sha1_to_hex(sha1);
2499 abblen = strlen(abbrev);
2500 if (abblen < 37) {
2501 static char hex[41];
2502 if (len < abblen && abblen <= len + 2)
2503 sprintf(hex, "%s%.*s", abbrev, len+3-abblen, "..");
2504 else
2505 sprintf(hex, "%s...", abbrev);
2506 return hex;
2507 }
2508 return sha1_to_hex(sha1);
2509}
2510
2511static void diff_flush_raw(struct diff_filepair *p,
c6744349 2512 struct diff_options *options)
6973dcae
JH
2513{
2514 int two_paths;
2515 char status[10];
2516 int abbrev = options->abbrev;
2517 const char *path_one, *path_two;
c6744349
TH
2518 int inter_name_termination = '\t';
2519 int line_termination = options->line_termination;
2520
2521 if (!line_termination)
2522 inter_name_termination = 0;
6973dcae
JH
2523
2524 path_one = p->one->path;
2525 path_two = p->two->path;
2526 if (line_termination) {
2527 path_one = quote_one(path_one);
2528 path_two = quote_one(path_two);
2529 }
2530
2531 if (p->score)
125b7630 2532 sprintf(status, "%c%03d", p->status, similarity_index(p));
6973dcae
JH
2533 else {
2534 status[0] = p->status;
2535 status[1] = 0;
2536 }
2537 switch (p->status) {
2538 case DIFF_STATUS_COPIED:
2539 case DIFF_STATUS_RENAMED:
2540 two_paths = 1;
2541 break;
2542 case DIFF_STATUS_ADDED:
2543 case DIFF_STATUS_DELETED:
2544 two_paths = 0;
2545 break;
2546 default:
2547 two_paths = 0;
2548 break;
2549 }
c6744349 2550 if (!(options->output_format & DIFF_FORMAT_NAME_STATUS)) {
6973dcae
JH
2551 printf(":%06o %06o %s ",
2552 p->one->mode, p->two->mode,
2553 diff_unique_abbrev(p->one->sha1, abbrev));
2554 printf("%s ",
2555 diff_unique_abbrev(p->two->sha1, abbrev));
2556 }
3cb56738
JS
2557 printf("%s%c%s", status, inter_name_termination,
2558 two_paths || p->one->mode ? path_one : path_two);
6973dcae
JH
2559 if (two_paths)
2560 printf("%c%s", inter_name_termination, path_two);
2561 putchar(line_termination);
2562 if (path_one != p->one->path)
2563 free((void*)path_one);
2564 if (path_two != p->two->path)
2565 free((void*)path_two);
2566}
2567
471efb09 2568static void diff_flush_name(struct diff_filepair *p, struct diff_options *opt)
6973dcae
JH
2569{
2570 char *path = p->two->path;
2571
471efb09 2572 if (opt->line_termination)
6973dcae 2573 path = quote_one(p->two->path);
471efb09 2574 printf("%s%c", path, opt->line_termination);
6973dcae
JH
2575 if (p->two->path != path)
2576 free(path);
2577}
2578
2579int diff_unmodified_pair(struct diff_filepair *p)
2580{
2581 /* This function is written stricter than necessary to support
2582 * the currently implemented transformers, but the idea is to
2583 * let transformers to produce diff_filepairs any way they want,
2584 * and filter and clean them up here before producing the output.
2585 */
2586 struct diff_filespec *one, *two;
2587
2588 if (DIFF_PAIR_UNMERGED(p))
2589 return 0; /* unmerged is interesting */
2590
2591 one = p->one;
2592 two = p->two;
2593
2594 /* deletion, addition, mode or type change
2595 * and rename are all interesting.
2596 */
2597 if (DIFF_FILE_VALID(one) != DIFF_FILE_VALID(two) ||
2598 DIFF_PAIR_MODE_CHANGED(p) ||
2599 strcmp(one->path, two->path))
2600 return 0;
2601
2602 /* both are valid and point at the same path. that is, we are
2603 * dealing with a change.
2604 */
2605 if (one->sha1_valid && two->sha1_valid &&
a89fccd2 2606 !hashcmp(one->sha1, two->sha1))
6973dcae
JH
2607 return 1; /* no change */
2608 if (!one->sha1_valid && !two->sha1_valid)
2609 return 1; /* both look at the same file on the filesystem. */
2610 return 0;
2611}
2612
2613static void diff_flush_patch(struct diff_filepair *p, struct diff_options *o)
2614{
2615 if (diff_unmodified_pair(p))
2616 return;
2617
2618 if ((DIFF_FILE_VALID(p->one) && S_ISDIR(p->one->mode)) ||
2619 (DIFF_FILE_VALID(p->two) && S_ISDIR(p->two->mode)))
2620 return; /* no tree diffs in patch format */
2621
2622 run_diff(p, o);
2623}
2624
2625static void diff_flush_stat(struct diff_filepair *p, struct diff_options *o,
2626 struct diffstat_t *diffstat)
2627{
2628 if (diff_unmodified_pair(p))
2629 return;
2630
2631 if ((DIFF_FILE_VALID(p->one) && S_ISDIR(p->one->mode)) ||
2632 (DIFF_FILE_VALID(p->two) && S_ISDIR(p->two->mode)))
2633 return; /* no tree diffs in patch format */
2634
2635 run_diffstat(p, o, diffstat);
2636}
2637
88246898
JS
2638static void diff_flush_checkdiff(struct diff_filepair *p,
2639 struct diff_options *o)
2640{
2641 if (diff_unmodified_pair(p))
2642 return;
2643
2644 if ((DIFF_FILE_VALID(p->one) && S_ISDIR(p->one->mode)) ||
2645 (DIFF_FILE_VALID(p->two) && S_ISDIR(p->two->mode)))
2646 return; /* no tree diffs in patch format */
2647
2648 run_checkdiff(p, o);
2649}
2650
6973dcae
JH
2651int diff_queue_is_empty(void)
2652{
2653 struct diff_queue_struct *q = &diff_queued_diff;
2654 int i;
2655 for (i = 0; i < q->nr; i++)
2656 if (!diff_unmodified_pair(q->queue[i]))
2657 return 0;
2658 return 1;
2659}
2660
2661#if DIFF_DEBUG
2662void diff_debug_filespec(struct diff_filespec *s, int x, const char *one)
2663{
2664 fprintf(stderr, "queue[%d] %s (%s) %s %06o %s\n",
2665 x, one ? one : "",
2666 s->path,
2667 DIFF_FILE_VALID(s) ? "valid" : "invalid",
2668 s->mode,
2669 s->sha1_valid ? sha1_to_hex(s->sha1) : "");
2670 fprintf(stderr, "queue[%d] %s size %lu flags %d\n",
2671 x, one ? one : "",
2672 s->size, s->xfrm_flags);
2673}
2674
2675void diff_debug_filepair(const struct diff_filepair *p, int i)
2676{
2677 diff_debug_filespec(p->one, i, "one");
2678 diff_debug_filespec(p->two, i, "two");
2679 fprintf(stderr, "score %d, status %c stays %d broken %d\n",
2680 p->score, p->status ? p->status : '?',
2681 p->source_stays, p->broken_pair);
2682}
2683
2684void diff_debug_queue(const char *msg, struct diff_queue_struct *q)
2685{
2686 int i;
2687 if (msg)
2688 fprintf(stderr, "%s\n", msg);
2689 fprintf(stderr, "q->nr = %d\n", q->nr);
2690 for (i = 0; i < q->nr; i++) {
2691 struct diff_filepair *p = q->queue[i];
2692 diff_debug_filepair(p, i);
2693 }
2694}
2695#endif
2696
2697static void diff_resolve_rename_copy(void)
2698{
2699 int i, j;
2700 struct diff_filepair *p, *pp;
2701 struct diff_queue_struct *q = &diff_queued_diff;
2702
2703 diff_debug_queue("resolve-rename-copy", q);
2704
2705 for (i = 0; i < q->nr; i++) {
2706 p = q->queue[i];
2707 p->status = 0; /* undecided */
2708 if (DIFF_PAIR_UNMERGED(p))
2709 p->status = DIFF_STATUS_UNMERGED;
2710 else if (!DIFF_FILE_VALID(p->one))
2711 p->status = DIFF_STATUS_ADDED;
2712 else if (!DIFF_FILE_VALID(p->two))
2713 p->status = DIFF_STATUS_DELETED;
2714 else if (DIFF_PAIR_TYPE_CHANGED(p))
2715 p->status = DIFF_STATUS_TYPE_CHANGED;
2716
2717 /* from this point on, we are dealing with a pair
2718 * whose both sides are valid and of the same type, i.e.
2719 * either in-place edit or rename/copy edit.
2720 */
2721 else if (DIFF_PAIR_RENAME(p)) {
2722 if (p->source_stays) {
2723 p->status = DIFF_STATUS_COPIED;
2724 continue;
2725 }
2726 /* See if there is some other filepair that
2727 * copies from the same source as us. If so
2728 * we are a copy. Otherwise we are either a
2729 * copy if the path stays, or a rename if it
2730 * does not, but we already handled "stays" case.
2731 */
2732 for (j = i + 1; j < q->nr; j++) {
2733 pp = q->queue[j];
2734 if (strcmp(pp->one->path, p->one->path))
2735 continue; /* not us */
2736 if (!DIFF_PAIR_RENAME(pp))
2737 continue; /* not a rename/copy */
2738 /* pp is a rename/copy from the same source */
2739 p->status = DIFF_STATUS_COPIED;
2740 break;
2741 }
2742 if (!p->status)
2743 p->status = DIFF_STATUS_RENAMED;
2744 }
a89fccd2 2745 else if (hashcmp(p->one->sha1, p->two->sha1) ||
d516c2d1
JS
2746 p->one->mode != p->two->mode ||
2747 is_null_sha1(p->one->sha1))
6973dcae
JH
2748 p->status = DIFF_STATUS_MODIFIED;
2749 else {
2750 /* This is a "no-change" entry and should not
2751 * happen anymore, but prepare for broken callers.
2752 */
2753 error("feeding unmodified %s to diffcore",
2754 p->one->path);
2755 p->status = DIFF_STATUS_UNKNOWN;
2756 }
2757 }
2758 diff_debug_queue("resolve-rename-copy done", q);
2759}
2760
c6744349 2761static int check_pair_status(struct diff_filepair *p)
6973dcae 2762{
6973dcae
JH
2763 switch (p->status) {
2764 case DIFF_STATUS_UNKNOWN:
c6744349 2765 return 0;
6973dcae
JH
2766 case 0:
2767 die("internal error in diff-resolve-rename-copy");
6973dcae 2768 default:
c6744349 2769 return 1;
6973dcae
JH
2770 }
2771}
2772
c6744349
TH
2773static void flush_one_pair(struct diff_filepair *p, struct diff_options *opt)
2774{
2775 int fmt = opt->output_format;
2776
2777 if (fmt & DIFF_FORMAT_CHECKDIFF)
2778 diff_flush_checkdiff(p, opt);
2779 else if (fmt & (DIFF_FORMAT_RAW | DIFF_FORMAT_NAME_STATUS))
2780 diff_flush_raw(p, opt);
2781 else if (fmt & DIFF_FORMAT_NAME)
471efb09 2782 diff_flush_name(p, opt);
c6744349
TH
2783}
2784
4bbd261b
SE
2785static void show_file_mode_name(const char *newdelete, struct diff_filespec *fs)
2786{
0d26a64e 2787 char *name = quote_one(fs->path);
4bbd261b 2788 if (fs->mode)
0d26a64e 2789 printf(" %s mode %06o %s\n", newdelete, fs->mode, name);
4bbd261b 2790 else
0d26a64e
AJ
2791 printf(" %s %s\n", newdelete, name);
2792 free(name);
4bbd261b
SE
2793}
2794
2795
2796static void show_mode_change(struct diff_filepair *p, int show_name)
2797{
2798 if (p->one->mode && p->two->mode && p->one->mode != p->two->mode) {
0d26a64e
AJ
2799 if (show_name) {
2800 char *name = quote_one(p->two->path);
4bbd261b 2801 printf(" mode change %06o => %06o %s\n",
0d26a64e
AJ
2802 p->one->mode, p->two->mode, name);
2803 free(name);
2804 }
4bbd261b
SE
2805 else
2806 printf(" mode change %06o => %06o\n",
2807 p->one->mode, p->two->mode);
2808 }
2809}
2810
2811static void show_rename_copy(const char *renamecopy, struct diff_filepair *p)
2812{
b9f44164 2813 char *names = pprint_rename(p->one->path, p->two->path);
4bbd261b 2814
125b7630 2815 printf(" %s %s (%d%%)\n", renamecopy, names, similarity_index(p));
b9f44164 2816 free(names);
4bbd261b
SE
2817 show_mode_change(p, 0);
2818}
2819
2820static void diff_summary(struct diff_filepair *p)
2821{
2822 switch(p->status) {
2823 case DIFF_STATUS_DELETED:
2824 show_file_mode_name("delete", p->one);
2825 break;
2826 case DIFF_STATUS_ADDED:
2827 show_file_mode_name("create", p->two);
2828 break;
2829 case DIFF_STATUS_COPIED:
2830 show_rename_copy("copy", p);
2831 break;
2832 case DIFF_STATUS_RENAMED:
2833 show_rename_copy("rename", p);
2834 break;
2835 default:
2836 if (p->score) {
0d26a64e
AJ
2837 char *name = quote_one(p->two->path);
2838 printf(" rewrite %s (%d%%)\n", name,
125b7630 2839 similarity_index(p));
0d26a64e 2840 free(name);
4bbd261b
SE
2841 show_mode_change(p, 0);
2842 } else show_mode_change(p, 1);
2843 break;
2844 }
2845}
2846
fcb3d0ad
JS
2847struct patch_id_t {
2848 struct xdiff_emit_state xm;
2849 SHA_CTX *ctx;
2850 int patchlen;
2851};
2852
2853static int remove_space(char *line, int len)
2854{
2855 int i;
2856 char *dst = line;
2857 unsigned char c;
2858
2859 for (i = 0; i < len; i++)
2860 if (!isspace((c = line[i])))
2861 *dst++ = c;
2862
2863 return dst - line;
2864}
2865
2866static void patch_id_consume(void *priv, char *line, unsigned long len)
2867{
2868 struct patch_id_t *data = priv;
2869 int new_len;
2870
2871 /* Ignore line numbers when computing the SHA1 of the patch */
cc44c765 2872 if (!prefixcmp(line, "@@ -"))
fcb3d0ad
JS
2873 return;
2874
2875 new_len = remove_space(line, len);
2876
2877 SHA1_Update(data->ctx, line, new_len);
2878 data->patchlen += new_len;
2879}
2880
2881/* returns 0 upon success, and writes result into sha1 */
2882static int diff_get_patch_id(struct diff_options *options, unsigned char *sha1)
2883{
2884 struct diff_queue_struct *q = &diff_queued_diff;
2885 int i;
2886 SHA_CTX ctx;
2887 struct patch_id_t data;
2888 char buffer[PATH_MAX * 4 + 20];
2889
2890 SHA1_Init(&ctx);
2891 memset(&data, 0, sizeof(struct patch_id_t));
2892 data.ctx = &ctx;
2893 data.xm.consume = patch_id_consume;
2894
2895 for (i = 0; i < q->nr; i++) {
2896 xpparam_t xpp;
2897 xdemitconf_t xecfg;
2898 xdemitcb_t ecb;
2899 mmfile_t mf1, mf2;
2900 struct diff_filepair *p = q->queue[i];
2901 int len1, len2;
2902
30b25010 2903 memset(&xecfg, 0, sizeof(xecfg));
fcb3d0ad
JS
2904 if (p->status == 0)
2905 return error("internal diff status error");
2906 if (p->status == DIFF_STATUS_UNKNOWN)
2907 continue;
2908 if (diff_unmodified_pair(p))
2909 continue;
2910 if ((DIFF_FILE_VALID(p->one) && S_ISDIR(p->one->mode)) ||
2911 (DIFF_FILE_VALID(p->two) && S_ISDIR(p->two->mode)))
2912 continue;
2913 if (DIFF_PAIR_UNMERGED(p))
2914 continue;
2915
2916 diff_fill_sha1_info(p->one);
2917 diff_fill_sha1_info(p->two);
2918 if (fill_mmfile(&mf1, p->one) < 0 ||
2919 fill_mmfile(&mf2, p->two) < 0)
2920 return error("unable to read files to diff");
2921
fcb3d0ad
JS
2922 len1 = remove_space(p->one->path, strlen(p->one->path));
2923 len2 = remove_space(p->two->path, strlen(p->two->path));
2924 if (p->one->mode == 0)
2925 len1 = snprintf(buffer, sizeof(buffer),
2926 "diff--gita/%.*sb/%.*s"
2927 "newfilemode%06o"
2928 "---/dev/null"
2929 "+++b/%.*s",
2930 len1, p->one->path,
2931 len2, p->two->path,
2932 p->two->mode,
2933 len2, p->two->path);
2934 else if (p->two->mode == 0)
2935 len1 = snprintf(buffer, sizeof(buffer),
2936 "diff--gita/%.*sb/%.*s"
2937 "deletedfilemode%06o"
2938 "---a/%.*s"
2939 "+++/dev/null",
2940 len1, p->one->path,
2941 len2, p->two->path,
2942 p->one->mode,
2943 len1, p->one->path);
2944 else
2945 len1 = snprintf(buffer, sizeof(buffer),
2946 "diff--gita/%.*sb/%.*s"
2947 "---a/%.*s"
2948 "+++b/%.*s",
2949 len1, p->one->path,
2950 len2, p->two->path,
2951 len1, p->one->path,
2952 len2, p->two->path);
2953 SHA1_Update(&ctx, buffer, len1);
2954
2955 xpp.flags = XDF_NEED_MINIMAL;
2956 xecfg.ctxlen = 3;
9fdc3bb5 2957 xecfg.flags = XDL_EMIT_FUNCNAMES;
fcb3d0ad
JS
2958 ecb.outf = xdiff_outf;
2959 ecb.priv = &data;
2960 xdl_diff(&mf1, &mf2, &xpp, &xecfg, &ecb);
2961 }
2962
2963 SHA1_Final(sha1, &ctx);
2964 return 0;
2965}
2966
2967int diff_flush_patch_id(struct diff_options *options, unsigned char *sha1)
2968{
2969 struct diff_queue_struct *q = &diff_queued_diff;
2970 int i;
2971 int result = diff_get_patch_id(options, sha1);
2972
2973 for (i = 0; i < q->nr; i++)
2974 diff_free_filepair(q->queue[i]);
2975
2976 free(q->queue);
2977 q->queue = NULL;
2978 q->nr = q->alloc = 0;
2979
2980 return result;
2981}
2982
946c3784 2983static int is_summary_empty(const struct diff_queue_struct *q)
6973dcae 2984{
6973dcae 2985 int i;
6973dcae 2986
946c3784
TH
2987 for (i = 0; i < q->nr; i++) {
2988 const struct diff_filepair *p = q->queue[i];
2989
2990 switch (p->status) {
2991 case DIFF_STATUS_DELETED:
2992 case DIFF_STATUS_ADDED:
2993 case DIFF_STATUS_COPIED:
2994 case DIFF_STATUS_RENAMED:
2995 return 0;
2996 default:
2997 if (p->score)
2998 return 0;
2999 if (p->one->mode && p->two->mode &&
3000 p->one->mode != p->two->mode)
3001 return 0;
3002 break;
3003 }
6973dcae 3004 }
946c3784
TH
3005 return 1;
3006}
3007
6973dcae
JH
3008void diff_flush(struct diff_options *options)
3009{
3010 struct diff_queue_struct *q = &diff_queued_diff;
c6744349 3011 int i, output_format = options->output_format;
946c3784 3012 int separator = 0;
6973dcae 3013
c6744349
TH
3014 /*
3015 * Order: raw, stat, summary, patch
3016 * or: name/name-status/checkdiff (other bits clear)
3017 */
946c3784
TH
3018 if (!q->nr)
3019 goto free_queue;
6973dcae 3020
c6744349
TH
3021 if (output_format & (DIFF_FORMAT_RAW |
3022 DIFF_FORMAT_NAME |
3023 DIFF_FORMAT_NAME_STATUS |
3024 DIFF_FORMAT_CHECKDIFF)) {
6973dcae
JH
3025 for (i = 0; i < q->nr; i++) {
3026 struct diff_filepair *p = q->queue[i];
c6744349
TH
3027 if (check_pair_status(p))
3028 flush_one_pair(p, options);
6973dcae 3029 }
946c3784 3030 separator++;
6973dcae 3031 }
c6744349 3032
ebd124c6 3033 if (output_format & (DIFF_FORMAT_DIFFSTAT|DIFF_FORMAT_SHORTSTAT|DIFF_FORMAT_NUMSTAT)) {
5e2b0636 3034 struct diffstat_t diffstat;
c6744349 3035
5e2b0636
TH
3036 memset(&diffstat, 0, sizeof(struct diffstat_t));
3037 diffstat.xm.consume = diffstat_consume;
6973dcae
JH
3038 for (i = 0; i < q->nr; i++) {
3039 struct diff_filepair *p = q->queue[i];
c6744349 3040 if (check_pair_status(p))
5e2b0636 3041 diff_flush_stat(p, options, &diffstat);
6973dcae 3042 }
74e2abe5
JH
3043 if (output_format & DIFF_FORMAT_NUMSTAT)
3044 show_numstat(&diffstat, options);
3045 if (output_format & DIFF_FORMAT_DIFFSTAT)
3046 show_stats(&diffstat, options);
ebd124c6
NP
3047 else if (output_format & DIFF_FORMAT_SHORTSTAT)
3048 show_shortstats(&diffstat);
3969cf7d 3049 separator++;
6973dcae
JH
3050 }
3051
946c3784 3052 if (output_format & DIFF_FORMAT_SUMMARY && !is_summary_empty(q)) {
c6744349
TH
3053 for (i = 0; i < q->nr; i++)
3054 diff_summary(q->queue[i]);
3969cf7d 3055 separator++;
6973dcae
JH
3056 }
3057
c6744349 3058 if (output_format & DIFF_FORMAT_PATCH) {
946c3784
TH
3059 if (separator) {
3060 if (options->stat_sep) {
3061 /* attach patch instead of inline */
c6744349 3062 fputs(options->stat_sep, stdout);
946c3784 3063 } else {
c6744349 3064 putchar(options->line_termination);
946c3784 3065 }
c6744349
TH
3066 }
3067
3068 for (i = 0; i < q->nr; i++) {
3069 struct diff_filepair *p = q->queue[i];
3070 if (check_pair_status(p))
3071 diff_flush_patch(p, options);
3072 }
4bbd261b
SE
3073 }
3074
04245581
JK
3075 if (output_format & DIFF_FORMAT_CALLBACK)
3076 options->format_callback(q, options, options->format_callback_data);
3077
c6744349
TH
3078 for (i = 0; i < q->nr; i++)
3079 diff_free_filepair(q->queue[i]);
946c3784 3080free_queue:
6973dcae
JH
3081 free(q->queue);
3082 q->queue = NULL;
3083 q->nr = q->alloc = 0;
3084}
3085
3086static void diffcore_apply_filter(const char *filter)
3087{
3088 int i;
3089 struct diff_queue_struct *q = &diff_queued_diff;
3090 struct diff_queue_struct outq;
3091 outq.queue = NULL;
3092 outq.nr = outq.alloc = 0;
3093
3094 if (!filter)
3095 return;
3096
3097 if (strchr(filter, DIFF_STATUS_FILTER_AON)) {
3098 int found;
3099 for (i = found = 0; !found && i < q->nr; i++) {
3100 struct diff_filepair *p = q->queue[i];
3101 if (((p->status == DIFF_STATUS_MODIFIED) &&
3102 ((p->score &&
3103 strchr(filter, DIFF_STATUS_FILTER_BROKEN)) ||
3104 (!p->score &&
3105 strchr(filter, DIFF_STATUS_MODIFIED)))) ||
3106 ((p->status != DIFF_STATUS_MODIFIED) &&
3107 strchr(filter, p->status)))
3108 found++;
3109 }
3110 if (found)
3111 return;
3112
3113 /* otherwise we will clear the whole queue
3114 * by copying the empty outq at the end of this
3115 * function, but first clear the current entries
3116 * in the queue.
3117 */
3118 for (i = 0; i < q->nr; i++)
3119 diff_free_filepair(q->queue[i]);
3120 }
3121 else {
3122 /* Only the matching ones */
3123 for (i = 0; i < q->nr; i++) {
3124 struct diff_filepair *p = q->queue[i];
3125
3126 if (((p->status == DIFF_STATUS_MODIFIED) &&
3127 ((p->score &&
3128 strchr(filter, DIFF_STATUS_FILTER_BROKEN)) ||
3129 (!p->score &&
3130 strchr(filter, DIFF_STATUS_MODIFIED)))) ||
3131 ((p->status != DIFF_STATUS_MODIFIED) &&
3132 strchr(filter, p->status)))
3133 diff_q(&outq, p);
3134 else
3135 diff_free_filepair(p);
3136 }
3137 }
3138 free(q->queue);
3139 *q = outq;
3140}
3141
fb13227e
JH
3142static void diffcore_skip_stat_unmatch(struct diff_options *diffopt)
3143{
3144 int i;
3145 struct diff_queue_struct *q = &diff_queued_diff;
3146 struct diff_queue_struct outq;
3147 outq.queue = NULL;
3148 outq.nr = outq.alloc = 0;
3149
3150 for (i = 0; i < q->nr; i++) {
3151 struct diff_filepair *p = q->queue[i];
3152
3153 /*
3154 * 1. Entries that come from stat info dirtyness
3155 * always have both sides (iow, not create/delete),
3156 * one side of the object name is unknown, with
3157 * the same mode and size. Keep the ones that
3158 * do not match these criteria. They have real
3159 * differences.
3160 *
3161 * 2. At this point, the file is known to be modified,
3162 * with the same mode and size, and the object
3163 * name of one side is unknown. Need to inspect
3164 * the identical contents.
3165 */
3166 if (!DIFF_FILE_VALID(p->one) || /* (1) */
3167 !DIFF_FILE_VALID(p->two) ||
3168 (p->one->sha1_valid && p->two->sha1_valid) ||
3169 (p->one->mode != p->two->mode) ||
3170 diff_populate_filespec(p->one, 1) ||
3171 diff_populate_filespec(p->two, 1) ||
3172 (p->one->size != p->two->size) ||
3173
3174 diff_populate_filespec(p->one, 0) || /* (2) */
3175 diff_populate_filespec(p->two, 0) ||
3176 memcmp(p->one->data, p->two->data, p->one->size))
3177 diff_q(&outq, p);
3178 else {
3179 /*
3180 * The caller can subtract 1 from skip_stat_unmatch
3181 * to determine how many paths were dirty only
3182 * due to stat info mismatch.
3183 */
6d2d9e86
RS
3184 if (!diffopt->no_index)
3185 diffopt->skip_stat_unmatch++;
fb13227e
JH
3186 diff_free_filepair(p);
3187 }
3188 }
3189 free(q->queue);
3190 *q = outq;
3191}
3192
6973dcae
JH
3193void diffcore_std(struct diff_options *options)
3194{
68aacb2f
JH
3195 if (options->quiet)
3196 return;
706098af 3197
fb13227e
JH
3198 if (options->skip_stat_unmatch && !options->find_copies_harder)
3199 diffcore_skip_stat_unmatch(options);
6973dcae
JH
3200 if (options->break_opt != -1)
3201 diffcore_break(options->break_opt);
3202 if (options->detect_rename)
3203 diffcore_rename(options);
3204 if (options->break_opt != -1)
3205 diffcore_merge_broken();
3206 if (options->pickaxe)
3207 diffcore_pickaxe(options->pickaxe, options->pickaxe_opts);
3208 if (options->orderfile)
3209 diffcore_order(options->orderfile);
3210 diff_resolve_rename_copy();
3211 diffcore_apply_filter(options->filter);
68aacb2f
JH
3212
3213 options->has_changes = !!diff_queued_diff.nr;
6973dcae
JH
3214}
3215
3216
6973dcae
JH
3217void diff_addremove(struct diff_options *options,
3218 int addremove, unsigned mode,
3219 const unsigned char *sha1,
3220 const char *base, const char *path)
3221{
3222 char concatpath[PATH_MAX];
3223 struct diff_filespec *one, *two;
3224
3225 /* This may look odd, but it is a preparation for
3226 * feeding "there are unchanged files which should
3227 * not produce diffs, but when you are doing copy
3228 * detection you would need them, so here they are"
3229 * entries to the diff-core. They will be prefixed
3230 * with something like '=' or '*' (I haven't decided
3231 * which but should not make any difference).
a6080a0a 3232 * Feeding the same new and old to diff_change()
6973dcae
JH
3233 * also has the same effect.
3234 * Before the final output happens, they are pruned after
3235 * merged into rename/copy pairs as appropriate.
3236 */
3237 if (options->reverse_diff)
3238 addremove = (addremove == '+' ? '-' :
3239 addremove == '-' ? '+' : addremove);
3240
3241 if (!path) path = "";
3242 sprintf(concatpath, "%s%s", base, path);
3243 one = alloc_filespec(concatpath);
3244 two = alloc_filespec(concatpath);
3245
3246 if (addremove != '+')
3247 fill_filespec(one, sha1, mode);
3248 if (addremove != '-')
3249 fill_filespec(two, sha1, mode);
3250
3251 diff_queue(&diff_queued_diff, one, two);
68aacb2f 3252 options->has_changes = 1;
6973dcae
JH
3253}
3254
3255void diff_change(struct diff_options *options,
3256 unsigned old_mode, unsigned new_mode,
3257 const unsigned char *old_sha1,
3258 const unsigned char *new_sha1,
a6080a0a 3259 const char *base, const char *path)
6973dcae
JH
3260{
3261 char concatpath[PATH_MAX];
3262 struct diff_filespec *one, *two;
3263
3264 if (options->reverse_diff) {
3265 unsigned tmp;
3266 const unsigned char *tmp_c;
3267 tmp = old_mode; old_mode = new_mode; new_mode = tmp;
3268 tmp_c = old_sha1; old_sha1 = new_sha1; new_sha1 = tmp_c;
3269 }
3270 if (!path) path = "";
3271 sprintf(concatpath, "%s%s", base, path);
3272 one = alloc_filespec(concatpath);
3273 two = alloc_filespec(concatpath);
3274 fill_filespec(one, old_sha1, old_mode);
3275 fill_filespec(two, new_sha1, new_mode);
3276
3277 diff_queue(&diff_queued_diff, one, two);
68aacb2f 3278 options->has_changes = 1;
6973dcae
JH
3279}
3280
3281void diff_unmerge(struct diff_options *options,
e9c84099
JH
3282 const char *path,
3283 unsigned mode, const unsigned char *sha1)
6973dcae
JH
3284{
3285 struct diff_filespec *one, *two;
3286 one = alloc_filespec(path);
3287 two = alloc_filespec(path);
e9c84099
JH
3288 fill_filespec(one, sha1, mode);
3289 diff_queue(&diff_queued_diff, one, two)->is_unmerged = 1;
6973dcae 3290}