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