]> git.ipfire.org Git - thirdparty/git.git/blame - diff.c
Output the graph columns at the end of the commit message
[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"
d5535ec7 12#include "run-command.h"
23707811 13#include "utf8.h"
be58e70d 14#include "userdiff.h"
4a16d072 15#include "sigchain.h"
752c0c24 16#include "submodule.h"
a757c646 17#include "ll-merge.h"
6973dcae 18
1510fea7
SP
19#ifdef NO_FAST_WORKING_DIRECTORY
20#define FAST_WORKING_DIRECTORY 0
21#else
22#define FAST_WORKING_DIRECTORY 1
23#endif
24
96f1e58f 25static int diff_detect_rename_default;
50705915 26static int diff_rename_limit_default = 200;
a624eaa7 27static int diff_suppress_blank_empty;
6b2f2d98 28int diff_use_color_default = -1;
98a4d87b 29static const char *diff_word_regex_cfg;
cbe02100 30static const char *external_diff_cmd_cfg;
aecbf914 31int diff_auto_refresh_index = 1;
a5a818ee 32static int diff_mnemonic_prefix;
6973dcae 33
7c92fe0e 34static char diff_colors[][COLOR_MAXLEN] = {
dc6ebd4c
AL
35 GIT_COLOR_RESET,
36 GIT_COLOR_NORMAL, /* PLAIN */
37 GIT_COLOR_BOLD, /* METAINFO */
38 GIT_COLOR_CYAN, /* FRAGINFO */
39 GIT_COLOR_RED, /* OLD */
40 GIT_COLOR_GREEN, /* NEW */
41 GIT_COLOR_YELLOW, /* COMMIT */
42 GIT_COLOR_BG_RED, /* WHITESPACE */
89cb73a1 43 GIT_COLOR_NORMAL, /* FUNCINFO */
cd112cef
JS
44};
45
9cb92c39 46static void diff_filespec_load_driver(struct diff_filespec *one);
d9bae1a1 47static size_t fill_textconv(struct userdiff_driver *driver,
840383b2 48 struct diff_filespec *df, char **outbuf);
9cb92c39 49
801235c5
JH
50static int parse_diff_color_slot(const char *var, int ofs)
51{
52 if (!strcasecmp(var+ofs, "plain"))
53 return DIFF_PLAIN;
54 if (!strcasecmp(var+ofs, "meta"))
55 return DIFF_METAINFO;
56 if (!strcasecmp(var+ofs, "frag"))
57 return DIFF_FRAGINFO;
58 if (!strcasecmp(var+ofs, "old"))
59 return DIFF_FILE_OLD;
60 if (!strcasecmp(var+ofs, "new"))
61 return DIFF_FILE_NEW;
ce436973
JK
62 if (!strcasecmp(var+ofs, "commit"))
63 return DIFF_COMMIT;
448c3ef1
JH
64 if (!strcasecmp(var+ofs, "whitespace"))
65 return DIFF_WHITESPACE;
89cb73a1
BW
66 if (!strcasecmp(var+ofs, "func"))
67 return DIFF_FUNCINFO;
8b8e8624 68 return -1;
801235c5
JH
69}
70
cced5fbc
LT
71static int git_config_rename(const char *var, const char *value)
72{
73 if (!value)
74 return DIFF_DETECT_RENAME;
75 if (!strcasecmp(value, "copies") || !strcasecmp(value, "copy"))
76 return DIFF_DETECT_COPY;
77 return git_config_bool(var,value) ? DIFF_DETECT_RENAME : 0;
78}
79
83ad63cf
JH
80/*
81 * These are to give UI layer defaults.
82 * The core-level commands such as git-diff-files should
83 * never be affected by the setting of diff.renames
84 * the user happens to have in the configuration file.
85 */
ef90d6d4 86int git_diff_ui_config(const char *var, const char *value, void *cb)
801235c5 87{
a159ca0c 88 if (!strcmp(var, "diff.color") || !strcmp(var, "color.diff")) {
0f6f5a40 89 diff_use_color_default = git_config_colorbool(var, value, -1);
801235c5
JH
90 return 0;
91 }
b68ea12e 92 if (!strcmp(var, "diff.renames")) {
cced5fbc 93 diff_detect_rename_default = git_config_rename(var, value);
b68ea12e
EW
94 return 0;
95 }
aecbf914
JH
96 if (!strcmp(var, "diff.autorefreshindex")) {
97 diff_auto_refresh_index = git_config_bool(var, value);
98 return 0;
99 }
a5a818ee
JH
100 if (!strcmp(var, "diff.mnemonicprefix")) {
101 diff_mnemonic_prefix = git_config_bool(var, value);
102 return 0;
103 }
daec808c
BH
104 if (!strcmp(var, "diff.external"))
105 return git_config_string(&external_diff_cmd_cfg, var, value);
98a4d87b
BSSJ
106 if (!strcmp(var, "diff.wordregex"))
107 return git_config_string(&diff_word_regex_cfg, var, value);
f1af60bd 108
ef90d6d4 109 return git_diff_basic_config(var, value, cb);
9a1805a8
JK
110}
111
ef90d6d4 112int git_diff_basic_config(const char *var, const char *value, void *cb)
9a1805a8 113{
2b6ca6df
LT
114 if (!strcmp(var, "diff.renamelimit")) {
115 diff_rename_limit_default = git_config_int(var, value);
116 return 0;
117 }
118
c7534ef4
JK
119 switch (userdiff_config(var, value)) {
120 case 0: break;
121 case -1: return -1;
122 default: return 0;
123 }
124
1968d77d 125 if (!prefixcmp(var, "diff.color.") || !prefixcmp(var, "color.diff.")) {
801235c5 126 int slot = parse_diff_color_slot(var, 11);
8b8e8624
JK
127 if (slot < 0)
128 return 0;
64f30e94
JH
129 if (!value)
130 return config_error_nonbool(var);
7c92fe0e 131 color_parse(value, var, diff_colors[slot]);
801235c5
JH
132 return 0;
133 }
f1af60bd 134
a624eaa7 135 /* like GNU diff's --suppress-blank-empty option */
950db879
JS
136 if (!strcmp(var, "diff.suppressblankempty") ||
137 /* for backwards compatibility */
138 !strcmp(var, "diff.suppress-blank-empty")) {
a624eaa7
JM
139 diff_suppress_blank_empty = git_config_bool(var, value);
140 return 0;
141 }
142
ef90d6d4 143 return git_color_default_config(var, value, cb);
801235c5
JH
144}
145
6973dcae
JH
146static char *quote_two(const char *one, const char *two)
147{
148 int need_one = quote_c_style(one, NULL, NULL, 1);
149 int need_two = quote_c_style(two, NULL, NULL, 1);
f285a2d7 150 struct strbuf res = STRBUF_INIT;
6973dcae
JH
151
152 if (need_one + need_two) {
663af342
PH
153 strbuf_addch(&res, '"');
154 quote_c_style(one, &res, NULL, 1);
155 quote_c_style(two, &res, NULL, 1);
156 strbuf_addch(&res, '"');
157 } else {
158 strbuf_addstr(&res, one);
159 strbuf_addstr(&res, two);
6973dcae 160 }
b315c5c0 161 return strbuf_detach(&res, NULL);
6973dcae
JH
162}
163
164static const char *external_diff(void)
165{
166 static const char *external_diff_cmd = NULL;
167 static int done_preparing = 0;
168
169 if (done_preparing)
170 return external_diff_cmd;
171 external_diff_cmd = getenv("GIT_EXTERNAL_DIFF");
cbe02100
JS
172 if (!external_diff_cmd)
173 external_diff_cmd = external_diff_cmd_cfg;
6973dcae
JH
174 done_preparing = 1;
175 return external_diff_cmd;
176}
177
6973dcae
JH
178static struct diff_tempfile {
179 const char *name; /* filename external diff should read from */
180 char hex[41];
181 char mode[10];
1472966c 182 char tmp_path[PATH_MAX];
6973dcae
JH
183} diff_temp[2];
184
6957eb9a
JH
185typedef unsigned long (*sane_truncate_fn)(char *line, unsigned long len);
186
187struct emit_callback {
6957eb9a
JH
188 int color_diff;
189 unsigned ws_rule;
190 int blank_at_eof_in_preimage;
191 int blank_at_eof_in_postimage;
192 int lno_in_preimage;
193 int lno_in_postimage;
194 sane_truncate_fn truncate;
195 const char **label_path;
196 struct diff_words_data *diff_words;
a3c158d4 197 struct diff_options *opt;
6957eb9a 198 int *found_changesp;
3e97c7c6 199 struct strbuf *header;
6957eb9a
JH
200};
201
6973dcae
JH
202static int count_lines(const char *data, int size)
203{
204 int count, ch, completely_empty = 1, nl_just_seen = 0;
205 count = 0;
206 while (0 < size--) {
207 ch = *data++;
208 if (ch == '\n') {
209 count++;
210 nl_just_seen = 1;
211 completely_empty = 0;
212 }
213 else {
214 nl_just_seen = 0;
215 completely_empty = 0;
216 }
217 }
218 if (completely_empty)
219 return 0;
220 if (!nl_just_seen)
221 count++; /* no trailing newline */
222 return count;
223}
224
6957eb9a
JH
225static int fill_mmfile(mmfile_t *mf, struct diff_filespec *one)
226{
227 if (!DIFF_FILE_VALID(one)) {
228 mf->ptr = (char *)""; /* does not matter */
229 mf->size = 0;
230 return 0;
231 }
232 else if (diff_populate_filespec(one, 0))
233 return -1;
bb35fefb 234
6957eb9a
JH
235 mf->ptr = one->data;
236 mf->size = one->size;
237 return 0;
238}
239
240static int count_trailing_blank(mmfile_t *mf, unsigned ws_rule)
241{
242 char *ptr = mf->ptr;
243 long size = mf->size;
244 int cnt = 0;
245
246 if (!size)
247 return cnt;
248 ptr += size - 1; /* pointing at the very end */
249 if (*ptr != '\n')
250 ; /* incomplete line */
251 else
252 ptr--; /* skip the last LF */
253 while (mf->ptr < ptr) {
254 char *prev_eol;
255 for (prev_eol = ptr; mf->ptr <= prev_eol; prev_eol--)
256 if (*prev_eol == '\n')
257 break;
258 if (!ws_blank_line(prev_eol + 1, ptr - prev_eol, ws_rule))
259 break;
260 cnt++;
261 ptr = prev_eol - 1;
262 }
263 return cnt;
264}
265
266static void check_blank_at_eof(mmfile_t *mf1, mmfile_t *mf2,
267 struct emit_callback *ecbdata)
268{
269 int l1, l2, at;
270 unsigned ws_rule = ecbdata->ws_rule;
271 l1 = count_trailing_blank(mf1, ws_rule);
272 l2 = count_trailing_blank(mf2, ws_rule);
273 if (l2 <= l1) {
274 ecbdata->blank_at_eof_in_preimage = 0;
275 ecbdata->blank_at_eof_in_postimage = 0;
276 return;
277 }
278 at = count_lines(mf1->ptr, mf1->size);
279 ecbdata->blank_at_eof_in_preimage = (at - l1) + 1;
280
281 at = count_lines(mf2->ptr, mf2->size);
282 ecbdata->blank_at_eof_in_postimage = (at - l2) + 1;
283}
284
a3c158d4 285static void emit_line_0(struct diff_options *o, const char *set, const char *reset,
250f7993 286 int first, const char *line, int len)
6957eb9a
JH
287{
288 int has_trailing_newline, has_trailing_carriage_return;
250f7993 289 int nofirst;
a3c158d4
BY
290 FILE *file = o->file;
291
292 if (o->output_prefix) {
293 struct strbuf *msg = NULL;
294 msg = o->output_prefix(o, o->output_prefix_data);
295 assert(msg);
296 fwrite(msg->buf, msg->len, 1, file);
297 }
6957eb9a 298
250f7993
JH
299 if (len == 0) {
300 has_trailing_newline = (first == '\n');
301 has_trailing_carriage_return = (!has_trailing_newline &&
302 (first == '\r'));
303 nofirst = has_trailing_newline || has_trailing_carriage_return;
304 } else {
305 has_trailing_newline = (len > 0 && line[len-1] == '\n');
306 if (has_trailing_newline)
307 len--;
308 has_trailing_carriage_return = (len > 0 && line[len-1] == '\r');
309 if (has_trailing_carriage_return)
310 len--;
311 nofirst = 0;
312 }
6957eb9a 313
06a47552
JH
314 if (len || !nofirst) {
315 fputs(set, file);
316 if (!nofirst)
317 fputc(first, file);
318 fwrite(line, len, 1, file);
319 fputs(reset, file);
320 }
6957eb9a
JH
321 if (has_trailing_carriage_return)
322 fputc('\r', file);
323 if (has_trailing_newline)
324 fputc('\n', file);
325}
326
a3c158d4 327static void emit_line(struct diff_options *o, const char *set, const char *reset,
250f7993
JH
328 const char *line, int len)
329{
a3c158d4 330 emit_line_0(o, set, reset, line[0], line+1, len-1);
250f7993
JH
331}
332
6957eb9a
JH
333static int new_blank_line_at_eof(struct emit_callback *ecbdata, const char *line, int len)
334{
335 if (!((ecbdata->ws_rule & WS_BLANK_AT_EOF) &&
336 ecbdata->blank_at_eof_in_preimage &&
337 ecbdata->blank_at_eof_in_postimage &&
338 ecbdata->blank_at_eof_in_preimage <= ecbdata->lno_in_preimage &&
339 ecbdata->blank_at_eof_in_postimage <= ecbdata->lno_in_postimage))
340 return 0;
018cff70 341 return ws_blank_line(line, len, ecbdata->ws_rule);
6957eb9a
JH
342}
343
018cff70
JH
344static void emit_add_line(const char *reset,
345 struct emit_callback *ecbdata,
346 const char *line, int len)
6957eb9a
JH
347{
348 const char *ws = diff_get_color(ecbdata->color_diff, DIFF_WHITESPACE);
349 const char *set = diff_get_color(ecbdata->color_diff, DIFF_FILE_NEW);
350
351 if (!*ws)
a3c158d4 352 emit_line_0(ecbdata->opt, set, reset, '+', line, len);
6957eb9a
JH
353 else if (new_blank_line_at_eof(ecbdata, line, len))
354 /* Blank line at EOF - paint '+' as well */
a3c158d4 355 emit_line_0(ecbdata->opt, ws, reset, '+', line, len);
6957eb9a
JH
356 else {
357 /* Emit just the prefix, then the rest. */
a3c158d4 358 emit_line_0(ecbdata->opt, set, reset, '+', "", 0);
018cff70 359 ws_check_emit(line, len, ecbdata->ws_rule,
a3c158d4 360 ecbdata->opt->file, set, reset, ws);
6957eb9a
JH
361 }
362}
363
89cb73a1
BW
364static void emit_hunk_header(struct emit_callback *ecbdata,
365 const char *line, int len)
366{
367 const char *plain = diff_get_color(ecbdata->color_diff, DIFF_PLAIN);
368 const char *frag = diff_get_color(ecbdata->color_diff, DIFF_FRAGINFO);
369 const char *func = diff_get_color(ecbdata->color_diff, DIFF_FUNCINFO);
370 const char *reset = diff_get_color(ecbdata->color_diff, DIFF_RESET);
371 static const char atat[2] = { '@', '@' };
372 const char *cp, *ep;
373
374 /*
375 * As a hunk header must begin with "@@ -<old>, +<new> @@",
376 * it always is at least 10 bytes long.
377 */
378 if (len < 10 ||
379 memcmp(line, atat, 2) ||
380 !(ep = memmem(line + 2, len - 2, atat, 2))) {
a3c158d4 381 emit_line(ecbdata->opt, plain, reset, line, len);
89cb73a1
BW
382 return;
383 }
384 ep += 2; /* skip over @@ */
385
386 /* The hunk header in fraginfo color */
a3c158d4 387 emit_line(ecbdata->opt, frag, reset, line, ep - line);
89cb73a1
BW
388
389 /* blank before the func header */
390 for (cp = ep; ep - line < len; ep++)
391 if (*ep != ' ' && *ep != '\t')
392 break;
393 if (ep != cp)
a3c158d4 394 emit_line(ecbdata->opt, plain, reset, cp, ep - cp);
89cb73a1
BW
395
396 if (ep < line + len)
a3c158d4 397 emit_line(ecbdata->opt, func, reset, ep, line + len - ep);
89cb73a1
BW
398}
399
479b0ae8
JK
400static struct diff_tempfile *claim_diff_tempfile(void) {
401 int i;
402 for (i = 0; i < ARRAY_SIZE(diff_temp); i++)
403 if (!diff_temp[i].name)
404 return diff_temp + i;
405 die("BUG: diff is failing to clean up its tempfiles");
406}
407
408static int remove_tempfile_installed;
409
410static void remove_tempfile(void)
411{
412 int i;
a8344abe
NR
413 for (i = 0; i < ARRAY_SIZE(diff_temp); i++) {
414 if (diff_temp[i].name == diff_temp[i].tmp_path)
691f1a28 415 unlink_or_warn(diff_temp[i].name);
a8344abe
NR
416 diff_temp[i].name = NULL;
417 }
479b0ae8
JK
418}
419
420static void remove_tempfile_on_signal(int signo)
421{
422 remove_tempfile();
4a16d072 423 sigchain_pop(signo);
479b0ae8
JK
424 raise(signo);
425}
426
c0c77734 427static void print_line_count(FILE *file, int count)
6973dcae
JH
428{
429 switch (count) {
430 case 0:
c0c77734 431 fprintf(file, "0,0");
6973dcae
JH
432 break;
433 case 1:
c0c77734 434 fprintf(file, "1");
6973dcae
JH
435 break;
436 default:
c0c77734 437 fprintf(file, "1,%d", count);
6973dcae
JH
438 break;
439 }
440}
441
7f7ee2ff
JH
442static void emit_rewrite_lines(struct emit_callback *ecb,
443 int prefix, const char *data, int size)
6973dcae 444{
7f7ee2ff
JH
445 const char *endp = NULL;
446 static const char *nneof = " No newline at end of file\n";
447 const char *old = diff_get_color(ecb->color_diff, DIFF_FILE_OLD);
448 const char *reset = diff_get_color(ecb->color_diff, DIFF_RESET);
449
450 while (0 < size) {
451 int len;
452
453 endp = memchr(data, '\n', size);
454 len = endp ? (endp - data + 1) : size;
455 if (prefix != '+') {
456 ecb->lno_in_preimage++;
a3c158d4 457 emit_line_0(ecb->opt, old, reset, '-',
7f7ee2ff
JH
458 data, len);
459 } else {
460 ecb->lno_in_postimage++;
461 emit_add_line(reset, ecb, data, len);
13e36ec5 462 }
7f7ee2ff
JH
463 size -= len;
464 data += len;
465 }
466 if (!endp) {
467 const char *plain = diff_get_color(ecb->color_diff,
468 DIFF_PLAIN);
a3c158d4 469 emit_line_0(ecb->opt, plain, reset, '\\',
7f7ee2ff 470 nneof, strlen(nneof));
6973dcae 471 }
6973dcae
JH
472}
473
474static void emit_rewrite_diff(const char *name_a,
475 const char *name_b,
476 struct diff_filespec *one,
13e36ec5 477 struct diff_filespec *two,
d9bae1a1
JK
478 struct userdiff_driver *textconv_one,
479 struct userdiff_driver *textconv_two,
eab9a40b 480 struct diff_options *o)
6973dcae
JH
481{
482 int lc_a, lc_b;
eab9a40b 483 int color_diff = DIFF_OPT_TST(o, COLOR_DIFF);
1a9eb3b9 484 const char *name_a_tab, *name_b_tab;
13e36ec5
JS
485 const char *metainfo = diff_get_color(color_diff, DIFF_METAINFO);
486 const char *fraginfo = diff_get_color(color_diff, DIFF_FRAGINFO);
13e36ec5 487 const char *reset = diff_get_color(color_diff, DIFF_RESET);
d5625091 488 static struct strbuf a_name = STRBUF_INIT, b_name = STRBUF_INIT;
a5a818ee 489 const char *a_prefix, *b_prefix;
840383b2 490 char *data_one, *data_two;
3aa1f7ca 491 size_t size_one, size_two;
7f7ee2ff 492 struct emit_callback ecbdata;
a5a818ee
JH
493
494 if (diff_mnemonic_prefix && DIFF_OPT_TST(o, REVERSE_DIFF)) {
495 a_prefix = o->b_prefix;
496 b_prefix = o->a_prefix;
497 } else {
498 a_prefix = o->a_prefix;
499 b_prefix = o->b_prefix;
500 }
1a9eb3b9 501
8a13becc
JH
502 name_a += (*name_a == '/');
503 name_b += (*name_b == '/');
1a9eb3b9
JH
504 name_a_tab = strchr(name_a, ' ') ? "\t" : "";
505 name_b_tab = strchr(name_b, ' ') ? "\t" : "";
506
d5625091
JH
507 strbuf_reset(&a_name);
508 strbuf_reset(&b_name);
a5a818ee
JH
509 quote_two_c_style(&a_name, a_prefix, name_a, 0);
510 quote_two_c_style(&b_name, b_prefix, name_b, 0);
d5625091 511
840383b2
JK
512 size_one = fill_textconv(textconv_one, one, &data_one);
513 size_two = fill_textconv(textconv_two, two, &data_two);
3aa1f7ca 514
d91ba8fa
JH
515 memset(&ecbdata, 0, sizeof(ecbdata));
516 ecbdata.color_diff = color_diff;
517 ecbdata.found_changesp = &o->found_changes;
518 ecbdata.ws_rule = whitespace_rule(name_b ? name_b : name_a);
a3c158d4 519 ecbdata.opt = o;
d91ba8fa
JH
520 if (ecbdata.ws_rule & WS_BLANK_AT_EOF) {
521 mmfile_t mf1, mf2;
522 mf1.ptr = (char *)data_one;
523 mf2.ptr = (char *)data_two;
524 mf1.size = size_one;
525 mf2.size = size_two;
526 check_blank_at_eof(&mf1, &mf2, &ecbdata);
527 }
528 ecbdata.lno_in_preimage = 1;
529 ecbdata.lno_in_postimage = 1;
530
3aa1f7ca
JK
531 lc_a = count_lines(data_one, size_one);
532 lc_b = count_lines(data_two, size_two);
c0c77734
DB
533 fprintf(o->file,
534 "%s--- %s%s%s\n%s+++ %s%s%s\n%s@@ -",
535 metainfo, a_name.buf, name_a_tab, reset,
536 metainfo, b_name.buf, name_b_tab, reset, fraginfo);
537 print_line_count(o->file, lc_a);
538 fprintf(o->file, " +");
539 print_line_count(o->file, lc_b);
540 fprintf(o->file, " @@%s\n", reset);
6973dcae 541 if (lc_a)
d91ba8fa 542 emit_rewrite_lines(&ecbdata, '-', data_one, size_one);
6973dcae 543 if (lc_b)
d91ba8fa 544 emit_rewrite_lines(&ecbdata, '+', data_two, size_two);
b76c056b 545 if (textconv_one)
aed6ca52 546 free((char *)data_one);
b76c056b 547 if (textconv_two)
aed6ca52 548 free((char *)data_two);
6973dcae
JH
549}
550
f59a59e2
JS
551struct diff_words_buffer {
552 mmfile_t text;
553 long alloc;
2e5d2003
JS
554 struct diff_words_orig {
555 const char *begin, *end;
556 } *orig;
557 int orig_nr, orig_alloc;
f59a59e2
JS
558};
559
560static void diff_words_append(char *line, unsigned long len,
561 struct diff_words_buffer *buffer)
562{
23c1575f 563 ALLOC_GROW(buffer->text.ptr, buffer->text.size + len, buffer->alloc);
f59a59e2
JS
564 line++;
565 len--;
566 memcpy(buffer->text.ptr + buffer->text.size, line, len);
567 buffer->text.size += len;
2b6a5417 568 buffer->text.ptr[buffer->text.size] = '\0';
f59a59e2
JS
569}
570
882749a0
TR
571struct diff_words_style_elem
572{
573 const char *prefix;
574 const char *suffix;
575 const char *color; /* NULL; filled in by the setup code if
576 * color is enabled */
577};
578
579struct diff_words_style
580{
581 enum diff_words_type type;
582 struct diff_words_style_elem new, old, ctx;
583 const char *newline;
584};
585
586struct diff_words_style diff_words_styles[] = {
587 { DIFF_WORDS_PORCELAIN, {"+", "\n"}, {"-", "\n"}, {" ", "\n"}, "~\n" },
588 { DIFF_WORDS_PLAIN, {"{+", "+}"}, {"[-", "-]"}, {"", ""}, "\n" },
589 { DIFF_WORDS_COLOR, {"", ""}, {"", ""}, {"", ""}, "\n" }
590};
591
f59a59e2 592struct diff_words_data {
f59a59e2 593 struct diff_words_buffer minus, plus;
2e5d2003 594 const char *current_plus;
c0c77734 595 FILE *file;
2b6a5417 596 regex_t *word_regex;
882749a0
TR
597 enum diff_words_type type;
598 struct diff_words_style *style;
f59a59e2
JS
599};
600
882749a0
TR
601static int fn_out_diff_words_write_helper(FILE *fp,
602 struct diff_words_style_elem *st_el,
603 const char *newline,
604 size_t count, const char *buf)
605{
606 while (count) {
607 char *p = memchr(buf, '\n', count);
608 if (p != buf) {
609 if (st_el->color && fputs(st_el->color, fp) < 0)
610 return -1;
611 if (fputs(st_el->prefix, fp) < 0 ||
612 fwrite(buf, p ? p - buf : count, 1, fp) != 1 ||
613 fputs(st_el->suffix, fp) < 0)
614 return -1;
615 if (st_el->color && *st_el->color
616 && fputs(GIT_COLOR_RESET, fp) < 0)
617 return -1;
618 }
619 if (!p)
620 return 0;
621 if (fputs(newline, fp) < 0)
622 return -1;
623 count -= p + 1 - buf;
624 buf = p + 1;
625 }
626 return 0;
627}
628
f59a59e2 629static void fn_out_diff_words_aux(void *priv, char *line, unsigned long len)
f59a59e2 630{
f59a59e2 631 struct diff_words_data *diff_words = priv;
882749a0 632 struct diff_words_style *style = diff_words->style;
2e5d2003
JS
633 int minus_first, minus_len, plus_first, plus_len;
634 const char *minus_begin, *minus_end, *plus_begin, *plus_end;
f59a59e2 635
2e5d2003
JS
636 if (line[0] != '@' || parse_hunk_header(line, len,
637 &minus_first, &minus_len, &plus_first, &plus_len))
f59a59e2
JS
638 return;
639
2e5d2003
JS
640 /* POSIX requires that first be decremented by one if len == 0... */
641 if (minus_len) {
642 minus_begin = diff_words->minus.orig[minus_first].begin;
643 minus_end =
644 diff_words->minus.orig[minus_first + minus_len - 1].end;
645 } else
646 minus_begin = minus_end =
647 diff_words->minus.orig[minus_first].end;
648
649 if (plus_len) {
650 plus_begin = diff_words->plus.orig[plus_first].begin;
651 plus_end = diff_words->plus.orig[plus_first + plus_len - 1].end;
652 } else
653 plus_begin = plus_end = diff_words->plus.orig[plus_first].end;
654
655 if (diff_words->current_plus != plus_begin)
882749a0
TR
656 fn_out_diff_words_write_helper(diff_words->file,
657 &style->ctx, style->newline,
658 plus_begin - diff_words->current_plus,
659 diff_words->current_plus);
2e5d2003 660 if (minus_begin != minus_end)
882749a0
TR
661 fn_out_diff_words_write_helper(diff_words->file,
662 &style->old, style->newline,
2e5d2003
JS
663 minus_end - minus_begin, minus_begin);
664 if (plus_begin != plus_end)
882749a0
TR
665 fn_out_diff_words_write_helper(diff_words->file,
666 &style->new, style->newline,
2e5d2003
JS
667 plus_end - plus_begin, plus_begin);
668
669 diff_words->current_plus = plus_end;
f59a59e2
JS
670}
671
2b6a5417
JS
672/* This function starts looking at *begin, and returns 0 iff a word was found. */
673static int find_word_boundaries(mmfile_t *buffer, regex_t *word_regex,
674 int *begin, int *end)
675{
676 if (word_regex && *begin < buffer->size) {
677 regmatch_t match[1];
678 if (!regexec(word_regex, buffer->ptr + *begin, 1, match, 0)) {
679 char *p = memchr(buffer->ptr + *begin + match[0].rm_so,
680 '\n', match[0].rm_eo - match[0].rm_so);
681 *end = p ? p - buffer->ptr : match[0].rm_eo + *begin;
682 *begin += match[0].rm_so;
683 return *begin >= *end;
684 }
685 return -1;
f59a59e2
JS
686 }
687
2b6a5417
JS
688 /* find the next word */
689 while (*begin < buffer->size && isspace(buffer->ptr[*begin]))
690 (*begin)++;
691 if (*begin >= buffer->size)
692 return -1;
f59a59e2 693
2b6a5417
JS
694 /* find the end of the word */
695 *end = *begin + 1;
696 while (*end < buffer->size && !isspace(buffer->ptr[*end]))
697 (*end)++;
698
699 return 0;
f59a59e2
JS
700}
701
23c1575f 702/*
2e5d2003
JS
703 * This function splits the words in buffer->text, stores the list with
704 * newline separator into out, and saves the offsets of the original words
705 * in buffer->orig.
23c1575f 706 */
2b6a5417
JS
707static void diff_words_fill(struct diff_words_buffer *buffer, mmfile_t *out,
708 regex_t *word_regex)
f59a59e2 709{
2e5d2003 710 int i, j;
2b6a5417 711 long alloc = 0;
f59a59e2 712
2e5d2003 713 out->size = 0;
2b6a5417 714 out->ptr = NULL;
f59a59e2 715
2e5d2003
JS
716 /* fake an empty "0th" word */
717 ALLOC_GROW(buffer->orig, 1, buffer->orig_alloc);
718 buffer->orig[0].begin = buffer->orig[0].end = buffer->text.ptr;
719 buffer->orig_nr = 1;
720
721 for (i = 0; i < buffer->text.size; i++) {
2b6a5417
JS
722 if (find_word_boundaries(&buffer->text, word_regex, &i, &j))
723 return;
2e5d2003
JS
724
725 /* store original boundaries */
726 ALLOC_GROW(buffer->orig, buffer->orig_nr + 1,
727 buffer->orig_alloc);
728 buffer->orig[buffer->orig_nr].begin = buffer->text.ptr + i;
729 buffer->orig[buffer->orig_nr].end = buffer->text.ptr + j;
730 buffer->orig_nr++;
731
732 /* store one word */
2b6a5417 733 ALLOC_GROW(out->ptr, out->size + j - i + 1, alloc);
2e5d2003
JS
734 memcpy(out->ptr + out->size, buffer->text.ptr + i, j - i);
735 out->ptr[out->size + j - i] = '\n';
736 out->size += j - i + 1;
737
738 i = j - 1;
f59a59e2
JS
739 }
740}
741
742/* this executes the word diff on the accumulated buffers */
743static void diff_words_show(struct diff_words_data *diff_words)
744{
745 xpparam_t xpp;
746 xdemitconf_t xecfg;
f59a59e2 747 mmfile_t minus, plus;
882749a0 748 struct diff_words_style *style = diff_words->style;
f59a59e2 749
2e5d2003
JS
750 /* special case: only removal */
751 if (!diff_words->plus.text.size) {
882749a0
TR
752 fn_out_diff_words_write_helper(diff_words->file,
753 &style->old, style->newline,
2e5d2003
JS
754 diff_words->minus.text.size, diff_words->minus.text.ptr);
755 diff_words->minus.text.size = 0;
756 return;
757 }
758
759 diff_words->current_plus = diff_words->plus.text.ptr;
f59a59e2 760
9ccd0a88 761 memset(&xpp, 0, sizeof(xpp));
30b25010 762 memset(&xecfg, 0, sizeof(xecfg));
2b6a5417
JS
763 diff_words_fill(&diff_words->minus, &minus, diff_words->word_regex);
764 diff_words_fill(&diff_words->plus, &plus, diff_words->word_regex);
f59a59e2 765 xpp.flags = XDF_NEED_MINIMAL;
2b6a5417 766 /* as only the hunk header will be parsed, we need a 0-context */
2e5d2003 767 xecfg.ctxlen = 0;
8a3f524b 768 xdi_diff_outf(&minus, &plus, fn_out_diff_words_aux, diff_words,
dfea7900 769 &xpp, &xecfg);
f59a59e2
JS
770 free(minus.ptr);
771 free(plus.ptr);
2e5d2003
JS
772 if (diff_words->current_plus != diff_words->plus.text.ptr +
773 diff_words->plus.text.size)
882749a0
TR
774 fn_out_diff_words_write_helper(diff_words->file,
775 &style->ctx, style->newline,
2e5d2003 776 diff_words->plus.text.ptr + diff_words->plus.text.size
882749a0 777 - diff_words->current_plus, diff_words->current_plus);
f59a59e2 778 diff_words->minus.text.size = diff_words->plus.text.size = 0;
f59a59e2
JS
779}
780
76fd2828
JH
781/* In "color-words" mode, show word-diff of words accumulated in the buffer */
782static void diff_words_flush(struct emit_callback *ecbdata)
783{
784 if (ecbdata->diff_words->minus.text.size ||
785 ecbdata->diff_words->plus.text.size)
786 diff_words_show(ecbdata->diff_words);
787}
788
f59a59e2
JS
789static void free_diff_words_data(struct emit_callback *ecbdata)
790{
791 if (ecbdata->diff_words) {
76fd2828 792 diff_words_flush(ecbdata);
8e0f7003 793 free (ecbdata->diff_words->minus.text.ptr);
2e5d2003 794 free (ecbdata->diff_words->minus.orig);
8e0f7003 795 free (ecbdata->diff_words->plus.text.ptr);
2e5d2003 796 free (ecbdata->diff_words->plus.orig);
2b6a5417 797 free(ecbdata->diff_words->word_regex);
f59a59e2
JS
798 free(ecbdata->diff_words);
799 ecbdata->diff_words = NULL;
800 }
801}
802
ce436973 803const char *diff_get_color(int diff_use_color, enum color_diff ix)
cd112cef
JS
804{
805 if (diff_use_color)
50f575fc
LT
806 return diff_colors[ix];
807 return "";
cd112cef
JS
808}
809
23707811
JH
810static unsigned long sane_truncate_line(struct emit_callback *ecb, char *line, unsigned long len)
811{
812 const char *cp;
813 unsigned long allot;
814 size_t l = len;
815
816 if (ecb->truncate)
817 return ecb->truncate(line, len);
818 cp = line;
819 allot = l;
820 while (0 < l) {
821 (void) utf8_width(&cp, &l);
822 if (!cp)
823 break; /* truncated in the middle? */
824 }
825 return allot - l;
826}
827
d68fe26f 828static void find_lno(const char *line, struct emit_callback *ecbdata)
690ed843 829{
d68fe26f
JH
830 const char *p;
831 ecbdata->lno_in_preimage = 0;
832 ecbdata->lno_in_postimage = 0;
833 p = strchr(line, '-');
690ed843 834 if (!p)
d68fe26f
JH
835 return; /* cannot happen */
836 ecbdata->lno_in_preimage = strtol(p + 1, NULL, 10);
837 p = strchr(p, '+');
838 if (!p)
839 return; /* cannot happen */
840 ecbdata->lno_in_postimage = strtol(p + 1, NULL, 10);
690ed843
JH
841}
842
cd112cef 843static void fn_out_consume(void *priv, char *line, unsigned long len)
6973dcae 844{
6973dcae 845 struct emit_callback *ecbdata = priv;
472ca780
JK
846 const char *meta = diff_get_color(ecbdata->color_diff, DIFF_METAINFO);
847 const char *plain = diff_get_color(ecbdata->color_diff, DIFF_PLAIN);
ce436973 848 const char *reset = diff_get_color(ecbdata->color_diff, DIFF_RESET);
6973dcae 849
3e97c7c6 850 if (ecbdata->header) {
a3c158d4 851 fprintf(ecbdata->opt->file, "%s", ecbdata->header->buf);
3e97c7c6
GB
852 strbuf_reset(ecbdata->header);
853 ecbdata->header = NULL;
854 }
34a5e1a2
JS
855 *(ecbdata->found_changesp) = 1;
856
6973dcae 857 if (ecbdata->label_path[0]) {
1a9eb3b9
JH
858 const char *name_a_tab, *name_b_tab;
859
860 name_a_tab = strchr(ecbdata->label_path[0], ' ') ? "\t" : "";
861 name_b_tab = strchr(ecbdata->label_path[1], ' ') ? "\t" : "";
862
a3c158d4 863 fprintf(ecbdata->opt->file, "%s--- %s%s%s\n",
c0c77734 864 meta, ecbdata->label_path[0], reset, name_a_tab);
a3c158d4 865 fprintf(ecbdata->opt->file, "%s+++ %s%s%s\n",
c0c77734 866 meta, ecbdata->label_path[1], reset, name_b_tab);
6973dcae
JH
867 ecbdata->label_path[0] = ecbdata->label_path[1] = NULL;
868 }
cd112cef 869
a624eaa7
JM
870 if (diff_suppress_blank_empty
871 && len == 2 && line[0] == ' ' && line[1] == '\n') {
872 line[0] = '\n';
873 len = 1;
874 }
875
b8d9c1a6 876 if (line[0] == '@') {
76fd2828
JH
877 if (ecbdata->diff_words)
878 diff_words_flush(ecbdata);
23707811 879 len = sane_truncate_line(ecbdata, line, len);
d68fe26f 880 find_lno(line, ecbdata);
89cb73a1 881 emit_hunk_header(ecbdata, line, len);
23707811 882 if (line[len-1] != '\n')
a3c158d4 883 putc('\n', ecbdata->opt->file);
448c3ef1 884 return;
cd112cef 885 }
448c3ef1 886
b8d9c1a6 887 if (len < 1) {
a3c158d4 888 emit_line(ecbdata->opt, reset, reset, line, len);
882749a0
TR
889 if (ecbdata->diff_words
890 && ecbdata->diff_words->type == DIFF_WORDS_PORCELAIN)
a3c158d4 891 fputs("~\n", ecbdata->opt->file);
448c3ef1 892 return;
cd112cef 893 }
448c3ef1 894
448c3ef1
JH
895 if (ecbdata->diff_words) {
896 if (line[0] == '-') {
897 diff_words_append(line, len,
898 &ecbdata->diff_words->minus);
899 return;
900 } else if (line[0] == '+') {
901 diff_words_append(line, len,
902 &ecbdata->diff_words->plus);
903 return;
904 }
76fd2828 905 diff_words_flush(ecbdata);
882749a0 906 if (ecbdata->diff_words->type == DIFF_WORDS_PORCELAIN) {
a3c158d4
BY
907 emit_line(ecbdata->opt, plain, reset, line, len);
908 fputs("~\n", ecbdata->opt->file);
882749a0
TR
909 } else {
910 /* don't print the prefix character */
a3c158d4 911 emit_line(ecbdata->opt, plain, reset, line+1, len-1);
882749a0 912 }
448c3ef1
JH
913 return;
914 }
448c3ef1 915
690ed843
JH
916 if (line[0] != '+') {
917 const char *color =
918 diff_get_color(ecbdata->color_diff,
919 line[0] == '-' ? DIFF_FILE_OLD : DIFF_PLAIN);
920 ecbdata->lno_in_preimage++;
d68fe26f
JH
921 if (line[0] == ' ')
922 ecbdata->lno_in_postimage++;
a3c158d4 923 emit_line(ecbdata->opt, color, reset, line, len);
d68fe26f
JH
924 } else {
925 ecbdata->lno_in_postimage++;
018cff70 926 emit_add_line(reset, ecbdata, line + 1, len - 1);
448c3ef1 927 }
6973dcae
JH
928}
929
930static char *pprint_rename(const char *a, const char *b)
931{
932 const char *old = a;
933 const char *new = b;
f285a2d7 934 struct strbuf name = STRBUF_INIT;
6973dcae
JH
935 int pfx_length, sfx_length;
936 int len_a = strlen(a);
937 int len_b = strlen(b);
663af342 938 int a_midlen, b_midlen;
e5bfbf9b
AJ
939 int qlen_a = quote_c_style(a, NULL, NULL, 0);
940 int qlen_b = quote_c_style(b, NULL, NULL, 0);
941
942 if (qlen_a || qlen_b) {
663af342
PH
943 quote_c_style(a, &name, NULL, 0);
944 strbuf_addstr(&name, " => ");
945 quote_c_style(b, &name, NULL, 0);
b315c5c0 946 return strbuf_detach(&name, NULL);
e5bfbf9b 947 }
6973dcae
JH
948
949 /* Find common prefix */
950 pfx_length = 0;
951 while (*old && *new && *old == *new) {
952 if (*old == '/')
953 pfx_length = old - a + 1;
954 old++;
955 new++;
956 }
957
958 /* Find common suffix */
959 old = a + len_a;
960 new = b + len_b;
961 sfx_length = 0;
962 while (a <= old && b <= new && *old == *new) {
963 if (*old == '/')
964 sfx_length = len_a - (old - a);
965 old--;
966 new--;
967 }
968
969 /*
970 * pfx{mid-a => mid-b}sfx
971 * {pfx-a => pfx-b}sfx
972 * pfx{sfx-a => sfx-b}
973 * name-a => name-b
974 */
663af342
PH
975 a_midlen = len_a - pfx_length - sfx_length;
976 b_midlen = len_b - pfx_length - sfx_length;
977 if (a_midlen < 0)
978 a_midlen = 0;
979 if (b_midlen < 0)
980 b_midlen = 0;
981
982 strbuf_grow(&name, pfx_length + a_midlen + b_midlen + sfx_length + 7);
6973dcae 983 if (pfx_length + sfx_length) {
663af342
PH
984 strbuf_add(&name, a, pfx_length);
985 strbuf_addch(&name, '{');
6973dcae 986 }
663af342
PH
987 strbuf_add(&name, a + pfx_length, a_midlen);
988 strbuf_addstr(&name, " => ");
989 strbuf_add(&name, b + pfx_length, b_midlen);
990 if (pfx_length + sfx_length) {
991 strbuf_addch(&name, '}');
992 strbuf_add(&name, a + len_a - sfx_length, sfx_length);
6973dcae 993 }
b315c5c0 994 return strbuf_detach(&name, NULL);
6973dcae
JH
995}
996
997struct diffstat_t {
6973dcae
JH
998 int nr;
999 int alloc;
1000 struct diffstat_file {
f604652e 1001 char *from_name;
6973dcae 1002 char *name;
f604652e 1003 char *print_name;
6973dcae
JH
1004 unsigned is_unmerged:1;
1005 unsigned is_binary:1;
1006 unsigned is_renamed:1;
0974c117 1007 uintmax_t added, deleted;
6973dcae
JH
1008 } **files;
1009};
1010
1011static struct diffstat_file *diffstat_add(struct diffstat_t *diffstat,
1012 const char *name_a,
1013 const char *name_b)
1014{
1015 struct diffstat_file *x;
1016 x = xcalloc(sizeof (*x), 1);
1017 if (diffstat->nr == diffstat->alloc) {
1018 diffstat->alloc = alloc_nr(diffstat->alloc);
1019 diffstat->files = xrealloc(diffstat->files,
1020 diffstat->alloc * sizeof(x));
1021 }
1022 diffstat->files[diffstat->nr++] = x;
1023 if (name_b) {
f604652e
JH
1024 x->from_name = xstrdup(name_a);
1025 x->name = xstrdup(name_b);
6973dcae
JH
1026 x->is_renamed = 1;
1027 }
f604652e
JH
1028 else {
1029 x->from_name = NULL;
9befac47 1030 x->name = xstrdup(name_a);
f604652e 1031 }
6973dcae
JH
1032 return x;
1033}
1034
1035static void diffstat_consume(void *priv, char *line, unsigned long len)
1036{
1037 struct diffstat_t *diffstat = priv;
1038 struct diffstat_file *x = diffstat->files[diffstat->nr - 1];
1039
1040 if (line[0] == '+')
1041 x->added++;
1042 else if (line[0] == '-')
1043 x->deleted++;
1044}
1045
698ce6f8 1046const char mime_boundary_leader[] = "------------";
6973dcae 1047
a2540023
JH
1048static int scale_linear(int it, int width, int max_change)
1049{
1050 /*
3ed74e60
JS
1051 * make sure that at least one '-' is printed if there were deletions,
1052 * and likewise for '+'.
a2540023 1053 */
3ed74e60
JS
1054 if (max_change < 2)
1055 return it;
1056 return ((it - 1) * (width - 1) + max_change - 1) / (max_change - 1);
a2540023
JH
1057}
1058
c0c77734 1059static void show_name(FILE *file,
a408e0e6 1060 const char *prefix, const char *name, int len)
a2540023 1061{
a408e0e6 1062 fprintf(file, " %s%-*s |", prefix, len, name);
a2540023
JH
1063}
1064
c0c77734 1065static void show_graph(FILE *file, char ch, int cnt, const char *set, const char *reset)
a2540023
JH
1066{
1067 if (cnt <= 0)
1068 return;
c0c77734 1069 fprintf(file, "%s", set);
a2540023 1070 while (cnt--)
c0c77734
DB
1071 putc(ch, file);
1072 fprintf(file, "%s", reset);
a2540023
JH
1073}
1074
f604652e
JH
1075static void fill_print_name(struct diffstat_file *file)
1076{
1077 char *pname;
1078
1079 if (file->print_name)
1080 return;
1081
1082 if (!file->is_renamed) {
f285a2d7 1083 struct strbuf buf = STRBUF_INIT;
f604652e
JH
1084 if (quote_c_style(file->name, &buf, NULL, 0)) {
1085 pname = strbuf_detach(&buf, NULL);
1086 } else {
1087 pname = file->name;
1088 strbuf_release(&buf);
1089 }
1090 } else {
1091 pname = pprint_rename(file->from_name, file->name);
1092 }
1093 file->print_name = pname;
1094}
1095
4b25d091 1096static void show_stats(struct diffstat_t *data, struct diff_options *options)
6973dcae 1097{
eb3a9dd3 1098 int i, len, add, del, adds = 0, dels = 0;
0974c117 1099 uintmax_t max_change = 0, max_len = 0;
6973dcae 1100 int total_files = data->nr;
a2540023 1101 int width, name_width;
785f7432 1102 const char *reset, *set, *add_c, *del_c;
6973dcae
JH
1103
1104 if (data->nr == 0)
1105 return;
1106
a2540023
JH
1107 width = options->stat_width ? options->stat_width : 80;
1108 name_width = options->stat_name_width ? options->stat_name_width : 50;
1109
1110 /* Sanity: give at least 5 columns to the graph,
1111 * but leave at least 10 columns for the name.
1112 */
861d1af3
OM
1113 if (width < 25)
1114 width = 25;
1115 if (name_width < 10)
1116 name_width = 10;
1117 else if (width < name_width + 15)
1118 name_width = width - 15;
a2540023
JH
1119
1120 /* Find the longest filename and max number of changes */
8f67f8ae
PH
1121 reset = diff_get_color_opt(options, DIFF_RESET);
1122 set = diff_get_color_opt(options, DIFF_PLAIN);
1123 add_c = diff_get_color_opt(options, DIFF_FILE_NEW);
1124 del_c = diff_get_color_opt(options, DIFF_FILE_OLD);
785f7432 1125
6973dcae
JH
1126 for (i = 0; i < data->nr; i++) {
1127 struct diffstat_file *file = data->files[i];
0974c117 1128 uintmax_t change = file->added + file->deleted;
f604652e
JH
1129 fill_print_name(file);
1130 len = strlen(file->print_name);
6973dcae
JH
1131 if (max_len < len)
1132 max_len = len;
1133
1134 if (file->is_binary || file->is_unmerged)
1135 continue;
a2540023
JH
1136 if (max_change < change)
1137 max_change = change;
6973dcae
JH
1138 }
1139
a2540023
JH
1140 /* Compute the width of the graph part;
1141 * 10 is for one blank at the beginning of the line plus
1142 * " | count " between the name and the graph.
1143 *
1144 * From here on, name_width is the width of the name area,
1145 * and width is the width of the graph area.
1146 */
1147 name_width = (name_width < max_len) ? name_width : max_len;
1148 if (width < (name_width + 10) + max_change)
1149 width = width - (name_width + 10);
1150 else
1151 width = max_change;
1152
6973dcae 1153 for (i = 0; i < data->nr; i++) {
d2543b8e 1154 const char *prefix = "";
f604652e 1155 char *name = data->files[i]->print_name;
0974c117
JK
1156 uintmax_t added = data->files[i]->added;
1157 uintmax_t deleted = data->files[i]->deleted;
a2540023 1158 int name_len;
6973dcae
JH
1159
1160 /*
1161 * "scale" the filename
1162 */
a2540023
JH
1163 len = name_width;
1164 name_len = strlen(name);
1165 if (name_width < name_len) {
6973dcae
JH
1166 char *slash;
1167 prefix = "...";
a2540023
JH
1168 len -= 3;
1169 name += name_len - len;
6973dcae
JH
1170 slash = strchr(name, '/');
1171 if (slash)
1172 name = slash;
1173 }
6973dcae
JH
1174
1175 if (data->files[i]->is_binary) {
a408e0e6 1176 show_name(options->file, prefix, name, len);
c0c77734 1177 fprintf(options->file, " Bin ");
0974c117
JK
1178 fprintf(options->file, "%s%"PRIuMAX"%s",
1179 del_c, deleted, reset);
c0c77734 1180 fprintf(options->file, " -> ");
0974c117
JK
1181 fprintf(options->file, "%s%"PRIuMAX"%s",
1182 add_c, added, reset);
c0c77734
DB
1183 fprintf(options->file, " bytes");
1184 fprintf(options->file, "\n");
f604652e 1185 continue;
6973dcae
JH
1186 }
1187 else if (data->files[i]->is_unmerged) {
a408e0e6 1188 show_name(options->file, prefix, name, len);
c0c77734 1189 fprintf(options->file, " Unmerged\n");
f604652e 1190 continue;
6973dcae
JH
1191 }
1192 else if (!data->files[i]->is_renamed &&
1193 (added + deleted == 0)) {
1194 total_files--;
f604652e 1195 continue;
6973dcae
JH
1196 }
1197
a2540023
JH
1198 /*
1199 * scale the add/delete
1200 */
6973dcae
JH
1201 add = added;
1202 del = deleted;
6973dcae
JH
1203 adds += add;
1204 dels += del;
1205
a2540023 1206 if (width <= max_change) {
a2540023 1207 add = scale_linear(add, width, max_change);
3ed74e60 1208 del = scale_linear(del, width, max_change);
6973dcae 1209 }
a408e0e6 1210 show_name(options->file, prefix, name, len);
0974c117 1211 fprintf(options->file, "%5"PRIuMAX"%s", added + deleted,
4d9b5359 1212 added + deleted ? " " : "");
c0c77734
DB
1213 show_graph(options->file, '+', add, add_c, reset);
1214 show_graph(options->file, '-', del, del_c, reset);
1215 fprintf(options->file, "\n");
1216 }
1217 fprintf(options->file,
a408e0e6
MH
1218 " %d files changed, %d insertions(+), %d deletions(-)\n",
1219 total_files, adds, dels);
6973dcae
JH
1220}
1221
2775d92c 1222static void show_shortstats(struct diffstat_t *data, struct diff_options *options)
ebd124c6
NP
1223{
1224 int i, adds = 0, dels = 0, total_files = data->nr;
1225
1226 if (data->nr == 0)
1227 return;
1228
1229 for (i = 0; i < data->nr; i++) {
1230 if (!data->files[i]->is_binary &&
1231 !data->files[i]->is_unmerged) {
1232 int added = data->files[i]->added;
1233 int deleted= data->files[i]->deleted;
1234 if (!data->files[i]->is_renamed &&
1235 (added + deleted == 0)) {
1236 total_files--;
1237 } else {
1238 adds += added;
1239 dels += deleted;
1240 }
1241 }
ebd124c6 1242 }
c0c77734 1243 fprintf(options->file, " %d files changed, %d insertions(+), %d deletions(-)\n",
ebd124c6
NP
1244 total_files, adds, dels);
1245}
1246
4b25d091 1247static void show_numstat(struct diffstat_t *data, struct diff_options *options)
74e2abe5
JH
1248{
1249 int i;
1250
f604652e
JH
1251 if (data->nr == 0)
1252 return;
1253
74e2abe5
JH
1254 for (i = 0; i < data->nr; i++) {
1255 struct diffstat_file *file = data->files[i];
1256
bfddbc5e 1257 if (file->is_binary)
c0c77734 1258 fprintf(options->file, "-\t-\t");
bfddbc5e 1259 else
c0c77734 1260 fprintf(options->file,
0974c117
JK
1261 "%"PRIuMAX"\t%"PRIuMAX"\t",
1262 file->added, file->deleted);
f604652e
JH
1263 if (options->line_termination) {
1264 fill_print_name(file);
1265 if (!file->is_renamed)
c0c77734 1266 write_name_quoted(file->name, options->file,
f604652e
JH
1267 options->line_termination);
1268 else {
c0c77734
DB
1269 fputs(file->print_name, options->file);
1270 putc(options->line_termination, options->file);
f604652e 1271 }
663af342 1272 } else {
f604652e 1273 if (file->is_renamed) {
c0c77734
DB
1274 putc('\0', options->file);
1275 write_name_quoted(file->from_name, options->file, '\0');
f604652e 1276 }
c0c77734 1277 write_name_quoted(file->name, options->file, '\0');
663af342 1278 }
74e2abe5
JH
1279 }
1280}
1281
c04a7155
JH
1282struct dirstat_file {
1283 const char *name;
1284 unsigned long changed;
7df7c019
LT
1285};
1286
c04a7155
JH
1287struct dirstat_dir {
1288 struct dirstat_file *files;
1289 int alloc, nr, percent, cumulative;
1290};
1291
1292static long gather_dirstat(FILE *file, struct dirstat_dir *dir, unsigned long changed, const char *base, int baselen)
7df7c019
LT
1293{
1294 unsigned long this_dir = 0;
1295 unsigned int sources = 0;
1296
1297 while (dir->nr) {
c04a7155 1298 struct dirstat_file *f = dir->files;
7df7c019
LT
1299 int namelen = strlen(f->name);
1300 unsigned long this;
1301 char *slash;
1302
1303 if (namelen < baselen)
1304 break;
1305 if (memcmp(f->name, base, baselen))
1306 break;
1307 slash = strchr(f->name + baselen, '/');
1308 if (slash) {
1309 int newbaselen = slash + 1 - f->name;
c0c77734 1310 this = gather_dirstat(file, dir, changed, f->name, newbaselen);
7df7c019
LT
1311 sources++;
1312 } else {
c04a7155 1313 this = f->changed;
7df7c019
LT
1314 dir->files++;
1315 dir->nr--;
1316 sources += 2;
1317 }
1318 this_dir += this;
1319 }
1320
1321 /*
1322 * We don't report dirstat's for
1323 * - the top level
1324 * - or cases where everything came from a single directory
1325 * under this directory (sources == 1).
1326 */
1327 if (baselen && sources != 1) {
1328 int permille = this_dir * 1000 / changed;
1329 if (permille) {
1330 int percent = permille / 10;
1331 if (percent >= dir->percent) {
c0c77734 1332 fprintf(file, "%4d.%01d%% %.*s\n", percent, permille % 10, baselen, base);
7df7c019
LT
1333 if (!dir->cumulative)
1334 return 0;
1335 }
1336 }
1337 }
1338 return this_dir;
1339}
1340
441bca0b
LT
1341static int dirstat_compare(const void *_a, const void *_b)
1342{
1343 const struct dirstat_file *a = _a;
1344 const struct dirstat_file *b = _b;
1345 return strcmp(a->name, b->name);
1346}
1347
c04a7155 1348static void show_dirstat(struct diff_options *options)
7df7c019
LT
1349{
1350 int i;
1351 unsigned long changed;
c04a7155
JH
1352 struct dirstat_dir dir;
1353 struct diff_queue_struct *q = &diff_queued_diff;
1354
1355 dir.files = NULL;
1356 dir.alloc = 0;
1357 dir.nr = 0;
1358 dir.percent = options->dirstat_percent;
f88d225f 1359 dir.cumulative = DIFF_OPT_TST(options, DIRSTAT_CUMULATIVE);
7df7c019 1360
7df7c019 1361 changed = 0;
c04a7155
JH
1362 for (i = 0; i < q->nr; i++) {
1363 struct diff_filepair *p = q->queue[i];
1364 const char *name;
1365 unsigned long copied, added, damage;
1366
1367 name = p->one->path ? p->one->path : p->two->path;
1368
1369 if (DIFF_FILE_VALID(p->one) && DIFF_FILE_VALID(p->two)) {
1370 diff_populate_filespec(p->one, 0);
1371 diff_populate_filespec(p->two, 0);
1372 diffcore_count_changes(p->one, p->two, NULL, NULL, 0,
1373 &copied, &added);
1374 diff_free_filespec_data(p->one);
1375 diff_free_filespec_data(p->two);
1376 } else if (DIFF_FILE_VALID(p->one)) {
1377 diff_populate_filespec(p->one, 1);
1378 copied = added = 0;
1379 diff_free_filespec_data(p->one);
1380 } else if (DIFF_FILE_VALID(p->two)) {
1381 diff_populate_filespec(p->two, 1);
1382 copied = 0;
1383 added = p->two->size;
1384 diff_free_filespec_data(p->two);
1385 } else
2b0b551d 1386 continue;
c04a7155
JH
1387
1388 /*
1389 * Original minus copied is the removed material,
1390 * added is the new material. They are both damages
fd33777b
HO
1391 * made to the preimage. In --dirstat-by-file mode, count
1392 * damaged files, not damaged lines. This is done by
1393 * counting only a single damaged line per file.
c04a7155
JH
1394 */
1395 damage = (p->one->size - copied) + added;
fd33777b
HO
1396 if (DIFF_OPT_TST(options, DIRSTAT_BY_FILE) && damage > 0)
1397 damage = 1;
c04a7155
JH
1398
1399 ALLOC_GROW(dir.files, dir.nr + 1, dir.alloc);
1400 dir.files[dir.nr].name = name;
1401 dir.files[dir.nr].changed = damage;
1402 changed += damage;
1403 dir.nr++;
7df7c019
LT
1404 }
1405
1406 /* This can happen even with many files, if everything was renames */
1407 if (!changed)
1408 return;
1409
1410 /* Show all directories with more than x% of the changes */
441bca0b 1411 qsort(dir.files, dir.nr, sizeof(dir.files[0]), dirstat_compare);
c0c77734 1412 gather_dirstat(options->file, &dir, changed, "", 0);
7df7c019
LT
1413}
1414
f604652e
JH
1415static void free_diffstat_info(struct diffstat_t *diffstat)
1416{
1417 int i;
1418 for (i = 0; i < diffstat->nr; i++) {
1419 struct diffstat_file *f = diffstat->files[i];
1420 if (f->name != f->print_name)
1421 free(f->print_name);
1422 free(f->name);
1423 free(f->from_name);
1424 free(f);
1425 }
1426 free(diffstat->files);
1427}
1428
88246898 1429struct checkdiff_t {
88246898 1430 const char *filename;
1ba111d1 1431 int lineno;
a757c646 1432 int conflict_marker_size;
1ba111d1 1433 struct diff_options *o;
cf1b7869 1434 unsigned ws_rule;
62c64895 1435 unsigned status;
88246898
JS
1436};
1437
a757c646 1438static int is_conflict_marker(const char *line, int marker_size, unsigned long len)
04954043
JH
1439{
1440 char firstchar;
1441 int cnt;
1442
a757c646 1443 if (len < marker_size + 1)
04954043
JH
1444 return 0;
1445 firstchar = line[0];
1446 switch (firstchar) {
a757c646 1447 case '=': case '>': case '<': case '|':
04954043
JH
1448 break;
1449 default:
1450 return 0;
1451 }
a757c646 1452 for (cnt = 1; cnt < marker_size; cnt++)
04954043
JH
1453 if (line[cnt] != firstchar)
1454 return 0;
a757c646
JH
1455 /* line[1] thru line[marker_size-1] are same as firstchar */
1456 if (len < marker_size + 1 || !isspace(line[marker_size]))
04954043 1457 return 0;
04954043
JH
1458 return 1;
1459}
1460
88246898
JS
1461static void checkdiff_consume(void *priv, char *line, unsigned long len)
1462{
1463 struct checkdiff_t *data = priv;
1ba111d1 1464 int color_diff = DIFF_OPT_TST(data->o, COLOR_DIFF);
a757c646 1465 int marker_size = data->conflict_marker_size;
1ba111d1
JH
1466 const char *ws = diff_get_color(color_diff, DIFF_WHITESPACE);
1467 const char *reset = diff_get_color(color_diff, DIFF_RESET);
1468 const char *set = diff_get_color(color_diff, DIFF_FILE_NEW);
c1795bb0 1469 char *err;
88246898
JS
1470
1471 if (line[0] == '+') {
18374e58 1472 unsigned bad;
0ef617f4 1473 data->lineno++;
a757c646 1474 if (is_conflict_marker(line + 1, marker_size, len - 1)) {
04954043
JH
1475 data->status |= 1;
1476 fprintf(data->o->file,
1477 "%s:%d: leftover conflict marker\n",
1478 data->filename, data->lineno);
1479 }
8f8841e9 1480 bad = ws_check(line + 1, len - 1, data->ws_rule);
18374e58 1481 if (!bad)
c1795bb0 1482 return;
18374e58
JH
1483 data->status |= bad;
1484 err = whitespace_error_string(bad);
1ba111d1
JH
1485 fprintf(data->o->file, "%s:%d: %s.\n",
1486 data->filename, data->lineno, err);
c1795bb0 1487 free(err);
a3c158d4 1488 emit_line(data->o, set, reset, line, 1);
8f8841e9 1489 ws_check_emit(line + 1, len - 1, data->ws_rule,
1ba111d1 1490 data->o->file, set, reset, ws);
877f23cc 1491 } else if (line[0] == ' ') {
88246898 1492 data->lineno++;
877f23cc 1493 } else if (line[0] == '@') {
88246898
JS
1494 char *plus = strchr(line, '+');
1495 if (plus)
0ef617f4 1496 data->lineno = strtol(plus, NULL, 10) - 1;
88246898
JS
1497 else
1498 die("invalid diff");
1499 }
1500}
1501
0660626c
JH
1502static unsigned char *deflate_it(char *data,
1503 unsigned long size,
1504 unsigned long *result_size)
051308f6 1505{
0660626c
JH
1506 int bound;
1507 unsigned char *deflated;
1508 z_stream stream;
1509
1510 memset(&stream, 0, sizeof(stream));
12f6c308 1511 deflateInit(&stream, zlib_compression_level);
0660626c
JH
1512 bound = deflateBound(&stream, size);
1513 deflated = xmalloc(bound);
1514 stream.next_out = deflated;
1515 stream.avail_out = bound;
1516
1517 stream.next_in = (unsigned char *)data;
1518 stream.avail_in = size;
1519 while (deflate(&stream, Z_FINISH) == Z_OK)
1520 ; /* nothing */
1521 deflateEnd(&stream);
1522 *result_size = stream.total_out;
1523 return deflated;
051308f6
JH
1524}
1525
c0c77734 1526static void emit_binary_diff_body(FILE *file, mmfile_t *one, mmfile_t *two)
051308f6 1527{
0660626c
JH
1528 void *cp;
1529 void *delta;
1530 void *deflated;
1531 void *data;
1532 unsigned long orig_size;
1533 unsigned long delta_size;
1534 unsigned long deflate_size;
1535 unsigned long data_size;
051308f6 1536
0660626c
JH
1537 /* We could do deflated delta, or we could do just deflated two,
1538 * whichever is smaller.
051308f6 1539 */
0660626c
JH
1540 delta = NULL;
1541 deflated = deflate_it(two->ptr, two->size, &deflate_size);
1542 if (one->size && two->size) {
1543 delta = diff_delta(one->ptr, one->size,
1544 two->ptr, two->size,
1545 &delta_size, deflate_size);
1546 if (delta) {
1547 void *to_free = delta;
1548 orig_size = delta_size;
1549 delta = deflate_it(delta, delta_size, &delta_size);
1550 free(to_free);
051308f6
JH
1551 }
1552 }
051308f6 1553
0660626c 1554 if (delta && delta_size < deflate_size) {
c0c77734 1555 fprintf(file, "delta %lu\n", orig_size);
0660626c
JH
1556 free(deflated);
1557 data = delta;
1558 data_size = delta_size;
1559 }
1560 else {
c0c77734 1561 fprintf(file, "literal %lu\n", two->size);
0660626c
JH
1562 free(delta);
1563 data = deflated;
1564 data_size = deflate_size;
1565 }
051308f6 1566
0660626c
JH
1567 /* emit data encoded in base85 */
1568 cp = data;
1569 while (data_size) {
1570 int bytes = (52 < data_size) ? 52 : data_size;
051308f6 1571 char line[70];
0660626c 1572 data_size -= bytes;
051308f6
JH
1573 if (bytes <= 26)
1574 line[0] = bytes + 'A' - 1;
1575 else
1576 line[0] = bytes - 26 + 'a' - 1;
1577 encode_85(line + 1, cp, bytes);
1d7f171c 1578 cp = (char *) cp + bytes;
c0c77734
DB
1579 fputs(line, file);
1580 fputc('\n', file);
051308f6 1581 }
c0c77734 1582 fprintf(file, "\n");
0660626c 1583 free(data);
051308f6
JH
1584}
1585
c0c77734 1586static void emit_binary_diff(FILE *file, mmfile_t *one, mmfile_t *two)
d4c452f0 1587{
c0c77734
DB
1588 fprintf(file, "GIT binary patch\n");
1589 emit_binary_diff_body(file, one, two);
1590 emit_binary_diff_body(file, two, one);
d4c452f0
JH
1591}
1592
72cf4841 1593static void diff_filespec_load_driver(struct diff_filespec *one)
6973dcae 1594{
122aa6f9
JK
1595 if (!one->driver)
1596 one->driver = userdiff_find_by_path(one->path);
1597 if (!one->driver)
1598 one->driver = userdiff_find_by_name("default");
29a3eefd
JH
1599}
1600
1601int diff_filespec_is_binary(struct diff_filespec *one)
1602{
122aa6f9
JK
1603 if (one->is_binary == -1) {
1604 diff_filespec_load_driver(one);
1605 if (one->driver->binary != -1)
1606 one->is_binary = one->driver->binary;
1607 else {
1608 if (!one->data && DIFF_FILE_VALID(one))
1609 diff_populate_filespec(one, 0);
1610 if (one->data)
1611 one->is_binary = buffer_is_binary(one->data,
1612 one->size);
1613 if (one->is_binary == -1)
1614 one->is_binary = 0;
1615 }
1616 }
29a3eefd 1617 return one->is_binary;
6973dcae
JH
1618}
1619
be58e70d 1620static const struct userdiff_funcname *diff_funcname_pattern(struct diff_filespec *one)
f258475a 1621{
122aa6f9
JK
1622 diff_filespec_load_driver(one);
1623 return one->driver->funcname.pattern ? &one->driver->funcname : NULL;
f258475a
JH
1624}
1625
80c49c3d
TR
1626static const char *userdiff_word_regex(struct diff_filespec *one)
1627{
1628 diff_filespec_load_driver(one);
1629 return one->driver->word_regex;
1630}
1631
a5a818ee
JH
1632void diff_set_mnemonic_prefix(struct diff_options *options, const char *a, const char *b)
1633{
1634 if (!options->a_prefix)
1635 options->a_prefix = a;
1636 if (!options->b_prefix)
1637 options->b_prefix = b;
1638}
1639
d9bae1a1 1640static struct userdiff_driver *get_textconv(struct diff_filespec *one)
04427ac8
JK
1641{
1642 if (!DIFF_FILE_VALID(one))
1643 return NULL;
2675773a
JK
1644 if (!S_ISREG(one->mode))
1645 return NULL;
04427ac8 1646 diff_filespec_load_driver(one);
d9bae1a1
JK
1647 if (!one->driver->textconv)
1648 return NULL;
1649
1650 if (one->driver->textconv_want_cache && !one->driver->textconv_cache) {
1651 struct notes_cache *c = xmalloc(sizeof(*c));
1652 struct strbuf name = STRBUF_INIT;
1653
1654 strbuf_addf(&name, "textconv/%s", one->driver->name);
1655 notes_cache_init(c, name.buf, one->driver->textconv);
1656 one->driver->textconv_cache = c;
1657 }
1658
1659 return one->driver;
04427ac8
JK
1660}
1661
6973dcae
JH
1662static void builtin_diff(const char *name_a,
1663 const char *name_b,
1664 struct diff_filespec *one,
1665 struct diff_filespec *two,
1666 const char *xfrm_msg,
051308f6 1667 struct diff_options *o,
6973dcae
JH
1668 int complete_rewrite)
1669{
1670 mmfile_t mf1, mf2;
1671 const char *lbl[2];
1672 char *a_one, *b_two;
8f67f8ae
PH
1673 const char *set = diff_get_color_opt(o, DIFF_METAINFO);
1674 const char *reset = diff_get_color_opt(o, DIFF_RESET);
a5a818ee 1675 const char *a_prefix, *b_prefix;
d9bae1a1
JK
1676 struct userdiff_driver *textconv_one = NULL;
1677 struct userdiff_driver *textconv_two = NULL;
3e97c7c6 1678 struct strbuf header = STRBUF_INIT;
a5a818ee 1679
752c0c24
JS
1680 if (DIFF_OPT_TST(o, SUBMODULE_LOG) &&
1681 (!one->mode || S_ISGITLINK(one->mode)) &&
1682 (!two->mode || S_ISGITLINK(two->mode))) {
1683 const char *del = diff_get_color_opt(o, DIFF_FILE_OLD);
1684 const char *add = diff_get_color_opt(o, DIFF_FILE_NEW);
1685 show_submodule_summary(o->file, one ? one->path : two->path,
721ceec1 1686 one->sha1, two->sha1, two->dirty_submodule,
752c0c24
JS
1687 del, add, reset);
1688 return;
1689 }
1690
3aa1f7ca
JK
1691 if (DIFF_OPT_TST(o, ALLOW_TEXTCONV)) {
1692 textconv_one = get_textconv(one);
1693 textconv_two = get_textconv(two);
1694 }
1695
a5a818ee
JH
1696 diff_set_mnemonic_prefix(o, "a/", "b/");
1697 if (DIFF_OPT_TST(o, REVERSE_DIFF)) {
1698 a_prefix = o->b_prefix;
1699 b_prefix = o->a_prefix;
1700 } else {
1701 a_prefix = o->a_prefix;
1702 b_prefix = o->b_prefix;
1703 }
6973dcae 1704
71b989e7
LT
1705 /* Never use a non-valid filename anywhere if at all possible */
1706 name_a = DIFF_FILE_VALID(one) ? name_a : name_b;
1707 name_b = DIFF_FILE_VALID(two) ? name_b : name_a;
1708
a5a818ee
JH
1709 a_one = quote_two(a_prefix, name_a + (*name_a == '/'));
1710 b_two = quote_two(b_prefix, name_b + (*name_b == '/'));
6973dcae
JH
1711 lbl[0] = DIFF_FILE_VALID(one) ? a_one : "/dev/null";
1712 lbl[1] = DIFF_FILE_VALID(two) ? b_two : "/dev/null";
3e97c7c6 1713 strbuf_addf(&header, "%sdiff --git %s %s%s\n", set, a_one, b_two, reset);
6973dcae
JH
1714 if (lbl[0][0] == '/') {
1715 /* /dev/null */
3e97c7c6 1716 strbuf_addf(&header, "%snew file mode %06o%s\n", set, two->mode, reset);
50f575fc 1717 if (xfrm_msg && xfrm_msg[0])
3e97c7c6 1718 strbuf_addf(&header, "%s%s%s\n", set, xfrm_msg, reset);
6973dcae
JH
1719 }
1720 else if (lbl[1][0] == '/') {
3e97c7c6 1721 strbuf_addf(&header, "%sdeleted file mode %06o%s\n", set, one->mode, reset);
50f575fc 1722 if (xfrm_msg && xfrm_msg[0])
3e97c7c6 1723 strbuf_addf(&header, "%s%s%s\n", set, xfrm_msg, reset);
6973dcae
JH
1724 }
1725 else {
1726 if (one->mode != two->mode) {
3e97c7c6
GB
1727 strbuf_addf(&header, "%sold mode %06o%s\n", set, one->mode, reset);
1728 strbuf_addf(&header, "%snew mode %06o%s\n", set, two->mode, reset);
cd112cef 1729 }
50f575fc 1730 if (xfrm_msg && xfrm_msg[0])
3e97c7c6
GB
1731 strbuf_addf(&header, "%s%s%s\n", set, xfrm_msg, reset);
1732
6973dcae
JH
1733 /*
1734 * we do not run diff between different kind
1735 * of objects.
1736 */
1737 if ((one->mode ^ two->mode) & S_IFMT)
1738 goto free_ab_and_return;
0c01857d 1739 if (complete_rewrite &&
3aa1f7ca
JK
1740 (textconv_one || !diff_filespec_is_binary(one)) &&
1741 (textconv_two || !diff_filespec_is_binary(two))) {
3e97c7c6
GB
1742 fprintf(o->file, "%s", header.buf);
1743 strbuf_reset(&header);
3aa1f7ca
JK
1744 emit_rewrite_diff(name_a, name_b, one, two,
1745 textconv_one, textconv_two, o);
34a5e1a2 1746 o->found_changes = 1;
6973dcae
JH
1747 goto free_ab_and_return;
1748 }
1749 }
1750
8f67f8ae 1751 if (!DIFF_OPT_TST(o, TEXT) &&
b3373982
JK
1752 ( (!textconv_one && diff_filespec_is_binary(one)) ||
1753 (!textconv_two && diff_filespec_is_binary(two)) )) {
1754 if (fill_mmfile(&mf1, one) < 0 || fill_mmfile(&mf2, two) < 0)
1755 die("unable to read files to diff");
0660626c
JH
1756 /* Quite common confusing case */
1757 if (mf1.size == mf2.size &&
1758 !memcmp(mf1.ptr, mf2.ptr, mf1.size))
1759 goto free_ab_and_return;
3e97c7c6
GB
1760 fprintf(o->file, "%s", header.buf);
1761 strbuf_reset(&header);
8f67f8ae 1762 if (DIFF_OPT_TST(o, BINARY))
c0c77734 1763 emit_binary_diff(o->file, &mf1, &mf2);
051308f6 1764 else
c0c77734
DB
1765 fprintf(o->file, "Binary files %s and %s differ\n",
1766 lbl[0], lbl[1]);
34a5e1a2 1767 o->found_changes = 1;
051308f6 1768 }
6973dcae
JH
1769 else {
1770 /* Crazy xdl interfaces.. */
1771 const char *diffopts = getenv("GIT_DIFF_OPTS");
1772 xpparam_t xpp;
1773 xdemitconf_t xecfg;
6973dcae 1774 struct emit_callback ecbdata;
be58e70d 1775 const struct userdiff_funcname *pe;
f258475a 1776
3e97c7c6
GB
1777 if (!DIFF_XDL_TST(o, WHITESPACE_FLAGS)) {
1778 fprintf(o->file, "%s", header.buf);
1779 strbuf_reset(&header);
1780 }
1781
840383b2
JK
1782 mf1.size = fill_textconv(textconv_one, one, &mf1.ptr);
1783 mf2.size = fill_textconv(textconv_two, two, &mf2.ptr);
04427ac8 1784
45e7ca0f
BC
1785 pe = diff_funcname_pattern(one);
1786 if (!pe)
1787 pe = diff_funcname_pattern(two);
6973dcae 1788
9ccd0a88 1789 memset(&xpp, 0, sizeof(xpp));
30b25010 1790 memset(&xecfg, 0, sizeof(xecfg));
cd112cef 1791 memset(&ecbdata, 0, sizeof(ecbdata));
6973dcae 1792 ecbdata.label_path = lbl;
8f67f8ae 1793 ecbdata.color_diff = DIFF_OPT_TST(o, COLOR_DIFF);
34a5e1a2 1794 ecbdata.found_changesp = &o->found_changes;
cf1b7869 1795 ecbdata.ws_rule = whitespace_rule(name_b ? name_b : name_a);
690ed843 1796 if (ecbdata.ws_rule & WS_BLANK_AT_EOF)
d68fe26f 1797 check_blank_at_eof(&mf1, &mf2, &ecbdata);
a3c158d4 1798 ecbdata.opt = o;
3e97c7c6 1799 ecbdata.header = header.len ? &header : NULL;
0d21efa5 1800 xpp.flags = XDF_NEED_MINIMAL | o->xdl_opts;
ee1e5412 1801 xecfg.ctxlen = o->context;
6d0e674a 1802 xecfg.interhunkctxlen = o->interhunkcontext;
6973dcae 1803 xecfg.flags = XDL_EMIT_FUNCNAMES;
45e7ca0f 1804 if (pe)
a013585b 1805 xdiff_set_find_func(&xecfg, pe->pattern, pe->cflags);
6973dcae
JH
1806 if (!diffopts)
1807 ;
cc44c765 1808 else if (!prefixcmp(diffopts, "--unified="))
6973dcae 1809 xecfg.ctxlen = strtoul(diffopts + 10, NULL, 10);
cc44c765 1810 else if (!prefixcmp(diffopts, "-u"))
6973dcae 1811 xecfg.ctxlen = strtoul(diffopts + 2, NULL, 10);
882749a0
TR
1812 if (o->word_diff) {
1813 int i;
1814
f59a59e2
JS
1815 ecbdata.diff_words =
1816 xcalloc(1, sizeof(struct diff_words_data));
c0c77734 1817 ecbdata.diff_words->file = o->file;
882749a0 1818 ecbdata.diff_words->type = o->word_diff;
80c49c3d
TR
1819 if (!o->word_regex)
1820 o->word_regex = userdiff_word_regex(one);
1821 if (!o->word_regex)
1822 o->word_regex = userdiff_word_regex(two);
98a4d87b
BSSJ
1823 if (!o->word_regex)
1824 o->word_regex = diff_word_regex_cfg;
2b6a5417
JS
1825 if (o->word_regex) {
1826 ecbdata.diff_words->word_regex = (regex_t *)
1827 xmalloc(sizeof(regex_t));
1828 if (regcomp(ecbdata.diff_words->word_regex,
bf82940d
TR
1829 o->word_regex,
1830 REG_EXTENDED | REG_NEWLINE))
2b6a5417
JS
1831 die ("Invalid regular expression: %s",
1832 o->word_regex);
1833 }
882749a0
TR
1834 for (i = 0; i < ARRAY_SIZE(diff_words_styles); i++) {
1835 if (o->word_diff == diff_words_styles[i].type) {
1836 ecbdata.diff_words->style =
1837 &diff_words_styles[i];
1838 break;
1839 }
1840 }
1841 if (DIFF_OPT_TST(o, COLOR_DIFF)) {
1842 struct diff_words_style *st = ecbdata.diff_words->style;
1843 st->old.color = diff_get_color_opt(o, DIFF_FILE_OLD);
1844 st->new.color = diff_get_color_opt(o, DIFF_FILE_NEW);
1845 st->ctx.color = diff_get_color_opt(o, DIFF_PLAIN);
1846 }
c0c77734 1847 }
8a3f524b 1848 xdi_diff_outf(&mf1, &mf2, fn_out_consume, &ecbdata,
dfea7900 1849 &xpp, &xecfg);
882749a0 1850 if (o->word_diff)
f59a59e2 1851 free_diff_words_data(&ecbdata);
04427ac8
JK
1852 if (textconv_one)
1853 free(mf1.ptr);
1854 if (textconv_two)
1855 free(mf2.ptr);
8cfe5f1c 1856 xdiff_clear_find_func(&xecfg);
6973dcae
JH
1857 }
1858
1859 free_ab_and_return:
3e97c7c6 1860 strbuf_release(&header);
fc3abdf5
JH
1861 diff_free_filespec_data(one);
1862 diff_free_filespec_data(two);
6973dcae
JH
1863 free(a_one);
1864 free(b_two);
1865 return;
1866}
1867
1868static void builtin_diffstat(const char *name_a, const char *name_b,
1869 struct diff_filespec *one,
1870 struct diff_filespec *two,
710158e3 1871 struct diffstat_t *diffstat,
0d21efa5 1872 struct diff_options *o,
710158e3 1873 int complete_rewrite)
6973dcae
JH
1874{
1875 mmfile_t mf1, mf2;
1876 struct diffstat_file *data;
1877
1878 data = diffstat_add(diffstat, name_a, name_b);
1879
1880 if (!one || !two) {
1881 data->is_unmerged = 1;
1882 return;
1883 }
710158e3
JH
1884 if (complete_rewrite) {
1885 diff_populate_filespec(one, 0);
1886 diff_populate_filespec(two, 0);
1887 data->deleted = count_lines(one->data, one->size);
1888 data->added = count_lines(two->data, two->size);
fc3abdf5 1889 goto free_and_return;
710158e3 1890 }
6973dcae
JH
1891 if (fill_mmfile(&mf1, one) < 0 || fill_mmfile(&mf2, two) < 0)
1892 die("unable to read files to diff");
1893
29a3eefd 1894 if (diff_filespec_is_binary(one) || diff_filespec_is_binary(two)) {
6973dcae 1895 data->is_binary = 1;
b1882587
AP
1896 data->added = mf2.size;
1897 data->deleted = mf1.size;
1898 } else {
6973dcae
JH
1899 /* Crazy xdl interfaces.. */
1900 xpparam_t xpp;
1901 xdemitconf_t xecfg;
6973dcae 1902
9ccd0a88 1903 memset(&xpp, 0, sizeof(xpp));
30b25010 1904 memset(&xecfg, 0, sizeof(xecfg));
0d21efa5 1905 xpp.flags = XDF_NEED_MINIMAL | o->xdl_opts;
8a3f524b 1906 xdi_diff_outf(&mf1, &mf2, diffstat_consume, diffstat,
dfea7900 1907 &xpp, &xecfg);
6973dcae 1908 }
fc3abdf5
JH
1909
1910 free_and_return:
1911 diff_free_filespec_data(one);
1912 diff_free_filespec_data(two);
6973dcae
JH
1913}
1914
88246898 1915static void builtin_checkdiff(const char *name_a, const char *name_b,
cd676a51 1916 const char *attr_path,
5ff10dd6
JH
1917 struct diff_filespec *one,
1918 struct diff_filespec *two,
1919 struct diff_options *o)
88246898
JS
1920{
1921 mmfile_t mf1, mf2;
1922 struct checkdiff_t data;
1923
1924 if (!two)
1925 return;
1926
1927 memset(&data, 0, sizeof(data));
88246898
JS
1928 data.filename = name_b ? name_b : name_a;
1929 data.lineno = 0;
1ba111d1 1930 data.o = o;
cd676a51 1931 data.ws_rule = whitespace_rule(attr_path);
a757c646 1932 data.conflict_marker_size = ll_merge_marker_size(attr_path);
88246898
JS
1933
1934 if (fill_mmfile(&mf1, one) < 0 || fill_mmfile(&mf2, two) < 0)
1935 die("unable to read files to diff");
1936
5ff10dd6
JH
1937 /*
1938 * All the other codepaths check both sides, but not checking
1939 * the "old" side here is deliberate. We are checking the newly
1940 * introduced changes, and as long as the "new" side is text, we
1941 * can and should check what it introduces.
1942 */
29a3eefd 1943 if (diff_filespec_is_binary(two))
fc3abdf5 1944 goto free_and_return;
88246898
JS
1945 else {
1946 /* Crazy xdl interfaces.. */
1947 xpparam_t xpp;
1948 xdemitconf_t xecfg;
88246898 1949
9ccd0a88 1950 memset(&xpp, 0, sizeof(xpp));
30b25010 1951 memset(&xecfg, 0, sizeof(xecfg));
c35539eb 1952 xecfg.ctxlen = 1; /* at least one context line */
88246898 1953 xpp.flags = XDF_NEED_MINIMAL;
8a3f524b 1954 xdi_diff_outf(&mf1, &mf2, checkdiff_consume, &data,
dfea7900 1955 &xpp, &xecfg);
877f23cc 1956
467babf8 1957 if (data.ws_rule & WS_BLANK_AT_EOF) {
d68fe26f
JH
1958 struct emit_callback ecbdata;
1959 int blank_at_eof;
1960
1961 ecbdata.ws_rule = data.ws_rule;
1962 check_blank_at_eof(&mf1, &mf2, &ecbdata);
1963 blank_at_eof = ecbdata.blank_at_eof_in_preimage;
1964
467babf8
JH
1965 if (blank_at_eof) {
1966 static char *err;
1967 if (!err)
1968 err = whitespace_error_string(WS_BLANK_AT_EOF);
1969 fprintf(o->file, "%s:%d: %s.\n",
1970 data.filename, blank_at_eof, err);
1971 data.status = 1; /* report errors */
1972 }
877f23cc 1973 }
88246898 1974 }
fc3abdf5
JH
1975 free_and_return:
1976 diff_free_filespec_data(one);
1977 diff_free_filespec_data(two);
62c64895
WC
1978 if (data.status)
1979 DIFF_OPT_SET(o, CHECK_FAILED);
88246898
JS
1980}
1981
6973dcae
JH
1982struct diff_filespec *alloc_filespec(const char *path)
1983{
1984 int namelen = strlen(path);
1985 struct diff_filespec *spec = xmalloc(sizeof(*spec) + namelen + 1);
1986
1987 memset(spec, 0, sizeof(*spec));
1988 spec->path = (char *)(spec + 1);
1989 memcpy(spec->path, path, namelen+1);
9fb88419 1990 spec->count = 1;
122aa6f9 1991 spec->is_binary = -1;
6973dcae
JH
1992 return spec;
1993}
1994
9fb88419
LT
1995void free_filespec(struct diff_filespec *spec)
1996{
1997 if (!--spec->count) {
1998 diff_free_filespec_data(spec);
1999 free(spec);
2000 }
2001}
2002
6973dcae
JH
2003void fill_filespec(struct diff_filespec *spec, const unsigned char *sha1,
2004 unsigned short mode)
2005{
2006 if (mode) {
2007 spec->mode = canon_mode(mode);
e702496e 2008 hashcpy(spec->sha1, sha1);
0bef57ee 2009 spec->sha1_valid = !is_null_sha1(sha1);
6973dcae
JH
2010 }
2011}
2012
2013/*
5adf317b 2014 * Given a name and sha1 pair, if the index tells us the file in
6973dcae
JH
2015 * the work tree has that object contents, return true, so that
2016 * prepare_temp_file() does not have to inflate and extract.
2017 */
1510fea7 2018static int reuse_worktree_file(const char *name, const unsigned char *sha1, int want_file)
6973dcae
JH
2019{
2020 struct cache_entry *ce;
2021 struct stat st;
2022 int pos, len;
2023
150115ad
JH
2024 /*
2025 * We do not read the cache ourselves here, because the
6973dcae
JH
2026 * benchmark with my previous version that always reads cache
2027 * shows that it makes things worse for diff-tree comparing
2028 * two linux-2.6 kernel trees in an already checked out work
2029 * tree. This is because most diff-tree comparisons deal with
2030 * only a small number of files, while reading the cache is
2031 * expensive for a large project, and its cost outweighs the
2032 * savings we get by not inflating the object to a temporary
2033 * file. Practically, this code only helps when we are used
2034 * by diff-cache --cached, which does read the cache before
2035 * calling us.
2036 */
2037 if (!active_cache)
2038 return 0;
2039
1510fea7
SP
2040 /* We want to avoid the working directory if our caller
2041 * doesn't need the data in a normal file, this system
2042 * is rather slow with its stat/open/mmap/close syscalls,
2043 * and the object is contained in a pack file. The pack
2044 * is probably already open and will be faster to obtain
2045 * the data through than the working directory. Loose
2046 * objects however would tend to be slower as they need
2047 * to be individually opened and inflated.
2048 */
cd673c1f 2049 if (!FAST_WORKING_DIRECTORY && !want_file && has_sha1_pack(sha1))
1510fea7
SP
2050 return 0;
2051
6973dcae
JH
2052 len = strlen(name);
2053 pos = cache_name_pos(name, len);
2054 if (pos < 0)
2055 return 0;
2056 ce = active_cache[pos];
eadb5831
JH
2057
2058 /*
2059 * This is not the sha1 we are looking for, or
2060 * unreusable because it is not a regular file.
2061 */
2062 if (hashcmp(sha1, ce->sha1) || !S_ISREG(ce->ce_mode))
6973dcae 2063 return 0;
eadb5831 2064
150115ad
JH
2065 /*
2066 * If ce is marked as "assume unchanged", there is no
2067 * guarantee that work tree matches what we are looking for.
2068 */
b4d1690d 2069 if ((ce->ce_flags & CE_VALID) || ce_skip_worktree(ce))
150115ad
JH
2070 return 0;
2071
eadb5831
JH
2072 /*
2073 * If ce matches the file in the work tree, we can reuse it.
6973dcae 2074 */
eadb5831
JH
2075 if (ce_uptodate(ce) ||
2076 (!lstat(name, &st) && !ce_match_stat(ce, &st, 0)))
2077 return 1;
2078
2079 return 0;
6973dcae
JH
2080}
2081
3afaa72d
JH
2082static int populate_from_stdin(struct diff_filespec *s)
2083{
f285a2d7 2084 struct strbuf buf = STRBUF_INIT;
c32f749f 2085 size_t size = 0;
af6eb822 2086
f1696ee3 2087 if (strbuf_read(&buf, 0, 0) < 0)
af6eb822 2088 return error("error while reading from stdin %s",
3afaa72d 2089 strerror(errno));
af6eb822 2090
3afaa72d 2091 s->should_munmap = 0;
c32f749f
RS
2092 s->data = strbuf_detach(&buf, &size);
2093 s->size = size;
3afaa72d
JH
2094 s->should_free = 1;
2095 return 0;
2096}
2097
04786756
LT
2098static int diff_populate_gitlink(struct diff_filespec *s, int size_only)
2099{
2100 int len;
8e08b419
JH
2101 char *data = xmalloc(100), *dirty = "";
2102
2103 /* Are we looking at the work tree? */
85adbf2f 2104 if (s->dirty_submodule)
8e08b419
JH
2105 dirty = "-dirty";
2106
04786756 2107 len = snprintf(data, 100,
8e08b419 2108 "Subproject commit %s%s\n", sha1_to_hex(s->sha1), dirty);
04786756
LT
2109 s->data = data;
2110 s->size = len;
2111 s->should_free = 1;
2112 if (size_only) {
2113 s->data = NULL;
2114 free(data);
2115 }
2116 return 0;
2117}
2118
6973dcae
JH
2119/*
2120 * While doing rename detection and pickaxe operation, we may need to
2121 * grab the data for the blob (or file) for our own in-core comparison.
2122 * diff_filespec has data and size fields for this purpose.
2123 */
2124int diff_populate_filespec(struct diff_filespec *s, int size_only)
2125{
2126 int err = 0;
2127 if (!DIFF_FILE_VALID(s))
2128 die("internal error: asking to populate invalid file.");
2129 if (S_ISDIR(s->mode))
2130 return -1;
2131
6973dcae 2132 if (s->data)
fc3abdf5 2133 return 0;
04786756 2134
6e0b8ed6
JH
2135 if (size_only && 0 < s->size)
2136 return 0;
2137
302b9282 2138 if (S_ISGITLINK(s->mode))
04786756
LT
2139 return diff_populate_gitlink(s, size_only);
2140
6973dcae 2141 if (!s->sha1_valid ||
1510fea7 2142 reuse_worktree_file(s->path, s->sha1, 0)) {
f285a2d7 2143 struct strbuf buf = STRBUF_INIT;
6973dcae
JH
2144 struct stat st;
2145 int fd;
6c510bee 2146
3afaa72d
JH
2147 if (!strcmp(s->path, "-"))
2148 return populate_from_stdin(s);
2149
6973dcae
JH
2150 if (lstat(s->path, &st) < 0) {
2151 if (errno == ENOENT) {
2152 err_empty:
2153 err = -1;
2154 empty:
d2543b8e 2155 s->data = (char *)"";
6973dcae
JH
2156 s->size = 0;
2157 return err;
2158 }
2159 }
dc49cd76 2160 s->size = xsize_t(st.st_size);
6973dcae
JH
2161 if (!s->size)
2162 goto empty;
6973dcae 2163 if (S_ISLNK(st.st_mode)) {
cf219d8c
LT
2164 struct strbuf sb = STRBUF_INIT;
2165
2166 if (strbuf_readlink(&sb, s->path, s->size))
6973dcae 2167 goto err_empty;
0956a6db
RS
2168 s->size = sb.len;
2169 s->data = strbuf_detach(&sb, NULL);
cf219d8c 2170 s->should_free = 1;
6973dcae
JH
2171 return 0;
2172 }
cf219d8c
LT
2173 if (size_only)
2174 return 0;
6973dcae
JH
2175 fd = open(s->path, O_RDONLY);
2176 if (fd < 0)
2177 goto err_empty;
c4712e45 2178 s->data = xmmap(NULL, s->size, PROT_READ, MAP_PRIVATE, fd, 0);
6973dcae 2179 close(fd);
6973dcae 2180 s->should_munmap = 1;
6c510bee
LT
2181
2182 /*
2183 * Convert from working tree format to canonical git format
2184 */
21e5ad50 2185 if (convert_to_git(s->path, s->data, s->size, &buf, safe_crlf)) {
c32f749f 2186 size_t size = 0;
6c510bee
LT
2187 munmap(s->data, s->size);
2188 s->should_munmap = 0;
c32f749f
RS
2189 s->data = strbuf_detach(&buf, &size);
2190 s->size = size;
6c510bee
LT
2191 s->should_free = 1;
2192 }
6973dcae
JH
2193 }
2194 else {
21666f1a 2195 enum object_type type;
6e0b8ed6 2196 if (size_only)
21666f1a 2197 type = sha1_object_info(s->sha1, &s->size);
6973dcae 2198 else {
21666f1a 2199 s->data = read_sha1_file(s->sha1, &type, &s->size);
6973dcae
JH
2200 s->should_free = 1;
2201 }
2202 }
2203 return 0;
2204}
2205
8ae92e63 2206void diff_free_filespec_blob(struct diff_filespec *s)
6973dcae
JH
2207{
2208 if (s->should_free)
2209 free(s->data);
2210 else if (s->should_munmap)
2211 munmap(s->data, s->size);
fc3abdf5
JH
2212
2213 if (s->should_free || s->should_munmap) {
2214 s->should_free = s->should_munmap = 0;
2215 s->data = NULL;
2216 }
eede7b7d
JK
2217}
2218
2219void diff_free_filespec_data(struct diff_filespec *s)
2220{
8ae92e63 2221 diff_free_filespec_blob(s);
6973dcae
JH
2222 free(s->cnt_data);
2223 s->cnt_data = NULL;
2224}
2225
4e218f54 2226static void prep_temp_blob(const char *path, struct diff_tempfile *temp,
6973dcae
JH
2227 void *blob,
2228 unsigned long size,
2229 const unsigned char *sha1,
2230 int mode)
2231{
2232 int fd;
4e218f54 2233 struct strbuf buf = STRBUF_INIT;
003b33a8
DA
2234 struct strbuf template = STRBUF_INIT;
2235 char *path_dup = xstrdup(path);
2236 const char *base = basename(path_dup);
6973dcae 2237
003b33a8
DA
2238 /* Generate "XXXXXX_basename.ext" */
2239 strbuf_addstr(&template, "XXXXXX_");
2240 strbuf_addstr(&template, base);
2241
2242 fd = git_mkstemps(temp->tmp_path, PATH_MAX, template.buf,
2243 strlen(base) + 1);
6973dcae 2244 if (fd < 0)
d824cbba 2245 die_errno("unable to create temp-file");
4e218f54
JS
2246 if (convert_to_working_tree(path,
2247 (const char *)blob, (size_t)size, &buf)) {
2248 blob = buf.buf;
2249 size = buf.len;
2250 }
93822c22 2251 if (write_in_full(fd, blob, size) != size)
0721c314 2252 die_errno("unable to write temp-file");
6973dcae
JH
2253 close(fd);
2254 temp->name = temp->tmp_path;
2255 strcpy(temp->hex, sha1_to_hex(sha1));
2256 temp->hex[40] = 0;
2257 sprintf(temp->mode, "%06o", mode);
4e218f54 2258 strbuf_release(&buf);
003b33a8
DA
2259 strbuf_release(&template);
2260 free(path_dup);
6973dcae
JH
2261}
2262
479b0ae8
JK
2263static struct diff_tempfile *prepare_temp_file(const char *name,
2264 struct diff_filespec *one)
6973dcae 2265{
479b0ae8
JK
2266 struct diff_tempfile *temp = claim_diff_tempfile();
2267
6973dcae
JH
2268 if (!DIFF_FILE_VALID(one)) {
2269 not_a_valid_file:
2270 /* A '-' entry produces this for file-2, and
2271 * a '+' entry produces this for file-1.
2272 */
2273 temp->name = "/dev/null";
2274 strcpy(temp->hex, ".");
2275 strcpy(temp->mode, ".");
479b0ae8
JK
2276 return temp;
2277 }
2278
2279 if (!remove_tempfile_installed) {
2280 atexit(remove_tempfile);
57b235a4 2281 sigchain_push_common(remove_tempfile_on_signal);
479b0ae8 2282 remove_tempfile_installed = 1;
6973dcae
JH
2283 }
2284
2285 if (!one->sha1_valid ||
1510fea7 2286 reuse_worktree_file(name, one->sha1, 1)) {
6973dcae
JH
2287 struct stat st;
2288 if (lstat(name, &st) < 0) {
2289 if (errno == ENOENT)
2290 goto not_a_valid_file;
d824cbba 2291 die_errno("stat(%s)", name);
6973dcae
JH
2292 }
2293 if (S_ISLNK(st.st_mode)) {
3cd7388d
JK
2294 struct strbuf sb = STRBUF_INIT;
2295 if (strbuf_readlink(&sb, name, st.st_size) < 0)
0721c314 2296 die_errno("readlink(%s)", name);
3cd7388d 2297 prep_temp_blob(name, temp, sb.buf, sb.len,
6973dcae
JH
2298 (one->sha1_valid ?
2299 one->sha1 : null_sha1),
2300 (one->sha1_valid ?
2301 one->mode : S_IFLNK));
3cd7388d 2302 strbuf_release(&sb);
6973dcae
JH
2303 }
2304 else {
2305 /* we can borrow from the file in the work tree */
2306 temp->name = name;
2307 if (!one->sha1_valid)
2308 strcpy(temp->hex, sha1_to_hex(null_sha1));
2309 else
2310 strcpy(temp->hex, sha1_to_hex(one->sha1));
2311 /* Even though we may sometimes borrow the
2312 * contents from the work tree, we always want
2313 * one->mode. mode is trustworthy even when
2314 * !(one->sha1_valid), as long as
2315 * DIFF_FILE_VALID(one).
2316 */
2317 sprintf(temp->mode, "%06o", one->mode);
2318 }
479b0ae8 2319 return temp;
6973dcae
JH
2320 }
2321 else {
2322 if (diff_populate_filespec(one, 0))
2323 die("cannot read data blob for %s", one->path);
4e218f54 2324 prep_temp_blob(name, temp, one->data, one->size,
6973dcae
JH
2325 one->sha1, one->mode);
2326 }
479b0ae8 2327 return temp;
6973dcae
JH
2328}
2329
6973dcae
JH
2330/* An external diff command takes:
2331 *
2332 * diff-cmd name infile1 infile1-sha1 infile1-mode \
2333 * infile2 infile2-sha1 infile2-mode [ rename-to ]
2334 *
2335 */
2336static void run_external_diff(const char *pgm,
2337 const char *name,
2338 const char *other,
2339 struct diff_filespec *one,
2340 struct diff_filespec *two,
2341 const char *xfrm_msg,
2342 int complete_rewrite)
2343{
2344 const char *spawn_arg[10];
6973dcae 2345 int retval;
6973dcae
JH
2346 const char **arg = &spawn_arg[0];
2347
6973dcae 2348 if (one && two) {
479b0ae8
JK
2349 struct diff_tempfile *temp_one, *temp_two;
2350 const char *othername = (other ? other : name);
2351 temp_one = prepare_temp_file(name, one);
2352 temp_two = prepare_temp_file(othername, two);
6973dcae
JH
2353 *arg++ = pgm;
2354 *arg++ = name;
479b0ae8
JK
2355 *arg++ = temp_one->name;
2356 *arg++ = temp_one->hex;
2357 *arg++ = temp_one->mode;
2358 *arg++ = temp_two->name;
2359 *arg++ = temp_two->hex;
2360 *arg++ = temp_two->mode;
6973dcae
JH
2361 if (other) {
2362 *arg++ = other;
2363 *arg++ = xfrm_msg;
2364 }
2365 } else {
2366 *arg++ = pgm;
2367 *arg++ = name;
2368 }
2369 *arg = NULL;
d5535ec7 2370 fflush(NULL);
7ed7fac4 2371 retval = run_command_v_opt(spawn_arg, RUN_USING_SHELL);
6973dcae
JH
2372 remove_tempfile();
2373 if (retval) {
2374 fprintf(stderr, "external diff died, stopping at %s.\n", name);
2375 exit(1);
2376 }
2377}
2378
b67b9612
JH
2379static int similarity_index(struct diff_filepair *p)
2380{
2381 return p->score * 100 / MAX_SCORE;
2382}
2383
2384static void fill_metainfo(struct strbuf *msg,
2385 const char *name,
2386 const char *other,
2387 struct diff_filespec *one,
2388 struct diff_filespec *two,
2389 struct diff_options *o,
2390 struct diff_filepair *p)
2391{
2392 strbuf_init(msg, PATH_MAX * 2 + 300);
2393 switch (p->status) {
2394 case DIFF_STATUS_COPIED:
2395 strbuf_addf(msg, "similarity index %d%%", similarity_index(p));
2396 strbuf_addstr(msg, "\ncopy from ");
2397 quote_c_style(name, msg, NULL, 0);
2398 strbuf_addstr(msg, "\ncopy to ");
2399 quote_c_style(other, msg, NULL, 0);
2400 strbuf_addch(msg, '\n');
2401 break;
2402 case DIFF_STATUS_RENAMED:
2403 strbuf_addf(msg, "similarity index %d%%", similarity_index(p));
2404 strbuf_addstr(msg, "\nrename from ");
2405 quote_c_style(name, msg, NULL, 0);
2406 strbuf_addstr(msg, "\nrename to ");
2407 quote_c_style(other, msg, NULL, 0);
2408 strbuf_addch(msg, '\n');
2409 break;
2410 case DIFF_STATUS_MODIFIED:
2411 if (p->score) {
2412 strbuf_addf(msg, "dissimilarity index %d%%\n",
2413 similarity_index(p));
2414 break;
2415 }
2416 /* fallthru */
2417 default:
2418 /* nothing */
2419 ;
2420 }
2421 if (one && two && hashcmp(one->sha1, two->sha1)) {
2422 int abbrev = DIFF_OPT_TST(o, FULL_INDEX) ? 40 : DEFAULT_ABBREV;
2423
2424 if (DIFF_OPT_TST(o, BINARY)) {
2425 mmfile_t mf;
2426 if ((!fill_mmfile(&mf, one) && diff_filespec_is_binary(one)) ||
2427 (!fill_mmfile(&mf, two) && diff_filespec_is_binary(two)))
2428 abbrev = 40;
2429 }
2430 strbuf_addf(msg, "index %.*s..%.*s",
2431 abbrev, sha1_to_hex(one->sha1),
2432 abbrev, sha1_to_hex(two->sha1));
2433 if (one->mode == two->mode)
2434 strbuf_addf(msg, " %06o", one->mode);
2435 strbuf_addch(msg, '\n');
2436 }
2437 if (msg->len)
2438 strbuf_setlen(msg, msg->len - 1);
2439}
2440
6973dcae
JH
2441static void run_diff_cmd(const char *pgm,
2442 const char *name,
2443 const char *other,
cd676a51 2444 const char *attr_path,
6973dcae
JH
2445 struct diff_filespec *one,
2446 struct diff_filespec *two,
b67b9612 2447 struct strbuf *msg,
051308f6 2448 struct diff_options *o,
b67b9612 2449 struct diff_filepair *p)
6973dcae 2450{
b67b9612
JH
2451 const char *xfrm_msg = NULL;
2452 int complete_rewrite = (p->status == DIFF_STATUS_MODIFIED) && p->score;
2453
2454 if (msg) {
2455 fill_metainfo(msg, name, other, one, two, o, p);
2456 xfrm_msg = msg->len ? msg->buf : NULL;
2457 }
2458
8f67f8ae 2459 if (!DIFF_OPT_TST(o, ALLOW_EXTERNAL))
f1af60bd
JH
2460 pgm = NULL;
2461 else {
be58e70d
JK
2462 struct userdiff_driver *drv = userdiff_find_by_path(attr_path);
2463 if (drv && drv->external)
2464 pgm = drv->external;
f1af60bd
JH
2465 }
2466
6973dcae
JH
2467 if (pgm) {
2468 run_external_diff(pgm, name, other, one, two, xfrm_msg,
2469 complete_rewrite);
2470 return;
2471 }
2472 if (one && two)
2473 builtin_diff(name, other ? other : name,
051308f6 2474 one, two, xfrm_msg, o, complete_rewrite);
6973dcae 2475 else
c0c77734 2476 fprintf(o->file, "* Unmerged path %s\n", name);
6973dcae
JH
2477}
2478
2479static void diff_fill_sha1_info(struct diff_filespec *one)
2480{
2481 if (DIFF_FILE_VALID(one)) {
2482 if (!one->sha1_valid) {
2483 struct stat st;
5332b2af
JS
2484 if (!strcmp(one->path, "-")) {
2485 hashcpy(one->sha1, null_sha1);
2486 return;
2487 }
6973dcae 2488 if (lstat(one->path, &st) < 0)
0721c314 2489 die_errno("stat '%s'", one->path);
6973dcae 2490 if (index_path(one->sha1, one->path, &st, 0))
d7530708 2491 die("cannot hash %s", one->path);
6973dcae
JH
2492 }
2493 }
2494 else
e702496e 2495 hashclr(one->sha1);
6973dcae
JH
2496}
2497
cd676a51
JH
2498static void strip_prefix(int prefix_length, const char **namep, const char **otherp)
2499{
2500 /* Strip the prefix but do not molest /dev/null and absolute paths */
2501 if (*namep && **namep != '/')
2502 *namep += prefix_length;
2503 if (*otherp && **otherp != '/')
2504 *otherp += prefix_length;
2505}
2506
6973dcae
JH
2507static void run_diff(struct diff_filepair *p, struct diff_options *o)
2508{
2509 const char *pgm = external_diff();
663af342 2510 struct strbuf msg;
663af342
PH
2511 struct diff_filespec *one = p->one;
2512 struct diff_filespec *two = p->two;
6973dcae
JH
2513 const char *name;
2514 const char *other;
cd676a51 2515 const char *attr_path;
663af342 2516
cd676a51
JH
2517 name = p->one->path;
2518 other = (strcmp(name, p->two->path) ? p->two->path : NULL);
2519 attr_path = name;
2520 if (o->prefix_length)
2521 strip_prefix(o->prefix_length, &name, &other);
6973dcae
JH
2522
2523 if (DIFF_PAIR_UNMERGED(p)) {
cd676a51 2524 run_diff_cmd(pgm, name, NULL, attr_path,
b67b9612 2525 NULL, NULL, NULL, o, p);
6973dcae
JH
2526 return;
2527 }
2528
6973dcae
JH
2529 diff_fill_sha1_info(one);
2530 diff_fill_sha1_info(two);
2531
6973dcae
JH
2532 if (!pgm &&
2533 DIFF_FILE_VALID(one) && DIFF_FILE_VALID(two) &&
2534 (S_IFMT & one->mode) != (S_IFMT & two->mode)) {
b67b9612
JH
2535 /*
2536 * a filepair that changes between file and symlink
6973dcae
JH
2537 * needs to be split into deletion and creation.
2538 */
2539 struct diff_filespec *null = alloc_filespec(two->path);
cd676a51 2540 run_diff_cmd(NULL, name, other, attr_path,
b67b9612 2541 one, null, &msg, o, p);
6973dcae 2542 free(null);
b67b9612
JH
2543 strbuf_release(&msg);
2544
6973dcae 2545 null = alloc_filespec(one->path);
cd676a51 2546 run_diff_cmd(NULL, name, other, attr_path,
b67b9612 2547 null, two, &msg, o, p);
6973dcae
JH
2548 free(null);
2549 }
2550 else
cd676a51 2551 run_diff_cmd(pgm, name, other, attr_path,
b67b9612 2552 one, two, &msg, o, p);
6973dcae 2553
663af342 2554 strbuf_release(&msg);
6973dcae
JH
2555}
2556
2557static void run_diffstat(struct diff_filepair *p, struct diff_options *o,
2558 struct diffstat_t *diffstat)
2559{
2560 const char *name;
2561 const char *other;
710158e3 2562 int complete_rewrite = 0;
6973dcae
JH
2563
2564 if (DIFF_PAIR_UNMERGED(p)) {
2565 /* unmerged */
0d21efa5 2566 builtin_diffstat(p->one->path, NULL, NULL, NULL, diffstat, o, 0);
6973dcae
JH
2567 return;
2568 }
2569
2570 name = p->one->path;
2571 other = (strcmp(name, p->two->path) ? p->two->path : NULL);
2572
cd676a51
JH
2573 if (o->prefix_length)
2574 strip_prefix(o->prefix_length, &name, &other);
2575
6973dcae
JH
2576 diff_fill_sha1_info(p->one);
2577 diff_fill_sha1_info(p->two);
2578
710158e3
JH
2579 if (p->status == DIFF_STATUS_MODIFIED && p->score)
2580 complete_rewrite = 1;
0d21efa5 2581 builtin_diffstat(name, other, p->one, p->two, diffstat, o, complete_rewrite);
6973dcae
JH
2582}
2583
88246898
JS
2584static void run_checkdiff(struct diff_filepair *p, struct diff_options *o)
2585{
2586 const char *name;
2587 const char *other;
cd676a51 2588 const char *attr_path;
88246898
JS
2589
2590 if (DIFF_PAIR_UNMERGED(p)) {
2591 /* unmerged */
2592 return;
2593 }
2594
2595 name = p->one->path;
2596 other = (strcmp(name, p->two->path) ? p->two->path : NULL);
cd676a51
JH
2597 attr_path = other ? other : name;
2598
2599 if (o->prefix_length)
2600 strip_prefix(o->prefix_length, &name, &other);
88246898
JS
2601
2602 diff_fill_sha1_info(p->one);
2603 diff_fill_sha1_info(p->two);
2604
cd676a51 2605 builtin_checkdiff(name, other, attr_path, p->one, p->two, o);
88246898
JS
2606}
2607
6973dcae
JH
2608void diff_setup(struct diff_options *options)
2609{
2610 memset(options, 0, sizeof(*options));
9ca5df90 2611 memset(&diff_queued_diff, 0, sizeof(diff_queued_diff));
c0c77734
DB
2612
2613 options->file = stdout;
2614
6973dcae
JH
2615 options->line_termination = '\n';
2616 options->break_opt = -1;
2617 options->rename_limit = -1;
7df7c019 2618 options->dirstat_percent = 3;
ee1e5412 2619 options->context = 3;
6973dcae
JH
2620
2621 options->change = diff_change;
2622 options->add_remove = diff_addremove;
6b2f2d98 2623 if (diff_use_color_default > 0)
8f67f8ae 2624 DIFF_OPT_SET(options, COLOR_DIFF);
b68ea12e 2625 options->detect_rename = diff_detect_rename_default;
eab9a40b 2626
a5a818ee
JH
2627 if (!diff_mnemonic_prefix) {
2628 options->a_prefix = "a/";
2629 options->b_prefix = "b/";
2630 }
6973dcae
JH
2631}
2632
2633int diff_setup_done(struct diff_options *options)
2634{
d7de00f7
TH
2635 int count = 0;
2636
2637 if (options->output_format & DIFF_FORMAT_NAME)
2638 count++;
2639 if (options->output_format & DIFF_FORMAT_NAME_STATUS)
2640 count++;
2641 if (options->output_format & DIFF_FORMAT_CHECKDIFF)
2642 count++;
2643 if (options->output_format & DIFF_FORMAT_NO_OUTPUT)
2644 count++;
2645 if (count > 1)
2646 die("--name-only, --name-status, --check and -s are mutually exclusive");
2647
f245194f
JH
2648 /*
2649 * Most of the time we can say "there are changes"
2650 * only by checking if there are changed paths, but
2651 * --ignore-whitespace* options force us to look
97bf2a08 2652 * inside contents.
f245194f
JH
2653 */
2654
2655 if (DIFF_XDL_TST(options, IGNORE_WHITESPACE) ||
2656 DIFF_XDL_TST(options, IGNORE_WHITESPACE_CHANGE) ||
2657 DIFF_XDL_TST(options, IGNORE_WHITESPACE_AT_EOL))
2658 DIFF_OPT_SET(options, DIFF_FROM_CONTENTS);
2659 else
2660 DIFF_OPT_CLR(options, DIFF_FROM_CONTENTS);
2661
8f67f8ae 2662 if (DIFF_OPT_TST(options, FIND_COPIES_HARDER))
03b9d560
JH
2663 options->detect_rename = DIFF_DETECT_COPY;
2664
cd676a51
JH
2665 if (!DIFF_OPT_TST(options, RELATIVE_NAME))
2666 options->prefix = NULL;
2667 if (options->prefix)
2668 options->prefix_length = strlen(options->prefix);
2669 else
2670 options->prefix_length = 0;
2671
c6744349
TH
2672 if (options->output_format & (DIFF_FORMAT_NAME |
2673 DIFF_FORMAT_NAME_STATUS |
2674 DIFF_FORMAT_CHECKDIFF |
2675 DIFF_FORMAT_NO_OUTPUT))
2676 options->output_format &= ~(DIFF_FORMAT_RAW |
74e2abe5 2677 DIFF_FORMAT_NUMSTAT |
c6744349 2678 DIFF_FORMAT_DIFFSTAT |
ebd124c6 2679 DIFF_FORMAT_SHORTSTAT |
7df7c019 2680 DIFF_FORMAT_DIRSTAT |
c6744349
TH
2681 DIFF_FORMAT_SUMMARY |
2682 DIFF_FORMAT_PATCH);
2683
6973dcae
JH
2684 /*
2685 * These cases always need recursive; we do not drop caller-supplied
2686 * recursive bits for other formats here.
2687 */
c6744349 2688 if (options->output_format & (DIFF_FORMAT_PATCH |
74e2abe5 2689 DIFF_FORMAT_NUMSTAT |
c6744349 2690 DIFF_FORMAT_DIFFSTAT |
ebd124c6 2691 DIFF_FORMAT_SHORTSTAT |
7df7c019 2692 DIFF_FORMAT_DIRSTAT |
d7014dc0 2693 DIFF_FORMAT_SUMMARY |
c6744349 2694 DIFF_FORMAT_CHECKDIFF))
8f67f8ae 2695 DIFF_OPT_SET(options, RECURSIVE);
5e363541 2696 /*
3969cf7d 2697 * Also pickaxe would not work very well if you do not say recursive
5e363541 2698 */
3969cf7d 2699 if (options->pickaxe)
8f67f8ae 2700 DIFF_OPT_SET(options, RECURSIVE);
ae6d5c1b
JL
2701 /*
2702 * When patches are generated, submodules diffed against the work tree
2703 * must be checked for dirtiness too so it can be shown in the output
2704 */
2705 if (options->output_format & DIFF_FORMAT_PATCH)
2706 DIFF_OPT_SET(options, DIRTY_SUBMODULES);
5e363541 2707
6973dcae
JH
2708 if (options->detect_rename && options->rename_limit < 0)
2709 options->rename_limit = diff_rename_limit_default;
2710 if (options->setup & DIFF_SETUP_USE_CACHE) {
2711 if (!active_cache)
2712 /* read-cache does not die even when it fails
2713 * so it is safe for us to do this here. Also
2714 * it does not smudge active_cache or active_nr
2715 * when it fails, so we do not have to worry about
2716 * cleaning it up ourselves either.
2717 */
2718 read_cache();
2719 }
6973dcae
JH
2720 if (options->abbrev <= 0 || 40 < options->abbrev)
2721 options->abbrev = 40; /* full */
2722
68aacb2f
JH
2723 /*
2724 * It does not make sense to show the first hit we happened
2725 * to have found. It does not make sense not to return with
2726 * exit code in such a case either.
2727 */
90b19941 2728 if (DIFF_OPT_TST(options, QUICK)) {
68aacb2f 2729 options->output_format = DIFF_FORMAT_NO_OUTPUT;
8f67f8ae 2730 DIFF_OPT_SET(options, EXIT_WITH_STATUS);
68aacb2f
JH
2731 }
2732
6973dcae
JH
2733 return 0;
2734}
2735
d2543b8e 2736static int opt_arg(const char *arg, int arg_short, const char *arg_long, int *val)
ee1e5412
LT
2737{
2738 char c, *eq;
2739 int len;
2740
2741 if (*arg != '-')
2742 return 0;
2743 c = *++arg;
2744 if (!c)
2745 return 0;
2746 if (c == arg_short) {
2747 c = *++arg;
2748 if (!c)
2749 return 1;
2750 if (val && isdigit(c)) {
2751 char *end;
2752 int n = strtoul(arg, &end, 10);
2753 if (*end)
2754 return 0;
2755 *val = n;
2756 return 1;
2757 }
2758 return 0;
2759 }
2760 if (c != '-')
2761 return 0;
2762 arg++;
2763 eq = strchr(arg, '=');
2764 if (eq)
2765 len = eq - arg;
2766 else
2767 len = strlen(arg);
2768 if (!len || strncmp(arg, arg_long, len))
2769 return 0;
2770 if (eq) {
2771 int n;
2772 char *end;
2773 if (!isdigit(*++eq))
2774 return 0;
2775 n = strtoul(eq, &end, 10);
2776 if (*end)
2777 return 0;
2778 *val = n;
2779 }
2780 return 1;
2781}
2782
16befb8b
JH
2783static int diff_scoreopt_parse(const char *opt);
2784
6973dcae
JH
2785int diff_opt_parse(struct diff_options *options, const char **av, int ac)
2786{
2787 const char *arg = av[0];
d054680c
PH
2788
2789 /* Output format options */
1c9eecff 2790 if (!strcmp(arg, "-p") || !strcmp(arg, "-u") || !strcmp(arg, "--patch"))
c6744349 2791 options->output_format |= DIFF_FORMAT_PATCH;
ee1e5412 2792 else if (opt_arg(arg, 'U', "unified", &options->context))
c6744349 2793 options->output_format |= DIFF_FORMAT_PATCH;
a610786f
TH
2794 else if (!strcmp(arg, "--raw"))
2795 options->output_format |= DIFF_FORMAT_RAW;
8f67f8ae 2796 else if (!strcmp(arg, "--patch-with-raw"))
c6744349 2797 options->output_format |= DIFF_FORMAT_PATCH | DIFF_FORMAT_RAW;
8f67f8ae 2798 else if (!strcmp(arg, "--numstat"))
74e2abe5 2799 options->output_format |= DIFF_FORMAT_NUMSTAT;
8f67f8ae 2800 else if (!strcmp(arg, "--shortstat"))
ebd124c6 2801 options->output_format |= DIFF_FORMAT_SHORTSTAT;
7df7c019
LT
2802 else if (opt_arg(arg, 'X', "dirstat", &options->dirstat_percent))
2803 options->output_format |= DIFF_FORMAT_DIRSTAT;
f88d225f
JH
2804 else if (!strcmp(arg, "--cumulative")) {
2805 options->output_format |= DIFF_FORMAT_DIRSTAT;
2806 DIFF_OPT_SET(options, DIRSTAT_CUMULATIVE);
fd33777b
HO
2807 } else if (opt_arg(arg, 0, "dirstat-by-file",
2808 &options->dirstat_percent)) {
2809 options->output_format |= DIFF_FORMAT_DIRSTAT;
2810 DIFF_OPT_SET(options, DIRSTAT_BY_FILE);
f88d225f 2811 }
d054680c
PH
2812 else if (!strcmp(arg, "--check"))
2813 options->output_format |= DIFF_FORMAT_CHECKDIFF;
2814 else if (!strcmp(arg, "--summary"))
2815 options->output_format |= DIFF_FORMAT_SUMMARY;
2816 else if (!strcmp(arg, "--patch-with-stat"))
2817 options->output_format |= DIFF_FORMAT_PATCH | DIFF_FORMAT_DIFFSTAT;
2818 else if (!strcmp(arg, "--name-only"))
2819 options->output_format |= DIFF_FORMAT_NAME;
2820 else if (!strcmp(arg, "--name-status"))
2821 options->output_format |= DIFF_FORMAT_NAME_STATUS;
2822 else if (!strcmp(arg, "-s"))
2823 options->output_format |= DIFF_FORMAT_NO_OUTPUT;
cc44c765 2824 else if (!prefixcmp(arg, "--stat")) {
5c5b2ea9
LT
2825 char *end;
2826 int width = options->stat_width;
2827 int name_width = options->stat_name_width;
2828 arg += 6;
2829 end = (char *)arg;
2830
2831 switch (*arg) {
2832 case '-':
cc44c765 2833 if (!prefixcmp(arg, "-width="))
5c5b2ea9 2834 width = strtoul(arg + 7, &end, 10);
cc44c765 2835 else if (!prefixcmp(arg, "-name-width="))
5c5b2ea9
LT
2836 name_width = strtoul(arg + 12, &end, 10);
2837 break;
2838 case '=':
2839 width = strtoul(arg+1, &end, 10);
2840 if (*end == ',')
2841 name_width = strtoul(end+1, &end, 10);
2842 }
2843
2844 /* Important! This checks all the error cases! */
2845 if (*end)
2846 return 0;
c6744349 2847 options->output_format |= DIFF_FORMAT_DIFFSTAT;
5c5b2ea9
LT
2848 options->stat_name_width = name_width;
2849 options->stat_width = width;
a2540023 2850 }
d054680c
PH
2851
2852 /* renames options */
cc44c765 2853 else if (!prefixcmp(arg, "-B")) {
8f67f8ae 2854 if ((options->break_opt = diff_scoreopt_parse(arg)) == -1)
6973dcae
JH
2855 return -1;
2856 }
cc44c765 2857 else if (!prefixcmp(arg, "-M")) {
8f67f8ae 2858 if ((options->rename_score = diff_scoreopt_parse(arg)) == -1)
6973dcae
JH
2859 return -1;
2860 options->detect_rename = DIFF_DETECT_RENAME;
2861 }
cc44c765 2862 else if (!prefixcmp(arg, "-C")) {
ca6c0970 2863 if (options->detect_rename == DIFF_DETECT_COPY)
8f67f8ae
PH
2864 DIFF_OPT_SET(options, FIND_COPIES_HARDER);
2865 if ((options->rename_score = diff_scoreopt_parse(arg)) == -1)
6973dcae
JH
2866 return -1;
2867 options->detect_rename = DIFF_DETECT_COPY;
2868 }
d054680c
PH
2869 else if (!strcmp(arg, "--no-renames"))
2870 options->detect_rename = 0;
cd676a51
JH
2871 else if (!strcmp(arg, "--relative"))
2872 DIFF_OPT_SET(options, RELATIVE_NAME);
c0cb4a06
JH
2873 else if (!prefixcmp(arg, "--relative=")) {
2874 DIFF_OPT_SET(options, RELATIVE_NAME);
2875 options->prefix = arg + 11;
2876 }
d054680c
PH
2877
2878 /* xdiff options */
2879 else if (!strcmp(arg, "-w") || !strcmp(arg, "--ignore-all-space"))
628d5c2b 2880 DIFF_XDL_SET(options, IGNORE_WHITESPACE);
d054680c 2881 else if (!strcmp(arg, "-b") || !strcmp(arg, "--ignore-space-change"))
628d5c2b 2882 DIFF_XDL_SET(options, IGNORE_WHITESPACE_CHANGE);
d054680c 2883 else if (!strcmp(arg, "--ignore-space-at-eol"))
628d5c2b 2884 DIFF_XDL_SET(options, IGNORE_WHITESPACE_AT_EOL);
34292bdd 2885 else if (!strcmp(arg, "--patience"))
628d5c2b 2886 DIFF_XDL_SET(options, PATIENCE_DIFF);
d054680c
PH
2887
2888 /* flags options */
2889 else if (!strcmp(arg, "--binary")) {
2890 options->output_format |= DIFF_FORMAT_PATCH;
2891 DIFF_OPT_SET(options, BINARY);
2892 }
2893 else if (!strcmp(arg, "--full-index"))
2894 DIFF_OPT_SET(options, FULL_INDEX);
2895 else if (!strcmp(arg, "-a") || !strcmp(arg, "--text"))
2896 DIFF_OPT_SET(options, TEXT);
2897 else if (!strcmp(arg, "-R"))
2898 DIFF_OPT_SET(options, REVERSE_DIFF);
6973dcae 2899 else if (!strcmp(arg, "--find-copies-harder"))
8f67f8ae 2900 DIFF_OPT_SET(options, FIND_COPIES_HARDER);
750f7b66 2901 else if (!strcmp(arg, "--follow"))
8f67f8ae 2902 DIFF_OPT_SET(options, FOLLOW_RENAMES);
cd112cef 2903 else if (!strcmp(arg, "--color"))
8f67f8ae 2904 DIFF_OPT_SET(options, COLOR_DIFF);
73e9da01
ML
2905 else if (!prefixcmp(arg, "--color=")) {
2906 int value = git_config_colorbool(NULL, arg+8, -1);
2907 if (value == 0)
2908 DIFF_OPT_CLR(options, COLOR_DIFF);
2909 else if (value > 0)
2910 DIFF_OPT_SET(options, COLOR_DIFF);
2911 else
2912 return error("option `color' expects \"always\", \"auto\", or \"never\"");
2913 }
fef88bb0 2914 else if (!strcmp(arg, "--no-color"))
8f67f8ae 2915 DIFF_OPT_CLR(options, COLOR_DIFF);
628d5c2b
KC
2916 else if (!strcmp(arg, "--color-words")) {
2917 DIFF_OPT_SET(options, COLOR_DIFF);
882749a0 2918 options->word_diff = DIFF_WORDS_COLOR;
628d5c2b 2919 }
2b6a5417 2920 else if (!prefixcmp(arg, "--color-words=")) {
628d5c2b 2921 DIFF_OPT_SET(options, COLOR_DIFF);
882749a0 2922 options->word_diff = DIFF_WORDS_COLOR;
2b6a5417
JS
2923 options->word_regex = arg + 14;
2924 }
882749a0
TR
2925 else if (!strcmp(arg, "--word-diff")) {
2926 if (options->word_diff == DIFF_WORDS_NONE)
2927 options->word_diff = DIFF_WORDS_PLAIN;
2928 }
2929 else if (!prefixcmp(arg, "--word-diff=")) {
2930 const char *type = arg + 12;
2931 if (!strcmp(type, "plain"))
2932 options->word_diff = DIFF_WORDS_PLAIN;
2933 else if (!strcmp(type, "color")) {
2934 DIFF_OPT_SET(options, COLOR_DIFF);
2935 options->word_diff = DIFF_WORDS_COLOR;
2936 }
2937 else if (!strcmp(type, "porcelain"))
2938 options->word_diff = DIFF_WORDS_PORCELAIN;
2939 else if (!strcmp(type, "none"))
2940 options->word_diff = DIFF_WORDS_NONE;
2941 else
2942 die("bad --word-diff argument: %s", type);
2943 }
2944 else if (!prefixcmp(arg, "--word-diff-regex=")) {
2945 if (options->word_diff == DIFF_WORDS_NONE)
2946 options->word_diff = DIFF_WORDS_PLAIN;
2947 options->word_regex = arg + 18;
2948 }
41bbf9d5 2949 else if (!strcmp(arg, "--exit-code"))
8f67f8ae 2950 DIFF_OPT_SET(options, EXIT_WITH_STATUS);
68aacb2f 2951 else if (!strcmp(arg, "--quiet"))
90b19941 2952 DIFF_OPT_SET(options, QUICK);
72909bef 2953 else if (!strcmp(arg, "--ext-diff"))
8f67f8ae 2954 DIFF_OPT_SET(options, ALLOW_EXTERNAL);
72909bef 2955 else if (!strcmp(arg, "--no-ext-diff"))
8f67f8ae 2956 DIFF_OPT_CLR(options, ALLOW_EXTERNAL);
e10ea812
JK
2957 else if (!strcmp(arg, "--textconv"))
2958 DIFF_OPT_SET(options, ALLOW_TEXTCONV);
2959 else if (!strcmp(arg, "--no-textconv"))
2960 DIFF_OPT_CLR(options, ALLOW_TEXTCONV);
50fd9bd8
JS
2961 else if (!strcmp(arg, "--ignore-submodules"))
2962 DIFF_OPT_SET(options, IGNORE_SUBMODULES);
752c0c24
JS
2963 else if (!strcmp(arg, "--submodule"))
2964 DIFF_OPT_SET(options, SUBMODULE_LOG);
2965 else if (!prefixcmp(arg, "--submodule=")) {
2966 if (!strcmp(arg + 12, "log"))
2967 DIFF_OPT_SET(options, SUBMODULE_LOG);
2968 }
d054680c
PH
2969
2970 /* misc options */
2971 else if (!strcmp(arg, "-z"))
2972 options->line_termination = 0;
2973 else if (!prefixcmp(arg, "-l"))
2974 options->rename_limit = strtoul(arg+2, NULL, 10);
2975 else if (!prefixcmp(arg, "-S"))
2976 options->pickaxe = arg + 2;
2977 else if (!strcmp(arg, "--pickaxe-all"))
2978 options->pickaxe_opts = DIFF_PICKAXE_ALL;
2979 else if (!strcmp(arg, "--pickaxe-regex"))
2980 options->pickaxe_opts = DIFF_PICKAXE_REGEX;
2981 else if (!prefixcmp(arg, "-O"))
2982 options->orderfile = arg + 2;
2983 else if (!prefixcmp(arg, "--diff-filter="))
2984 options->filter = arg + 14;
2985 else if (!strcmp(arg, "--abbrev"))
2986 options->abbrev = DEFAULT_ABBREV;
2987 else if (!prefixcmp(arg, "--abbrev=")) {
2988 options->abbrev = strtoul(arg + 9, NULL, 10);
2989 if (options->abbrev < MINIMUM_ABBREV)
2990 options->abbrev = MINIMUM_ABBREV;
2991 else if (40 < options->abbrev)
2992 options->abbrev = 40;
2993 }
eab9a40b
JS
2994 else if (!prefixcmp(arg, "--src-prefix="))
2995 options->a_prefix = arg + 13;
2996 else if (!prefixcmp(arg, "--dst-prefix="))
2997 options->b_prefix = arg + 13;
2998 else if (!strcmp(arg, "--no-prefix"))
2999 options->a_prefix = options->b_prefix = "";
6d0e674a
RS
3000 else if (opt_arg(arg, '\0', "inter-hunk-context",
3001 &options->interhunkcontext))
3002 ;
c0c77734
DB
3003 else if (!prefixcmp(arg, "--output=")) {
3004 options->file = fopen(arg + strlen("--output="), "w");
8324b977
LA
3005 if (!options->file)
3006 die_errno("Could not open '%s'", arg + strlen("--output="));
c0c77734
DB
3007 options->close_file = 1;
3008 } else
6973dcae
JH
3009 return 0;
3010 return 1;
3011}
3012
3013static int parse_num(const char **cp_p)
3014{
3015 unsigned long num, scale;
3016 int ch, dot;
3017 const char *cp = *cp_p;
3018
3019 num = 0;
3020 scale = 1;
3021 dot = 0;
eeefa7c9 3022 for (;;) {
6973dcae
JH
3023 ch = *cp;
3024 if ( !dot && ch == '.' ) {
3025 scale = 1;
3026 dot = 1;
3027 } else if ( ch == '%' ) {
3028 scale = dot ? scale*100 : 100;
3029 cp++; /* % is always at the end */
3030 break;
3031 } else if ( ch >= '0' && ch <= '9' ) {
3032 if ( scale < 100000 ) {
3033 scale *= 10;
3034 num = (num*10) + (ch-'0');
3035 }
3036 } else {
3037 break;
3038 }
3039 cp++;
3040 }
3041 *cp_p = cp;
3042
3043 /* user says num divided by scale and we say internally that
3044 * is MAX_SCORE * num / scale.
3045 */
dc49cd76 3046 return (int)((num >= scale) ? MAX_SCORE : (MAX_SCORE * num / scale));
6973dcae
JH
3047}
3048
16befb8b 3049static int diff_scoreopt_parse(const char *opt)
6973dcae
JH
3050{
3051 int opt1, opt2, cmd;
3052
3053 if (*opt++ != '-')
3054 return -1;
3055 cmd = *opt++;
3056 if (cmd != 'M' && cmd != 'C' && cmd != 'B')
3057 return -1; /* that is not a -M, -C nor -B option */
3058
3059 opt1 = parse_num(&opt);
3060 if (cmd != 'B')
3061 opt2 = 0;
3062 else {
3063 if (*opt == 0)
3064 opt2 = 0;
3065 else if (*opt != '/')
3066 return -1; /* we expect -B80/99 or -B80 */
3067 else {
3068 opt++;
3069 opt2 = parse_num(&opt);
3070 }
3071 }
3072 if (*opt != 0)
3073 return -1;
3074 return opt1 | (opt2 << 16);
3075}
3076
3077struct diff_queue_struct diff_queued_diff;
3078
3079void diff_q(struct diff_queue_struct *queue, struct diff_filepair *dp)
3080{
3081 if (queue->alloc <= queue->nr) {
3082 queue->alloc = alloc_nr(queue->alloc);
3083 queue->queue = xrealloc(queue->queue,
3084 sizeof(dp) * queue->alloc);
3085 }
3086 queue->queue[queue->nr++] = dp;
3087}
3088
3089struct diff_filepair *diff_queue(struct diff_queue_struct *queue,
3090 struct diff_filespec *one,
3091 struct diff_filespec *two)
3092{
ef677686 3093 struct diff_filepair *dp = xcalloc(1, sizeof(*dp));
6973dcae
JH
3094 dp->one = one;
3095 dp->two = two;
6973dcae
JH
3096 if (queue)
3097 diff_q(queue, dp);
3098 return dp;
3099}
3100
3101void diff_free_filepair(struct diff_filepair *p)
3102{
9fb88419
LT
3103 free_filespec(p->one);
3104 free_filespec(p->two);
6973dcae
JH
3105 free(p);
3106}
3107
3108/* This is different from find_unique_abbrev() in that
3109 * it stuffs the result with dots for alignment.
3110 */
3111const char *diff_unique_abbrev(const unsigned char *sha1, int len)
3112{
3113 int abblen;
3114 const char *abbrev;
3115 if (len == 40)
3116 return sha1_to_hex(sha1);
3117
3118 abbrev = find_unique_abbrev(sha1, len);
6973dcae
JH
3119 abblen = strlen(abbrev);
3120 if (abblen < 37) {
3121 static char hex[41];
3122 if (len < abblen && abblen <= len + 2)
3123 sprintf(hex, "%s%.*s", abbrev, len+3-abblen, "..");
3124 else
3125 sprintf(hex, "%s...", abbrev);
3126 return hex;
3127 }
3128 return sha1_to_hex(sha1);
3129}
3130
663af342 3131static void diff_flush_raw(struct diff_filepair *p, struct diff_options *opt)
6973dcae 3132{
663af342
PH
3133 int line_termination = opt->line_termination;
3134 int inter_name_termination = line_termination ? '\t' : '\0';
6973dcae 3135
663af342 3136 if (!(opt->output_format & DIFF_FORMAT_NAME_STATUS)) {
c0c77734
DB
3137 fprintf(opt->file, ":%06o %06o %s ", p->one->mode, p->two->mode,
3138 diff_unique_abbrev(p->one->sha1, opt->abbrev));
3139 fprintf(opt->file, "%s ", diff_unique_abbrev(p->two->sha1, opt->abbrev));
6973dcae 3140 }
663af342 3141 if (p->score) {
c0c77734
DB
3142 fprintf(opt->file, "%c%03d%c", p->status, similarity_index(p),
3143 inter_name_termination);
663af342 3144 } else {
c0c77734 3145 fprintf(opt->file, "%c%c", p->status, inter_name_termination);
6973dcae 3146 }
6973dcae 3147
cd676a51
JH
3148 if (p->status == DIFF_STATUS_COPIED ||
3149 p->status == DIFF_STATUS_RENAMED) {
3150 const char *name_a, *name_b;
3151 name_a = p->one->path;
3152 name_b = p->two->path;
3153 strip_prefix(opt->prefix_length, &name_a, &name_b);
c0c77734
DB
3154 write_name_quoted(name_a, opt->file, inter_name_termination);
3155 write_name_quoted(name_b, opt->file, line_termination);
663af342 3156 } else {
cd676a51
JH
3157 const char *name_a, *name_b;
3158 name_a = p->one->mode ? p->one->path : p->two->path;
3159 name_b = NULL;
3160 strip_prefix(opt->prefix_length, &name_a, &name_b);
c0c77734 3161 write_name_quoted(name_a, opt->file, line_termination);
663af342 3162 }
6973dcae
JH
3163}
3164
3165int diff_unmodified_pair(struct diff_filepair *p)
3166{
3167 /* This function is written stricter than necessary to support
3168 * the currently implemented transformers, but the idea is to
3169 * let transformers to produce diff_filepairs any way they want,
3170 * and filter and clean them up here before producing the output.
3171 */
663af342 3172 struct diff_filespec *one = p->one, *two = p->two;
6973dcae
JH
3173
3174 if (DIFF_PAIR_UNMERGED(p))
3175 return 0; /* unmerged is interesting */
3176
6973dcae
JH
3177 /* deletion, addition, mode or type change
3178 * and rename are all interesting.
3179 */
3180 if (DIFF_FILE_VALID(one) != DIFF_FILE_VALID(two) ||
3181 DIFF_PAIR_MODE_CHANGED(p) ||
3182 strcmp(one->path, two->path))
3183 return 0;
3184
3185 /* both are valid and point at the same path. that is, we are
3186 * dealing with a change.
3187 */
3188 if (one->sha1_valid && two->sha1_valid &&
85adbf2f
JL
3189 !hashcmp(one->sha1, two->sha1) &&
3190 !one->dirty_submodule && !two->dirty_submodule)
6973dcae
JH
3191 return 1; /* no change */
3192 if (!one->sha1_valid && !two->sha1_valid)
3193 return 1; /* both look at the same file on the filesystem. */
3194 return 0;
3195}
3196
3197static void diff_flush_patch(struct diff_filepair *p, struct diff_options *o)
3198{
3199 if (diff_unmodified_pair(p))
3200 return;
3201
3202 if ((DIFF_FILE_VALID(p->one) && S_ISDIR(p->one->mode)) ||
3203 (DIFF_FILE_VALID(p->two) && S_ISDIR(p->two->mode)))
3204 return; /* no tree diffs in patch format */
3205
3206 run_diff(p, o);
3207}
3208
3209static void diff_flush_stat(struct diff_filepair *p, struct diff_options *o,
3210 struct diffstat_t *diffstat)
3211{
3212 if (diff_unmodified_pair(p))
3213 return;
3214
3215 if ((DIFF_FILE_VALID(p->one) && S_ISDIR(p->one->mode)) ||
3216 (DIFF_FILE_VALID(p->two) && S_ISDIR(p->two->mode)))
3217 return; /* no tree diffs in patch format */
3218
3219 run_diffstat(p, o, diffstat);
3220}
3221
88246898
JS
3222static void diff_flush_checkdiff(struct diff_filepair *p,
3223 struct diff_options *o)
3224{
3225 if (diff_unmodified_pair(p))
3226 return;
3227
3228 if ((DIFF_FILE_VALID(p->one) && S_ISDIR(p->one->mode)) ||
3229 (DIFF_FILE_VALID(p->two) && S_ISDIR(p->two->mode)))
3230 return; /* no tree diffs in patch format */
3231
3232 run_checkdiff(p, o);
3233}
3234
6973dcae
JH
3235int diff_queue_is_empty(void)
3236{
3237 struct diff_queue_struct *q = &diff_queued_diff;
3238 int i;
3239 for (i = 0; i < q->nr; i++)
3240 if (!diff_unmodified_pair(q->queue[i]))
3241 return 0;
3242 return 1;
3243}
3244
3245#if DIFF_DEBUG
3246void diff_debug_filespec(struct diff_filespec *s, int x, const char *one)
3247{
3248 fprintf(stderr, "queue[%d] %s (%s) %s %06o %s\n",
3249 x, one ? one : "",
3250 s->path,
3251 DIFF_FILE_VALID(s) ? "valid" : "invalid",
3252 s->mode,
3253 s->sha1_valid ? sha1_to_hex(s->sha1) : "");
3254 fprintf(stderr, "queue[%d] %s size %lu flags %d\n",
3255 x, one ? one : "",
3256 s->size, s->xfrm_flags);
3257}
3258
3259void diff_debug_filepair(const struct diff_filepair *p, int i)
3260{
3261 diff_debug_filespec(p->one, i, "one");
3262 diff_debug_filespec(p->two, i, "two");
64479711 3263 fprintf(stderr, "score %d, status %c rename_used %d broken %d\n",
6973dcae 3264 p->score, p->status ? p->status : '?',
64479711 3265 p->one->rename_used, p->broken_pair);
6973dcae
JH
3266}
3267
3268void diff_debug_queue(const char *msg, struct diff_queue_struct *q)
3269{
3270 int i;
3271 if (msg)
3272 fprintf(stderr, "%s\n", msg);
3273 fprintf(stderr, "q->nr = %d\n", q->nr);
3274 for (i = 0; i < q->nr; i++) {
3275 struct diff_filepair *p = q->queue[i];
3276 diff_debug_filepair(p, i);
3277 }
3278}
3279#endif
3280
3281static void diff_resolve_rename_copy(void)
3282{
64479711
LT
3283 int i;
3284 struct diff_filepair *p;
6973dcae
JH
3285 struct diff_queue_struct *q = &diff_queued_diff;
3286
3287 diff_debug_queue("resolve-rename-copy", q);
3288
3289 for (i = 0; i < q->nr; i++) {
3290 p = q->queue[i];
3291 p->status = 0; /* undecided */
3292 if (DIFF_PAIR_UNMERGED(p))
3293 p->status = DIFF_STATUS_UNMERGED;
3294 else if (!DIFF_FILE_VALID(p->one))
3295 p->status = DIFF_STATUS_ADDED;
3296 else if (!DIFF_FILE_VALID(p->two))
3297 p->status = DIFF_STATUS_DELETED;
3298 else if (DIFF_PAIR_TYPE_CHANGED(p))
3299 p->status = DIFF_STATUS_TYPE_CHANGED;
3300
3301 /* from this point on, we are dealing with a pair
3302 * whose both sides are valid and of the same type, i.e.
3303 * either in-place edit or rename/copy edit.
3304 */
3305 else if (DIFF_PAIR_RENAME(p)) {
64479711
LT
3306 /*
3307 * A rename might have re-connected a broken
3308 * pair up, causing the pathnames to be the
3309 * same again. If so, that's not a rename at
3310 * all, just a modification..
3311 *
3312 * Otherwise, see if this source was used for
3313 * multiple renames, in which case we decrement
3314 * the count, and call it a copy.
6973dcae 3315 */
64479711
LT
3316 if (!strcmp(p->one->path, p->two->path))
3317 p->status = DIFF_STATUS_MODIFIED;
3318 else if (--p->one->rename_used > 0)
6973dcae 3319 p->status = DIFF_STATUS_COPIED;
64479711 3320 else
6973dcae
JH
3321 p->status = DIFF_STATUS_RENAMED;
3322 }
a89fccd2 3323 else if (hashcmp(p->one->sha1, p->two->sha1) ||
d516c2d1 3324 p->one->mode != p->two->mode ||
85adbf2f
JL
3325 p->one->dirty_submodule ||
3326 p->two->dirty_submodule ||
d516c2d1 3327 is_null_sha1(p->one->sha1))
6973dcae
JH
3328 p->status = DIFF_STATUS_MODIFIED;
3329 else {
3330 /* This is a "no-change" entry and should not
3331 * happen anymore, but prepare for broken callers.
3332 */
3333 error("feeding unmodified %s to diffcore",
3334 p->one->path);
3335 p->status = DIFF_STATUS_UNKNOWN;
3336 }
3337 }
3338 diff_debug_queue("resolve-rename-copy done", q);
3339}
3340
c6744349 3341static int check_pair_status(struct diff_filepair *p)
6973dcae 3342{
6973dcae
JH
3343 switch (p->status) {
3344 case DIFF_STATUS_UNKNOWN:
c6744349 3345 return 0;
6973dcae
JH
3346 case 0:
3347 die("internal error in diff-resolve-rename-copy");
6973dcae 3348 default:
c6744349 3349 return 1;
6973dcae
JH
3350 }
3351}
3352
c6744349
TH
3353static void flush_one_pair(struct diff_filepair *p, struct diff_options *opt)
3354{
3355 int fmt = opt->output_format;
3356
3357 if (fmt & DIFF_FORMAT_CHECKDIFF)
3358 diff_flush_checkdiff(p, opt);
3359 else if (fmt & (DIFF_FORMAT_RAW | DIFF_FORMAT_NAME_STATUS))
3360 diff_flush_raw(p, opt);
cd676a51
JH
3361 else if (fmt & DIFF_FORMAT_NAME) {
3362 const char *name_a, *name_b;
3363 name_a = p->two->path;
3364 name_b = NULL;
3365 strip_prefix(opt->prefix_length, &name_a, &name_b);
c0c77734 3366 write_name_quoted(name_a, opt->file, opt->line_termination);
cd676a51 3367 }
c6744349
TH
3368}
3369
c0c77734 3370static void show_file_mode_name(FILE *file, const char *newdelete, struct diff_filespec *fs)
4bbd261b
SE
3371{
3372 if (fs->mode)
c0c77734 3373 fprintf(file, " %s mode %06o ", newdelete, fs->mode);
4bbd261b 3374 else
c0c77734
DB
3375 fprintf(file, " %s ", newdelete);
3376 write_name_quoted(fs->path, file, '\n');
4bbd261b
SE
3377}
3378
3379
c0c77734 3380static void show_mode_change(FILE *file, struct diff_filepair *p, int show_name)
4bbd261b
SE
3381{
3382 if (p->one->mode && p->two->mode && p->one->mode != p->two->mode) {
c0c77734 3383 fprintf(file, " mode change %06o => %06o%c", p->one->mode, p->two->mode,
663af342 3384 show_name ? ' ' : '\n');
0d26a64e 3385 if (show_name) {
c0c77734 3386 write_name_quoted(p->two->path, file, '\n');
0d26a64e 3387 }
4bbd261b
SE
3388 }
3389}
3390
c0c77734 3391static void show_rename_copy(FILE *file, const char *renamecopy, struct diff_filepair *p)
4bbd261b 3392{
b9f44164 3393 char *names = pprint_rename(p->one->path, p->two->path);
4bbd261b 3394
c0c77734 3395 fprintf(file, " %s %s (%d%%)\n", renamecopy, names, similarity_index(p));
b9f44164 3396 free(names);
c0c77734 3397 show_mode_change(file, p, 0);
4bbd261b
SE
3398}
3399
c0c77734 3400static void diff_summary(FILE *file, struct diff_filepair *p)
4bbd261b
SE
3401{
3402 switch(p->status) {
3403 case DIFF_STATUS_DELETED:
c0c77734 3404 show_file_mode_name(file, "delete", p->one);
4bbd261b
SE
3405 break;
3406 case DIFF_STATUS_ADDED:
c0c77734 3407 show_file_mode_name(file, "create", p->two);
4bbd261b
SE
3408 break;
3409 case DIFF_STATUS_COPIED:
c0c77734 3410 show_rename_copy(file, "copy", p);
4bbd261b
SE
3411 break;
3412 case DIFF_STATUS_RENAMED:
c0c77734 3413 show_rename_copy(file, "rename", p);
4bbd261b
SE
3414 break;
3415 default:
3416 if (p->score) {
c0c77734
DB
3417 fputs(" rewrite ", file);
3418 write_name_quoted(p->two->path, file, ' ');
3419 fprintf(file, "(%d%%)\n", similarity_index(p));
663af342 3420 }
c0c77734 3421 show_mode_change(file, p, !p->score);
4bbd261b
SE
3422 break;
3423 }
3424}
3425
fcb3d0ad 3426struct patch_id_t {
9126f009 3427 git_SHA_CTX *ctx;
fcb3d0ad
JS
3428 int patchlen;
3429};
3430
3431static int remove_space(char *line, int len)
3432{
3433 int i;
663af342
PH
3434 char *dst = line;
3435 unsigned char c;
fcb3d0ad 3436
663af342
PH
3437 for (i = 0; i < len; i++)
3438 if (!isspace((c = line[i])))
3439 *dst++ = c;
fcb3d0ad 3440
663af342 3441 return dst - line;
fcb3d0ad
JS
3442}
3443
3444static void patch_id_consume(void *priv, char *line, unsigned long len)
3445{
3446 struct patch_id_t *data = priv;
3447 int new_len;
3448
3449 /* Ignore line numbers when computing the SHA1 of the patch */
cc44c765 3450 if (!prefixcmp(line, "@@ -"))
fcb3d0ad
JS
3451 return;
3452
3453 new_len = remove_space(line, len);
3454
9126f009 3455 git_SHA1_Update(data->ctx, line, new_len);
fcb3d0ad
JS
3456 data->patchlen += new_len;
3457}
3458
3459/* returns 0 upon success, and writes result into sha1 */
3460static int diff_get_patch_id(struct diff_options *options, unsigned char *sha1)
3461{
3462 struct diff_queue_struct *q = &diff_queued_diff;
3463 int i;
9126f009 3464 git_SHA_CTX ctx;
fcb3d0ad
JS
3465 struct patch_id_t data;
3466 char buffer[PATH_MAX * 4 + 20];
3467
9126f009 3468 git_SHA1_Init(&ctx);
fcb3d0ad
JS
3469 memset(&data, 0, sizeof(struct patch_id_t));
3470 data.ctx = &ctx;
fcb3d0ad
JS
3471
3472 for (i = 0; i < q->nr; i++) {
3473 xpparam_t xpp;
3474 xdemitconf_t xecfg;
fcb3d0ad
JS
3475 mmfile_t mf1, mf2;
3476 struct diff_filepair *p = q->queue[i];
3477 int len1, len2;
3478
9ccd0a88 3479 memset(&xpp, 0, sizeof(xpp));
30b25010 3480 memset(&xecfg, 0, sizeof(xecfg));
fcb3d0ad
JS
3481 if (p->status == 0)
3482 return error("internal diff status error");
3483 if (p->status == DIFF_STATUS_UNKNOWN)
3484 continue;
3485 if (diff_unmodified_pair(p))
3486 continue;
3487 if ((DIFF_FILE_VALID(p->one) && S_ISDIR(p->one->mode)) ||
3488 (DIFF_FILE_VALID(p->two) && S_ISDIR(p->two->mode)))
3489 continue;
3490 if (DIFF_PAIR_UNMERGED(p))
3491 continue;
3492
3493 diff_fill_sha1_info(p->one);
3494 diff_fill_sha1_info(p->two);
3495 if (fill_mmfile(&mf1, p->one) < 0 ||
3496 fill_mmfile(&mf2, p->two) < 0)
3497 return error("unable to read files to diff");
3498
fcb3d0ad
JS
3499 len1 = remove_space(p->one->path, strlen(p->one->path));
3500 len2 = remove_space(p->two->path, strlen(p->two->path));
3501 if (p->one->mode == 0)
3502 len1 = snprintf(buffer, sizeof(buffer),
3503 "diff--gita/%.*sb/%.*s"
3504 "newfilemode%06o"
3505 "---/dev/null"
3506 "+++b/%.*s",
3507 len1, p->one->path,
3508 len2, p->two->path,
3509 p->two->mode,
3510 len2, p->two->path);
3511 else if (p->two->mode == 0)
3512 len1 = snprintf(buffer, sizeof(buffer),
3513 "diff--gita/%.*sb/%.*s"
3514 "deletedfilemode%06o"
3515 "---a/%.*s"
3516 "+++/dev/null",
3517 len1, p->one->path,
3518 len2, p->two->path,
3519 p->one->mode,
3520 len1, p->one->path);
3521 else
3522 len1 = snprintf(buffer, sizeof(buffer),
3523 "diff--gita/%.*sb/%.*s"
3524 "---a/%.*s"
3525 "+++b/%.*s",
3526 len1, p->one->path,
3527 len2, p->two->path,
3528 len1, p->one->path,
3529 len2, p->two->path);
9126f009 3530 git_SHA1_Update(&ctx, buffer, len1);
fcb3d0ad
JS
3531
3532 xpp.flags = XDF_NEED_MINIMAL;
3533 xecfg.ctxlen = 3;
9fdc3bb5 3534 xecfg.flags = XDL_EMIT_FUNCNAMES;
8a3f524b 3535 xdi_diff_outf(&mf1, &mf2, patch_id_consume, &data,
dfea7900 3536 &xpp, &xecfg);
fcb3d0ad
JS
3537 }
3538
9126f009 3539 git_SHA1_Final(sha1, &ctx);
fcb3d0ad
JS
3540 return 0;
3541}
3542
3543int diff_flush_patch_id(struct diff_options *options, unsigned char *sha1)
3544{
3545 struct diff_queue_struct *q = &diff_queued_diff;
3546 int i;
3547 int result = diff_get_patch_id(options, sha1);
3548
3549 for (i = 0; i < q->nr; i++)
3550 diff_free_filepair(q->queue[i]);
3551
3552 free(q->queue);
9ca5df90 3553 DIFF_QUEUE_CLEAR(q);
fcb3d0ad
JS
3554
3555 return result;
3556}
3557
946c3784 3558static int is_summary_empty(const struct diff_queue_struct *q)
6973dcae 3559{
6973dcae 3560 int i;
6973dcae 3561
946c3784
TH
3562 for (i = 0; i < q->nr; i++) {
3563 const struct diff_filepair *p = q->queue[i];
3564
3565 switch (p->status) {
3566 case DIFF_STATUS_DELETED:
3567 case DIFF_STATUS_ADDED:
3568 case DIFF_STATUS_COPIED:
3569 case DIFF_STATUS_RENAMED:
3570 return 0;
3571 default:
3572 if (p->score)
3573 return 0;
3574 if (p->one->mode && p->two->mode &&
3575 p->one->mode != p->two->mode)
3576 return 0;
3577 break;
3578 }
6973dcae 3579 }
946c3784
TH
3580 return 1;
3581}
3582
6973dcae
JH
3583void diff_flush(struct diff_options *options)
3584{
3585 struct diff_queue_struct *q = &diff_queued_diff;
c6744349 3586 int i, output_format = options->output_format;
946c3784 3587 int separator = 0;
6973dcae 3588
c6744349
TH
3589 /*
3590 * Order: raw, stat, summary, patch
3591 * or: name/name-status/checkdiff (other bits clear)
3592 */
946c3784
TH
3593 if (!q->nr)
3594 goto free_queue;
6973dcae 3595
c6744349
TH
3596 if (output_format & (DIFF_FORMAT_RAW |
3597 DIFF_FORMAT_NAME |
3598 DIFF_FORMAT_NAME_STATUS |
3599 DIFF_FORMAT_CHECKDIFF)) {
6973dcae
JH
3600 for (i = 0; i < q->nr; i++) {
3601 struct diff_filepair *p = q->queue[i];
c6744349
TH
3602 if (check_pair_status(p))
3603 flush_one_pair(p, options);
6973dcae 3604 }
946c3784 3605 separator++;
6973dcae 3606 }
c6744349 3607
c04a7155 3608 if (output_format & (DIFF_FORMAT_DIFFSTAT|DIFF_FORMAT_SHORTSTAT|DIFF_FORMAT_NUMSTAT)) {
5e2b0636 3609 struct diffstat_t diffstat;
c6744349 3610
5e2b0636 3611 memset(&diffstat, 0, sizeof(struct diffstat_t));
6973dcae
JH
3612 for (i = 0; i < q->nr; i++) {
3613 struct diff_filepair *p = q->queue[i];
c6744349 3614 if (check_pair_status(p))
5e2b0636 3615 diff_flush_stat(p, options, &diffstat);
6973dcae 3616 }
74e2abe5
JH
3617 if (output_format & DIFF_FORMAT_NUMSTAT)
3618 show_numstat(&diffstat, options);
3619 if (output_format & DIFF_FORMAT_DIFFSTAT)
3620 show_stats(&diffstat, options);
f604652e 3621 if (output_format & DIFF_FORMAT_SHORTSTAT)
c0c77734 3622 show_shortstats(&diffstat, options);
f604652e 3623 free_diffstat_info(&diffstat);
3969cf7d 3624 separator++;
6973dcae 3625 }
c04a7155
JH
3626 if (output_format & DIFF_FORMAT_DIRSTAT)
3627 show_dirstat(options);
6973dcae 3628
946c3784 3629 if (output_format & DIFF_FORMAT_SUMMARY && !is_summary_empty(q)) {
c6744349 3630 for (i = 0; i < q->nr; i++)
c0c77734 3631 diff_summary(options->file, q->queue[i]);
3969cf7d 3632 separator++;
6973dcae
JH
3633 }
3634
6977c250
LA
3635 if (output_format & DIFF_FORMAT_NO_OUTPUT &&
3636 DIFF_OPT_TST(options, EXIT_WITH_STATUS) &&
3637 DIFF_OPT_TST(options, DIFF_FROM_CONTENTS)) {
3638 /*
3639 * run diff_flush_patch for the exit status. setting
3640 * options->file to /dev/null should be safe, becaue we
3641 * aren't supposed to produce any output anyway.
3642 */
3643 if (options->close_file)
3644 fclose(options->file);
3645 options->file = fopen("/dev/null", "w");
3646 if (!options->file)
3647 die_errno("Could not open /dev/null");
3648 options->close_file = 1;
3649 for (i = 0; i < q->nr; i++) {
3650 struct diff_filepair *p = q->queue[i];
3651 if (check_pair_status(p))
3652 diff_flush_patch(p, options);
3653 if (options->found_changes)
3654 break;
3655 }
3656 }
3657
c6744349 3658 if (output_format & DIFF_FORMAT_PATCH) {
946c3784 3659 if (separator) {
6b2fbaaf 3660 putc(options->line_termination, options->file);
946c3784
TH
3661 if (options->stat_sep) {
3662 /* attach patch instead of inline */
c0c77734 3663 fputs(options->stat_sep, options->file);
946c3784 3664 }
c6744349
TH
3665 }
3666
3667 for (i = 0; i < q->nr; i++) {
3668 struct diff_filepair *p = q->queue[i];
3669 if (check_pair_status(p))
3670 diff_flush_patch(p, options);
3671 }
4bbd261b
SE
3672 }
3673
04245581
JK
3674 if (output_format & DIFF_FORMAT_CALLBACK)
3675 options->format_callback(q, options, options->format_callback_data);
3676
c6744349
TH
3677 for (i = 0; i < q->nr; i++)
3678 diff_free_filepair(q->queue[i]);
946c3784 3679free_queue:
6973dcae 3680 free(q->queue);
9ca5df90 3681 DIFF_QUEUE_CLEAR(q);
c0c77734
DB
3682 if (options->close_file)
3683 fclose(options->file);
f245194f
JH
3684
3685 /*
97bf2a08 3686 * Report the content-level differences with HAS_CHANGES;
f245194f
JH
3687 * diff_addremove/diff_change does not set the bit when
3688 * DIFF_FROM_CONTENTS is in effect (e.g. with -w).
3689 */
3690 if (DIFF_OPT_TST(options, DIFF_FROM_CONTENTS)) {
3691 if (options->found_changes)
3692 DIFF_OPT_SET(options, HAS_CHANGES);
3693 else
3694 DIFF_OPT_CLR(options, HAS_CHANGES);
3695 }
6973dcae
JH
3696}
3697
3698static void diffcore_apply_filter(const char *filter)
3699{
3700 int i;
3701 struct diff_queue_struct *q = &diff_queued_diff;
3702 struct diff_queue_struct outq;
9ca5df90 3703 DIFF_QUEUE_CLEAR(&outq);
6973dcae
JH
3704
3705 if (!filter)
3706 return;
3707
3708 if (strchr(filter, DIFF_STATUS_FILTER_AON)) {
3709 int found;
3710 for (i = found = 0; !found && i < q->nr; i++) {
3711 struct diff_filepair *p = q->queue[i];
3712 if (((p->status == DIFF_STATUS_MODIFIED) &&
3713 ((p->score &&
3714 strchr(filter, DIFF_STATUS_FILTER_BROKEN)) ||
3715 (!p->score &&
3716 strchr(filter, DIFF_STATUS_MODIFIED)))) ||
3717 ((p->status != DIFF_STATUS_MODIFIED) &&
3718 strchr(filter, p->status)))
3719 found++;
3720 }
3721 if (found)
3722 return;
3723
3724 /* otherwise we will clear the whole queue
3725 * by copying the empty outq at the end of this
3726 * function, but first clear the current entries
3727 * in the queue.
3728 */
3729 for (i = 0; i < q->nr; i++)
3730 diff_free_filepair(q->queue[i]);
3731 }
3732 else {
3733 /* Only the matching ones */
3734 for (i = 0; i < q->nr; i++) {
3735 struct diff_filepair *p = q->queue[i];
3736
3737 if (((p->status == DIFF_STATUS_MODIFIED) &&
3738 ((p->score &&
3739 strchr(filter, DIFF_STATUS_FILTER_BROKEN)) ||
3740 (!p->score &&
3741 strchr(filter, DIFF_STATUS_MODIFIED)))) ||
3742 ((p->status != DIFF_STATUS_MODIFIED) &&
3743 strchr(filter, p->status)))
3744 diff_q(&outq, p);
3745 else
3746 diff_free_filepair(p);
3747 }
3748 }
3749 free(q->queue);
3750 *q = outq;
3751}
3752
5701115a
SV
3753/* Check whether two filespecs with the same mode and size are identical */
3754static int diff_filespec_is_identical(struct diff_filespec *one,
3755 struct diff_filespec *two)
3756{
2b459b48
JH
3757 if (S_ISGITLINK(one->mode))
3758 return 0;
5701115a
SV
3759 if (diff_populate_filespec(one, 0))
3760 return 0;
3761 if (diff_populate_filespec(two, 0))
3762 return 0;
3763 return !memcmp(one->data, two->data, one->size);
3764}
3765
fb13227e
JH
3766static void diffcore_skip_stat_unmatch(struct diff_options *diffopt)
3767{
3768 int i;
3769 struct diff_queue_struct *q = &diff_queued_diff;
3770 struct diff_queue_struct outq;
9ca5df90 3771 DIFF_QUEUE_CLEAR(&outq);
fb13227e
JH
3772
3773 for (i = 0; i < q->nr; i++) {
3774 struct diff_filepair *p = q->queue[i];
3775
3776 /*
9517e6b8 3777 * 1. Entries that come from stat info dirtiness
fb13227e
JH
3778 * always have both sides (iow, not create/delete),
3779 * one side of the object name is unknown, with
3780 * the same mode and size. Keep the ones that
3781 * do not match these criteria. They have real
3782 * differences.
3783 *
3784 * 2. At this point, the file is known to be modified,
3785 * with the same mode and size, and the object
3786 * name of one side is unknown. Need to inspect
3787 * the identical contents.
3788 */
3789 if (!DIFF_FILE_VALID(p->one) || /* (1) */
3790 !DIFF_FILE_VALID(p->two) ||
3791 (p->one->sha1_valid && p->two->sha1_valid) ||
3792 (p->one->mode != p->two->mode) ||
3793 diff_populate_filespec(p->one, 1) ||
3794 diff_populate_filespec(p->two, 1) ||
3795 (p->one->size != p->two->size) ||
5701115a 3796 !diff_filespec_is_identical(p->one, p->two)) /* (2) */
fb13227e
JH
3797 diff_q(&outq, p);
3798 else {
3799 /*
3800 * The caller can subtract 1 from skip_stat_unmatch
3801 * to determine how many paths were dirty only
3802 * due to stat info mismatch.
3803 */
8f67f8ae 3804 if (!DIFF_OPT_TST(diffopt, NO_INDEX))
6d2d9e86 3805 diffopt->skip_stat_unmatch++;
fb13227e
JH
3806 diff_free_filepair(p);
3807 }
3808 }
3809 free(q->queue);
3810 *q = outq;
3811}
3812
730f7284
JH
3813static int diffnamecmp(const void *a_, const void *b_)
3814{
3815 const struct diff_filepair *a = *((const struct diff_filepair **)a_);
3816 const struct diff_filepair *b = *((const struct diff_filepair **)b_);
3817 const char *name_a, *name_b;
3818
3819 name_a = a->one ? a->one->path : a->two->path;
3820 name_b = b->one ? b->one->path : b->two->path;
3821 return strcmp(name_a, name_b);
3822}
3823
3824void diffcore_fix_diff_index(struct diff_options *options)
3825{
3826 struct diff_queue_struct *q = &diff_queued_diff;
3827 qsort(q->queue, q->nr, sizeof(q->queue[0]), diffnamecmp);
3828}
3829
6973dcae
JH
3830void diffcore_std(struct diff_options *options)
3831{
1da6175d
BY
3832 /* We never run this function more than one time, because the
3833 * rename/copy detection logic can only run once.
3834 */
3835 if (diff_queued_diff.run)
3836 return;
3837
9d865356 3838 if (options->skip_stat_unmatch)
fb13227e 3839 diffcore_skip_stat_unmatch(options);
6973dcae
JH
3840 if (options->break_opt != -1)
3841 diffcore_break(options->break_opt);
3842 if (options->detect_rename)
3843 diffcore_rename(options);
3844 if (options->break_opt != -1)
3845 diffcore_merge_broken();
3846 if (options->pickaxe)
3847 diffcore_pickaxe(options->pickaxe, options->pickaxe_opts);
3848 if (options->orderfile)
3849 diffcore_order(options->orderfile);
3850 diff_resolve_rename_copy();
3851 diffcore_apply_filter(options->filter);
68aacb2f 3852
f245194f 3853 if (diff_queued_diff.nr && !DIFF_OPT_TST(options, DIFF_FROM_CONTENTS))
8f67f8ae
PH
3854 DIFF_OPT_SET(options, HAS_CHANGES);
3855 else
3856 DIFF_OPT_CLR(options, HAS_CHANGES);
1da6175d
BY
3857
3858 diff_queued_diff.run = 1;
6973dcae
JH
3859}
3860
da31b358
JH
3861int diff_result_code(struct diff_options *opt, int status)
3862{
3863 int result = 0;
3864 if (!DIFF_OPT_TST(opt, EXIT_WITH_STATUS) &&
3865 !(opt->output_format & DIFF_FORMAT_CHECKDIFF))
3866 return status;
3867 if (DIFF_OPT_TST(opt, EXIT_WITH_STATUS) &&
3868 DIFF_OPT_TST(opt, HAS_CHANGES))
3869 result |= 01;
3870 if ((opt->output_format & DIFF_FORMAT_CHECKDIFF) &&
3871 DIFF_OPT_TST(opt, CHECK_FAILED))
3872 result |= 02;
3873 return result;
3874}
6973dcae 3875
6973dcae
JH
3876void diff_addremove(struct diff_options *options,
3877 int addremove, unsigned mode,
3878 const unsigned char *sha1,
e3d42c47 3879 const char *concatpath, unsigned dirty_submodule)
6973dcae 3880{
6973dcae
JH
3881 struct diff_filespec *one, *two;
3882
50fd9bd8
JS
3883 if (DIFF_OPT_TST(options, IGNORE_SUBMODULES) && S_ISGITLINK(mode))
3884 return;
3885
6973dcae
JH
3886 /* This may look odd, but it is a preparation for
3887 * feeding "there are unchanged files which should
3888 * not produce diffs, but when you are doing copy
3889 * detection you would need them, so here they are"
3890 * entries to the diff-core. They will be prefixed
3891 * with something like '=' or '*' (I haven't decided
3892 * which but should not make any difference).
a6080a0a 3893 * Feeding the same new and old to diff_change()
6973dcae
JH
3894 * also has the same effect.
3895 * Before the final output happens, they are pruned after
3896 * merged into rename/copy pairs as appropriate.
3897 */
8f67f8ae 3898 if (DIFF_OPT_TST(options, REVERSE_DIFF))
6973dcae
JH
3899 addremove = (addremove == '+' ? '-' :
3900 addremove == '-' ? '+' : addremove);
3901
cd676a51
JH
3902 if (options->prefix &&
3903 strncmp(concatpath, options->prefix, options->prefix_length))
3904 return;
3905
6973dcae
JH
3906 one = alloc_filespec(concatpath);
3907 two = alloc_filespec(concatpath);
3908
3909 if (addremove != '+')
3910 fill_filespec(one, sha1, mode);
e3d42c47 3911 if (addremove != '-') {
6973dcae 3912 fill_filespec(two, sha1, mode);
e3d42c47
JL
3913 two->dirty_submodule = dirty_submodule;
3914 }
6973dcae
JH
3915
3916 diff_queue(&diff_queued_diff, one, two);
f245194f
JH
3917 if (!DIFF_OPT_TST(options, DIFF_FROM_CONTENTS))
3918 DIFF_OPT_SET(options, HAS_CHANGES);
6973dcae
JH
3919}
3920
3921void diff_change(struct diff_options *options,
3922 unsigned old_mode, unsigned new_mode,
3923 const unsigned char *old_sha1,
3924 const unsigned char *new_sha1,
e3d42c47
JL
3925 const char *concatpath,
3926 unsigned old_dirty_submodule, unsigned new_dirty_submodule)
6973dcae 3927{
6973dcae
JH
3928 struct diff_filespec *one, *two;
3929
50fd9bd8
JS
3930 if (DIFF_OPT_TST(options, IGNORE_SUBMODULES) && S_ISGITLINK(old_mode)
3931 && S_ISGITLINK(new_mode))
3932 return;
3933
8f67f8ae 3934 if (DIFF_OPT_TST(options, REVERSE_DIFF)) {
6973dcae
JH
3935 unsigned tmp;
3936 const unsigned char *tmp_c;
3937 tmp = old_mode; old_mode = new_mode; new_mode = tmp;
3938 tmp_c = old_sha1; old_sha1 = new_sha1; new_sha1 = tmp_c;
e3d42c47
JL
3939 tmp = old_dirty_submodule; old_dirty_submodule = new_dirty_submodule;
3940 new_dirty_submodule = tmp;
6973dcae 3941 }
cd676a51
JH
3942
3943 if (options->prefix &&
3944 strncmp(concatpath, options->prefix, options->prefix_length))
3945 return;
3946
6973dcae
JH
3947 one = alloc_filespec(concatpath);
3948 two = alloc_filespec(concatpath);
3949 fill_filespec(one, old_sha1, old_mode);
3950 fill_filespec(two, new_sha1, new_mode);
e3d42c47
JL
3951 one->dirty_submodule = old_dirty_submodule;
3952 two->dirty_submodule = new_dirty_submodule;
6973dcae
JH
3953
3954 diff_queue(&diff_queued_diff, one, two);
f245194f
JH
3955 if (!DIFF_OPT_TST(options, DIFF_FROM_CONTENTS))
3956 DIFF_OPT_SET(options, HAS_CHANGES);
6973dcae
JH
3957}
3958
3959void diff_unmerge(struct diff_options *options,
e9c84099
JH
3960 const char *path,
3961 unsigned mode, const unsigned char *sha1)
6973dcae
JH
3962{
3963 struct diff_filespec *one, *two;
cd676a51
JH
3964
3965 if (options->prefix &&
3966 strncmp(path, options->prefix, options->prefix_length))
3967 return;
3968
6973dcae
JH
3969 one = alloc_filespec(path);
3970 two = alloc_filespec(path);
e9c84099
JH
3971 fill_filespec(one, sha1, mode);
3972 diff_queue(&diff_queued_diff, one, two)->is_unmerged = 1;
6973dcae 3973}
9cb92c39
JK
3974
3975static char *run_textconv(const char *pgm, struct diff_filespec *spec,
3976 size_t *outsize)
3977{
479b0ae8 3978 struct diff_tempfile *temp;
9cb92c39
JK
3979 const char *argv[3];
3980 const char **arg = argv;
3981 struct child_process child;
3982 struct strbuf buf = STRBUF_INIT;
da1fbed3 3983 int err = 0;
9cb92c39 3984
479b0ae8 3985 temp = prepare_temp_file(spec->path, spec);
9cb92c39 3986 *arg++ = pgm;
479b0ae8 3987 *arg++ = temp->name;
9cb92c39
JK
3988 *arg = NULL;
3989
3990 memset(&child, 0, sizeof(child));
41a457e4 3991 child.use_shell = 1;
9cb92c39
JK
3992 child.argv = argv;
3993 child.out = -1;
da1fbed3 3994 if (start_command(&child)) {
479b0ae8 3995 remove_tempfile();
9cb92c39
JK
3996 return NULL;
3997 }
da1fbed3
JS
3998
3999 if (strbuf_read(&buf, child.out, 0) < 0)
4000 err = error("error reading from textconv command '%s'", pgm);
70d70999 4001 close(child.out);
da1fbed3
JS
4002
4003 if (finish_command(&child) || err) {
4004 strbuf_release(&buf);
4005 remove_tempfile();
4006 return NULL;
4007 }
479b0ae8 4008 remove_tempfile();
9cb92c39
JK
4009
4010 return strbuf_detach(&buf, outsize);
4011}
840383b2 4012
d9bae1a1 4013static size_t fill_textconv(struct userdiff_driver *driver,
840383b2
JK
4014 struct diff_filespec *df,
4015 char **outbuf)
4016{
4017 size_t size;
4018
d9bae1a1 4019 if (!driver || !driver->textconv) {
840383b2
JK
4020 if (!DIFF_FILE_VALID(df)) {
4021 *outbuf = "";
4022 return 0;
4023 }
4024 if (diff_populate_filespec(df, 0))
4025 die("unable to read files to diff");
4026 *outbuf = df->data;
4027 return df->size;
4028 }
4029
d9bae1a1
JK
4030 if (driver->textconv_cache) {
4031 *outbuf = notes_cache_get(driver->textconv_cache, df->sha1,
4032 &size);
4033 if (*outbuf)
4034 return size;
4035 }
4036
4037 *outbuf = run_textconv(driver->textconv, df, &size);
840383b2
JK
4038 if (!*outbuf)
4039 die("unable to read files to diff");
d9bae1a1
JK
4040
4041 if (driver->textconv_cache) {
4042 /* ignore errors, as we might be in a readonly repository */
4043 notes_cache_put(driver->textconv_cache, df->sha1, *outbuf,
4044 size);
4045 /*
4046 * we could save up changes and flush them all at the end,
4047 * but we would need an extra call after all diffing is done.
4048 * Since generating a cache entry is the slow path anyway,
4049 * this extra overhead probably isn't a big deal.
4050 */
4051 notes_cache_write(driver->textconv_cache);
4052 }
4053
840383b2
JK
4054 return size;
4055}